GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-74020: Raise ValueError for negative values converted to unsigned by serhiy-storchaka · Pull Request #121114 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jun 28, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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().

  • Issue: Raise ValueError rather of OverflowError in PyLong_AsUnsignedLong() #74020

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

nineteendo reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@serhiy-storchaka
pythongh-74020: Raise ValueError for negative values converted to uns…
ff4cfc8
…igned

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().
@serhiy-storchaka
serhiy-storchaka requested a review from ericsnowcurrently as a code owner June 28, 2024 07:58
@bedevere-app bedevere-app Bot mentioned this pull request Jun 28, 2024
Raise ValueError rather of OverflowError in PyLong_AsUnsignedLong() #74020
Open
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 28, 2024
@serhiy-storchaka
Fix some tests on Windows.
1af7897
@serhiy-storchaka serhiy-storchaka mentioned this pull request Jul 1, 2024
Conversion to unsigned integers capi-workgroup/api-evolution#49
Open
@ericsnowcurrently
ericsnowcurrently removed their request for review November 19, 2024 21:30
StanFromIreland
StanFromIreland reviewed Apr 23, 2025
View reviewed changes
Comment thread Doc/c-api/arg.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added 2 commits April 28, 2025 23:36
@serhiy-storchaka
Merge branch 'main' into convert-negative-int-to-unsigned-value-error
51c8b22
@serhiy-storchaka
Use "versionchanged:: next".
8701b4a
@serhiy-storchaka
serhiy-storchaka requested review from gpshead and picnixz as code owners April 28, 2025 21:07
skirpichev
skirpichev reviewed Apr 29, 2025
View reviewed changes

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_long.py
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)

@skirpichev skirpichev Apr 29, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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 and signed=True.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Apr 29, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@skirpichev skirpichev Apr 29, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
serhiy-storchaka added 4 commits April 29, 2025 10:38
@serhiy-storchaka
Update docs for int.to_bytes().
7685781
@serhiy-storchaka
Copy tests from socket-unsigned-int-index.
0300d31
@serhiy-storchaka
Remove unneeded bitwise=False.
8526ac6
@serhiy-storchaka
Merge branch 'main' into convert-negative-int-to-unsigned-value-error
d54127d
picnixz
picnixz reviewed May 9, 2025
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/whatsnew/3.14.rst
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

@picnixz picnixz May 9, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions

github-actions Bot commented Apr 17, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@skirpichev skirpichev skirpichev left review comments
@picnixz picnixz picnixz left review comments
@StanFromIreland StanFromIreland StanFromIreland left review comments
@gpshead gpshead Awaiting requested review from gpshead

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@serhiy-storchaka @skirpichev @picnixz @StanFromIreland
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.