{"record":{"id":"cfa074a7e094490a","repo":"PyO3/pyo3","slug":"converting-pyerr-arguments-failed","errorCode":null,"errorMessage":"Converting PyErr arguments failed: {}","messagePattern":"Converting PyErr arguments failed: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/err/mod.rs","lineNumber":69,"sourceCode":"\n/// Represents the result of a Python call.\npub type PyResult<T> = Result<T, PyErr>;\n\n/// Helper conversion trait that allows to use custom arguments for lazy exception construction.\npub trait PyErrArguments: Send + Sync {\n    /// Arguments for exception\n    fn arguments(self, py: Python<'_>) -> Py<PyAny>;\n}\n\nimpl<T> PyErrArguments for T\nwhere\n    T: for<'py> IntoPyObject<'py> + Send + Sync,\n{\n    fn arguments(self, py: Python<'_>) -> Py<PyAny> {\n        // FIXME: `arguments` should become fallible\n        match self.into_pyobject(py) {\n            Ok(obj) => obj.into_any().unbind(),\n            Err(e) => panic!(\"Converting PyErr arguments failed: {}\", e.into()),\n        }\n    }\n}\n\nimpl PyErr {\n    /// Creates a new PyErr of type `T`.\n    ///\n    /// `args` can be:\n    /// * a tuple: the exception instance will be created using the equivalent to the Python\n    ///   expression `T(*tuple)`\n    /// * any other value: the exception instance will be created using the equivalent to the Python\n    ///   expression `T(value)`\n    ///\n    /// This exception instance will be initialized lazily. This avoids the need for the Python GIL\n    /// to be held, but requires `args` to be `Send` and `Sync`. If `args` is not `Send` or `Sync`,\n    /// consider using [`PyErr::from_value`] instead.\n    ///\n    /// If `T` does not inherit from `BaseException`, then a `TypeError` will be returned.","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/err/mod.rs#L51-L87","documentation":"PyErr::arguments converts the exception's arguments into a Python object and is documented as infallible, but IntoPyObject can now fail. Since the signature can't return the error, pyo3 panics with the conversion error.","triggerScenarios":"Creating an exception whose args' IntoPyObject conversion fails (e.g. PyTypeError::new_err(value) where value's IntoPyObject impl returns Err, such as a type that can't be converted in the current interpreter configuration).","commonSituations":"new_err with a custom type whose IntoPyObject conversion errors at runtime; API evolution where arguments() was fallible-ized but callers kept the old infallible path.","solutions":["Ensure the value passed to new_err has an infallible IntoPyObject impl (or convert to a supported type first, e.g. String)","Provide your own error message instead of relying on arguments conversion","Downcast/manually build the exception with .into_pyobject and handle the Err case yourself"],"exampleFix":"// before\nPyValueError::new_err(my_custom_obj)\n// after\nPyValueError::new_err(format!(\"{}\", my_custom_obj))","handlingStrategy":"validation","validationCode":"fn args_are_trivially_convertible<T: for<'py> IntoPyObject<'py>>() -> bool { true } // prefer plain str/String/numbers as PyErr args","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass only infallibly-convertible types (String, i64, f64, &str) to new_err","Format custom objects with Display before using them as exception args","Test exception creation in debug mode to surface conversion failures early"],"tags":["pyerr","into-pyobject","conversion","panic"],"backgroundTag":"conversion-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"}