FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64() by vstinner · Pull Request #120390 · python/cpython · GitHub

/ cpython Public

gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64() - #120390

Merged
vstinner merged 12 commits into
python:mainfrom
vstinner:stdint
Aug 28, 2024
Merged

gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64()#120390
vstinner merged 12 commits into
python:mainfrom
vstinner:stdint

Conversation

vstinner commented Jun 12, 2024
edited
Loading

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

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.

vstinner changed the title gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64() gh-120389: Add PyLong_FromInt64() and PyLong_ToInt64() Jun 17, 2024
vstinner force-pushed the stdint branch 4 times, most recently from 93e27a1 to 684360e Compare June 19, 2024 15:29
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 marked this pull request as ready for review June 19, 2024 16:36
vstinner requested review from a team and encukou as code owners June 19, 2024 16:36

Copy link
Copy Markdown
Member Author

This PR is now ready for review.

Comment thread Objects/longobject.c Outdated
PyLong_ToUInt32() and PyLong_ToUInt64() can now use the __index__()
method if the object has the method.
Comment thread Doc/c-api/long.rst Outdated

zooba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

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.

zooba commented Jun 20, 2024
edited
Loading

Copy link
Copy Markdown
Member

The formatting in the docs preview looks good 👍


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.

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.

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.

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM.

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));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is not needed. You can define them in the Python code as INT32_MAX = 2**31 - 1 etc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Doc/c-api/long.rst Outdated
Comment thread Doc/c-api/long.rst Outdated
Comment thread Objects/longobject.c Outdated
Comment thread Objects/longobject.c
vstinner changed the title gh-120389: Add PyLong_FromInt64() and PyLong_ToInt64() gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64() 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").

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM, but check_long_asunsignedint is now superseded by check_long_asint.

Comment thread Lib/test/test_capi/test_long.py Outdated

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

Comment thread Doc/c-api/long.rst Outdated
Comment thread Doc/c-api/long.rst Outdated
vstinner and others added 2 commits August 28, 2024 11:51
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
vstinner enabled auto-merge (squash) August 28, 2024 09:52
vstinner merged commit 4c6dca8 into python:main Aug 28, 2024
vstinner deleted the stdint branch August 28, 2024 10:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL