GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||
GeezerVison is showing github.com — gh-74020: Raise ValueError for negative values converted to unsigned by serhiy-storchaka · Pull Request #121114 · python/cpython Conversation
|
| self.assertRaises(ValueError, (-1).to_bytes, 2, 'big', signed=False) | ||
| self.assertRaises(ValueError, (-1).to_bytes, 2, 'little', signed=False) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should adjust int.to_bytes() docs (both docstring and sphinx).
BTW, I think there could be a test with a negative input and signed=True.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Updated docs.
There is a test with a negative input and signed=True above.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There is a test with a negative input and signed=True above.
Hmm, maybe. I don't see it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm really worried that now, anyone using PyLong_AsUnsignedLong would need to either change how they handle the exception or now be dealing with two different exceptions.
I'm not confident enough to actually just make this change. OverflowError fits in the sense that it's outside of the expected range, but the transition is too hard. I suggested raising DomainError which would inherit from OverflowError and ValueError but code paths with distinct handling of ValueError and OverflowError could be broken.
An easy way to make it work is to introduce a new function that would properly raise what is meant to be raised, and deprecate those functions.
So I'm not -1 as strong as @rhettinger, but I still think it's better to be continue with this, even if it's annoying on our side. We could have some private helpers that would do something like:
try: x = asulong(X) except OverflowError: if X < 0: raise ValueError raise
Or something that can store in addition the fact that something is < 0 or not, like PyLong_AsLongAndOverflow but for ULong instead where we add also a isnegative flag:
int _PyLong_AsUnsignedLong(PyObject *long, int *overflow, int *negative);
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| integer must implement either :meth:`~object.__int__` or | ||
| :meth:`~object.__index__`. (Contributed by Mark Dickinson in :gh:`119743`.) | ||
|
|
||
| * Converting negative Python integer to a C unsigned integer type now raises |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This will need to be moved to 3.15 and marked as an incompatible change.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.