bencoder icon indicating copy to clipboard operation
bencoder copied to clipboard

UnicodeDecodeError in Python2

Open rkrp opened this issue 9 years ago • 1 comments

I would like to first thank you for the library :)

I ran into a little trouble while trying to use it with Python2. This happens when it is attempted to convert the entire data to be parsed as ascii. However in my humble opinion, I think the content to be converted need not be converted to ascii as any torrent file will obviously contain non-printable ascii characters, resulting in UnicodeDecodeError.

Python 2.7.11 (default, Mar 31 2016, 06:18:34) 
[GCC 5.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bencoder
>>> data = open("../archlinux-2016.06.01-dual.iso.torrent","rb").read()
>>> bencoder.decode(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bencoder.py", line 90, in decode
    s = s.encode("ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbe in position 267: ordinal not in range(128)

Removing this particular block of code, which attempts the ascii conversion, seems to make it work flawlessly in Python2.

 89     if isinstance(s, str):
 90         s = s.encode("ascii")

Attached the torrent file, I was attempting to decode. archlinux-2016.06.01-dual.iso.torrent.zip

rkrp avatar Jun 30 '16 20:06 rkrp

Should be UTF-8 I guess. But the project needs some tests IMO.

bitplane avatar Sep 13 '22 08:09 bitplane