[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-29 Thread Martin Panter
Martin Panter added the comment: I’m not familiar with the implementation, but it does seem like this should be fixed within _PyLong_AsByteArray(). Also, what about int.from_bytes(b"", ..., signed=True)? There are existing tests for this case, but it seems like it should be an error, rather

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-29 Thread Anders Lorentsen
Anders Lorentsen added the comment: I updated my patch to account for that second corner case. But ideally, shouldn't it rather be accounted for in the function that does the actual conversion, that is, in _PyLong_AsByteArray? -- Added file:

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-28 Thread Mark Dickinson
Mark Dickinson added the comment: I guess that would be the minimal change necessary to remove ambiguity. But I tend to think that >>> (0).to_bytes(0, 'big', signed=True) should also be an error. (And of course both these should still be errors with 'big' replaced with 'little'.) --

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-28 Thread Anders Lorentsen
Anders Lorentsen added the comment: So, am I to understand that the only corner case we should fix is that >>> (-1).to_bytes(0, 'big', signed=True) should raise an overflow error (currently it returns b'') ? -- Added file:

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-28 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: I agree with Martin. The ambiguous signed conversion cases should be an error, the unambiguous unsigned conversion case should be supported (especially if there are tests for this). -- nosy: +serhiy.storchaka ___

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-27 Thread Martin Panter
Martin Panter added the comment: I agree that the signed conversion cases should be an error. However the unsigned case would break working code that I have written for bijective numeration. See _bytes_to_int() and _int_to_bytes() in Issue 20132, inc-codecs.diff, for example. Since non-zero

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-27 Thread Emanuel Barry
Emanuel Barry added the comment: I don't use this feature enough to have a clear opinion, however it's specifically accounted for in the code and has a test for it. It might be a good idea to bring this up on Python-ideas. It's very likely to break some code, but I wonder if said code wasn't

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-27 Thread Марк Коренберг
Марк Коренберг added the comment: I think, we should deny: * Passing `0` to `to_bytes` (even if integer is equal to zero) * Passing empty string to `from_bytes` I do not see any use cases for this to work. It was never guaranteed to work earlier. Everyone pass constant to `to_bytes` that is >

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-27 Thread Emanuel Barry
Emanuel Barry added the comment: Here's a patch that fixes this. -- components: +Interpreter Core -Library (Lib) keywords: +patch nosy: +ebarry stage: -> patch review Added file: http://bugs.python.org/file43913/int_to_bytes_overflow_1.patch ___

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-27 Thread Anders Lorentsen
Anders Lorentsen added the comment: Isn't it possible to just add a small line of code that checks if length is less than or equal to 0, and if it is, call the necessary c functions to have python raise a valueerror...? Sorry if this is giving a solution without actually submitting the patch

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-27 Thread Anders Lorentsen
Changes by Anders Lorentsen : -- nosy: +Phaqui ___ Python tracker ___ ___ Python-bugs-list

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-27 Thread Lucas Morales
Lucas Morales added the comment: This is actually a problem in Objects/longobject.c, in the _PyLong_AsByteArray function. It should have given an overflow error, because -1 cannot be encoded in 0 bytes. -- nosy: +lucasem ___ Python tracker

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-26 Thread Марк Коренберг
Марк Коренберг added the comment: So, as I think, it must ValueError when bytes count is zero. This is like division by zero. -- ___ Python tracker ___

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-26 Thread Марк Коренберг
Changes by Марк Коренберг : -- components: +Library (Lib) type: -> behavior versions: +Python 3.6 ___ Python tracker ___

[issue27623] int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero

2016-07-26 Thread Марк Коренберг
New submission from Марк Коренберг: As you can see, these conversions are not consistent. What is use case to allow that? === In [22]: (-1).to_bytes(0, 'big', signed=True) Out[22]: b'' In [23]: (0).to_bytes(0, 'big', signed=True) Out[23]: b'' As you can see, two different