Skip to content

Commit

Permalink
Add BaseExceptionGroup for Python >= 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed May 7, 2023
1 parent 1cdac4f commit 16d333f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions newsfragments/3141.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add FFI definition for `BaseExceptionGroup`, which was added in Python 3.11 by [PEP 0654](https://peps.python.org/pep-0654/).
2 changes: 2 additions & 0 deletions pyo3-ffi/src/pyerrors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pub unsafe fn PyUnicodeDecodeError_Create(
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyExc_BaseException")]
pub static mut PyExc_BaseException: *mut PyObject;
#[cfg(Py_3_11)]
pub static mut PyExc_BaseExceptionGroup: *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyExc_Exception")]
pub static mut PyExc_Exception: *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyExc_StopAsyncIteration")]
Expand Down
13 changes: 11 additions & 2 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ impl_native_exception!(
PyExc_UnicodeError,
native_doc!("UnicodeError")
);
// these three errors need arguments, so they're too annoying to write tests for using macros...
// these four errors need arguments, so they're too annoying to write tests for using macros...
impl_native_exception!(
PyUnicodeDecodeError,
PyExc_UnicodeDecodeError,
Expand All @@ -509,6 +509,12 @@ impl_native_exception!(
PyExc_UnicodeTranslateError,
native_doc!("UnicodeTranslateError", "")
);
#[cfg(Py_3_11)]
impl_native_exception!(
PyBaseExceptionGroup,
PyExc_BaseExceptionGroup,
native_doc!("BaseExceptionGroup", "")
);
impl_native_exception!(PyValueError, PyExc_ValueError, native_doc!("ValueError"));
impl_native_exception!(
PyZeroDivisionError,
Expand Down Expand Up @@ -1028,7 +1034,10 @@ mod tests {
);
});
}

#[cfg(Py_3_11)]
test_exception!(PyBaseExceptionGroup, |_| {
PyBaseExceptionGroup::new_err(("msg", vec![PyValueError::new_err("err")]))
});
test_exception!(PyBaseException);
test_exception!(PyException);
test_exception!(PyStopAsyncIteration);
Expand Down

0 comments on commit 16d333f

Please sign in to comment.