Skip to content

'_PyLong_NumBits': identifier not found in 3.13 #119336

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

Closed
clin1234 opened this issue May 21, 2024 · 9 comments
Closed

'_PyLong_NumBits': identifier not found in 3.13 #119336

clin1234 opened this issue May 21, 2024 · 9 comments
Labels
topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@clin1234
Copy link

clin1234 commented May 21, 2024

Bug report

Bug description:

While fixing pywin32 to let CI builds work in Python 3.13, the build process complained of not being able to find _PyLong_NumBits. All other Python versions in the CI built successfully.

BOOL PyCom_VariantFromPyObject(PyObject *obj, VARIANT *var)
{
    // ...
    if (PyLong_Check(obj)) {
        int sign = _PyLong_Sign(obj);
        size_t nbits = _PyLong_NumBits(obj);
        if (nbits == (size_t)-1 && PyErr_Occurred())
            return FALSE;
    // ...
}

The function still exists in main, apparently inaccessible via the public C API since 3.13. What's the recommended replacement?

CPython versions tested on:

3.8, 3.9, 3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Windows

Linked PRs

@clin1234 clin1234 added the type-bug An unexpected behavior, bug, or error label May 21, 2024
@emmatyping
Copy link
Member

emmatyping commented May 22, 2024

In 5846924 Victor restored

  • _PyLong_Copy()
  • _PyLong_FromDigits()
  • _PyLong_New()
  • _PyLong_Sign()

It seems reasonable to include _PyLong_NumBits as well, since like the others there isn't a great replacement.

@clin1234
Copy link
Author

Will it be restored in the next beta 3.13 release, or will I have to wait until the first 3.14 alpha release?

emmatyping added a commit to emmatyping/cpython that referenced this issue May 22, 2024
This is used in pywin32, and the function being moved to internal headers
broke with 3.13. Restore it for now to eventually be considered for being
made a public symbol.

See also pythongh-112026 (previous restoration) and pythongh-111481 (public replacements)
@emmatyping
Copy link
Member

I cannot make that call, but I made #119418 against 3.14 for now (it can be backported for 3.13 if that's what people decide). Eventually it seems like a good idea to make this public.

@vstinner
Copy link
Member

You can modify pywin32 to call the bit_length() method which returns a Python int object. Then if you need a size_t, you can call PyLong_AsSize_t.

If this function is called by one project, we should consider making it public, rather than restoring the old private function.

@clin1234
Copy link
Author

You can modify pywin32 to call the bit_length() method which returns a Python int object. Then if you need a size_t, you can call PyLong_AsSize_t.

If this function is called by one project, we should consider making it public, rather than restoring the old private function.

How would I access the built-in bit_length() method within the C API?

@da-woods
Copy link
Contributor

How would I access the built-in bit_length() method within the C API?

PyObject_CallMethod(obj, "bit_length", NULL)

(Not taking a view on whether that justifies not restoring the function though)

@vstinner
Copy link
Member

A code search in PyPI top 7,500 projects found other projects:

  • mariadb (1.1.10)
  • pickle5 (0.0.12)
  • zodbpickle (3.2)

pickle5 and zodbpickle contain copies of the Python source code, but MariaDB is a good usage example. So maybe it's worth it to add a function.

I created issue gh-119714: [C API] Add PyLong_GetNumBits() function.

@vstinner
Copy link
Member

A code search in PyPI top 7,500 projects found other projects:

@encukou also identified that panda3d uses it: https://github.com/panda3d/panda3d/blob/adb92428856853019583f398d27c5d34e44a9343/panda/src/putil/bitArray_ext.cxx#L28

vstinner pushed a commit that referenced this issue May 30, 2024
@vstinner
Copy link
Member

vstinner commented Jun 3, 2024

Function restored by commit e50fac9

@vstinner vstinner closed this as completed Jun 3, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 3, 2024
…-119418)

It is used by the pywin32 project.
(cherry picked from commit e50fac9)

Co-authored-by: Ethan Smith <ethan@ethanhs.me>
vstinner pushed a commit that referenced this issue Jun 3, 2024
…) (#119970)

gh-119336: Restore removed _PyLong_NumBits() function (GH-119418)

It is used by the pywin32 project.
(cherry picked from commit e50fac9)

Co-authored-by: Ethan Smith <ethan@ethanhs.me>
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants