Skip to content

Commit

Permalink
Merge pull request #242 from ijl/get-type-ptr
Browse files Browse the repository at this point in the history
PyObjectProtocol::get_type_ptr()
  • Loading branch information
konstin authored Oct 3, 2018
2 parents 0ec24aa + d0f79fb commit b5902af
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/objectprotocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ pub trait ObjectProtocol {
/// Gets the Python type object for this object's type.
fn get_type(&self) -> &PyType;

/// Gets the Python type pointer for this object.
fn get_type_ptr(&self) -> *mut ffi::PyTypeObject;

/// Gets the Python base object for this object.
fn get_base(&self) -> &<Self as PyTypeInfo>::BaseType
where
Expand Down Expand Up @@ -436,6 +439,11 @@ where
unsafe { PyType::from_type_ptr(self.py(), (*self.as_ptr()).ob_type) }
}

#[inline]
fn get_type_ptr(&self) -> *mut ffi::PyTypeObject {
unsafe { (*self.as_ptr()).ob_type }
}

fn get_base(&self) -> &<Self as PyTypeInfo>::BaseType
where
Self: PyTypeInfo,
Expand Down Expand Up @@ -512,4 +520,12 @@ mod test {
assert!(a.call_method0("nonexistent_method").is_err());
assert!(a.call_method1("nonexistent_method", (1,)).is_err());
}

#[test]
fn test_type() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = py.eval("42", None, None).unwrap();
assert_eq!(unsafe { obj.get_type().as_type_ptr() }, obj.get_type_ptr())
}
}

0 comments on commit b5902af

Please sign in to comment.