Skip to content

Commit 7813643

Browse files
committed
Fixed an exception raised while attempting to decode strings on Python 3+
Fixes #13 #14
1 parent 0d0bef1 commit 7813643

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

bencode/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def bdecode(value):
146146
:rtype: object
147147
"""
148148
try:
149+
value = to_binary(value)
149150
data, length = decode_func[value[0:1]](value, 0)
150151
except (IndexError, KeyError, TypeError, ValueError):
151152
raise BencodeDecodeError("not a valid bencoded string")

tests/bencode_tests.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@
3333
(b'foo', 42)
3434
)), b'd3:bar4:spam3:fooi42ee'))
3535

36+
ENCODE = VALUES
37+
38+
DECODE = VALUES + [
39+
(0, 'i0e'),
40+
([b'parrot sketch', 42], 'l13:parrot sketchi42ee'),
41+
]
42+
3643

3744
def test_encode():
3845
"""Encode should give known result with known input."""
39-
for plain, encoded in VALUES:
46+
for plain, encoded in ENCODE:
4047
assert encoded == bencode(plain)
4148

4249

@@ -52,7 +59,7 @@ def test_encode_bytes():
5259

5360
def test_decode():
5461
"""Decode should give known result with known input."""
55-
for plain, encoded in VALUES:
62+
for plain, encoded in DECODE:
5663
assert plain == bdecode(encoded)
5764

5865

@@ -63,7 +70,7 @@ def test_decode_bytes():
6370

6471
def test_encode_roundtrip():
6572
"""Consecutive calls to decode and encode should deliver the original data again."""
66-
for plain, encoded in VALUES:
73+
for plain, encoded in ENCODE:
6774
assert encoded == bencode(bdecode(encoded))
6875

6976

0 commit comments

Comments
 (0)