{"record":{"id":"4996be2fe3719832","repo":"PyO3/pyo3","slug":"length-of-buffer-fits-in-py-ssize-t","errorCode":null,"errorMessage":"length of buffer fits in Py_ssize_t","messagePattern":"length of buffer fits in Py_ssize_t","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/conversions/std/num.rs","lineNumber":600,"sourceCode":"                            )\n                        })?;\n                        Ok(<$rust_type>::from_le_bytes(buffer))\n                    }\n                    #[cfg(Py_3_13)]\n                    {\n                        let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN;\n                        if !$is_signed {\n                            flags |= ffi::Py_ASNATIVEBYTES_UNSIGNED_BUFFER\n                                | ffi::Py_ASNATIVEBYTES_REJECT_NEGATIVE;\n                        }\n                        let actual_size: usize = unsafe {\n                            ffi::PyLong_AsNativeBytes(\n                                num.as_ptr(),\n                                buffer.as_mut_ptr().cast(),\n                                buffer\n                                    .len()\n                                    .try_into()\n                                    .expect(\"length of buffer fits in Py_ssize_t\"),\n                                flags,\n                            )\n                        }\n                        .try_into()\n                        .map_err(|_| PyErr::fetch(ob.py()))?;\n                        if actual_size as usize > buffer.len() {\n                            return Err(crate::exceptions::PyOverflowError::new_err(\n                                \"Python int larger than 128 bits\",\n                            ));\n                        }\n                        Ok(<$rust_type>::from_ne_bytes(buffer))\n                    }\n                }\n            }\n        };\n    }\n\n    int_convert_128!(i128, true);","sourceCodeStart":582,"sourceCodeEnd":618,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/conversions/std/num.rs#L582-L618","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hypothetical only — exotic embedded targets or a future platform where cPy_ssize_t is narrower than the buffer's length.","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."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"fn buffer_fits_ssize(len: usize) -> bool { len <= isize::MAX as usize }","tryCatchPattern":"// Not catchable in Rust (panic); on supported platforms it is statically unreachable.\n// If paranoid, wrap the conversion call in catch_unwind.","preventionTips":["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."],"tags":["pyo3","integer-conversion","platform","unreachable"],"backgroundTag":"unreachable-invariant-panic","analyzedSha":"ac9b6899d348be4d54614d060dea53a645a12e36","analyzedAt":"2026-09-05T09:20:35.319Z","contentChangedAt":"2026-09-05T09:20:35.319Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}