Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C API] Add PyLong_AsInt() public function #108444

Closed
vstinner opened this issue Aug 24, 2023 · 1 comment
Closed

[C API] Add PyLong_AsInt() public function #108444

vstinner opened this issue Aug 24, 2023 · 1 comment
Labels
type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Aug 24, 2023

Feature or enhancement

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Proposal:

The _PyLong_AsInt() function was added in 2013 by commit 74f49ab. It was added a private function (_Py) prefix and it's not part of the limited C API.

This function is widely used in the Python code base which means that it's useful. Casting PyLong_AsLong() to int and then checking for overflow is error prone, it requires to raise the right exception.

Using a private function is a bad practive: I removed many private functions from Python 3.13 C API, see issue #106320.

I propose to add PyLong_AsInt() to the limited C API, add documentation and tests.

Linked PRs

@vstinner vstinner added the type-feature A feature request or enhancement label Aug 24, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
* Rename _PyLong_AsInt() to PyLong_AsInt().
* Add documentation.
* Add test.
* For now, keep _PyLong_AsInt() as an alias to PyLong_AsInt().
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
* Rename _PyLong_AsInt() to PyLong_AsInt().
* Add documentation.
* Add test.
* For now, keep _PyLong_AsInt() as an alias to PyLong_AsInt().
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
* Rename _PyLong_AsInt() to PyLong_AsInt().
* Add documentation.
* Add test.
* For now, keep _PyLong_AsInt() as an alias to PyLong_AsInt().
vstinner added a commit that referenced this issue Aug 24, 2023
* Rename _PyLong_AsInt() to PyLong_AsInt().
* Add documentation.
* Add test.
* For now, keep _PyLong_AsInt() as an alias to PyLong_AsInt().
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
Argument Clinic now uses the new public PyLong_AsInt(), rather than
the old name _PyLong_AsInt().
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
Change generated by the command:

    sed -i -e 's!_PyLong_AsInt!PyLong_AsInt!g' \
        $(find -name "*.c" -o -name "*.h")
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
Change generated by the command:

sed -i -e 's!_PyLong_AsInt!PyLong_AsInt!g' \
    $(find -name "*.c" -o -name "*.h")
vstinner added a commit that referenced this issue Aug 24, 2023
Argument Clinic now uses the new public PyLong_AsInt(), rather than
the old name _PyLong_AsInt().
vstinner added a commit that referenced this issue Aug 24, 2023
Change generated by the command:

sed -i -e 's!_PyLong_AsInt!PyLong_AsInt!g' \
    $(find -name "*.c" -o -name "*.h")
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
* Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
* Remove _PyLong_AsInt() alias to PyLong_AsInt().
vstinner added a commit that referenced this issue Aug 25, 2023
* Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
* Remove _PyLong_AsInt() alias to PyLong_AsInt().
@vstinner
Copy link
Member Author

Function added by be436e0

I also added the function to pythoncapi-compat: python/pythoncapi-compat@8109811

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant