-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt: make argument extraction code smaller
- Loading branch information
1 parent
ebed1db
commit 90479dd
Showing
6 changed files
with
120 additions
and
72 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
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,30 @@ | ||
use crate::{ | ||
exceptions::PyTypeError, type_object::PyTypeObject, FromPyObject, PyAny, PyErr, PyResult, | ||
Python, | ||
}; | ||
|
||
#[doc(hidden)] | ||
#[inline] | ||
pub fn extract_argument<'py, T>(obj: &'py PyAny, arg_name: &str) -> PyResult<T> | ||
where | ||
T: FromPyObject<'py>, | ||
{ | ||
match obj.extract() { | ||
Ok(e) => Ok(e), | ||
Err(e) => Err(argument_extraction_error(obj.py(), arg_name, e)), | ||
} | ||
} | ||
|
||
/// Adds the argument name to the error message of an error which occurred during argument extraction. | ||
/// | ||
/// Only modifies TypeError. (Cannot guarantee all exceptions have constructors from | ||
/// single string.) | ||
#[doc(hidden)] | ||
#[cold] | ||
pub fn argument_extraction_error(py: Python, arg_name: &str, error: PyErr) -> PyErr { | ||
if error.get_type(py) == PyTypeError::type_object(py) { | ||
PyTypeError::new_err(format!("argument '{}': {}", arg_name, error.value(py))) | ||
} else { | ||
error | ||
} | ||
} |