PyO3/pyo3 · info
length of buffer fits in Py_ssize_t
Error message
length of buffer fits in Py_ssize_t
What it means
When converting a Python int to a native integer, PyO3 uses PyLong_AsNativeBytes with a fixed-size stack buffer; the buffer length is cast to Py_ssize_t with expect. The buffer is a compile-time constant (well under isize::MAX), so this panic is statically impossible in practice and documents a platform-width invariant.
Source
Thrown at src/conversions/std/num.rs:600
)
})?;
Ok(<$rust_type>::from_le_bytes(buffer))
}
#[cfg(Py_3_13)]
{
let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN;
if !$is_signed {
flags |= ffi::Py_ASNATIVEBYTES_UNSIGNED_BUFFER
| ffi::Py_ASNATIVEBYTES_REJECT_NEGATIVE;
}
let actual_size: usize = unsafe {
ffi::PyLong_AsNativeBytes(
num.as_ptr(),
buffer.as_mut_ptr().cast(),
buffer
.len()
.try_into()
.expect("length of buffer fits in Py_ssize_t"),
flags,
)
}
.try_into()
.map_err(|_| PyErr::fetch(ob.py()))?;
if actual_size as usize > buffer.len() {
return Err(crate::exceptions::PyOverflowError::new_err(
"Python int larger than 128 bits",
));
}
Ok(<$rust_type>::from_ne_bytes(buffer))
}
}
}
};
}
int_convert_128!(i128, true);View on GitHub (pinned to ac9b6899d3)
Solutions
- No action needed on supported platforms.
- If targeting an exotic platform, verify that isize can represent small buffer sizes (it always can) before using this conversion path.
- Report to pyo3 only if this actually fires on a real target.
Defensive patterns
Strategy: type-guard
Type guard
fn buffer_fits_ssize(len: usize) -> bool { len <= isize::MAX as usize } Try / catch
// Not catchable in Rust (panic); on supported platforms it is statically unreachable. // If paranoid, wrap the conversion call in catch_unwind.
Prevention
- No action needed on supported platforms.
- When porting to exotic targets, confirm cPy_ssize_t covers small buffer sizes.
- Keep the buffer size a small compile-time constant.
When it happens
Trigger: Effectively unreachable: would require a target platform where usize does not fit in Py_ssize_t (e.g. isize smaller than the buffer size), which does not exist among supported platforms.
Common situations: Hypothetical only — exotic embedded targets or a future platform where cPy_ssize_t is narrower than the buffer's length.
Related errors
- seconds overflow
- Attaching a thread to the interpreter is prohibited while a
- Cannot attach to the Python interpreter while it is finalizi
- Attaching a thread to the interpreter is currently prohibite
- Cannot drop pointer into Python heap without the thread bein
AI-assisted analysis of PyO3/pyo3@ac9b6899d3 (2026-09-05).
Data as JSON: /api/errors/4996be2fe3719832.
Report an issue: GitHub.