From 9a2aa8e7829460b41f00352af4f7f047d434f190 Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Sun, 19 Apr 2026 10:49:23 -0400 Subject: [PATCH 1/9] Add PyNameErrorObject and PyAttributeErrorObject structs --- pyo3-ffi/src/cpython/pyerrors.rs | 66 +++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/pyo3-ffi/src/cpython/pyerrors.rs b/pyo3-ffi/src/cpython/pyerrors.rs index c9831669ac7..e020038f713 100644 --- a/pyo3-ffi/src/cpython/pyerrors.rs +++ b/pyo3-ffi/src/cpython/pyerrors.rs @@ -1,6 +1,7 @@ use crate::PyObject; #[cfg(not(any(PyPy, GraalPy)))] use crate::Py_ssize_t; +use std::ffi::{c_char, c_int}; #[repr(C)] #[derive(Debug)] @@ -156,8 +157,48 @@ pub struct PyStopIterationObject { // skipped _PyErr_ChainExceptions -// skipped PyNameErrorObject -// skipped PyAttributeErrorObject +#[repr(C)] +#[derive(Debug)] +pub struct PyNameErrorObject { + pub ob_base: PyObject, + #[cfg(not(PyPy))] + pub dict: *mut PyObject, + #[cfg(not(PyPy))] + pub args: *mut PyObject, + #[cfg(all(Py_3_11, not(PyPy)))] + pub notes: *mut PyObject, + #[cfg(not(PyPy))] + pub traceback: *mut PyObject, + #[cfg(not(PyPy))] + pub context: *mut PyObject, + #[cfg(not(PyPy))] + pub cause: *mut PyObject, + #[cfg(not(PyPy))] + pub suppress_context: char, + pub name: *mut PyObject +} + +#[repr(C)] +#[derive(Debug)] +pub struct PyAttributeErrorObject { + pub ob_base: PyObject, + #[cfg(not(PyPy))] + pub dict: *mut PyObject, + #[cfg(not(PyPy))] + pub args: *mut PyObject, + #[cfg(all(Py_3_11, not(PyPy)))] + pub notes: *mut PyObject, + #[cfg(not(PyPy))] + pub traceback: *mut PyObject, + #[cfg(not(PyPy))] + pub context: *mut PyObject, + #[cfg(not(PyPy))] + pub cause: *mut PyObject, + #[cfg(not(PyPy))] + pub suppress_context: char, + pub obj: *mut PyObject, + pub name: *mut PyObject +} // skipped PyEnvironmentErrorObject // skipped PyWindowsErrorObject @@ -188,3 +229,24 @@ pub struct PyStopIterationObject { // skipped _Py_FatalErrorFunc // skipped _Py_FatalErrorFormat // skipped Py_FatalError + +extern_libpython! { + pub fn PySignal_SetWakeupFd(fd: c_int) -> c_int; + pub fn PyErr_SyntaxLocationObject( + filename: *mut PyObject, + lineno: c_int, + col_offset: c_int); + pub fn PyErr_RangedSyntaxLocationObject( + filename: *mut PyObject, + lineno: c_int, + col_offset: c_int, + end_lineno: c_int, + end_col_offset: c_int); + pub fn PyErr_ProgramTextObject( + filename: *mut PyObject, + lineno: c_int) -> *mut PyObject; + pub(crate) fn _Py_FatalErrorFunc( + func: *const c_char, + message: *const c_char); + pub static PyExc_PythonFinalizationError: *mut PyObject; +} From 19f057650321d194803e047e02facace3ff0a03e Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Sun, 19 Apr 2026 10:50:57 -0400 Subject: [PATCH 2/9] news --- newsfragments/5989.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/5989.added.md diff --git a/newsfragments/5989.added.md b/newsfragments/5989.added.md new file mode 100644 index 00000000000..dff3300b294 --- /dev/null +++ b/newsfragments/5989.added.md @@ -0,0 +1 @@ +Add PyNameErrorObject and PyAttributeErrorObject structs, and miscellaneous API functions From b3247e6db9672cd42e3d4b3c5147d335028cb33a Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Sun, 19 Apr 2026 10:52:29 -0400 Subject: [PATCH 3/9] Fix formatting of name field in pyerrors.rs --- pyo3-ffi/src/cpython/pyerrors.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyo3-ffi/src/cpython/pyerrors.rs b/pyo3-ffi/src/cpython/pyerrors.rs index e020038f713..f33354862c5 100644 --- a/pyo3-ffi/src/cpython/pyerrors.rs +++ b/pyo3-ffi/src/cpython/pyerrors.rs @@ -175,7 +175,7 @@ pub struct PyNameErrorObject { pub cause: *mut PyObject, #[cfg(not(PyPy))] pub suppress_context: char, - pub name: *mut PyObject + pub name: *mut PyObject, } #[repr(C)] @@ -197,7 +197,7 @@ pub struct PyAttributeErrorObject { #[cfg(not(PyPy))] pub suppress_context: char, pub obj: *mut PyObject, - pub name: *mut PyObject + pub name: *mut PyObject, } // skipped PyEnvironmentErrorObject From d33447b53442eb2794702d41484d16076492c9a3 Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Sun, 19 Apr 2026 14:55:53 +0000 Subject: [PATCH 4/9] format-ffi-extern --- pyo3-ffi/src/cpython/pyerrors.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/pyo3-ffi/src/cpython/pyerrors.rs b/pyo3-ffi/src/cpython/pyerrors.rs index f33354862c5..583f01692dc 100644 --- a/pyo3-ffi/src/cpython/pyerrors.rs +++ b/pyo3-ffi/src/cpython/pyerrors.rs @@ -232,21 +232,15 @@ pub struct PyAttributeErrorObject { extern_libpython! { pub fn PySignal_SetWakeupFd(fd: c_int) -> c_int; - pub fn PyErr_SyntaxLocationObject( - filename: *mut PyObject, - lineno: c_int, - col_offset: c_int); + pub fn PyErr_SyntaxLocationObject(filename: *mut PyObject, lineno: c_int, col_offset: c_int); pub fn PyErr_RangedSyntaxLocationObject( filename: *mut PyObject, lineno: c_int, col_offset: c_int, end_lineno: c_int, - end_col_offset: c_int); - pub fn PyErr_ProgramTextObject( - filename: *mut PyObject, - lineno: c_int) -> *mut PyObject; - pub(crate) fn _Py_FatalErrorFunc( - func: *const c_char, - message: *const c_char); + end_col_offset: c_int, + ); + pub fn PyErr_ProgramTextObject(filename: *mut PyObject, lineno: c_int) -> *mut PyObject; + pub(crate) fn _Py_FatalErrorFunc(func: *const c_char, message: *const c_char); pub static PyExc_PythonFinalizationError: *mut PyObject; } From 47ee19df07c24fd5ba7e10ec570814bb0f1b7c72 Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Sun, 19 Apr 2026 10:57:53 -0400 Subject: [PATCH 5/9] Update pyo3-ffi/src/cpython/pyerrors.rs Co-authored-by: Thomas Tanon --- pyo3-ffi/src/cpython/pyerrors.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/pyo3-ffi/src/cpython/pyerrors.rs b/pyo3-ffi/src/cpython/pyerrors.rs index 583f01692dc..b056cb09f53 100644 --- a/pyo3-ffi/src/cpython/pyerrors.rs +++ b/pyo3-ffi/src/cpython/pyerrors.rs @@ -233,6 +233,7 @@ pub struct PyAttributeErrorObject { extern_libpython! { pub fn PySignal_SetWakeupFd(fd: c_int) -> c_int; pub fn PyErr_SyntaxLocationObject(filename: *mut PyObject, lineno: c_int, col_offset: c_int); + #[cfg(Py_3_10)] pub fn PyErr_RangedSyntaxLocationObject( filename: *mut PyObject, lineno: c_int, From c45ae503b179fda01e2be03284047817376fde11 Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Sun, 19 Apr 2026 15:03:50 +0000 Subject: [PATCH 6/9] Drop `_Py_FatalErrorFunc` --- pyo3-ffi/src/cpython/pyerrors.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pyo3-ffi/src/cpython/pyerrors.rs b/pyo3-ffi/src/cpython/pyerrors.rs index b056cb09f53..436248d5aba 100644 --- a/pyo3-ffi/src/cpython/pyerrors.rs +++ b/pyo3-ffi/src/cpython/pyerrors.rs @@ -242,6 +242,5 @@ extern_libpython! { end_col_offset: c_int, ); pub fn PyErr_ProgramTextObject(filename: *mut PyObject, lineno: c_int) -> *mut PyObject; - pub(crate) fn _Py_FatalErrorFunc(func: *const c_char, message: *const c_char); pub static PyExc_PythonFinalizationError: *mut PyObject; } From 6b45601941023a097111ab65bde62f083edaef2a Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Sun, 19 Apr 2026 15:10:07 +0000 Subject: [PATCH 7/9] Drop c_char --- pyo3-ffi/src/cpython/pyerrors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyo3-ffi/src/cpython/pyerrors.rs b/pyo3-ffi/src/cpython/pyerrors.rs index 436248d5aba..a724cfe00b7 100644 --- a/pyo3-ffi/src/cpython/pyerrors.rs +++ b/pyo3-ffi/src/cpython/pyerrors.rs @@ -1,7 +1,7 @@ use crate::PyObject; #[cfg(not(any(PyPy, GraalPy)))] use crate::Py_ssize_t; -use std::ffi::{c_char, c_int}; +use std::ffi::c_int; #[repr(C)] #[derive(Debug)] From 39fa69bcf73403ffb2641653539c716319b27e52 Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Thu, 23 Apr 2026 10:45:42 -0500 Subject: [PATCH 8/9] Clean up skipped error handling functions Removed several skipped error handling functions from pyerrors.rs. --- pyo3-ffi/src/cpython/pyerrors.rs | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/pyo3-ffi/src/cpython/pyerrors.rs b/pyo3-ffi/src/cpython/pyerrors.rs index a724cfe00b7..c90d440def8 100644 --- a/pyo3-ffi/src/cpython/pyerrors.rs +++ b/pyo3-ffi/src/cpython/pyerrors.rs @@ -155,8 +155,6 @@ pub struct PyStopIterationObject { pub value: *mut PyObject, } -// skipped _PyErr_ChainExceptions - #[repr(C)] #[derive(Debug)] pub struct PyNameErrorObject { @@ -203,30 +201,9 @@ pub struct PyAttributeErrorObject { // skipped PyEnvironmentErrorObject // skipped PyWindowsErrorObject -// skipped _PyErr_SetKeyError -// skipped _PyErr_GetTopmostException -// skipped _PyErr_GetExcInfo - -// skipped PyErr_SetFromErrnoWithUnicodeFilename - -// skipped _PyErr_FormatFromCause - -// skipped PyErr_SetFromWindowsErrWithUnicodeFilename -// skipped PyErr_SetExcFromWindowsErrWithUnicodeFilename - -// skipped _PyErr_TrySetFromCause - -// skipped PySignal_SetWakeupFd -// skipped _PyErr_CheckSignals - -// skipped PyErr_SyntaxLocationObject -// skipped PyErr_RangedSyntaxLocationObject -// skipped PyErr_ProgramTextObject +// skipped _PyErr_ChainExceptions +// skipped PyUnstable_Exc_PrepReraiseStar -// skipped _PyErr_ProgramDecodedTextObject -// skipped _PyUnicodeTranslateError_Create -// skipped _PyErr_WriteUnraisableMsg -// skipped _Py_FatalErrorFunc // skipped _Py_FatalErrorFormat // skipped Py_FatalError From d730b9079be2350ef3de29860d96d3f987ef6017 Mon Sep 17 00:00:00 2001 From: Charlie Lin Date: Thu, 23 Apr 2026 10:47:40 -0500 Subject: [PATCH 9/9] Add PyNameErrorObject, PyAttributeErrorObject structs Added new structs and several API functions related to error handling. --- newsfragments/5989.added.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/newsfragments/5989.added.md b/newsfragments/5989.added.md index dff3300b294..7dac86a49dc 100644 --- a/newsfragments/5989.added.md +++ b/newsfragments/5989.added.md @@ -1 +1,13 @@ -Add PyNameErrorObject and PyAttributeErrorObject structs, and miscellaneous API functions +Add `PyNameErrorObject` and `PyAttributeErrorObject` structs, and the following API functions: + +* pub fn PySignal_SetWakeupFd(fd: c_int) -> c_int +* pub fn PyErr_SyntaxLocationObject(filename: *mut PyObject, lineno: c_int, col_offset: c_int) +* pub fn PyErr_RangedSyntaxLocationObject + filename: *mut PyObject, + lineno: c_int, + col_offset: c_int, + end_lineno: c_int, + end_col_offset: c_int, + ) +* pub fn PyErr_ProgramTextObject(filename: *mut PyObject, lineno: c_int) -> *mut PyObject +* pub static PyExc_PythonFinalizationError: *mut PyObject