simplify PyTypeInfo and PyTypeObject
This commit is contained in:
parent
65a42a2b1a
commit
86252cda4c
20
src/err.rs
20
src/err.rs
|
@ -83,7 +83,10 @@ macro_rules! py_exception {
|
|||
|
||||
#[inline]
|
||||
fn type_object() -> $crate::Py<$crate::PyType> {
|
||||
unsafe { $crate::PyType::new($name::type_object()) }
|
||||
unsafe {
|
||||
$crate::Py::from_borrowed_ptr(
|
||||
$name::type_object() as *const _ as *mut $crate::ffi::PyObject)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -137,7 +140,7 @@ impl PyErr {
|
|||
|
||||
/// Gets whether an error is present in the Python interpreter's global state.
|
||||
#[inline]
|
||||
pub fn occurred(_ : Python) -> bool {
|
||||
pub fn occurred(_: Python) -> bool {
|
||||
unsafe { !ffi::PyErr_Occurred().is_null() }
|
||||
}
|
||||
|
||||
|
@ -146,8 +149,7 @@ impl PyErr {
|
|||
///
|
||||
/// `base` can be an existing exception type to subclass, or a tuple of classes
|
||||
/// `dict` specifies an optional dictionary of class variables and methods
|
||||
pub fn new_type<'p>(_py: Python<'p>,
|
||||
name: &str, base: Option<&PyType>, dict: Option<PyObject>)
|
||||
pub fn new_type<'p>(_: Python<'p>, name: &str, base: Option<&PyType>, dict: Option<PyObject>)
|
||||
-> *mut ffi::PyTypeObject
|
||||
{
|
||||
let base: *mut ffi::PyObject = match base {
|
||||
|
@ -161,9 +163,10 @@ impl PyErr {
|
|||
};
|
||||
|
||||
unsafe {
|
||||
let null_terminated_name = CString::new(name).expect("Failed to initialize nul terminated exception name");
|
||||
ffi::PyErr_NewException(null_terminated_name.as_ptr() as *mut c_char, base, dict)
|
||||
as *mut ffi::PyTypeObject
|
||||
let null_terminated_name = CString::new(name)
|
||||
.expect("Failed to initialize nul terminated exception name");
|
||||
ffi::PyErr_NewException(
|
||||
null_terminated_name.as_ptr() as *mut c_char, base, dict) as *mut ffi::PyTypeObject
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,7 +212,8 @@ impl PyErr {
|
|||
/// Creates a new PyErr.
|
||||
///
|
||||
/// `obj` must be an Python exception instance, the PyErr will use that instance.
|
||||
/// If `obj` is a Python exception type object, the PyErr will (lazily) create a new instance of that type.
|
||||
/// If `obj` is a Python exception type object, the PyErr will (lazily) create a new
|
||||
/// instance of that type.
|
||||
/// Otherwise, a `TypeError` is created instead.
|
||||
pub fn from_instance<O>(py: Python, obj: O) -> PyErr where O: IntoPyObject {
|
||||
PyErr::from_instance_helper(py, obj.into_object(py))
|
||||
|
|
|
@ -12,7 +12,7 @@ use ffi;
|
|||
use object::PyObject;
|
||||
use python::{Python, ToPyPointer};
|
||||
use err::PyResult;
|
||||
use super::{PyTuple, PyType};
|
||||
use super::PyTuple;
|
||||
|
||||
macro_rules! exc_type(
|
||||
($name:ident, $exc_name:ident) => (
|
||||
|
@ -27,7 +27,7 @@ macro_rules! exc_type(
|
|||
#[inline]
|
||||
fn type_object() -> $crate::Py<$crate::PyType> {
|
||||
unsafe {
|
||||
PyType::new(ffi::$exc_name as *mut ffi::PyTypeObject)
|
||||
$crate::Py::from_borrowed_ptr(ffi::$exc_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -158,10 +158,7 @@ macro_rules! pyobject_nativetype(
|
|||
|
||||
#[inline]
|
||||
fn type_object() -> $crate::Py<$crate::PyType> {
|
||||
unsafe {
|
||||
$crate::PyType::new(
|
||||
<$name as $crate::typeob::PyTypeInfo>::type_object())
|
||||
}
|
||||
$crate::PyType::new::<$name>()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -113,13 +113,13 @@ impl PyModule {
|
|||
let ty = <T as PyTypeInfo>::type_object();
|
||||
|
||||
if ((*ty).tp_flags & ffi::Py_TPFLAGS_READY) != 0 {
|
||||
PyType::new(ty)
|
||||
PyType::new::<T>()
|
||||
} else {
|
||||
// automatically initialize the class
|
||||
initialize_type::<T>(self.py(), Some(self.name()?))
|
||||
.expect(
|
||||
format!("An error occurred while initializing class {}", T::NAME).as_ref());
|
||||
PyType::new(ty)
|
||||
PyType::new::<T>()
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ use object::PyObject;
|
|||
use python::{Python, ToPyPointer};
|
||||
use err::{PyErr, PyResult};
|
||||
use instance::{Py, PyObjectWithToken};
|
||||
use typeob::PyTypeObject;
|
||||
use typeob::{PyTypeInfo, PyTypeObject};
|
||||
|
||||
/// Represents a reference to a Python `type object`.
|
||||
pub struct PyType(PyObject);
|
||||
|
@ -21,9 +21,10 @@ pyobject_nativetype!(PyType, PyType_Type, PyType_Check);
|
|||
|
||||
impl PyType {
|
||||
#[inline]
|
||||
pub unsafe fn new(ptr: *mut ffi::PyTypeObject) -> Py<PyType>
|
||||
{
|
||||
Py::from_borrowed_ptr(ptr as *mut ffi::PyObject)
|
||||
pub fn new<T: PyTypeInfo>() -> Py<PyType> {
|
||||
unsafe {
|
||||
Py::from_borrowed_ptr(T::type_object() as *const _ as *mut ffi::PyObject)
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves the underlying FFI pointer associated with this Python object.
|
||||
|
|
|
@ -145,7 +145,7 @@ impl<T> PyTypeObject for T where T: PyObjectAlloc<T> + PyTypeInfo {
|
|||
#[inline]
|
||||
default fn type_object() -> Py<PyType> {
|
||||
<T as PyTypeObject>::init_type();
|
||||
unsafe { PyType::new(T::type_object()) }
|
||||
PyType::new::<T>()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,6 @@ macro_rules! py_expect_exception {
|
|||
}}
|
||||
}
|
||||
|
||||
|
||||
#[py::class]
|
||||
struct EmptyClass { }
|
||||
|
||||
|
|
Loading…
Reference in New Issue