[bencode] Fix errors with unicode dict keys or values
This commit is contained in:
parent
6ec32a85e4
commit
4247013446
|
@ -11,6 +11,8 @@
|
||||||
# Written by Petru Paler
|
# Written by Petru Paler
|
||||||
# Updated by Calum Lind to support both Python 2 and Python 3.
|
# Updated by Calum Lind to support both Python 2 and Python 3.
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from sys import version_info
|
from sys import version_info
|
||||||
|
|
||||||
PY2 = version_info.major == 2
|
PY2 = version_info.major == 2
|
||||||
|
@ -109,7 +111,7 @@ def encode_bool(x, r):
|
||||||
|
|
||||||
|
|
||||||
def encode_string(x, r):
|
def encode_string(x, r):
|
||||||
encode_string(x.encode('utf8'), r)
|
encode_bytes(x.encode('utf8'), r)
|
||||||
|
|
||||||
|
|
||||||
def encode_bytes(x, r):
|
def encode_bytes(x, r):
|
||||||
|
@ -126,6 +128,10 @@ def encode_list(x, r):
|
||||||
def encode_dict(x, r):
|
def encode_dict(x, r):
|
||||||
r.append(DICT_DELIM)
|
r.append(DICT_DELIM)
|
||||||
for k, v in sorted(x.items()):
|
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))
|
r.extend((str(len(k)).encode('utf8'), BYTE_SEP, k))
|
||||||
encode_func[type(v)](v, r)
|
encode_func[type(v)](v, r)
|
||||||
r.append(END_DELIM)
|
r.append(END_DELIM)
|
||||||
|
|
|
@ -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')
|
|
@ -153,7 +153,7 @@ class CoreTestCase(BaseTestCase):
|
||||||
# Get the info hash from the test.torrent
|
# Get the info hash from the test.torrent
|
||||||
from deluge.bencode import bdecode, bencode
|
from deluge.bencode import bdecode, bencode
|
||||||
with open(filename, 'rb') as _file:
|
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)
|
self.assertEqual(torrent_id, info_hash)
|
||||||
|
|
||||||
def test_add_torrent_file_invalid_filedump(self):
|
def test_add_torrent_file_invalid_filedump(self):
|
||||||
|
|
Loading…
Reference in New Issue