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-120389: Add PyLong_FromInt64() and PyLong_AsInt64() by vstinner · Pull Request #120390 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Jun 12, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Add new functions to convert C <stdint.h> numbers from/to Python int:

  • PyLong_FromInt32()
  • PyLong_FromUInt32()
  • PyLong_FromInt64()
  • PyLong_FromUInt64()
  • PyLong_AsInt32()
  • PyLong_AsUInt32()
  • PyLong_AsInt64()
  • PyLong_AsUInt64()
  • Issue: [C API] Add PyLong_FromInt64() and PyLong_ToInt64() #120389

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Jun 12, 2024
[C API] Add PyLong_FromInt64() and PyLong_ToInt64() #120389
Closed
@vstinner

vstinner commented Jun 12, 2024

Copy link
Copy Markdown
Member Author

Draft PR to see how #120389 can be implemented. I will complete the PR once we agree on #120389 design.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner added the skip news label Jun 12, 2024
@vstinner vstinner changed the title gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64() gh-120389: Add PyLong_FromInt64() and PyLong_ToInt64() Jun 17, 2024
@vstinner
vstinner force-pushed the stdint branch 4 times, most recently from 93e27a1 to 684360e Compare June 19, 2024 15:29
@vstinner
pythongh-120389: Add PyLong_FromInt64() and PyLong_ToInt64()
3e50341
Add new functions to convert C <stdint.h> numbers from/to Python int:

* PyLong_FromInt32()
* PyLong_FromUInt32()
* PyLong_FromInt64()
* PyLong_FromUInt64()
* PyLong_ToInt32()
* PyLong_ToUInt32()
* PyLong_ToInt64()
* PyLong_ToUInt64()
@vstinner
vstinner force-pushed the stdint branch from 684360e to 3e50341 Compare June 19, 2024 15:47
@vstinner
Add uint32_t to Doc/conf.py
7c7b976
@vstinner
vstinner marked this pull request as ready for review June 19, 2024 16:36
@vstinner
vstinner requested review from a team and encukou as code owners June 19, 2024 16:36
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 19, 2024
@vstinner

vstinner commented Jun 19, 2024

Copy link
Copy Markdown
Member Author

This PR is now ready for review.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Jun 19, 2024
Avoid C-specific Types capi-workgroup/api-evolution#10
Closed
zooba
zooba reviewed Jun 19, 2024
View reviewed changes
Comment thread Objects/longobject.c Resolved

Uh oh!

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

@vstinner
Use PyLong_AsNativeBytes()
c239d18
PyLong_ToUInt32() and PyLong_ToUInt64() can now use the __index__()
method if the object has the method.
zooba
zooba reviewed Jun 20, 2024
View reviewed changes
Comment thread Doc/c-api/long.rst Resolved

Uh oh!

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

zooba
zooba approved these changes Jun 20, 2024
View reviewed changes

@zooba zooba 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.

LGTM. Would like it if we don't have to have docs like "like this other function", but if that's the best we can do then I'll survive.

Sorry, something went wrong.

Uh oh!

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

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jun 20, 2024
@vstinner
Group documentations
1186043
@vstinner

vstinner commented Jun 20, 2024

Copy link
Copy Markdown
Member Author

Would like it if we don't have to have docs like "like this other function", but if that's the best we can do then I'll survive.

I grouped functions by pairs in the documentation.

All reactions

Sorry, something went wrong.

Uh oh!

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

@zooba

zooba commented Jun 20, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

The formatting in the docs preview looks good 👍

image
image

vstinner and encukou reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Jun 24, 2024
Add PyLong_FromInt64() and PyLong_AsInt64() capi-workgroup/decisions#32
Closed
@serhiy-storchaka

serhiy-storchaka commented Jun 24, 2024

Copy link
Copy Markdown
Member

Calling __index__ can release the GIL and make values saved in C variables (borrowed references, the size of a list, etc) invalid. Existing code can rely on atomicity of PyLong_AsUnsignedLong(), so calling __index__ in it is an unsafe change.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Jun 24, 2024

Copy link
Copy Markdown
Member Author

Existing code can rely on atomicity of PyLong_AsUnsignedLong(), so calling index in it is an unsafe change.

This PR doesn't change PyLong_AsUnsignedLong(). Are you referring to code replacing PyLong_AsUnsignedLong() with one of these functions?

Calling index can release the GIL and make values saved in C variables (borrowed references, the size of a list, etc) invalid.

For me, it's strange that signed and unsigned integers are treated diffferently. I would like to treat them the same.

The GIL issue can be mentioned in the doc? For example:

This function can release the GIL temporarily indirectly while calling an __index__() method.

zooba reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Jun 24, 2024

Copy link
Copy Markdown
Member Author

I created capi-workgroup/decisions#32 "Add PyLong_FromInt64() and PyLong_ToInt64()" in the C API WG Decisions project.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Jul 4, 2024
The C-API for Python to C integer conversion is, to be frank, a mess. #102471
Open
serhiy-storchaka
serhiy-storchaka approved these changes Jul 22, 2024
View reviewed changes

@serhiy-storchaka serhiy-storchaka 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.

LGTM.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Modules/_testlimitedcapi/long.c Resolved

Uh oh!

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

Comment thread Modules/_testcapimodule.c
Comment on lines +4050 to +4055
PyModule_AddObject(m, "INT32_MIN", PyLong_FromInt32(INT32_MIN));
PyModule_AddObject(m, "INT32_MAX", PyLong_FromInt32(INT32_MAX));
PyModule_AddObject(m, "UINT32_MAX", PyLong_FromUInt32(UINT32_MAX));
PyModule_AddObject(m, "INT64_MIN", PyLong_FromInt64(INT64_MIN));
PyModule_AddObject(m, "INT64_MAX", PyLong_FromInt64(INT64_MAX));
PyModule_AddObject(m, "UINT64_MAX", PyLong_FromUInt64(UINT64_MAX));

@serhiy-storchaka serhiy-storchaka Jul 22, 2024

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 is not needed. You can define them in the Python code as INT32_MAX = 2**31 - 1 etc.

Sorry, something went wrong.

Uh oh!

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

All reactions

@vstinner vstinner Aug 27, 2024

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.

I prefer to reuse <limits.h> C constants to avoid any typo.

Sorry, something went wrong.

Uh oh!

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

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Lib/test/test_capi/test_long.py Resolved

Uh oh!

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

@serhiy-storchaka serhiy-storchaka mentioned this pull request Jul 22, 2024
Refactor test_capi.test_long #122113
Merged
picnixz
picnixz reviewed Jul 22, 2024
View reviewed changes
Comment thread Doc/c-api/long.rst Resolved

Uh oh!

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

Comment thread Doc/c-api/long.rst Resolved

Uh oh!

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

Comment thread Objects/longobject.c Resolved

Uh oh!

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

Comment thread Objects/longobject.c Resolved

Uh oh!

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

vstinner added 5 commits August 27, 2024 14:45
@vstinner
Replace "To" with "As"
c2150c1
@vstinner
Merge branch 'main' into stdint
050f415
@vstinner
Use Py_ASNATIVEBYTES_ALLOW_INDEX
830d250
@vstinner
stable_abi: replace "To" with "As"
42b9902
@vstinner
Address Bénédikt's review
0199a80
picnixz
picnixz approved these changes Aug 27, 2024
View reviewed changes
@vstinner vstinner changed the title gh-120389: Add PyLong_FromInt64() and PyLong_ToInt64() gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64() Aug 27, 2024
@vstinner

vstinner commented Aug 27, 2024

Copy link
Copy Markdown
Member Author

@serhiy-storchaka @zooba @encukou: Would you mind to review the updated PR? I updated the PR to the API approved the C API WG. It now uses the __index__() method and functions are called "As" (instead of "To").

All reactions

Sorry, something went wrong.

Uh oh!

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

serhiy-storchaka
serhiy-storchaka approved these changes Aug 27, 2024
View reviewed changes

@serhiy-storchaka serhiy-storchaka 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.

LGTM, but check_long_asunsignedint is now superseded by check_long_asint.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_capi/test_long.py Resolved

Uh oh!

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

@vstinner
Remove check_long_asunsignedint() in tests
71748a7
Reuse check_long_asint().
@vstinner

vstinner commented Aug 28, 2024

Copy link
Copy Markdown
Member Author

but check_long_asunsignedint is now superseded by check_long_asint.

I removed check_long_asunsignedint() to reuse check_long_asint().

All reactions

Sorry, something went wrong.

Uh oh!

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

serhiy-storchaka
serhiy-storchaka approved these changes Aug 28, 2024
View reviewed changes
encukou
encukou approved these changes Aug 28, 2024
View reviewed changes
Comment thread Doc/c-api/long.rst Resolved

Uh oh!

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

Comment thread Doc/c-api/long.rst Resolved

Uh oh!

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

vstinner and others added 2 commits August 28, 2024 11:51
@vstinner @encukou
Update Doc/c-api/long.rst
dd2194e
Co-authored-by: Petr Viktorin <[email protected]>
@vstinner @encukou
Update Doc/c-api/long.rst
7070514
Co-authored-by: Petr Viktorin <[email protected]>
@vstinner
vstinner enabled auto-merge (squash) August 28, 2024 09:52
@vstinner
vstinner merged commit 4c6dca8 into python:main Aug 28, 2024
@vstinner
vstinner deleted the stdint branch August 28, 2024 10:16
@bedevere-app bedevere-app Bot removed the awaiting merge label Aug 28, 2024
@KRRT7 KRRT7 mentioned this pull request May 28, 2026
Widen specialized int fast paths to full int64 range #150424
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou approved these changes
@zooba zooba zooba approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@picnixz picnixz picnixz approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

skip news

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.

5 participants

@vstinner @zooba @serhiy-storchaka @encukou @picnixz
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 01402503 — 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.