Skip to content

[C API] Add PyLong_GetNumBits() function #119714

Closed
@vstinner

Description

@vstinner

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:

Py_ssize_t _PyLong_GetNumBits(PyObject *obj);
  • Return the number of bits on success: greater than or equal to zero.
  • Set an exception and return -1 on error.
  • Set an OverflowError exception, and return -1 if the number of bits doesn't fit into Py_ssize_t.

The C function is similar to the Python int.bit_length() method.

See also the proposed PyLong_GetSign() function.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions