Add convnience function to unwrap Option<&PyObject>

This commit is contained in:
Paul Ganssle 2018-08-21 17:19:18 -04:00
parent 7053c897aa
commit a09ffad32a
No known key found for this signature in database
GPG Key ID: CD54FCE3D964BEFB
1 changed files with 13 additions and 12 deletions

View File

@ -1,4 +1,5 @@
use err::PyResult;
use ffi;
use ffi::PyDateTimeAPI;
use ffi::{PyDateTime_Check, PyDateTime_DateTimeType};
use ffi::{
@ -112,10 +113,7 @@ impl PyDateTime {
minute as c_int,
second as c_int,
microsecond as c_int,
match tzinfo {
Some(o) => o.as_ptr(),
None => py.None().as_ptr(),
},
opt_to_pyobj(py, tzinfo),
PyDateTimeAPI.DateTimeType,
);
Py::from_owned_ptr_or_err(py, ptr)
@ -194,10 +192,7 @@ impl PyTime {
minute as c_int,
second as c_int,
microsecond as c_int,
match tzinfo {
Some(o) => o.as_ptr(),
None => py.None().as_ptr(),
},
opt_to_pyobj(py, tzinfo),
PyDateTimeAPI.TimeType,
);
Py::from_owned_ptr_or_err(py, ptr)
@ -220,10 +215,7 @@ impl PyTime {
minute as c_int,
second as c_int,
microsecond as c_int,
match tzinfo {
Some(o) => o.as_ptr(),
None => py.None().as_ptr(),
},
opt_to_pyobj(py, tzinfo),
fold as c_int,
PyDateTimeAPI.TimeType,
);
@ -297,3 +289,12 @@ impl PyDeltaAccess for PyDelta {
unsafe { PyDateTime_DELTA_GET_MICROSECONDS(self.as_ptr()) as i32 }
}
}
// Utility function
unsafe fn opt_to_pyobj(py: Python, opt: Option<&PyObject>) -> *mut ffi::PyObject {
// Convenience function for unpacking Options to either an Object or None
match opt {
Some(tzi) => tzi.as_ptr(),
None => py.None().as_ptr(),
}
}