1use crate::{ffi, Bound, PyResult, Python};
13use std::ffi::CStr;
14use std::ops;
15
16#[doc(hidden)]
18#[macro_export]
19macro_rules! impl_exception_boilerplate {
20 ($name: ident) => {
21 impl $name {
22 #[inline]
26 #[allow(dead_code, reason = "user may not call this function")]
27 pub fn new_err<A>(args: A) -> $crate::PyErr
28 where
29 A: $crate::PyErrArguments + ::std::marker::Send + ::std::marker::Sync + 'static,
30 {
31 $crate::PyErr::new::<$name, A>(args)
32 }
33 }
34
35 impl $crate::ToPyErr for $name {}
36 };
37}
38
39#[macro_export]
66macro_rules! import_exception {
67 ($module: expr, $name: ident) => {
68 #[repr(transparent)]
75 #[allow(non_camel_case_types, reason = "matches imported exception name, e.g. `socket.herror`")]
76 pub struct $name($crate::PyAny);
77
78 $crate::impl_exception_boilerplate!($name);
79
80 $crate::pyobject_native_type_core!(
81 $name,
82 $name::type_object_raw,
83 stringify!($name),
84 stringify!($module),
85 #module=::std::option::Option::Some(stringify!($module))
86 );
87
88 impl $name {
89 fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
90 use $crate::types::PyTypeMethods;
91 static TYPE_OBJECT: $crate::impl_::exceptions::ImportedExceptionTypeObject =
92 $crate::impl_::exceptions::ImportedExceptionTypeObject::new(stringify!($module), stringify!($name));
93 TYPE_OBJECT.get(py).as_type_ptr()
94 }
95 }
96 };
97}
98
99#[macro_export]
101#[deprecated(since = "0.27.0", note = "renamed to `import_exception!` instead")]
102macro_rules! import_exception_bound {
103 ($module: expr, $name: ident) => {
104 $crate::import_exception!($module, $name);
105 };
106}
107
108#[macro_export]
175macro_rules! create_exception {
176 ($module: expr, $name: ident, $base: ty) => {
177 #[repr(transparent)]
178 pub struct $name($crate::PyAny);
179
180 $crate::impl_exception_boilerplate!($name);
181
182 $crate::create_exception_type_object!($module, $name, $base, None);
183 };
184 ($module: expr, $name: ident, $base: ty, $doc: expr) => {
185 #[repr(transparent)]
186 #[doc = $doc]
187 pub struct $name($crate::PyAny);
188
189 $crate::impl_exception_boilerplate!($name);
190
191 $crate::create_exception_type_object!($module, $name, $base, Some($doc));
192 };
193}
194
195#[doc(hidden)]
198#[macro_export]
199macro_rules! create_exception_type_object {
200 ($module: expr, $name: ident, $base: ty, None) => {
201 $crate::create_exception_type_object!($module, $name, $base, ::std::option::Option::None);
202 };
203 ($module: expr, $name: ident, $base: ty, Some($doc: expr)) => {
204 $crate::create_exception_type_object!(
205 $module,
206 $name,
207 $base,
208 ::std::option::Option::Some($crate::ffi::c_str!($doc))
209 );
210 };
211 ($module: expr, $name: ident, $base: ty, $doc: expr) => {
212 $crate::pyobject_native_type_named!($name);
213
214 unsafe impl $crate::type_object::PyTypeInfo for $name {
216 const NAME: &'static str = stringify!($name);
217 const MODULE: ::std::option::Option<&'static str> =
218 ::std::option::Option::Some(stringify!($module));
219 $crate::create_exception_type_hint!($module, $name);
220
221 #[inline]
222 #[allow(clippy::redundant_closure_call)]
223 fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
224 use $crate::sync::PyOnceLock;
225 static TYPE_OBJECT: PyOnceLock<$crate::Py<$crate::types::PyType>> =
226 PyOnceLock::new();
227
228 TYPE_OBJECT
229 .get_or_init(py, || {
230 $crate::PyErr::new_type(
231 py,
232 $crate::ffi::c_str!(concat!(
233 stringify!($module),
234 ".",
235 stringify!($name)
236 )),
237 $doc,
238 ::std::option::Option::Some(&py.get_type::<$base>()),
239 ::std::option::Option::None,
240 )
241 .expect("Failed to initialize new exception type.")
242 })
243 .as_ptr()
244 .cast()
245 }
246 }
247
248 impl $name {
249 #[doc(hidden)]
250 pub const _PYO3_DEF: $crate::impl_::pymodule::AddTypeToModule<Self> =
251 $crate::impl_::pymodule::AddTypeToModule::new();
252
253 #[allow(dead_code)]
254 #[doc(hidden)]
255 pub const _PYO3_INTROSPECTION_ID: &'static str =
256 concat!(stringify!($module), stringify!($name));
257 }
258 };
259}
260
261#[cfg(not(feature = "experimental-inspect"))]
263#[doc(hidden)]
264#[macro_export]
265macro_rules! create_exception_type_hint(
266 ($module: expr, $name: ident) => {};
267);
268
269#[cfg(feature = "experimental-inspect")]
270#[doc(hidden)]
271#[macro_export]
272macro_rules! create_exception_type_hint(
273 ($module: expr, $name: ident) => {
274 const TYPE_HINT: $crate::inspect::PyStaticExpr = $crate::inspect::PyStaticExpr::PyClass($crate::inspect::PyClassNameStaticExpr::new(
275 &$crate::type_hint_identifier!(stringify!($module), stringify!($name)),
276 Self::_PYO3_INTROSPECTION_ID
277 ));
278 };
279);
280
281macro_rules! impl_native_exception (
282 ($name:ident, $exc_name:ident, $python_name:expr, $doc:expr, $layout:path $(, #checkfunction=$checkfunction:path)?) => (
283 #[doc = $doc]
284 #[repr(transparent)]
285 #[allow(clippy::upper_case_acronyms, reason = "Python exception names")]
286 pub struct $name($crate::PyAny);
287
288 $crate::impl_exception_boilerplate!($name);
289 $crate::pyobject_native_type!($name, $layout, |_py| unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject }, "builtins", $python_name $(, #checkfunction=$checkfunction)?);
290 $crate::pyobject_subclassable_native_type!($name, $layout);
291 );
292 ($name:ident, $exc_name:ident, $python_name:expr, $doc:expr) => (
293 impl_native_exception!($name, $exc_name, $python_name, $doc, $crate::ffi::PyBaseExceptionObject);
294 )
295);
296
297macro_rules! native_doc(
298 ($name: literal, $alt: literal) => (
299 concat!(
300"Represents Python's [`", $name, "`](https://docs.python.org/3/library/exceptions.html#", $name, ") exception.
301
302", $alt
303 )
304 );
305 ($name: literal) => (
306 concat!(
307"
308Represents Python's [`", $name, "`](https://docs.python.org/3/library/exceptions.html#", $name, ") exception.
309
310# Example: Raising ", $name, " from Rust
311
312This exception can be sent to Python code by converting it into a
313[`PyErr`](crate::PyErr), where Python code can then catch it.
314```
315use pyo3::prelude::*;
316use pyo3::exceptions::Py", $name, ";
317
318#[pyfunction]
319fn always_throws() -> PyResult<()> {
320 let message = \"I'm ", $name ,", and I was raised from Rust.\";
321 Err(Py", $name, "::new_err(message))
322}
323#
324# Python::attach(|py| {
325# let fun = pyo3::wrap_pyfunction!(always_throws, py).unwrap();
326# let err = fun.call0().expect_err(\"called a function that should always return an error but the return value was Ok\");
327# assert!(err.is_instance_of::<Py", $name, ">(py))
328# });
329```
330
331Python code:
332 ```python
333 from my_module import always_throws
334
335try:
336 always_throws()
337except ", $name, " as e:
338 print(f\"Caught an exception: {e}\")
339```
340
341# Example: Catching ", $name, " in Rust
342
343```
344use pyo3::prelude::*;
345use pyo3::exceptions::Py", $name, ";
346use pyo3::ffi::c_str;
347
348Python::attach(|py| {
349 let result: PyResult<()> = py.run(c_str!(\"raise ", $name, "\"), None, None);
350
351 let error_type = match result {
352 Ok(_) => \"Not an error\",
353 Err(error) if error.is_instance_of::<Py", $name, ">(py) => \"" , $name, "\",
354 Err(_) => \"Some other error\",
355 };
356
357 assert_eq!(error_type, \"", $name, "\");
358});
359```
360"
361 )
362 );
363);
364
365impl_native_exception!(
366 PyBaseException,
367 PyExc_BaseException,
368 "BaseException",
369 native_doc!("BaseException"),
370 ffi::PyBaseExceptionObject,
371 #checkfunction=ffi::PyExceptionInstance_Check
372);
373impl_native_exception!(
374 PyException,
375 PyExc_Exception,
376 "Exception",
377 native_doc!("Exception")
378);
379impl_native_exception!(
380 PyStopAsyncIteration,
381 PyExc_StopAsyncIteration,
382 "StopAsyncIteration",
383 native_doc!("StopAsyncIteration")
384);
385impl_native_exception!(
386 PyStopIteration,
387 PyExc_StopIteration,
388 "StopIteration",
389 native_doc!("StopIteration"),
390 ffi::PyStopIterationObject
391);
392impl_native_exception!(
393 PyGeneratorExit,
394 PyExc_GeneratorExit,
395 "GeneratorExit",
396 native_doc!("GeneratorExit")
397);
398impl_native_exception!(
399 PyArithmeticError,
400 PyExc_ArithmeticError,
401 "ArithmeticError",
402 native_doc!("ArithmeticError")
403);
404impl_native_exception!(
405 PyLookupError,
406 PyExc_LookupError,
407 "LookupError",
408 native_doc!("LookupError")
409);
410
411impl_native_exception!(
412 PyAssertionError,
413 PyExc_AssertionError,
414 "AssertionError",
415 native_doc!("AssertionError")
416);
417impl_native_exception!(
418 PyAttributeError,
419 PyExc_AttributeError,
420 "AttributeError",
421 native_doc!("AttributeError")
422);
423impl_native_exception!(
424 PyBufferError,
425 PyExc_BufferError,
426 "BufferError",
427 native_doc!("BufferError")
428);
429impl_native_exception!(
430 PyEOFError,
431 PyExc_EOFError,
432 "EOFError",
433 native_doc!("EOFError")
434);
435impl_native_exception!(
436 PyFloatingPointError,
437 PyExc_FloatingPointError,
438 "FloatingPointError",
439 native_doc!("FloatingPointError")
440);
441#[cfg(not(any(PyPy, GraalPy)))]
442impl_native_exception!(
443 PyOSError,
444 PyExc_OSError,
445 "OSError",
446 native_doc!("OSError"),
447 ffi::PyOSErrorObject
448);
449#[cfg(any(PyPy, GraalPy))]
450impl_native_exception!(PyOSError, PyExc_OSError, "OSError", native_doc!("OSError"));
451impl_native_exception!(
452 PyImportError,
453 PyExc_ImportError,
454 "ImportError",
455 native_doc!("ImportError")
456);
457
458impl_native_exception!(
459 PyModuleNotFoundError,
460 PyExc_ModuleNotFoundError,
461 "ModuleNotFoundError",
462 native_doc!("ModuleNotFoundError")
463);
464
465impl_native_exception!(
466 PyIndexError,
467 PyExc_IndexError,
468 "IndexError",
469 native_doc!("IndexError")
470);
471impl_native_exception!(
472 PyKeyError,
473 PyExc_KeyError,
474 "KeyError",
475 native_doc!("KeyError")
476);
477impl_native_exception!(
478 PyKeyboardInterrupt,
479 PyExc_KeyboardInterrupt,
480 "KeyboardInterrupt",
481 native_doc!("KeyboardInterrupt")
482);
483impl_native_exception!(
484 PyMemoryError,
485 PyExc_MemoryError,
486 "MemoryError",
487 native_doc!("MemoryError")
488);
489impl_native_exception!(
490 PyNameError,
491 PyExc_NameError,
492 "NameError",
493 native_doc!("NameError")
494);
495impl_native_exception!(
496 PyOverflowError,
497 PyExc_OverflowError,
498 "OverflowError",
499 native_doc!("OverflowError")
500);
501impl_native_exception!(
502 PyRuntimeError,
503 PyExc_RuntimeError,
504 "RuntimeError",
505 native_doc!("RuntimeError")
506);
507impl_native_exception!(
508 PyRecursionError,
509 PyExc_RecursionError,
510 "RecursionError",
511 native_doc!("RecursionError")
512);
513impl_native_exception!(
514 PyNotImplementedError,
515 PyExc_NotImplementedError,
516 "NotImplementedError",
517 native_doc!("NotImplementedError")
518);
519#[cfg(not(any(PyPy, GraalPy)))]
520impl_native_exception!(
521 PySyntaxError,
522 PyExc_SyntaxError,
523 "SyntaxError",
524 native_doc!("SyntaxError"),
525 ffi::PySyntaxErrorObject
526);
527#[cfg(any(PyPy, GraalPy))]
528impl_native_exception!(
529 PySyntaxError,
530 PyExc_SyntaxError,
531 "SyntaxError",
532 native_doc!("SyntaxError")
533);
534impl_native_exception!(
535 PyReferenceError,
536 PyExc_ReferenceError,
537 "ReferenceError",
538 native_doc!("ReferenceError")
539);
540impl_native_exception!(
541 PySystemError,
542 PyExc_SystemError,
543 "SystemError",
544 native_doc!("SystemError")
545);
546#[cfg(not(any(PyPy, GraalPy)))]
547impl_native_exception!(
548 PySystemExit,
549 PyExc_SystemExit,
550 "SystemExit",
551 native_doc!("SystemExit"),
552 ffi::PySystemExitObject
553);
554#[cfg(any(PyPy, GraalPy))]
555impl_native_exception!(
556 PySystemExit,
557 PyExc_SystemExit,
558 "SystemExit",
559 native_doc!("SystemExit")
560);
561impl_native_exception!(
562 PyTypeError,
563 PyExc_TypeError,
564 "TypeError",
565 native_doc!("TypeError")
566);
567impl_native_exception!(
568 PyUnboundLocalError,
569 PyExc_UnboundLocalError,
570 "UnboundLocalError",
571 native_doc!("UnboundLocalError")
572);
573#[cfg(not(any(PyPy, GraalPy)))]
574impl_native_exception!(
575 PyUnicodeError,
576 PyExc_UnicodeError,
577 "UnicodeError",
578 native_doc!("UnicodeError"),
579 ffi::PyUnicodeErrorObject
580);
581#[cfg(any(PyPy, GraalPy))]
582impl_native_exception!(
583 PyUnicodeError,
584 PyExc_UnicodeError,
585 "UnicodeError",
586 native_doc!("UnicodeError")
587);
588impl_native_exception!(
590 PyUnicodeDecodeError,
591 PyExc_UnicodeDecodeError,
592 "UnicodeDecodeError",
593 native_doc!("UnicodeDecodeError", "")
594);
595impl_native_exception!(
596 PyUnicodeEncodeError,
597 PyExc_UnicodeEncodeError,
598 "UnicodeEncodeError",
599 native_doc!("UnicodeEncodeError", "")
600);
601impl_native_exception!(
602 PyUnicodeTranslateError,
603 PyExc_UnicodeTranslateError,
604 "UnicodeTranslateError",
605 native_doc!("UnicodeTranslateError", "")
606);
607#[cfg(Py_3_11)]
608impl_native_exception!(
609 PyBaseExceptionGroup,
610 PyExc_BaseExceptionGroup,
611 "BaseExceptionGroup",
612 native_doc!("BaseExceptionGroup", "")
613);
614impl_native_exception!(
615 PyValueError,
616 PyExc_ValueError,
617 "ValueError",
618 native_doc!("ValueError")
619);
620impl_native_exception!(
621 PyZeroDivisionError,
622 PyExc_ZeroDivisionError,
623 "ZeroDivisionError",
624 native_doc!("ZeroDivisionError")
625);
626
627impl_native_exception!(
628 PyBlockingIOError,
629 PyExc_BlockingIOError,
630 "BlockingIOError",
631 native_doc!("BlockingIOError")
632);
633impl_native_exception!(
634 PyBrokenPipeError,
635 PyExc_BrokenPipeError,
636 "BrokenPipeError",
637 native_doc!("BrokenPipeError")
638);
639impl_native_exception!(
640 PyChildProcessError,
641 PyExc_ChildProcessError,
642 "ChildProcessError",
643 native_doc!("ChildProcessError")
644);
645impl_native_exception!(
646 PyConnectionError,
647 PyExc_ConnectionError,
648 "ConnectionError",
649 native_doc!("ConnectionError")
650);
651impl_native_exception!(
652 PyConnectionAbortedError,
653 PyExc_ConnectionAbortedError,
654 "ConnectionAbortedError",
655 native_doc!("ConnectionAbortedError")
656);
657impl_native_exception!(
658 PyConnectionRefusedError,
659 PyExc_ConnectionRefusedError,
660 "ConnectionRefusedError",
661 native_doc!("ConnectionRefusedError")
662);
663impl_native_exception!(
664 PyConnectionResetError,
665 PyExc_ConnectionResetError,
666 "ConnectionResetError",
667 native_doc!("ConnectionResetError")
668);
669impl_native_exception!(
670 PyFileExistsError,
671 PyExc_FileExistsError,
672 "FileExistsError",
673 native_doc!("FileExistsError")
674);
675impl_native_exception!(
676 PyFileNotFoundError,
677 PyExc_FileNotFoundError,
678 "FileNotFoundError",
679 native_doc!("FileNotFoundError")
680);
681impl_native_exception!(
682 PyInterruptedError,
683 PyExc_InterruptedError,
684 "InterruptedError",
685 native_doc!("InterruptedError")
686);
687impl_native_exception!(
688 PyIsADirectoryError,
689 PyExc_IsADirectoryError,
690 "IsADirectoryError",
691 native_doc!("IsADirectoryError")
692);
693impl_native_exception!(
694 PyNotADirectoryError,
695 PyExc_NotADirectoryError,
696 "NotADirectoryError",
697 native_doc!("NotADirectoryError")
698);
699impl_native_exception!(
700 PyPermissionError,
701 PyExc_PermissionError,
702 "PermissionError",
703 native_doc!("PermissionError")
704);
705impl_native_exception!(
706 PyProcessLookupError,
707 PyExc_ProcessLookupError,
708 "ProcessLookupError",
709 native_doc!("ProcessLookupError")
710);
711impl_native_exception!(
712 PyTimeoutError,
713 PyExc_TimeoutError,
714 "TimeoutError",
715 native_doc!("TimeoutError")
716);
717
718pub type PyEnvironmentError = PyOSError;
720
721pub type PyIOError = PyOSError;
723
724#[cfg(windows)]
725pub type PyWindowsError = PyOSError;
727
728impl PyUnicodeDecodeError {
729 pub fn new<'py>(
731 py: Python<'py>,
732 encoding: &CStr,
733 input: &[u8],
734 range: ops::Range<usize>,
735 reason: &CStr,
736 ) -> PyResult<Bound<'py, PyUnicodeDecodeError>> {
737 use crate::ffi_ptr_ext::FfiPtrExt;
738 use crate::py_result_ext::PyResultExt;
739 unsafe {
740 ffi::PyUnicodeDecodeError_Create(
741 encoding.as_ptr(),
742 input.as_ptr().cast(),
743 input.len() as ffi::Py_ssize_t,
744 range.start as ffi::Py_ssize_t,
745 range.end as ffi::Py_ssize_t,
746 reason.as_ptr(),
747 )
748 .assume_owned_or_err(py)
749 }
750 .cast_into()
751 }
752
753 pub fn new_utf8<'py>(
775 py: Python<'py>,
776 input: &[u8],
777 err: std::str::Utf8Error,
778 ) -> PyResult<Bound<'py, PyUnicodeDecodeError>> {
779 let pos = err.valid_up_to();
780 PyUnicodeDecodeError::new(py, c"utf-8", input, pos..(pos + 1), c"invalid utf-8")
781 }
782}
783
784impl_native_exception!(PyWarning, PyExc_Warning, "Warning", native_doc!("Warning"));
785impl_native_exception!(
786 PyUserWarning,
787 PyExc_UserWarning,
788 "UserWarning",
789 native_doc!("UserWarning")
790);
791impl_native_exception!(
792 PyDeprecationWarning,
793 PyExc_DeprecationWarning,
794 "DeprecationWarning",
795 native_doc!("DeprecationWarning")
796);
797impl_native_exception!(
798 PyPendingDeprecationWarning,
799 PyExc_PendingDeprecationWarning,
800 "PendingDeprecationWarning",
801 native_doc!("PendingDeprecationWarning")
802);
803impl_native_exception!(
804 PySyntaxWarning,
805 PyExc_SyntaxWarning,
806 "SyntaxWarning",
807 native_doc!("SyntaxWarning")
808);
809impl_native_exception!(
810 PyRuntimeWarning,
811 PyExc_RuntimeWarning,
812 "RuntimeWarning",
813 native_doc!("RuntimeWarning")
814);
815impl_native_exception!(
816 PyFutureWarning,
817 PyExc_FutureWarning,
818 "FutureWarning",
819 native_doc!("FutureWarning")
820);
821impl_native_exception!(
822 PyImportWarning,
823 PyExc_ImportWarning,
824 "ImportWarning",
825 native_doc!("ImportWarning")
826);
827impl_native_exception!(
828 PyUnicodeWarning,
829 PyExc_UnicodeWarning,
830 "UnicodeWarning",
831 native_doc!("UnicodeWarning")
832);
833impl_native_exception!(
834 PyBytesWarning,
835 PyExc_BytesWarning,
836 "BytesWarning",
837 native_doc!("BytesWarning")
838);
839impl_native_exception!(
840 PyResourceWarning,
841 PyExc_ResourceWarning,
842 "ResourceWarning",
843 native_doc!("ResourceWarning")
844);
845
846#[cfg(Py_3_10)]
847impl_native_exception!(
848 PyEncodingWarning,
849 PyExc_EncodingWarning,
850 "EncodingWarning",
851 native_doc!("EncodingWarning")
852);
853
854#[cfg(test)]
855macro_rules! test_exception {
856 ($exc_ty:ident $(, |$py:tt| $constructor:expr )?) => {
857 #[allow(non_snake_case, reason = "test matches exception name")]
858 #[test]
859 fn $exc_ty () {
860 use super::$exc_ty;
861
862 $crate::Python::attach(|py| {
863 let err: $crate::PyErr = {
864 None
865 $(
866 .or(Some({ let $py = py; $constructor }))
867 )?
868 .unwrap_or($exc_ty::new_err("a test exception"))
869 };
870
871 assert!(err.is_instance_of::<$exc_ty>(py));
872
873 let value = err.value(py).as_any().cast::<$exc_ty>().unwrap();
874
875 assert!($crate::PyErr::from(value.clone()).is_instance_of::<$exc_ty>(py));
876 })
877 }
878 };
879}
880
881pub mod asyncio {
884 import_exception!(asyncio, CancelledError);
885 import_exception!(asyncio, InvalidStateError);
886 import_exception!(asyncio, TimeoutError);
887 import_exception!(asyncio, IncompleteReadError);
888 import_exception!(asyncio, LimitOverrunError);
889 import_exception!(asyncio, QueueEmpty);
890 import_exception!(asyncio, QueueFull);
891
892 #[cfg(test)]
893 mod tests {
894 test_exception!(CancelledError);
895 test_exception!(InvalidStateError);
896 test_exception!(TimeoutError);
897 test_exception!(IncompleteReadError, |_| IncompleteReadError::new_err((
898 "partial", "expected"
899 )));
900 test_exception!(LimitOverrunError, |_| LimitOverrunError::new_err((
901 "message", "consumed"
902 )));
903 test_exception!(QueueEmpty);
904 test_exception!(QueueFull);
905 }
906}
907
908pub mod socket {
911 import_exception!(socket, herror);
912 import_exception!(socket, gaierror);
913 import_exception!(socket, timeout);
914
915 #[cfg(test)]
916 mod tests {
917 test_exception!(herror);
918 test_exception!(gaierror);
919 test_exception!(timeout);
920 }
921}
922
923#[cfg(test)]
924mod tests {
925 use super::*;
926 use crate::types::any::PyAnyMethods;
927 use crate::types::{IntoPyDict, PyDict};
928 use crate::PyErr;
929
930 import_exception!(socket, gaierror);
931 import_exception!(email.errors, MessageError);
932
933 #[test]
934 fn test_check_exception() {
935 Python::attach(|py| {
936 let err: PyErr = gaierror::new_err(());
937 let socket = py
938 .import("socket")
939 .map_err(|e| e.display(py))
940 .expect("could not import socket");
941
942 let d = PyDict::new(py);
943 d.set_item("socket", socket)
944 .map_err(|e| e.display(py))
945 .expect("could not setitem");
946
947 d.set_item("exc", err)
948 .map_err(|e| e.display(py))
949 .expect("could not setitem");
950
951 py.run(c"assert isinstance(exc, socket.gaierror)", None, Some(&d))
952 .map_err(|e| e.display(py))
953 .expect("assertion failed");
954 });
955 }
956
957 #[test]
958 fn test_check_exception_nested() {
959 Python::attach(|py| {
960 let err: PyErr = MessageError::new_err(());
961 let email = py
962 .import("email")
963 .map_err(|e| e.display(py))
964 .expect("could not import email");
965
966 let d = PyDict::new(py);
967 d.set_item("email", email)
968 .map_err(|e| e.display(py))
969 .expect("could not setitem");
970 d.set_item("exc", err)
971 .map_err(|e| e.display(py))
972 .expect("could not setitem");
973
974 py.run(
975 c"assert isinstance(exc, email.errors.MessageError)",
976 None,
977 Some(&d),
978 )
979 .map_err(|e| e.display(py))
980 .expect("assertion failed");
981 });
982 }
983
984 #[test]
985 fn custom_exception() {
986 create_exception!(mymodule, CustomError, PyException);
987
988 Python::attach(|py| {
989 let error_type = py.get_type::<CustomError>();
990 let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap();
991 let type_description: String = py
992 .eval(c"str(CustomError)", None, Some(&ctx))
993 .unwrap()
994 .extract()
995 .unwrap();
996 assert_eq!(type_description, "<class 'mymodule.CustomError'>");
997 py.run(
998 c"assert CustomError('oops').args == ('oops',)",
999 None,
1000 Some(&ctx),
1001 )
1002 .unwrap();
1003 py.run(c"assert CustomError.__doc__ is None", None, Some(&ctx))
1004 .unwrap();
1005 });
1006 }
1007
1008 #[test]
1009 fn custom_exception_dotted_module() {
1010 create_exception!(mymodule.exceptions, CustomError, PyException);
1011 Python::attach(|py| {
1012 let error_type = py.get_type::<CustomError>();
1013 let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap();
1014 let type_description: String = py
1015 .eval(c"str(CustomError)", None, Some(&ctx))
1016 .unwrap()
1017 .extract()
1018 .unwrap();
1019 assert_eq!(
1020 type_description,
1021 "<class 'mymodule.exceptions.CustomError'>"
1022 );
1023 });
1024 }
1025
1026 #[test]
1027 fn custom_exception_doc() {
1028 create_exception!(mymodule, CustomError, PyException, "Some docs");
1029
1030 Python::attach(|py| {
1031 let error_type = py.get_type::<CustomError>();
1032 let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap();
1033 let type_description: String = py
1034 .eval(c"str(CustomError)", None, Some(&ctx))
1035 .unwrap()
1036 .extract()
1037 .unwrap();
1038 assert_eq!(type_description, "<class 'mymodule.CustomError'>");
1039 py.run(
1040 c"assert CustomError('oops').args == ('oops',)",
1041 None,
1042 Some(&ctx),
1043 )
1044 .unwrap();
1045 py.run(
1046 c"assert CustomError.__doc__ == 'Some docs'",
1047 None,
1048 Some(&ctx),
1049 )
1050 .unwrap();
1051 });
1052 }
1053
1054 #[test]
1055 fn custom_exception_doc_expr() {
1056 create_exception!(
1057 mymodule,
1058 CustomError,
1059 PyException,
1060 concat!("Some", " more ", stringify!(docs))
1061 );
1062
1063 Python::attach(|py| {
1064 let error_type = py.get_type::<CustomError>();
1065 let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap();
1066 let type_description: String = py
1067 .eval(c"str(CustomError)", None, Some(&ctx))
1068 .unwrap()
1069 .extract()
1070 .unwrap();
1071 assert_eq!(type_description, "<class 'mymodule.CustomError'>");
1072 py.run(
1073 c"assert CustomError('oops').args == ('oops',)",
1074 None,
1075 Some(&ctx),
1076 )
1077 .unwrap();
1078 py.run(
1079 c"assert CustomError.__doc__ == 'Some more docs'",
1080 None,
1081 Some(&ctx),
1082 )
1083 .unwrap();
1084 });
1085 }
1086
1087 #[test]
1088 fn native_exception_debug() {
1089 Python::attach(|py| {
1090 let exc = py
1091 .run(c"raise Exception('banana')", None, None)
1092 .expect_err("raising should have given us an error")
1093 .into_value(py)
1094 .into_bound(py);
1095 assert_eq!(
1096 format!("{exc:?}"),
1097 exc.repr().unwrap().extract::<String>().unwrap()
1098 );
1099 });
1100 }
1101
1102 #[test]
1103 fn native_exception_display() {
1104 Python::attach(|py| {
1105 let exc = py
1106 .run(c"raise Exception('banana')", None, None)
1107 .expect_err("raising should have given us an error")
1108 .into_value(py)
1109 .into_bound(py);
1110 assert_eq!(
1111 exc.to_string(),
1112 exc.str().unwrap().extract::<String>().unwrap()
1113 );
1114 });
1115 }
1116
1117 #[test]
1118 fn unicode_decode_error() {
1119 let invalid_utf8 = b"fo\xd8o";
1120 #[expect(invalid_from_utf8)]
1121 let err = std::str::from_utf8(invalid_utf8).expect_err("should be invalid utf8");
1122 Python::attach(|py| {
1123 let decode_err = PyUnicodeDecodeError::new_utf8(py, invalid_utf8, err).unwrap();
1124 assert_eq!(
1125 format!("{decode_err:?}"),
1126 "UnicodeDecodeError('utf-8', b'fo\\xd8o', 2, 3, 'invalid utf-8')"
1127 );
1128
1129 let e: PyErr = decode_err.into();
1131 e.restore(py);
1132
1133 assert_eq!(
1134 PyErr::fetch(py).to_string(),
1135 "UnicodeDecodeError: \'utf-8\' codec can\'t decode byte 0xd8 in position 2: invalid utf-8"
1136 );
1137 });
1138 }
1139 #[cfg(Py_3_11)]
1140 test_exception!(PyBaseExceptionGroup, |_| PyBaseExceptionGroup::new_err((
1141 "msg",
1142 vec![PyValueError::new_err("err")]
1143 )));
1144 test_exception!(PyBaseException);
1145 test_exception!(PyException);
1146 test_exception!(PyStopAsyncIteration);
1147 test_exception!(PyStopIteration);
1148 test_exception!(PyGeneratorExit);
1149 test_exception!(PyArithmeticError);
1150 test_exception!(PyLookupError);
1151 test_exception!(PyAssertionError);
1152 test_exception!(PyAttributeError);
1153 test_exception!(PyBufferError);
1154 test_exception!(PyEOFError);
1155 test_exception!(PyFloatingPointError);
1156 test_exception!(PyOSError);
1157 test_exception!(PyImportError);
1158 test_exception!(PyModuleNotFoundError);
1159 test_exception!(PyIndexError);
1160 test_exception!(PyKeyError);
1161 test_exception!(PyKeyboardInterrupt);
1162 test_exception!(PyMemoryError);
1163 test_exception!(PyNameError);
1164 test_exception!(PyOverflowError);
1165 test_exception!(PyRuntimeError);
1166 test_exception!(PyRecursionError);
1167 test_exception!(PyNotImplementedError);
1168 test_exception!(PySyntaxError);
1169 test_exception!(PyReferenceError);
1170 test_exception!(PySystemError);
1171 test_exception!(PySystemExit);
1172 test_exception!(PyTypeError);
1173 test_exception!(PyUnboundLocalError);
1174 test_exception!(PyUnicodeError);
1175 test_exception!(PyUnicodeDecodeError, |py| {
1176 let invalid_utf8 = b"fo\xd8o";
1177 #[expect(invalid_from_utf8)]
1178 let err = std::str::from_utf8(invalid_utf8).expect_err("should be invalid utf8");
1179 PyErr::from_value(
1180 PyUnicodeDecodeError::new_utf8(py, invalid_utf8, err)
1181 .unwrap()
1182 .into_any(),
1183 )
1184 });
1185 test_exception!(PyUnicodeEncodeError, |py| py
1186 .eval(c"chr(40960).encode('ascii')", None, None)
1187 .unwrap_err());
1188 test_exception!(PyUnicodeTranslateError, |_| {
1189 PyUnicodeTranslateError::new_err(("\u{3042}", 0, 1, "ouch"))
1190 });
1191 test_exception!(PyValueError);
1192 test_exception!(PyZeroDivisionError);
1193 test_exception!(PyBlockingIOError);
1194 test_exception!(PyBrokenPipeError);
1195 test_exception!(PyChildProcessError);
1196 test_exception!(PyConnectionError);
1197 test_exception!(PyConnectionAbortedError);
1198 test_exception!(PyConnectionRefusedError);
1199 test_exception!(PyConnectionResetError);
1200 test_exception!(PyFileExistsError);
1201 test_exception!(PyFileNotFoundError);
1202 test_exception!(PyInterruptedError);
1203 test_exception!(PyIsADirectoryError);
1204 test_exception!(PyNotADirectoryError);
1205 test_exception!(PyPermissionError);
1206 test_exception!(PyProcessLookupError);
1207 test_exception!(PyTimeoutError);
1208 test_exception!(PyEnvironmentError);
1209 test_exception!(PyIOError);
1210 #[cfg(windows)]
1211 test_exception!(PyWindowsError);
1212
1213 test_exception!(PyWarning);
1214 test_exception!(PyUserWarning);
1215 test_exception!(PyDeprecationWarning);
1216 test_exception!(PyPendingDeprecationWarning);
1217 test_exception!(PySyntaxWarning);
1218 test_exception!(PyRuntimeWarning);
1219 test_exception!(PyFutureWarning);
1220 test_exception!(PyImportWarning);
1221 test_exception!(PyUnicodeWarning);
1222 test_exception!(PyBytesWarning);
1223 #[cfg(Py_3_10)]
1224 test_exception!(PyEncodingWarning);
1225}