diff --git a/examples/rustapi_module/src/lib.rs b/examples/rustapi_module/src/lib.rs index fe9bbcac..be7fd6ed 100644 --- a/examples/rustapi_module/src/lib.rs +++ b/examples/rustapi_module/src/lib.rs @@ -1,4 +1,3 @@ - pub mod datetime; pub mod dict_iter; pub mod othermod; diff --git a/guide/src/class.md b/guide/src/class.md index a05c0310..d5e0d3ca 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -5,7 +5,6 @@ To define python custom class, rust struct needs to be annotated with `#[pyclass]` attribute. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; #[pyclass] @@ -42,11 +41,8 @@ To declare a constructor, you need to define a class method and annotate it with attribute. Only the python `__new__` method can be specified, `__init__` is not available. ```rust -# #![feature(specialization)] -# # use pyo3::prelude::*; # use pyo3::PyRawObject; - #[pyclass] struct MyClass { num: i32, @@ -86,7 +82,6 @@ By default `PyObject` is used as default base class. To override default base cl with value of custom class struct. Subclass must call parent's `__new__` method. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # use pyo3::PyRawObject; #[pyclass] @@ -136,7 +131,6 @@ Descriptor methods can be defined in attributes. i.e. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -161,7 +155,6 @@ Descriptor name becomes function name with prefix removed. This is useful in cas rust's special keywords like `type`. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -190,7 +183,6 @@ Also both `#[getter]` and `#[setter]` attributes accepts one parameter. If parameter is specified, it is used and property name. i.e. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -218,7 +210,6 @@ In this case property `number` is defined. And it is available from python code For simple cases you can also define getters and setters in your Rust struct field definition, for example: ```rust -# #![feature(specialization)] # use pyo3::prelude::*; #[pyclass] struct MyClass { @@ -237,7 +228,6 @@ wrappers for all functions in this block with some variations, like descriptors, class method static methods, etc. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -265,7 +255,6 @@ The return type must be `PyResult` for some `T` that implements `IntoPyObject get injected by method wrapper. i.e ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -289,7 +278,6 @@ To specify class method for custom class, method needs to be annotated with`#[classmethod]` attribute. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -321,7 +309,6 @@ with `#[staticmethod]` attribute. The return type must be `PyResult` for some `T` that implements `IntoPyObject`. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -344,7 +331,6 @@ To specify custom `__call__` method for custom class, call method needs to be an with `#[call]` attribute. Arguments of the method are specified same as for instance method. ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # #[pyclass] # struct MyClass { @@ -387,7 +373,6 @@ Each parameter could one of following type: Example: ```rust -# #![feature(specialization)] # use pyo3::prelude::*; # # #[pyclass] diff --git a/pyo3-derive-backend/src/py_class.rs b/pyo3-derive-backend/src/py_class.rs index 956e218a..713d4e60 100644 --- a/pyo3-derive-backend/src/py_class.rs +++ b/pyo3-derive-backend/src/py_class.rs @@ -326,8 +326,7 @@ fn impl_descriptors(cls: &syn::Type, descriptors: Vec<(syn::Field, Vec)> #(#methods)* ::pyo3::inventory::submit! { - #![crate = pyo3] - { + #![crate = pyo3] { type ClsInventory = <#cls as ::pyo3::class::methods::PyMethodsInventoryDispatch>::InventoryType; ::new(&[#(#py_methods),*]) } diff --git a/pyo3-derive-backend/src/py_impl.rs b/pyo3-derive-backend/src/py_impl.rs index 39b565a5..a3556c40 100644 --- a/pyo3-derive-backend/src/py_impl.rs +++ b/pyo3-derive-backend/src/py_impl.rs @@ -31,9 +31,8 @@ pub fn impl_methods(ty: &syn::Type, impls: &mut Vec) -> TokenStre } quote! { - ::pyo3::inventory::submit! { - #![crate = pyo3] - { + ::pyo3::inventory::submit! { + #![crate = pyo3] { type TyInventory = <#ty as ::pyo3::class::methods::PyMethodsInventoryDispatch>::InventoryType; ::new(&[#(#methods),*]) } diff --git a/src/lib.rs b/src/lib.rs index 0f3bd5d9..d5aa9ecc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,8 +55,6 @@ //! **`src/lib.rs`** //! //! ```rust -//! #![feature(specialization)] -//! //! use pyo3::prelude::*; //! use pyo3::wrap_pyfunction; //! @@ -101,8 +99,6 @@ //! Example program displaying the value of `sys.version`: //! //! ```rust -//! #![feature(specialization)] -//! //! use pyo3::prelude::*; //! use pyo3::types::PyDict; //! @@ -146,12 +142,12 @@ pub use crate::pythonrun::{init_once, prepare_freethreaded_python, GILGuard, GIL pub use crate::typeob::{PyObjectAlloc, PyRawObject, PyTypeInfo}; pub use crate::types::exceptions; -// We need those types in the macro exports -#[doc(hidden)] -pub use libc; // We need that reexport for wrap_function #[doc(hidden)] pub use mashup; +// We need that reexport for pymethods +#[doc(hidden)] +pub use inventory; /// Rust FFI declarations for Python pub mod ffi; @@ -207,7 +203,7 @@ pub mod proc_macro { macro_rules! wrap_pyfunction { ($function_name:ident) => {{ // Get the mashup macro and its helpers into scope - use mashup::*; + use pyo3::mashup::*; mashup! { // Make sure this ident matches the one in function_wrapper_ident @@ -227,7 +223,7 @@ macro_rules! wrap_pyfunction { #[macro_export] macro_rules! wrap_pymodule { ($module_name:ident) => {{ - use mashup::*; + use pyo3::mashup::*; mashup! { m["method"] = PyInit_ $module_name; diff --git a/src/typeob.rs b/src/typeob.rs index be997c2f..2c83d661 100644 --- a/src/typeob.rs +++ b/src/typeob.rs @@ -2,6 +2,12 @@ //! Python type object information +use std::collections::HashMap; +use std::ffi::CString; +use std::os::raw::c_void; + +use class::methods::PyMethodsProtocol; + use crate::class::methods::PyMethodDefType; use crate::err::{PyErr, PyResult}; use crate::instance::{Py, PyObjectWithGIL}; @@ -10,10 +16,6 @@ use crate::python::{IntoPyPointer, Python}; use crate::types::PyObjectRef; use crate::types::PyType; use crate::{class, ffi, pythonrun}; -use class::methods::PyMethodsProtocol; -use std::collections::HashMap; -use std::ffi::CString; -use std::os::raw::c_void; /// Python type information. pub trait PyTypeInfo { @@ -72,8 +74,6 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3; /// /// Example of custom class implementation with `__new__` method: /// ``` -/// #![feature(specialization)] -/// /// use pyo3::prelude::*; /// /// #[pyclass] @@ -295,7 +295,7 @@ where let gil = Python::acquire_gil(); let py = gil.python(); - initialize_type::(py, None).unwrap_or_else(|_| { + initialize_type::(py).unwrap_or_else(|_| { panic!("An error occurred while initializing class {}", Self::NAME) }); } @@ -317,26 +317,19 @@ where /// Register new type in python object system. /// -/// Currently, module_name is always None, so it defaults to builtins. +/// Currently, module_name is always None, so it defaults to pyo3_extension #[cfg(not(Py_LIMITED_API))] -pub fn initialize_type(py: Python, module_name: Option<&str>) -> PyResult<*mut ffi::PyTypeObject> +pub fn initialize_type(py: Python) -> PyResult<*mut ffi::PyTypeObject> where T: PyObjectAlloc + PyTypeInfo + PyMethodsProtocol, { - // type name - let name = match module_name { - Some(module_name) => CString::new(format!("{}.{}", module_name, T::NAME)), - None => CString::new(T::NAME), - }; - let name = name - .expect("Module name/type name must not contain NUL byte") - .into_raw(); + let type_name = CString::new(T::NAME).expect("class name must not contain NUL byte"); - let type_object: &mut ffi::PyTypeObject = unsafe { &mut *T::type_object() }; + let type_object: &mut ffi::PyTypeObject = unsafe { T::type_object() }; let base_type_object: &mut ffi::PyTypeObject = - unsafe { &mut *::type_object() }; + unsafe { ::type_object() }; - type_object.tp_name = name; + type_object.tp_name = type_name.into_raw(); type_object.tp_doc = T::DESCRIPTION.as_ptr() as *const _; type_object.tp_base = base_type_object; diff --git a/src/types/module.rs b/src/types/module.rs index 02be3dfa..3f98977b 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -9,11 +9,11 @@ use crate::instance::PyObjectWithGIL; use crate::object::PyObject; use crate::objectprotocol::ObjectProtocol; use crate::python::{Python, ToPyPointer}; +use crate::typeob::PyTypeCreate; use crate::types::{exceptions, PyDict, PyObjectRef}; use std::ffi::{CStr, CString}; use std::os::raw::c_char; use std::str; -use typeob::PyTypeCreate; /// Represents a Python `module` object. #[repr(transparent)]