adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
MIT License
3.98k stars 1.17k forks source link

from_bytes() does not implement `signed=True` #2624

Open dhalbert opened 4 years ago

dhalbert commented 4 years ago

Related to #2579. This was harder to fix: the underlying longint routines don't handle signed values.

jpecor commented 4 years ago

I just bumped into this testing the new CP 5.0 release. I had to change code for to_bytes() and just did a global search/replace which made my from_bytes() calls fail.

Thanks for documenting it here, though. Didn't take me long to find this thread and see it's a known issue on the long term list.

taylorcenters commented 3 years ago

I'm running into this issue as well.

anecdata commented 1 year ago

Adafruit CircuitPython 8.0.0-beta.6-25-gbb3a1c0a2 on 2023-01-04; Raspberry Pi Pico W with rp2040

>>> (-1).to_bytes(1, 'big', signed=True)
b'\xff'
>>> int.from_bytes(b'\xff', 'big', signed=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: signed=True
>>> int.from_bytes(b'\xff', 'big')
255

"// TODO: Support signed param (assumes signed=False at the moment)"

(just waking up this sleeping issue)

Is the underlying code for this vastly different?

>>> struct.pack("b", -1)
b'\xff'
>>> struct.unpack("b", b'\xff')
(-1,)