rust 2018 fixes
This commit is contained in:
parent
c71c116f29
commit
56f2257e90
|
@ -1,4 +1,3 @@
|
|||
|
||||
pub mod datetime;
|
||||
pub mod dict_iter;
|
||||
pub mod othermod;
|
||||
|
|
|
@ -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<T>` 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<T>`
|
|||
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]
|
||||
|
|
|
@ -326,8 +326,7 @@ fn impl_descriptors(cls: &syn::Type, descriptors: Vec<(syn::Field, Vec<FnType>)>
|
|||
#(#methods)*
|
||||
|
||||
::pyo3::inventory::submit! {
|
||||
#![crate = pyo3]
|
||||
{
|
||||
#![crate = pyo3] {
|
||||
type ClsInventory = <#cls as ::pyo3::class::methods::PyMethodsInventoryDispatch>::InventoryType;
|
||||
<ClsInventory as ::pyo3::class::methods::PyMethodsInventory>::new(&[#(#py_methods),*])
|
||||
}
|
||||
|
|
|
@ -31,9 +31,8 @@ pub fn impl_methods(ty: &syn::Type, impls: &mut Vec<syn::ImplItem>) -> TokenStre
|
|||
}
|
||||
|
||||
quote! {
|
||||
::pyo3::inventory::submit! {
|
||||
#![crate = pyo3]
|
||||
{
|
||||
::pyo3::inventory::submit! {
|
||||
#![crate = pyo3] {
|
||||
type TyInventory = <#ty as ::pyo3::class::methods::PyMethodsInventoryDispatch>::InventoryType;
|
||||
<TyInventory as ::pyo3::class::methods::PyMethodsInventory>::new(&[#(#methods),*])
|
||||
}
|
||||
|
|
14
src/lib.rs
14
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;
|
||||
|
|
|
@ -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::<Self>(py, None).unwrap_or_else(|_| {
|
||||
initialize_type::<Self>(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<T>(py: Python, module_name: Option<&str>) -> PyResult<*mut ffi::PyTypeObject>
|
||||
pub fn initialize_type<T>(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 *<T::BaseType as PyTypeInfo>::type_object() };
|
||||
unsafe { <T::BaseType as PyTypeInfo>::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;
|
||||
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Reference in New Issue