-
Notifications
You must be signed in to change notification settings - Fork 788
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22e8dd1
commit 33371a4
Showing
3 changed files
with
109 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::{ | ||
sync::GILOnceCell, | ||
types::{PyAnyMethods, PyTracebackMethods, PyType}, | ||
Bound, Py, Python, | ||
}; | ||
|
||
pub struct ImportedExceptionTypeObject { | ||
imported_value: GILOnceCell<Py<PyType>>, | ||
module: &'static str, | ||
name: &'static str, | ||
} | ||
|
||
impl ImportedExceptionTypeObject { | ||
pub const fn new(module: &'static str, name: &'static str) -> Self { | ||
Self { | ||
imported_value: GILOnceCell::new(), | ||
module, | ||
name, | ||
} | ||
} | ||
|
||
pub fn get<'py>(&self, py: Python<'py>) -> &Bound<'py, PyType> { | ||
self.imported_value | ||
.get_or_init(py, || { | ||
let imp = py.import_bound(self.module).unwrap_or_else(|err| { | ||
let traceback = err | ||
.traceback_bound(py) | ||
.map(|tb| tb.format().expect("raised exception will have a traceback")) | ||
.unwrap_or_default(); | ||
panic!( | ||
"Can not import module {}: {}\n{}", | ||
self.module, err, traceback | ||
); | ||
}); | ||
let cls = imp.getattr(self.name).unwrap_or_else(|_| { | ||
panic!( | ||
"Can not load exception class: {}.{}", | ||
self.module, self.name | ||
) | ||
}); | ||
|
||
cls.extract() | ||
.expect("Imported exception should be a type object") | ||
}) | ||
.bind(py) | ||
} | ||
} |