utdemir / bencoder

A simple bencode decoder-encoder library in pure Python.
Creative Commons Zero v1.0 Universal
31 stars 5 forks source link

UnicodeDecodeError in Python2 #2

Open rkrp opened 8 years ago

rkrp commented 8 years ago

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

bitplane commented 2 years ago

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