Get rid of unwrap_py! macro
This commit is contained in:
parent
f68d0932f4
commit
003351ba61
|
@ -10,38 +10,30 @@ fn _get_subclasses<'p>(
|
||||||
py: &'p Python,
|
py: &'p Python,
|
||||||
py_type: &str,
|
py_type: &str,
|
||||||
args: &str,
|
args: &str,
|
||||||
) -> (&'p PyObjectRef, &'p PyObjectRef, &'p PyObjectRef) {
|
) -> PyResult<(&'p PyObjectRef, &'p PyObjectRef, &'p PyObjectRef)> {
|
||||||
macro_rules! unwrap_py {
|
|
||||||
($e:expr) => {
|
|
||||||
($e).map_err(|e| e.print(*py)).unwrap()
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// Import the class from Python and create some subclasses
|
// Import the class from Python and create some subclasses
|
||||||
let datetime = unwrap_py!(py.import("datetime"));
|
let datetime = py.import("datetime")?;
|
||||||
|
|
||||||
let locals = PyDict::new(*py);
|
let locals = PyDict::new(*py);
|
||||||
locals
|
locals.set_item(py_type, datetime.get(py_type)?).unwrap();
|
||||||
.set_item(py_type, datetime.get(py_type).unwrap())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let make_subclass_py = format!("class Subklass({}):\n pass", py_type);
|
let make_subclass_py = format!("class Subklass({}):\n pass", py_type);
|
||||||
|
|
||||||
let make_sub_subclass_py = "class SubSubklass(Subklass):\n pass";
|
let make_sub_subclass_py = "class SubSubklass(Subklass):\n pass";
|
||||||
|
|
||||||
unwrap_py!(py.run(&make_subclass_py, None, Some(&locals)));
|
py.run(&make_subclass_py, None, Some(&locals))?;
|
||||||
unwrap_py!(py.run(&make_sub_subclass_py, None, Some(&locals)));
|
py.run(&make_sub_subclass_py, None, Some(&locals))?;
|
||||||
|
|
||||||
// Construct an instance of the base class
|
// Construct an instance of the base class
|
||||||
let obj = unwrap_py!(py.eval(&format!("{}({})", py_type, args), None, Some(&locals)));
|
let obj = py.eval(&format!("{}({})", py_type, args), None, Some(&locals))?;
|
||||||
|
|
||||||
// Construct an instance of the subclass
|
// Construct an instance of the subclass
|
||||||
let sub_obj = unwrap_py!(py.eval(&format!("Subklass({})", args), None, Some(&locals)));
|
let sub_obj = py.eval(&format!("Subklass({})", args), None, Some(&locals))?;
|
||||||
|
|
||||||
// Construct an instance of the sub-subclass
|
// Construct an instance of the sub-subclass
|
||||||
let sub_sub_obj = unwrap_py!(py.eval(&format!("SubSubklass({})", args), None, Some(&locals)));
|
let sub_sub_obj = py.eval(&format!("SubSubklass({})", args), None, Some(&locals))?;
|
||||||
|
|
||||||
(obj, sub_obj, sub_sub_obj)
|
Ok((obj, sub_obj, sub_sub_obj))
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! assert_check_exact {
|
macro_rules! assert_check_exact {
|
||||||
|
@ -66,7 +58,7 @@ macro_rules! assert_check_only {
|
||||||
fn test_date_check() {
|
fn test_date_check() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "date", "2018, 1, 1");
|
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "date", "2018, 1, 1").unwrap();
|
||||||
|
|
||||||
assert_check_exact!(PyDate_Check, obj);
|
assert_check_exact!(PyDate_Check, obj);
|
||||||
assert_check_only!(PyDate_Check, sub_obj);
|
assert_check_only!(PyDate_Check, sub_obj);
|
||||||
|
@ -77,7 +69,7 @@ fn test_date_check() {
|
||||||
fn test_time_check() {
|
fn test_time_check() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "time", "12, 30, 15");
|
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "time", "12, 30, 15").unwrap();
|
||||||
|
|
||||||
assert_check_exact!(PyTime_Check, obj);
|
assert_check_exact!(PyTime_Check, obj);
|
||||||
assert_check_only!(PyTime_Check, sub_obj);
|
assert_check_only!(PyTime_Check, sub_obj);
|
||||||
|
@ -88,7 +80,8 @@ fn test_time_check() {
|
||||||
fn test_datetime_check() {
|
fn test_datetime_check() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "datetime", "2018, 1, 1, 13, 30, 15");
|
let (obj, sub_obj, sub_sub_obj) =
|
||||||
|
_get_subclasses(&py, "datetime", "2018, 1, 1, 13, 30, 15").unwrap();
|
||||||
|
|
||||||
assert_check_only!(PyDate_Check, obj);
|
assert_check_only!(PyDate_Check, obj);
|
||||||
assert_check_exact!(PyDateTime_Check, obj);
|
assert_check_exact!(PyDateTime_Check, obj);
|
||||||
|
@ -100,7 +93,7 @@ fn test_datetime_check() {
|
||||||
fn test_delta_check() {
|
fn test_delta_check() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "timedelta", "1, -3");
|
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "timedelta", "1, -3").unwrap();
|
||||||
|
|
||||||
assert_check_exact!(PyDelta_Check, obj);
|
assert_check_exact!(PyDelta_Check, obj);
|
||||||
assert_check_only!(PyDelta_Check, sub_obj);
|
assert_check_only!(PyDelta_Check, sub_obj);
|
||||||
|
|
Loading…
Reference in New Issue