bdecode fails on python3 with bytes objects
tritium21 opened this issue · 2 comments
bencoded files can, and in their most common use case do, include raw binary data. The native string type in python 2 is just binary data (str). This means loading a torrent file, and decoding it with python 2 works just fine with bencode.py (as it uses uses the native string type exclusively internally).
In python 3, the native string type is a sequence of unicode code points, not bytes, and never the twain shall they meet. So while a unicode string (python 3 str) will encode and decode just fine with bencode.py, it cannot decode or encode a bytes structures. ...And you need to be able to do that to handle torrent files (which cannot be represented in unicode - there is raw binary in the file)
My fork seems to handle str versus bytes correctly for both encoding and decoding https://github.com/lostnihilist/bencode.py
I've just merged @lostnihilist's improvements and made some adjustments to ensure Python 2 is still supported. (released in v2.0.0)
Let me know if there are still changes required, test cases will help as well.