1 | use crate::{sync::GILOnceCell, types::PyType, Bound, Py, Python}; |
2 | |
3 | pub struct ImportedExceptionTypeObject { |
4 | imported_value: GILOnceCell<Py<PyType>>, |
5 | module: &'static str, |
6 | name: &'static str, |
7 | } |
8 | |
9 | impl ImportedExceptionTypeObject { |
10 | pub const fn new(module: &'static str, name: &'static str) -> Self { |
11 | Self { |
12 | imported_value: GILOnceCell::new(), |
13 | module, |
14 | name, |
15 | } |
16 | } |
17 | |
18 | pub fn get<'py>(&self, py: Python<'py>) -> &Bound<'py, PyType> { |
19 | self.imported_value |
20 | .import(py, self.module, self.name) |
21 | .unwrap_or_else(|e: PyErr| { |
22 | panic!( |
23 | "failed to import exception {}. {}: {}" , |
24 | self.module, self.name, e |
25 | ) |
26 | }) |
27 | } |
28 | } |
29 | |