From 42470134468d41f275a79f869223c3361cd7e11d Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Wed, 20 Jun 2018 15:36:42 +0100 Subject: [PATCH] [bencode] Fix errors with unicode dict keys or values --- deluge/bencode.py | 8 +++++++- deluge/tests/test_bencode.py | 25 +++++++++++++++++++++++++ deluge/tests/test_core.py | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 deluge/tests/test_bencode.py diff --git a/deluge/bencode.py b/deluge/bencode.py index e9610a0da..997f2f2c0 100644 --- a/deluge/bencode.py +++ b/deluge/bencode.py @@ -11,6 +11,8 @@ # Written by Petru Paler # Updated by Calum Lind to support both Python 2 and Python 3. +from __future__ import unicode_literals + from sys import version_info PY2 = version_info.major == 2 @@ -109,7 +111,7 @@ def encode_bool(x, r): def encode_string(x, r): - encode_string(x.encode('utf8'), r) + encode_bytes(x.encode('utf8'), r) def encode_bytes(x, r): @@ -126,6 +128,10 @@ def encode_list(x, r): def encode_dict(x, r): r.append(DICT_DELIM) for k, v in sorted(x.items()): + try: + k = k.encode('utf8') + except AttributeError: + pass r.extend((str(len(k)).encode('utf8'), BYTE_SEP, k)) encode_func[type(v)](v, r) r.append(END_DELIM) diff --git a/deluge/tests/test_bencode.py b/deluge/tests/test_bencode.py new file mode 100644 index 000000000..4d0de3546 --- /dev/null +++ b/deluge/tests/test_bencode.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# +from __future__ import unicode_literals + +from twisted.trial import unittest + +from deluge import bencode + +from . import common + + +class BencodeTestCase(unittest.TestCase): + + def test_bencode_unicode_metainfo(self): + filename = common.get_test_data_file('test.torrent') + with open(filename, 'rb') as _file: + metainfo = bencode.bdecode(_file.read())['info'] + bencode.bencode({b'info': metainfo}) + + def test_bencode_unicode_value(self): + self.assertEqual(bencode.bencode('abc'), '3:abc') diff --git a/deluge/tests/test_core.py b/deluge/tests/test_core.py index c77253c98..44ed8e51e 100644 --- a/deluge/tests/test_core.py +++ b/deluge/tests/test_core.py @@ -153,7 +153,7 @@ class CoreTestCase(BaseTestCase): # Get the info hash from the test.torrent from deluge.bencode import bdecode, bencode with open(filename, 'rb') as _file: - info_hash = sha(bencode(bdecode(_file.read())[b'info'])).hexdigest() + info_hash = sha(bencode(bdecode(_file.read())['info'])).hexdigest() self.assertEqual(torrent_id, info_hash) def test_add_torrent_file_invalid_filedump(self):