-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
[C API] Add PyLong_GetNumBits() function #119714
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
Comments
Why not return |
We have a size type that can be negative to express errors. IMO, this should be Can we first add the underscored version back? IMO, we should design the API first, and only then start removing a function that's been there since 2003 and is used in popular software like mariadb or panda3d. |
The revert is discussed in a separated issue: #119336 I just enabled automerge on its PR: #119418. First, I wasn't sure that it's worth it to restore it if a single project uses it, since you can easily call |
I prefer to return an |
I wrote the PR #119715 to implement the function. |
It is still limited. This is why Perhaps returning a 64-bit integer, even on 32-bit platform, can solve the issue. In this case it does not matter whether the return type is signed or unsigned. |
IMO 2**31 limit on a 32-bit system is a reasonable limitation, since Python is slow to manage such very large number. The OverflowError is already documented in the API documentation, we can also suggest calling |
Done in the PR. |
I created a ticket in the C API Working Group: capi-workgroup/decisions#28 |
See also previous slightly related discussion: |
The C API Working Group rejected this API: |
Feature or enhancement
In Python 3.13 alpha1, I removed the private _PyLong_NumBits() function. It seems like this function is used by pywin32 and MariaDB projects: see issue gh-119336.
I propose to add a public function to replace it:
-1
on error.OverflowError
exception, and return-1
if the number of bits doesn't fit intoPy_ssize_t
.The C function is similar to the Python
int.bit_length()
method.See also the proposed PyLong_GetSign() function.
Linked PRs
The text was updated successfully, but these errors were encountered: