Rename PyRustTypeBuilder -> TypeBuilder
This commit is contained in:
parent
acc5712536
commit
061e46bde8
|
@ -20,7 +20,7 @@ use std::{mem, ptr};
|
||||||
use python::{Python, PythonObject};
|
use python::{Python, PythonObject};
|
||||||
use objects::{PyObject, PyTuple, PyDict, PyString, exc};
|
use objects::{PyObject, PyTuple, PyDict, PyString, exc};
|
||||||
use conversion::ToPyObject;
|
use conversion::ToPyObject;
|
||||||
use rustobject::{PyRustTypeBuilder, TypeConstructor};
|
use rustobject::{TypeBuilder, TypeConstructor};
|
||||||
use ffi;
|
use ffi;
|
||||||
use err::{self, PyResult};
|
use err::{self, PyResult};
|
||||||
|
|
||||||
|
|
|
@ -106,10 +106,10 @@ impl PyModule {
|
||||||
|
|
||||||
/// Adds a new extension type to the module.
|
/// Adds a new extension type to the module.
|
||||||
///
|
///
|
||||||
/// This is a convenience function that creates a new `PyRustTypeBuilder` and
|
/// This is a convenience function that creates a new `TypeBuilder` and
|
||||||
/// sets `new_type.__module__` to this module's name.
|
/// sets `new_type.__module__` to this module's name.
|
||||||
/// The new type will be added to this module when `finish()` is called on the builder.
|
/// The new type will be added to this module when `finish()` is called on the builder.
|
||||||
pub fn add_type<'p, T>(&self, py: Python<'p>, name: &str) -> ::rustobject::PyRustTypeBuilder<'p, T>
|
pub fn add_type<'p, T>(&self, py: Python<'p>, name: &str) -> ::rustobject::TypeBuilder<'p, T>
|
||||||
where T: 'static + Send {
|
where T: 'static + Send {
|
||||||
::rustobject::new_typebuilder_for_module(py, self, name)
|
::rustobject::new_typebuilder_for_module(py, self, name)
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,7 +113,7 @@ macro_rules! py_class_impl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn init($py: Python) -> $crate::PyResult<$crate::PyType> {
|
fn init($py: Python) -> $crate::PyResult<$crate::PyType> {
|
||||||
let b = $crate::rustobject::PyRustTypeBuilder::<$name>::new(
|
let b = $crate::rustobject::TypeBuilder::<$name>::new(
|
||||||
$py, stringify!($name));
|
$py, stringify!($name));
|
||||||
//let b = b.base(); TODO inheritance
|
//let b = b.base(); TODO inheritance
|
||||||
//py_class_parse_body!($py, b, $( $body )* );
|
//py_class_parse_body!($py, b, $( $body )* );
|
||||||
|
|
|
@ -78,7 +78,7 @@ macro_rules! py_method_wrap {
|
||||||
/// #[macro_use] extern crate cpython;
|
/// #[macro_use] extern crate cpython;
|
||||||
/// use cpython::{Python, PythonObject, PyResult, PyErr, ObjectProtocol};
|
/// use cpython::{Python, PythonObject, PyResult, PyErr, ObjectProtocol};
|
||||||
/// use cpython::{exc};
|
/// use cpython::{exc};
|
||||||
/// use cpython::rustobject::{PyRustObject, PyRustTypeBuilder};
|
/// use cpython::rustobject::{PyRustObject, TypeBuilder};
|
||||||
///
|
///
|
||||||
/// fn mul(py: Python, slf: &PyRustObject<i32>, arg: i32) -> PyResult<i32> {
|
/// fn mul(py: Python, slf: &PyRustObject<i32>, arg: i32) -> PyResult<i32> {
|
||||||
/// match slf.get(py).checked_mul(arg) {
|
/// match slf.get(py).checked_mul(arg) {
|
||||||
|
@ -90,7 +90,7 @@ macro_rules! py_method_wrap {
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let gil = Python::acquire_gil();
|
/// let gil = Python::acquire_gil();
|
||||||
/// let py = gil.python();
|
/// let py = gil.python();
|
||||||
/// let multiplier_type = PyRustTypeBuilder::<i32>::new(py, "Multiplier")
|
/// let multiplier_type = TypeBuilder::<i32>::new(py, "Multiplier")
|
||||||
/// .add("mul", py_method!(mul(arg: i32)))
|
/// .add("mul", py_method!(mul(arg: i32)))
|
||||||
/// .finish().unwrap();
|
/// .finish().unwrap();
|
||||||
/// let obj = multiplier_type.create_instance(py, 3, ()).into_object();
|
/// let obj = multiplier_type.create_instance(py, 3, ()).into_object();
|
||||||
|
@ -260,7 +260,7 @@ macro_rules! py_class_method_wrap {
|
||||||
/// ```
|
/// ```
|
||||||
/// #[macro_use] extern crate cpython;
|
/// #[macro_use] extern crate cpython;
|
||||||
/// use cpython::{Python, PythonObject, PyResult, ObjectProtocol, PyType, NoArgs};
|
/// use cpython::{Python, PythonObject, PyResult, ObjectProtocol, PyType, NoArgs};
|
||||||
/// use cpython::rustobject::PyRustTypeBuilder;
|
/// use cpython::rustobject::TypeBuilder;
|
||||||
///
|
///
|
||||||
/// fn method(py: Python, cls: &PyType) -> PyResult<i32> {
|
/// fn method(py: Python, cls: &PyType) -> PyResult<i32> {
|
||||||
/// Ok(42)
|
/// Ok(42)
|
||||||
|
@ -269,7 +269,7 @@ macro_rules! py_class_method_wrap {
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let gil = Python::acquire_gil();
|
/// let gil = Python::acquire_gil();
|
||||||
/// let py = gil.python();
|
/// let py = gil.python();
|
||||||
/// let my_type = PyRustTypeBuilder::<i32>::new(py, "MyType")
|
/// let my_type = TypeBuilder::<i32>::new(py, "MyType")
|
||||||
/// .add("method", py_class_method!(method()))
|
/// .add("method", py_class_method!(method()))
|
||||||
/// .finish().unwrap();
|
/// .finish().unwrap();
|
||||||
/// let result = my_type.as_object().call_method(py, "method", NoArgs, None).unwrap();
|
/// let result = my_type.as_object().call_method(py, "method", NoArgs, None).unwrap();
|
||||||
|
|
|
@ -34,7 +34,7 @@ pub use self::typebuilder::*;
|
||||||
pub use self::method::*;
|
pub use self::method::*;
|
||||||
pub use self::class::*;
|
pub use self::class::*;
|
||||||
|
|
||||||
/// A PythonObject that is usable as a base type with `PyRustTypeBuilder::base()`.
|
/// A PythonObject that is usable as a base type with `TypeBuilder::base()`.
|
||||||
pub trait BaseObject : PythonObject {
|
pub trait BaseObject : PythonObject {
|
||||||
/// Gets the size of the object, in bytes.
|
/// Gets the size of the object, in bytes.
|
||||||
fn size() -> usize;
|
fn size() -> usize;
|
||||||
|
|
|
@ -34,7 +34,7 @@ fn rustobject_calls_drop() {
|
||||||
|
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let t = rustobject::PyRustTypeBuilder::<MyObj>::new(py, "TypeWithDrop")
|
let t = rustobject::TypeBuilder::<MyObj>::new(py, "TypeWithDrop")
|
||||||
.finish().unwrap();
|
.finish().unwrap();
|
||||||
|
|
||||||
let drop_called = Arc::new(AtomicBool::new(false));
|
let drop_called = Arc::new(AtomicBool::new(false));
|
||||||
|
@ -49,7 +49,7 @@ fn rustobject_calls_drop() {
|
||||||
fn no_init_from_python() {
|
fn no_init_from_python() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let t = rustobject::PyRustTypeBuilder::<i32>::new(py, "MyType")
|
let t = rustobject::TypeBuilder::<i32>::new(py, "MyType")
|
||||||
.finish().unwrap();
|
.finish().unwrap();
|
||||||
assert!(t.call(py, &NoArgs, None).is_err());
|
assert!(t.call(py, &NoArgs, None).is_err());
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ fn no_init_from_python() {
|
||||||
fn heaptype_refcount() {
|
fn heaptype_refcount() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let t = rustobject::PyRustTypeBuilder::<i32>::new(py, "MyType")
|
let t = rustobject::TypeBuilder::<i32>::new(py, "MyType")
|
||||||
.finish().unwrap();
|
.finish().unwrap();
|
||||||
// TODO: investigate why the refcnt isn't 1.
|
// TODO: investigate why the refcnt isn't 1.
|
||||||
//assert_eq!(1, t.as_object().get_refcnt());
|
//assert_eq!(1, t.as_object().get_refcnt());
|
||||||
|
|
|
@ -29,7 +29,7 @@ use super::{BaseObject, PyRustObject, PyRustType, method};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub struct PyRustTypeBuilder<'p, T, B = PyObject> where T: 'static + Send, B: BaseObject {
|
pub struct TypeBuilder<'p, T, B = PyObject> where T: 'static + Send, B: BaseObject {
|
||||||
// In Python 2.7, we can create a new PyHeapTypeObject and fill it.
|
// In Python 2.7, we can create a new PyHeapTypeObject and fill it.
|
||||||
|
|
||||||
/// The python type object under construction.
|
/// The python type object under construction.
|
||||||
|
@ -69,10 +69,10 @@ pub struct PyRustTypeBuilder<'p, T, B = PyObject> where T: 'static + Send, B: Ba
|
||||||
phantom: marker::PhantomData<&'p (B, T)>
|
phantom: marker::PhantomData<&'p (B, T)>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_typebuilder_for_module<'p, T>(py: Python<'p>, m: &PyModule, name: &str) -> PyRustTypeBuilder<'p, T>
|
pub fn new_typebuilder_for_module<'p, T>(py: Python<'p>, m: &PyModule, name: &str) -> TypeBuilder<'p, T>
|
||||||
where T: 'static + Send {
|
where T: 'static + Send {
|
||||||
let b = PyRustTypeBuilder::new(py, name);
|
let b = TypeBuilder::new(py, name);
|
||||||
PyRustTypeBuilder { target_module: Some(m.clone_ref(py)), .. b }
|
TypeBuilder { target_module: Some(m.clone_ref(py)), .. b }
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C" fn disabled_tp_new_callback
|
unsafe extern "C" fn disabled_tp_new_callback
|
||||||
|
@ -91,14 +91,14 @@ unsafe extern "C" fn tp_dealloc_callback<T, B>(obj: *mut ffi::PyObject)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
impl <'p, T> TypeBuilder<'p, T> where T: 'static + Send {
|
||||||
/// Create a new type builder.
|
/// Create a new type builder.
|
||||||
///
|
///
|
||||||
/// py: proof that the GIL is held by the current thread.
|
/// py: proof that the GIL is held by the current thread.
|
||||||
/// name: name of the new type
|
/// name: name of the new type
|
||||||
pub fn new(py: Python<'p>, name: &str) -> PyRustTypeBuilder<'p, T> {
|
pub fn new(py: Python<'p>, name: &str) -> TypeBuilder<'p, T> {
|
||||||
#[cfg(feature="python27-sys")]
|
#[cfg(feature="python27-sys")]
|
||||||
fn new_impl<'p, T>(py: Python<'p>, name: &str) -> PyRustTypeBuilder<'p, T>
|
fn new_impl<'p, T>(py: Python<'p>, name: &str) -> TypeBuilder<'p, T>
|
||||||
where T: 'static + Send
|
where T: 'static + Send
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -113,7 +113,7 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
||||||
(*ht).ht_name = PyString::new(py, name.as_bytes()).steal_ptr(py);
|
(*ht).ht_name = PyString::new(py, name.as_bytes()).steal_ptr(py);
|
||||||
(*ht).ht_type.tp_name = ffi::PyString_AS_STRING((*ht).ht_name);
|
(*ht).ht_type.tp_name = ffi::PyString_AS_STRING((*ht).ht_name);
|
||||||
(*ht).ht_type.tp_new = Some(disabled_tp_new_callback);
|
(*ht).ht_type.tp_new = Some(disabled_tp_new_callback);
|
||||||
return PyRustTypeBuilder {
|
return TypeBuilder {
|
||||||
type_obj: PyType::unchecked_downcast_from(PyObject::from_owned_ptr(py, obj)),
|
type_obj: PyType::unchecked_downcast_from(PyObject::from_owned_ptr(py, obj)),
|
||||||
doc_str: None,
|
doc_str: None,
|
||||||
target_module: None,
|
target_module: None,
|
||||||
|
@ -125,10 +125,10 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature="python3-sys")]
|
#[cfg(feature="python3-sys")]
|
||||||
fn new_impl<'p, T>(py: Python<'p>, name: &str) -> PyRustTypeBuilder<'p, T>
|
fn new_impl<'p, T>(py: Python<'p>, name: &str) -> TypeBuilder<'p, T>
|
||||||
where T: 'static + Send
|
where T: 'static + Send
|
||||||
{
|
{
|
||||||
PyRustTypeBuilder {
|
TypeBuilder {
|
||||||
name: CString::new(name).unwrap(),
|
name: CString::new(name).unwrap(),
|
||||||
flags: ffi::Py_TPFLAGS_DEFAULT as libc::c_uint,
|
flags: ffi::Py_TPFLAGS_DEFAULT as libc::c_uint,
|
||||||
slots: Vec::new(),
|
slots: Vec::new(),
|
||||||
|
@ -146,15 +146,15 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
||||||
|
|
||||||
/// Sets the base class that this type is inheriting from.
|
/// Sets the base class that this type is inheriting from.
|
||||||
pub fn base<T2, B2>(self, base_type: &PyRustType<T2, B2>)
|
pub fn base<T2, B2>(self, base_type: &PyRustType<T2, B2>)
|
||||||
-> PyRustTypeBuilder<'p, T, PyRustObject<T2, B2>>
|
-> TypeBuilder<'p, T, PyRustObject<T2, B2>>
|
||||||
where T2: 'static + Send, B2: BaseObject
|
where T2: 'static + Send, B2: BaseObject
|
||||||
{
|
{
|
||||||
// Ensure we can't change the base after any callbacks are registered.
|
// Ensure we can't change the base after any callbacks are registered.
|
||||||
assert!(self.can_change_base,
|
assert!(self.can_change_base,
|
||||||
"base() must be called before any members are added to the type");
|
"base() must be called before any members are added to the type");
|
||||||
#[cfg(feature="python27-sys")]
|
#[cfg(feature="python27-sys")]
|
||||||
fn base_impl<'p, T, T2, B2>(slf: PyRustTypeBuilder<'p, T>, base_type: &PyRustType<T2, B2>)
|
fn base_impl<'p, T, T2, B2>(slf: TypeBuilder<'p, T>, base_type: &PyRustType<T2, B2>)
|
||||||
-> PyRustTypeBuilder<'p, T, PyRustObject<T2, B2>>
|
-> TypeBuilder<'p, T, PyRustObject<T2, B2>>
|
||||||
where T: 'static + Send, T2: 'static + Send, B2: BaseObject
|
where T: 'static + Send, T2: 'static + Send, B2: BaseObject
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -162,7 +162,7 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
||||||
(*slf.ht).ht_type.tp_base = base_type.as_type_ptr();
|
(*slf.ht).ht_type.tp_base = base_type.as_type_ptr();
|
||||||
ffi::Py_INCREF(base_type.as_object().as_ptr());
|
ffi::Py_INCREF(base_type.as_object().as_ptr());
|
||||||
}
|
}
|
||||||
return PyRustTypeBuilder {
|
return TypeBuilder {
|
||||||
type_obj: slf.type_obj,
|
type_obj: slf.type_obj,
|
||||||
doc_str: slf.doc_str,
|
doc_str: slf.doc_str,
|
||||||
target_module: slf.target_module,
|
target_module: slf.target_module,
|
||||||
|
@ -173,12 +173,12 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature="python3-sys")]
|
#[cfg(feature="python3-sys")]
|
||||||
fn base_impl<'p, T, T2, B2>(slf: PyRustTypeBuilder<'p, T>, base_type: &PyRustType<T2, B2>)
|
fn base_impl<'p, T, T2, B2>(slf: TypeBuilder<'p, T>, base_type: &PyRustType<T2, B2>)
|
||||||
-> PyRustTypeBuilder<'p, T, PyRustObject<T2, B2>>
|
-> TypeBuilder<'p, T, PyRustObject<T2, B2>>
|
||||||
where T: 'static + Send, T2: 'static + Send, B2: BaseObject
|
where T: 'static + Send, T2: 'static + Send, B2: BaseObject
|
||||||
{
|
{
|
||||||
let base_type_obj: &PyType = base_type;
|
let base_type_obj: &PyType = base_type;
|
||||||
return PyRustTypeBuilder {
|
return TypeBuilder {
|
||||||
name: slf.name,
|
name: slf.name,
|
||||||
flags: slf.flags,
|
flags: slf.flags,
|
||||||
slots: slf.slots,
|
slots: slf.slots,
|
||||||
|
@ -195,7 +195,7 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'p, T, B> PyRustTypeBuilder<'p, T, B> where T: 'static + Send, B: BaseObject {
|
impl <'p, T, B> TypeBuilder<'p, T, B> where T: 'static + Send, B: BaseObject {
|
||||||
|
|
||||||
/// Retrieves the type dictionary of the type being built.
|
/// Retrieves the type dictionary of the type being built.
|
||||||
#[cfg(feature="python27-sys")]
|
#[cfg(feature="python27-sys")]
|
||||||
|
@ -210,7 +210,7 @@ impl <'p, T, B> PyRustTypeBuilder<'p, T, B> where T: 'static + Send, B: BaseObje
|
||||||
|
|
||||||
/// Set the doc string on the type being built.
|
/// Set the doc string on the type being built.
|
||||||
pub fn doc(self, doc_str: &str) -> Self {
|
pub fn doc(self, doc_str: &str) -> Self {
|
||||||
PyRustTypeBuilder { doc_str: Some(CString::new(doc_str).unwrap()), .. self }
|
TypeBuilder { doc_str: Some(CString::new(doc_str).unwrap()), .. self }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a new member to the type.
|
/// Adds a new member to the type.
|
||||||
|
|
Loading…
Reference in New Issue