pub fn take(py: Python) -> Option<PyErr> {
        let (ptype, pvalue, ptraceback) = unsafe {
            let mut ptype: *mut ffi::PyObject = std::ptr::null_mut();
            let mut pvalue: *mut ffi::PyObject = std::ptr::null_mut();
            let mut ptraceback: *mut ffi::PyObject = std::ptr::null_mut();
            ffi::PyErr_Fetch(&mut ptype, &mut pvalue, &mut ptraceback);

            // Convert to Py immediately so that any references are freed by early return.
            let ptype = Py::from_owned_ptr_or_opt(py, ptype);
            let pvalue = Py::from_owned_ptr_or_opt(py, pvalue);
            let ptraceback = Py::from_owned_ptr_or_opt(py, ptraceback);

            // A valid exception state should always have a non-null ptype, but the other two may be
            // null.
            let ptype = match ptype {
                Some(ptype) => ptype,
                None => {
                    debug_assert!(
                        pvalue.is_none(),
                        "Exception type was null but value was not null"
                    );
                    debug_assert!(
                        ptraceback.is_none(),
                        "Exception type was null but traceback was not null"
                    );
                    return None;
                }
            };

            (ptype, pvalue, ptraceback)
        };

        if ptype.as_ptr() == PanicException::type_object(py).as_ptr() {
            let msg: String = pvalue
                .as_ref()
                .and_then(|obj| obj.extract(py).ok())
                .unwrap_or_else(|| String::from("Unwrapped panic from Python code"));

            eprintln!(
                "--- PyO3 is resuming a panic after fetching a PanicException from Python. ---"
            );
            eprintln!("Python stack trace below:");

            unsafe {
                ffi::PyErr_Restore(ptype.into_ptr(), pvalue.into_ptr(), ptraceback.into_ptr());
                ffi::PyErr_PrintEx(0);
            }

            std::panic::resume_unwind(Box::new(msg))
        }

        Some(PyErr::from_state(PyErrState::FfiTuple {
            ptype,
            pvalue,
            ptraceback,
        }))
    }
/// Takes the current error from the Python interpreter's global state and clears the global
/// state. If no error is set, returns `None`.
///
/// If the error is a `PanicException` (which would have originated from a panic in a pyo3
/// callback) then this function will resume the panic.
///
/// Use this function when it is not known if an error should be present. If the error is
/// expected to have been set, for example from [`PyErr::occurred`] or by an error return value
/// from a C FFI function, use [`PyErr::fetch`].
Takes the current error from the Python interpreter's global state and clears the global
state. If no error is set, returns `None`.

If the error is a `PanicException` (which would have originated from a panic in a pyo3
callback) then this function will resume the panic.

Use this function when it is not known if an error should be present. If the error is
expected to have been set, for example from [`PyErr::occurred`] or by an error return value
from a C FFI function, use [`PyErr::fetch`].
Takes the current error from the Python interpreter ' s global state and clears the global state . If no error is set returns ` None ` . If the error is a ` PanicException ` ( which would have originated from a panic in a pyo3 callback ) then this function will resume the panic . Use this function when it is not known if an error should be present . If the error is expected to have been set for example from [ ` PyErr :: occurred ` ] or by an error return value from a C FFI function use [ ` PyErr :: fetch ` ] .
pub fn take(py: Python) -> Option<PyErr> {
        let (ptype, pvalue, ptraceback) = unsafe {
            let mut ptype: *mut ffi::PyObject = std::ptr::null_mut();
            let mut pvalue: *mut ffi::PyObject = std::ptr::null_mut();
            let mut ptraceback: *mut ffi::PyObject = std::ptr::null_mut();
            ffi::PyErr_Fetch(&mut ptype, &mut pvalue, &mut ptraceback);
            // Convert to Py immediately so that any references are freed by early return.
            let ptype = Py::from_owned_ptr_or_opt(py, ptype);
            let pvalue = Py::from_owned_ptr_or_opt(py, pvalue);
            let ptraceback = Py::from_owned_ptr_or_opt(py, ptraceback);
            // A valid exception state should always have a non-null ptype, but the other two may be
            // null.
            let ptype = match ptype {
                Some(ptype) => ptype,
                None => {
                    debug_assert!(
                        pvalue.is_none(),
                        "Exception type was null but value was not null"
                    );
                    debug_assert!(
                        ptraceback.is_none(),
                        "Exception type was null but traceback was not null"
                    );
                    return None;
                }
            };
            (ptype, pvalue, ptraceback)
        };
        if ptype.as_ptr() == PanicException::type_object(py).as_ptr() {
            let msg: String = pvalue
                .as_ref()
                .and_then(|obj| obj.extract(py).ok())
                .unwrap_or_else(|| String::from("Unwrapped panic from Python code"));
            eprintln!(
                "--- PyO3 is resuming a panic after fetching a PanicException from Python. ---"
            );
            eprintln!("Python stack trace below:");
            unsafe {
                ffi::PyErr_Restore(ptype.into_ptr(), pvalue.into_ptr(), ptraceback.into_ptr());
                ffi::PyErr_PrintEx(0);
            }
            std::panic::resume_unwind(Box::new(msg))
        }
        Some(PyErr::from_state(PyErrState::FfiTuple {
            ptype,
            pvalue,
            ptraceback,
        }))
    }
pub fn take ( py : Python ) -> Option < PyErr > { let ( ptype , pvalue , ptraceback ) = unsafe { let mut ptype : * mut ffi :: PyObject = std :: ptr :: null_mut ( ) ; let mut pvalue : * mut ffi :: PyObject = std :: ptr :: null_mut ( ) ; let mut ptraceback : * mut ffi :: PyObject = std :: ptr :: null_mut ( ) ; ffi :: PyErr_Fetch ( & mut ptype , & mut pvalue , & mut ptraceback ) ; // Convert to Py immediately so that any references are freed by early return. let ptype = Py :: from_owned_ptr_or_opt ( py , ptype ) ; let pvalue = Py :: from_owned_ptr_or_opt ( py , pvalue ) ; let ptraceback = Py :: from_owned_ptr_or_opt ( py , ptraceback ) ; // A valid exception state should always have a non-null ptype, but the other two may be // null. let ptype = match ptype { Some ( ptype ) => ptype , None => { debug_assert ! ( pvalue is_none ( ) " " ) ; debug_assert ! ( ptraceback is_none ( ) " " ) ; return None ; } } ; ( ptype , pvalue , ptraceback ) } ; if ptype . as_ptr ( ) == PanicException :: type_object ( py ) . as_ptr ( ) { let msg : String = pvalue . as_ref ( ) . and_then ( | obj | obj . extract ( py ) . ok ( ) ) . unwrap_or_else ( | | String :: from ( " " ) ) ; eprintln ! ( " " ) ; eprintln ! ( " " ) ; unsafe { ffi :: PyErr_Restore ( ptype . into_ptr ( ) , pvalue . into_ptr ( ) , ptraceback . into_ptr ( ) ) ; ffi :: PyErr_PrintEx ( 0 ) ; } std :: panic :: resume_unwind ( Box :: new ( msg ) ) } Some ( PyErr :: from_state ( PyErrState :: FfiTuple { ptype , pvalue , ptraceback , } ) ) }
Takes the current error from the Python interpreter's global state and clears the global
state.
Takes the current error from the Python interpreter ' s global state and clears the global state .
py
Python
py
Python