PyO3/pyo3 · error
size_t should fit the flag bits
Error message
size_t should fit the flag bits
What it means
The CPython <3.12 variant of PY_VECTORCALL_ARGUMENTS_OFFSET is computed as 1 << (size_t bits - 1) and guarded by an expect panicking 'size_t should fit the flag bits'. It's a defensive check that the shift is valid for the platform's size_t; on standard platforms it never fires.
Source
Thrown at pyo3-ffi/src/cpython/abstract_.rs:48
callable: *mut PyObject,
result: *mut PyObject,
where_: *const c_char,
) -> *mut PyObject;
#[cfg(not(PyPy))]
fn _PyObject_MakeTpCall(
tstate: *mut PyThreadState,
callable: *mut PyObject,
args: *const *mut PyObject,
nargs: Py_ssize_t,
keywords: *mut PyObject,
) -> *mut PyObject;
}
#[cfg(not(Py_3_12))]
const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = (1 as size_t)
.checked_shl((8 * core::mem::size_of::<size_t>() - 1) as u32)
.expect("size_t should fit the flag bits");
#[cfg(Py_3_12)] // public API from 3.12
use crate::PY_VECTORCALL_ARGUMENTS_OFFSET;
#[inline(always)]
pub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t {
let n = n & !PY_VECTORCALL_ARGUMENTS_OFFSET;
n.try_into().expect("cannot fail due to mask")
}
#[cfg(any(PyPy, Py_3_11))]
extern_libpython! {
#[cfg_attr(PyPy, link_name = "PyPyVectorcall_Function")]
pub fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcallfunc>;
}
#[cfg(not(any(PyPy, Py_3_11)))]
#[inline(always)]View on GitHub (pinned to ac9b6899d3)
Solutions
- Use a supported CPython/target combination
- Fix the target specification/toolchain so size_t has a standard width
- Report to pyo3 if a real-world target triggers this
Defensive patterns
Strategy: type-guard
Type guard
// ensure size_t can represent the shifted flag assert!(8 * std::mem::size_of::<usize>() >= 16);
Prevention
- Use mainstream platforms
- Test cross-compilation in CI
- Avoid nonstandard size_t target specs
When it happens
Trigger: Building for a target where (8 * size_of::<size_t>()) yields an invalid shift amount for size_t — only possible on exotic/nonstandard ABIs.
Common situations: Cross-compilation to unusual embedded targets; toolchain misconfiguration giving a bogus size_t size.
Related errors
- size_t should fit the flag bits
- cannot fail due to mask
- string contains nul bytes
- Neither abi3 or abi3t features are enabled
- Cannot target an abi3t version below {MINIMUM_SUPPORTED_VERS
AI-assisted analysis of PyO3/pyo3@ac9b6899d3 (2026-09-05).
Data as JSON: /api/errors/93628c5748cbe499.
Report an issue: GitHub.