synodriver / fast-bencode

fast bencode for python, based on cython
8 stars 1 forks source link

int value is incorrect on Windows #3

Closed xlongshu closed 2 weeks ago

xlongshu commented 1 month ago
import bencode
meta = {"intA": 2147483647, "intB": 2147483648}
print(bencode.bencode(meta))
zyblog123 commented 2 weeks ago

This seems to be due to the C int32's range. If I make a copy of "bencode.py" and import bencode from it, the result will be correct.

from bencode import bencode
print(bencode(2147483647))
print(bencode(2147483648))
print(bencode(11856280181))

Result: b'i2147483647e' b'i-2147483648e' b'i-1028621707e'

Result from the copied "bencode.py": b'i2147483647e' b'i2147483648e' b'i11856280181e'

synodriver commented 2 weeks ago

Sorry for the late reply. It seems PyOS_snprintf does not take very large int into account. or should we just use PyLongObject instead of c int?

synodriver commented 2 weeks ago

How about this one?

zyblog123 commented 2 weeks ago

How about this one?

Thanks a lot, it works great for me. (I tried it in Python 3.11, on Windows 10 22H2 x64)

synodriver commented 2 weeks ago

Ok, I'll release a new version later.

synodriver commented 2 weeks ago

1.1.7 is ready.