Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-74020: Raise ValueError for negative values converted to unsigned#121114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
serhiy-storchaka wants to merge8 commits intopython:main
base:main
Choose a base branch
Loading
fromserhiy-storchaka:convert-negative-int-to-unsigned-value-error

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commentedJun 28, 2024
edited by github-actionsbot
Loading

Converting negative Python integer to a C unsigned integer type now raises ValueError, not OverflowError.

Affected C API: PyLong_AsUnsignedLong(), PyLong_AsUnsignedLongLong(), PyLong_AsSize_t() and the "b" format unit in PyArg_Parse().


📚 Documentation preview 📚:https://cpython-previews--121114.org.readthedocs.build/

nineteendo reacted with thumbs up emoji
…ignedConverting negative Python integer to a C unsigned integer type now raisesValueError, not OverflowError.Affected C API: PyLong_AsUnsignedLong(), PyLong_AsUnsignedLongLong(),PyLong_AsSize_t() and the "b" format unit in PyArg_Parse().
Copy link
Contributor

@skirpichevskirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would expect this breaks a lot of people code.

Lets do this with a deprecation period.

Comment on lines +1380 to +1381
self.assertRaises(ValueError, (-1).to_bytes, 2, 'big', signed=False)
self.assertRaises(ValueError, (-1).to_bytes, 2, 'little', signed=False)
Copy link
Contributor

Choose a reason for 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 andsigned=True.

Copy link
MemberAuthor

Choose a reason for 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.

Copy link
Contributor

Choose a reason for 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.

Copy link
Member

@picnixzpicnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm really worried that now, anyone usingPyLong_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 raisingDomainError which would inherit from OverflowErrorand 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)exceptOverflowError:ifX<0:raiseValueErrorraise

Or something that can store in addition the fact that something is< 0 or not, likePyLong_AsLongAndOverflow but forULong instead where we add also aisnegative flag:

int_PyLong_AsUnsignedLong(PyObject*long,int*overflow,int*negative);

@@ -1920,6 +1920,10 @@ Others
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
Copy link
Member

Choose a reason for 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.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@skirpichevskirpichevskirpichev left review comments

@picnixzpicnixzpicnixz left review comments

@StanFromIrelandStanFromIrelandStanFromIreland left review comments

@gpsheadgpsheadAwaiting requested review from gpshead

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@serhiy-storchaka@skirpichev@picnixz@StanFromIreland

[8]ページ先頭

©2009-2025 Movatter.jp