{"record":{"id":"7d63c22cf929d43f","repo":"PyO3/pyo3","slug":"error","errorCode":null,"errorMessage":"{}","messagePattern":"\\{\\}","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/macros.rs","lineNumber":128,"sourceCode":"#[doc(hidden)]\nmacro_rules! py_run_impl {\n    ($py:expr, $($val:ident)+, $code:expr) => {{\n        use $crate::types::IntoPyDict;\n        use $crate::conversion::IntoPyObject;\n        use $crate::BoundObject;\n        let d = [$((stringify!($val), (&$val).into_pyobject($py).unwrap().into_any().into_bound()),)+].into_py_dict($py).unwrap();\n        $crate::py_run_impl!($py, *d, $code)\n    }};\n    ($py:expr, *$dict:expr, $code:expr) => {{\n        use ::core::option::Option::*;\n        if let ::core::result::Result::Err(e) = $py.run(&$crate::impl_::alloc::ffi::CString::new($code).unwrap(), None, Some(&$dict)) {\n            e.print($py);\n            // So when this c api function the last line called printed the error to stderr,\n            // the output is only written into a buffer which is never flushed because we\n            // panic before flushing. This is where this hack comes into place\n            $py.run(c\"import sys; sys.stderr.flush()\", None, None)\n                .unwrap();\n            ::core::panic!(\"{}\", $code)\n        }\n    }};\n}\n\n/// Wraps a Rust function annotated with [`#[pyfunction]`](macro@crate::pyfunction).\n///\n/// This can be used with [`PyModule::add_function`](crate::types::PyModuleMethods::add_function) to\n/// add free functions to a [`PyModule`](crate::types::PyModule) - see its documentation for more\n/// information.\n///\n/// # Examples\n/// ```\n/// use pyo3::prelude::*;\n/// #[pyfunction]\n/// fn add(x: i32, y: i32) -> i32 {\n///     x + y\n/// }\n///","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/macros.rs#L110-L146","documentation":"This is the panic! expansion inside PyO3's error-wrapping macros: after printing the Python exception to stderr and flushing it, the macro raises a Rust panic with the given message ($code). The message text is dynamic ('{}' interpolated from the call site), so the actual string depends on the enclosing macro use — commonly PyResult unwraps in internal C-API shims.","triggerScenarios":"Internal PyO3 macros that run C API calls, print the resulting Python error via e.print($py), flush stderr, and then panic with a caller-supplied code; hit when an embedded PyO3 C-API helper fails and the error is converted to a panic instead of a PyResult.","commonSituations":"Embedding scenarios where ffi-level helpers fail (syntax errors in generated code, missing modules); debugging macros like pyo3's internal error printing; misconfigured embedded interpreter calls.","solutions":["Read the Python traceback printed to stderr above the panic — it contains the real failure","Fix the underlying C-API/Python error reported (bad code string, missing module, wrong arguments)","Prefer APIs returning PyResult and handle errors instead of the panicking macro paths","Wrap entry points so Python exceptions are captured before reaching panicking shims"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// validate Python state before invoking ffi-level helpers\nif unsafe { pyo3::ffi::PyErr_Occurred() }.is_null() { /* safe to call */ }","typeGuard":null,"tryCatchPattern":"std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| risky_call())).map_err(|e| /* extract payload, log stderr traceback */);","preventionTips":["Read the Python traceback printed to stderr above the panic for the root cause","Prefer PyResult-returning APIs over panicking macro helpers","Validate Python code/inputs passed to embedded run/call helpers"],"tags":["pyo3","panic","python-c-api","error-printing"],"backgroundTag":"python-error-panicked-in-rust","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"}