Skip to content

Commit f99aba0

Browse files
committed
pyo3-ffi: deprecate _PyLong_NumBits, use bit_length() for Python < 3.13
1 parent 1135935 commit f99aba0

File tree

3 files changed

+5
-18
lines changed

3 files changed

+5
-18
lines changed

newsfragments/10489.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Removed use of the CPython-internal `_PyUnicode_COMPACT_DATA` symbol from PyO3's FFI test suite.
22
Deprecated `_PySet_NextEntry` in `pyo3-ffi`; it is a CPython-internal API with no stability guarantee.
3+
Deprecated `_PyLong_NumBits` in `pyo3-ffi` and replaced its internal use with `int.bit_length()` for Python < 3.13 builds.

pyo3-ffi/src/longobject.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extern_libpython! {
8585
#[cfg(not(Py_LIMITED_API))]
8686
extern_libpython! {
8787
#[cfg_attr(PyPy, link_name = "_PyPyLong_NumBits")]
88+
#[deprecated(note = "`_PyLong_NumBits` is a CPython-internal API; use `PyLong_AsNativeBytes` on Python 3.13+ or `int.bit_length()` instead")]
8889
pub fn _PyLong_NumBits(obj: *mut PyObject) -> size_t;
8990
}
9091

src/conversions/num_bigint.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,9 @@ fn int_to_py_bytes<'py>(
318318
#[inline]
319319
#[cfg(any(not(Py_3_13), Py_LIMITED_API))]
320320
fn int_n_bits(long: &Bound<'_, PyInt>) -> PyResult<usize> {
321-
let py = long.py();
322-
#[cfg(not(Py_LIMITED_API))]
323-
{
324-
// fast path
325-
let n_bits = unsafe { crate::ffi::_PyLong_NumBits(long.as_ptr()) };
326-
if n_bits == (-1isize as usize) {
327-
return Err(crate::PyErr::fetch(py));
328-
}
329-
Ok(n_bits)
330-
}
331-
332-
#[cfg(Py_LIMITED_API)]
333-
{
334-
// slow path
335-
use crate::types::PyAnyMethods;
336-
long.call_method0(crate::intern!(py, "bit_length"))
337-
.and_then(|any| any.extract())
338-
}
321+
use crate::types::PyAnyMethods;
322+
long.call_method0(crate::intern!(long.py(), "bit_length"))
323+
.and_then(|any| any.extract())
339324
}
340325

341326
#[cfg(test)]

0 commit comments

Comments
 (0)