From b92056b429f288ada8854c4150ecd2e88fff256a Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 11 Aug 2017 20:42:26 -0700 Subject: [PATCH] update doc string --- CHANGES.txt | 4 +++- src/typeob.rs | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index d2340a38..d1b04135 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,9 +1,11 @@ Changes ------- -0.2.x (xx-xx-2017) +0.2.x (08-12-2017) ^^^^^^^^^^^^^^^^^^ +* Added inheritance support #15 + * Added weakref support #56 * Allow to add gc support without implementing PyGCProtocol #57 diff --git a/src/typeob.rs b/src/typeob.rs index be32d0dc..224e5d8c 100644 --- a/src/typeob.rs +++ b/src/typeob.rs @@ -93,12 +93,37 @@ impl<'a, T: ?Sized> PyTypeInfo for &'a T where T: PyTypeInfo { } } +/// Special object that is used for python object creation. +/// `pyo3` library automatically creates this object for class `__new__` method. +/// Behavior is undefined if constructor of custom class does not initialze +/// instance of `PyRawObject` with rust value with `init` method. +/// Calling of `__new__` method of base class is developer's responsibility. +/// +/// Example of custom class implementation with `__new__` method: +/// ```rust +/// #[py::class] +/// struct MyClass { +/// token: PyToken +/// } +/// +/// #[py::methods] +/// impl MyClass { +/// #[new] +/// fn __new__(obj: &PyRawObject) -> PyResult<()> { +/// obj.init(|token| MyClass{token| token}) +/// MyClass::BaseType::__new__(obj) +/// } +/// } +/// ``` #[allow(dead_code)] pub struct PyRawObject { ptr: *mut ffi::PyObject, + /// Type object of class which __new__ method get called tp_ptr: *mut ffi::PyTypeObject, + /// Type object of top most class in inheritance chain, + /// it might be python class. curr_ptr: *mut ffi::PyTypeObject, - initialized: usize, + // initialized: usize, } impl PyRawObject { @@ -138,23 +163,6 @@ impl PyRawObject { } } - /// Initialize memory using value. - /// `PyRawObject` is used by class `__new__` method. - /// ``` - /// #[py::class] - /// struct MyClass { - /// token: PyToken - /// } - /// - /// #[py::methods] - /// impl MyClass { - /// #[new] - /// fn __new__(obj: &PyRawObject) -> PyResult<()> { - /// obj.init(|token| MyClass{token| token}) - /// MyClass::BaseType::__new__(obj) - /// } - /// } - /// ``` pub fn init(&self, f: F) -> PyResult<()> where F: FnOnce(PyToken) -> T, T: PyTypeInfo @@ -186,7 +194,7 @@ impl PyRawObject { impl IntoPyPointer for PyRawObject { fn into_ptr(self) -> *mut ffi::PyObject { // TODO: panic if not all types initialized - return self.ptr + self.ptr } }