Get rid of #[py*] methods
This commit is contained in:
parent
2f1e1a2a3f
commit
6645708e4f
|
@ -11,17 +11,15 @@ use std::io::prelude::*;
|
|||
use pyo3::prelude::*;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::modinit as pymodinit;
|
||||
use pyo3::py::{class, methods, modinit};
|
||||
|
||||
#[pyclass(dict)]
|
||||
#[class(dict)]
|
||||
struct WordCounter {
|
||||
path: String,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl WordCounter {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject, path: String) -> PyResult<()> {
|
||||
|
@ -83,7 +81,7 @@ fn wc_parallel(lines: &str, search: &str) -> i32 {
|
|||
lines.par_lines().map(|line| wc_line(line, search)).sum()
|
||||
}
|
||||
|
||||
#[pymodinit(_word_count)]
|
||||
#[modinit(_word_count)]
|
||||
fn init_mod(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<WordCounter>()?;
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ use std::io::prelude::*;
|
|||
use pyo3::prelude::*;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use pyo3::py::modinit as pymodinit;
|
||||
use pyo3::py::modinit;
|
||||
|
||||
fn matches(word: &str, search: &str) -> bool {
|
||||
let mut search = search.chars();
|
||||
|
@ -50,7 +50,7 @@ fn wc_parallel(lines: &str, search: &str) -> i32 {
|
|||
lines.par_lines().map(|line| wc_line(line, search)).sum()
|
||||
}
|
||||
|
||||
#[pymodinit(_word_count)]
|
||||
#[modinit(_word_count)]
|
||||
fn init_mod(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
#[pyfn(m, "search")]
|
||||
fn search(py: Python, path: String, search: String) -> PyResult<i32> {
|
||||
|
|
|
@ -566,7 +566,7 @@ Example:
|
|||
extern crate pyo3;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::py::*;
|
||||
use pyo3::py::{class, proto};
|
||||
|
||||
#[class]
|
||||
struct ClassWithGCSupport {
|
||||
|
|
|
@ -10,9 +10,9 @@ One way is defining the function in the module definition.
|
|||
|
||||
extern crate pyo3;
|
||||
use pyo3::{py, PyResult, Python, PyModule};
|
||||
use pyo3::py::modinit as pymodinit;
|
||||
use pyo3::py::modint;
|
||||
|
||||
#[pymodinit(rust2py)]
|
||||
#[modinit(rust2py)]
|
||||
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
// Note that the `#[pyfn()]` annotation automatically converts the arguments from
|
||||
|
@ -41,14 +41,14 @@ extern crate pyo3;
|
|||
use pyo3::{py, PyResult, Python, PyModule};
|
||||
|
||||
use pyo3::py::function as pyfunction;
|
||||
use pyo3::py::modinit as pymodinit;
|
||||
use pyo3::py::modint;
|
||||
|
||||
#[pyfunction]
|
||||
fn double(x: usize) -> usize {
|
||||
x * 2
|
||||
}
|
||||
|
||||
#[pymodinit(module_with_functions)]
|
||||
#[modinit(module_with_functions)]
|
||||
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_function!(double)).unwrap();
|
||||
|
||||
|
|
|
@ -8,12 +8,12 @@ As shown in the Getting Started chapter, you can create a module as follows:
|
|||
extern crate pyo3;
|
||||
use pyo3::{py, PyResult, Python, PyModule};
|
||||
|
||||
use pyo3::py::modinit as pymodinit;
|
||||
use pyo3::py::modint;
|
||||
|
||||
// add bindings to the generated python module
|
||||
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
|
||||
/// This module is implemented in Rust.
|
||||
#[pymodinit(rust2py)]
|
||||
#[modinit(rust2py)]
|
||||
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
// pyo3 aware function. All of our python interface could be declared in a separate module.
|
||||
|
|
|
@ -64,12 +64,12 @@ features = ["extension-module"]
|
|||
extern crate pyo3;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use pyo3::py::modinit as pymodinit;
|
||||
use pyo3::py::modint;
|
||||
|
||||
// Add bindings to the generated python module
|
||||
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
|
||||
/// This module is implemented in Rust.
|
||||
#[pymodinit(rust2py)]
|
||||
#[modinit(rust2py)]
|
||||
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
#[pyfn(m, "sum_as_string")]
|
||||
|
|
|
@ -28,7 +28,7 @@ Then in the Python bridge, we have a function `search` exposed to Python runtime
|
|||
`Python::allow_threads` method to enable true parallelism:
|
||||
|
||||
```rust,ignore
|
||||
#[pymodinit(_word_count)]
|
||||
#[modinit(_word_count)]
|
||||
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
#[pyfn(m, "search")]
|
||||
|
|
|
@ -50,8 +50,7 @@ fn check_generic(name: &syn::Ident, sig: &syn::MethodSig) {
|
|||
pub fn body_to_result(body: &TokenStream, spec: &FnSpec) -> TokenStream {
|
||||
let output = &spec.output;
|
||||
quote! {
|
||||
use pyo3::ReturnTypeIntoPyResult;
|
||||
let _result: PyResult<<#output as ReturnTypeIntoPyResult>::Inner> = {
|
||||
let _result: ::pyo3::PyResult<<#output as ::pyo3::ReturnTypeIntoPyResult>::Inner> = {
|
||||
#body
|
||||
};
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ use ffi;
|
|||
static NO_PY_METHODS: &'static [PyMethodDefType] = &[];
|
||||
|
||||
/// `PyMethodDefType` represents different types of python callable objects.
|
||||
/// It is used by `#[pymethods]` and `#[pyproto]` annotations.
|
||||
/// It is used by `#[methods]` and `#[proto]` annotations.
|
||||
#[derive(Debug)]
|
||||
pub enum PyMethodDefType {
|
||||
/// Represents class `__new__` method
|
||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -54,10 +54,10 @@
|
|||
//! # Python extension
|
||||
//!
|
||||
//! To allow Python to load the rust code as a Python extension
|
||||
//! module, you need provide initialization function and annotate it with `#[pymodinit(name)]`.
|
||||
//! module, you need provide initialization function and annotate it with `#[modinit(name)]`.
|
||||
//! `pymodinit` expands to an `extern "C"` function.
|
||||
//!
|
||||
//! Macro syntax: `#[pymodinit(name)]`
|
||||
//! Macro syntax: `#[modinit(name)]`
|
||||
//!
|
||||
//! 1. `name`: The module name as a Rust identifier
|
||||
//! 2. Decorate init function `Fn(Python, &PyModule) -> PyResult<()>`.
|
||||
|
@ -78,15 +78,15 @@
|
|||
//! #![feature(proc_macro, specialization)]
|
||||
//!
|
||||
//! extern crate pyo3;
|
||||
//! use pyo3::{py, Python, PyResult, PyModule, PyString};
|
||||
//! use pyo3::prelude::*;
|
||||
//!
|
||||
//! use pyo3::py::modinit as pymodinit;
|
||||
//! use pyo3::py::modinit;
|
||||
//!
|
||||
//! // add bindings to the generated python module
|
||||
//! // N.B: names: "libhello" must be the name of the `.so` or `.pyd` file
|
||||
//!
|
||||
//! /// Module documentation string
|
||||
//! #[pymodinit(hello)]
|
||||
//! #[modinit(hello)]
|
||||
//! fn init_module(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
//!
|
||||
//! // pyo3 aware function. All of our python interface could be declared
|
||||
|
|
|
@ -27,7 +27,7 @@ static START_PYO3: sync::Once = sync::ONCE_INIT;
|
|||
/// thread (the thread which originally initialized Python) also initializes
|
||||
/// threading.
|
||||
///
|
||||
/// When writing an extension module, the `#[pymodinit(..)]` macro
|
||||
/// When writing an extension module, the `#[modinit(..)]` macro
|
||||
/// will ensure that Python threading is initialized.
|
||||
///
|
||||
pub fn prepare_freethreaded_python() {
|
||||
|
|
|
@ -103,15 +103,15 @@ impl<'a, T: ?Sized> PyTypeInfo for &'a T where T: PyTypeInfo {
|
|||
///
|
||||
/// Example of custom class implementation with `__new__` method:
|
||||
/// ```rust,ignore
|
||||
/// use pyo3::py::class as pyclass;
|
||||
/// use pyo3::py::methods as pymethods;
|
||||
/// use pyo3::py::class;
|
||||
/// use pyo3::py::methods;
|
||||
///
|
||||
/// #[pyclass]
|
||||
/// #[class]
|
||||
/// struct MyClass {
|
||||
/// token: PyToken
|
||||
/// }
|
||||
///
|
||||
/// #[pymethods]
|
||||
/// #[methods]
|
||||
/// impl MyClass {
|
||||
/// #[new]
|
||||
/// fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
|
|
@ -3,18 +3,17 @@
|
|||
extern crate pyo3;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::proto as pyproto;
|
||||
use pyo3::py::{class, proto};
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct UnaryArithmetic {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyNumberProtocol for UnaryArithmetic {
|
||||
fn __neg__(&self) -> PyResult<&'static str> {
|
||||
Ok("neg")
|
||||
|
@ -45,32 +44,32 @@ fn unary_arithmetic() {
|
|||
py_run!(py, c, "assert ~c == 'invert'");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct BinaryArithmetic {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyObjectProtocol for BinaryArithmetic {
|
||||
fn __repr__(&self) -> PyResult<&'static str> {
|
||||
Ok("BA")
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct InPlaceOperations {
|
||||
value: u32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyObjectProtocol for InPlaceOperations {
|
||||
fn __repr__(&self) -> PyResult<String> {
|
||||
Ok(format!("IPO({:?})", self.value))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyNumberProtocol for InPlaceOperations {
|
||||
fn __iadd__(&mut self, other: u32) -> PyResult<()> {
|
||||
self.value += other;
|
||||
|
@ -133,7 +132,7 @@ fn inplace_operations() {
|
|||
init(12, "d = c; c ^= 5; assert repr(c) == repr(d) == 'IPO(9)'");
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyNumberProtocol for BinaryArithmetic {
|
||||
fn __add__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} + {:?}", lhs, rhs))
|
||||
|
@ -194,12 +193,12 @@ fn binary_arithmetic() {
|
|||
py_run!(py, c, "assert 1 | c == '1 | BA'");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct RichComparisons {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyObjectProtocol for RichComparisons {
|
||||
fn __repr__(&self) -> PyResult<&'static str> {
|
||||
Ok("RC")
|
||||
|
@ -217,12 +216,12 @@ impl PyObjectProtocol for RichComparisons {
|
|||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct RichComparisons2 {
|
||||
py: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyObjectProtocol for RichComparisons2 {
|
||||
fn __repr__(&self) -> PyResult<&'static str> {
|
||||
Ok("RC2")
|
||||
|
|
|
@ -8,16 +8,16 @@ use std::ptr;
|
|||
use pyo3::ffi;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::proto as pyproto;
|
||||
use pyo3::py::class;
|
||||
use pyo3::py::proto;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct TestClass {
|
||||
vec: Vec<u8>,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyBufferProtocol for TestClass {
|
||||
fn bf_getbuffer(&self, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> {
|
||||
if view.is_null() {
|
||||
|
|
|
@ -4,12 +4,12 @@ extern crate pyo3;
|
|||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::class;
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct EmptyClass {}
|
||||
|
||||
#[test]
|
||||
|
@ -27,7 +27,7 @@ fn empty_class() {
|
|||
///Line2
|
||||
/// Line3
|
||||
// this is not doc string
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct ClassWithDocs {}
|
||||
|
||||
#[test]
|
||||
|
@ -44,7 +44,7 @@ fn class_with_docstr() {
|
|||
}
|
||||
}
|
||||
|
||||
#[pyclass(name=CustomName)]
|
||||
#[class(name=CustomName)]
|
||||
struct EmptyClass2 {}
|
||||
|
||||
#[test]
|
||||
|
@ -55,7 +55,7 @@ fn custom_class_name() {
|
|||
py_assert!(py, typeobj, "typeobj.__name__ == 'CustomName'");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct EmptyClassInModule {}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -4,15 +4,15 @@ extern crate pyo3;
|
|||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::class;
|
||||
use pyo3::py::methods;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct EmptyClassWithNew {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl EmptyClassWithNew {
|
||||
#[__new__]
|
||||
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
@ -34,13 +34,13 @@ fn empty_class_with_new() {
|
|||
);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct NewWithOneArg {
|
||||
_data: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl NewWithOneArg {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject, arg: i32) -> PyResult<()> {
|
||||
|
@ -61,7 +61,7 @@ fn new_with_one_arg() {
|
|||
assert_eq!(obj._data, 42);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct NewWithTwoArgs {
|
||||
_data1: i32,
|
||||
_data2: i32,
|
||||
|
@ -69,7 +69,7 @@ struct NewWithTwoArgs {
|
|||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl NewWithTwoArgs {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject, arg1: i32, arg2: i32) -> PyResult<()> {
|
||||
|
|
|
@ -6,20 +6,20 @@ use pyo3::ffi;
|
|||
use pyo3::prelude::*;
|
||||
use std::{isize, iter};
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::proto as pyproto;
|
||||
use pyo3::py::class;
|
||||
use pyo3::py::methods;
|
||||
use pyo3::py::proto;
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
pub struct Len {
|
||||
l: usize,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyMappingProtocol for Len {
|
||||
fn __len__(&self) -> PyResult<usize> {
|
||||
Ok(self.l)
|
||||
|
@ -45,13 +45,13 @@ fn len() {
|
|||
py_expect_exception!(py, inst, "len(inst)", OverflowError);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct Iterator {
|
||||
iter: Box<iter::Iterator<Item = i32> + Send>,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyIterProtocol for Iterator {
|
||||
fn __iter__(&mut self) -> PyResult<Py<Iterator>> {
|
||||
Ok(self.into())
|
||||
|
@ -75,12 +75,12 @@ fn iterator() {
|
|||
py_assert!(py, inst, "list(inst) == [5, 6, 7]");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct StringMethods {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl<'p> PyObjectProtocol<'p> for StringMethods {
|
||||
fn __str__(&self) -> PyResult<&'static str> {
|
||||
Ok("str")
|
||||
|
@ -129,13 +129,13 @@ fn string_methods() {
|
|||
py_assert!(py, obj, "'{0:x}'.format(obj) == 'format(x)'");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct Comparisons {
|
||||
val: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyObjectProtocol for Comparisons {
|
||||
fn __hash__(&self) -> PyResult<isize> {
|
||||
Ok(self.val as isize)
|
||||
|
@ -162,12 +162,12 @@ fn comparisons() {
|
|||
py_assert!(py, zero, "not zero");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct Sequence {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PySequenceProtocol for Sequence {
|
||||
fn __len__(&self) -> PyResult<usize> {
|
||||
Ok(5)
|
||||
|
@ -191,12 +191,12 @@ fn sequence() {
|
|||
py_expect_exception!(py, c, "c['abc']", TypeError);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct Callable {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl Callable {
|
||||
#[__call__]
|
||||
fn __call__(&self, arg: i32) -> PyResult<i32> {
|
||||
|
@ -217,14 +217,14 @@ fn callable() {
|
|||
py_assert!(py, nc, "not callable(nc)");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct SetItem {
|
||||
key: i32,
|
||||
val: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyMappingProtocol<'a> for SetItem {
|
||||
fn __setitem__(&mut self, key: i32, val: i32) -> PyResult<()> {
|
||||
self.key = key;
|
||||
|
@ -250,13 +250,13 @@ fn setitem() {
|
|||
py_expect_exception!(py, c, "del c[1]", NotImplementedError);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct DelItem {
|
||||
key: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyMappingProtocol<'a> for DelItem {
|
||||
fn __delitem__(&mut self, key: i32) -> PyResult<()> {
|
||||
self.key = key;
|
||||
|
@ -275,13 +275,13 @@ fn delitem() {
|
|||
py_expect_exception!(py, c, "c[1] = 2", NotImplementedError);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct SetDelItem {
|
||||
val: Option<i32>,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyMappingProtocol for SetDelItem {
|
||||
fn __setitem__(&mut self, _key: i32, val: i32) -> PyResult<()> {
|
||||
self.val = Some(val);
|
||||
|
@ -310,12 +310,12 @@ fn setdelitem() {
|
|||
assert_eq!(c.val, None);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct Reversed {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyMappingProtocol for Reversed {
|
||||
fn __reversed__(&self) -> PyResult<&'static str> {
|
||||
Ok("I am reversed")
|
||||
|
@ -331,12 +331,12 @@ fn reversed() {
|
|||
py_run!(py, c, "assert reversed(c) == 'I am reversed'");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct Contains {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PySequenceProtocol for Contains {
|
||||
fn __contains__(&self, item: i32) -> PyResult<bool> {
|
||||
Ok(item >= 0)
|
||||
|
@ -354,13 +354,13 @@ fn contains() {
|
|||
py_expect_exception!(py, c, "assert 'wrong type' not in c", TypeError);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct ContextManager {
|
||||
exit_called: bool,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl<'p> PyContextProtocol<'p> for ContextManager {
|
||||
fn __enter__(&mut self) -> PyResult<i32> {
|
||||
Ok(42)
|
||||
|
@ -421,12 +421,12 @@ fn test_basics() {
|
|||
assert_eq!(5, indices.slicelength);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct Test {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl<'p> PyMappingProtocol<'p> for Test {
|
||||
fn __getitem__(&self, idx: &PyObjectRef) -> PyResult<PyObject> {
|
||||
if let Ok(slice) = idx.cast_as::<PySlice>() {
|
||||
|
@ -457,7 +457,7 @@ fn test_cls_impl() {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[pyclass(dict)]
|
||||
#[class(dict)]
|
||||
struct DunderDictSupport {
|
||||
token: PyToken,
|
||||
}
|
||||
|
@ -477,7 +477,7 @@ fn dunder_dict_support() {
|
|||
);
|
||||
}
|
||||
|
||||
#[pyclass(weakref, dict)]
|
||||
#[class(weakref, dict)]
|
||||
struct WeakRefDunderDictSupport {
|
||||
token: PyToken,
|
||||
}
|
||||
|
|
|
@ -8,14 +8,14 @@ use std::cell::RefCell;
|
|||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::proto as pyproto;
|
||||
use pyo3::py::class;
|
||||
use pyo3::py::methods;
|
||||
use pyo3::py::proto;
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass(freelist = 2)]
|
||||
#[class(freelist = 2)]
|
||||
struct ClassWithFreelist {
|
||||
token: PyToken,
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ impl Drop for TestDropCall {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct DataIsDropped {
|
||||
member1: TestDropCall,
|
||||
member2: TestDropCall,
|
||||
|
@ -89,7 +89,7 @@ fn data_is_dropped() {
|
|||
assert!(drop_called2.load(Ordering::Relaxed));
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct ClassWithDrop {
|
||||
token: PyToken,
|
||||
}
|
||||
|
@ -136,14 +136,14 @@ fn create_pointers_in_drop() {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct GCIntegration {
|
||||
self_ref: RefCell<PyObject>,
|
||||
dropped: TestDropCall,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
#[proto]
|
||||
impl PyGCProtocol for GCIntegration {
|
||||
fn __traverse__(&self, visit: PyVisit) -> Result<(), PyTraverseError> {
|
||||
visit.call(&*self.self_ref.borrow())
|
||||
|
@ -178,7 +178,7 @@ fn gc_integration() {
|
|||
assert!(drop_called.load(Ordering::Relaxed));
|
||||
}
|
||||
|
||||
#[pyclass(gc)]
|
||||
#[class(gc)]
|
||||
struct GCIntegration2 {
|
||||
token: PyToken,
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ fn gc_integration2() {
|
|||
py_run!(py, inst, "import gc; assert inst in gc.get_objects()");
|
||||
}
|
||||
|
||||
#[pyclass(weakref)]
|
||||
#[class(weakref)]
|
||||
struct WeakRefSupport {
|
||||
token: PyToken,
|
||||
}
|
||||
|
@ -206,13 +206,13 @@ fn weakref_support() {
|
|||
);
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct BaseClassWithDrop {
|
||||
token: PyToken,
|
||||
data: Option<Arc<AtomicBool>>,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl BaseClassWithDrop {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
@ -231,13 +231,13 @@ impl Drop for BaseClassWithDrop {
|
|||
}
|
||||
}
|
||||
|
||||
#[pyclass(base=BaseClassWithDrop)]
|
||||
#[class(base=BaseClassWithDrop)]
|
||||
struct SubClassWithDrop {
|
||||
token: PyToken,
|
||||
data: Option<Arc<AtomicBool>>,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl SubClassWithDrop {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
|
|
@ -5,19 +5,19 @@ extern crate pyo3;
|
|||
use pyo3::prelude::*;
|
||||
use std::isize;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::class;
|
||||
use pyo3::py::methods;
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct ClassWithProperties {
|
||||
num: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl ClassWithProperties {
|
||||
fn get_num(&self) -> PyResult<i32> {
|
||||
Ok(self.num)
|
||||
|
@ -50,7 +50,7 @@ fn class_with_properties() {
|
|||
py_run!(py, inst, "assert inst.get_num() == inst.DATA");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct GetterSetter {
|
||||
#[prop(get, set)]
|
||||
num: i32,
|
||||
|
@ -59,7 +59,7 @@ struct GetterSetter {
|
|||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl GetterSetter {
|
||||
fn get_num2(&self) -> PyResult<i32> {
|
||||
Ok(self.num)
|
||||
|
|
|
@ -5,19 +5,18 @@ extern crate pyo3;
|
|||
use pyo3::prelude::*;
|
||||
use std::isize;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::{class, methods};
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct BaseClass {
|
||||
#[prop(get)]
|
||||
val1: usize,
|
||||
}
|
||||
|
||||
#[pyclass(subclass)]
|
||||
#[class(subclass)]
|
||||
struct SubclassAble {}
|
||||
|
||||
#[test]
|
||||
|
@ -36,7 +35,7 @@ fn subclass() {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl BaseClass {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
@ -44,13 +43,13 @@ impl BaseClass {
|
|||
}
|
||||
}
|
||||
|
||||
#[pyclass(base=BaseClass)]
|
||||
#[class(base=BaseClass)]
|
||||
struct SubClass {
|
||||
#[prop(get)]
|
||||
val2: usize,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl SubClass {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
|
|
@ -4,19 +4,19 @@ extern crate pyo3;
|
|||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::class;
|
||||
use pyo3::py::methods;
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct InstanceMethod {
|
||||
member: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl InstanceMethod {
|
||||
/// Test method
|
||||
fn method(&self) -> PyResult<i32> {
|
||||
|
@ -42,13 +42,13 @@ fn instance_method() {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct InstanceMethodWithArgs {
|
||||
member: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl InstanceMethodWithArgs {
|
||||
fn method(&self, multiplier: i32) -> PyResult<i32> {
|
||||
Ok(self.member * multiplier)
|
||||
|
@ -74,12 +74,12 @@ fn instance_method_with_args() {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct ClassMethod {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl ClassMethod {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
@ -111,12 +111,12 @@ fn class_method() {
|
|||
).unwrap();
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct ClassMethodWithArgs {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl ClassMethodWithArgs {
|
||||
#[classmethod]
|
||||
fn method(cls: &PyType, input: &PyString) -> PyResult<String> {
|
||||
|
@ -139,12 +139,12 @@ fn class_method_with_args() {
|
|||
).unwrap();
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct StaticMethod {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl StaticMethod {
|
||||
#[new]
|
||||
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
||||
|
@ -177,12 +177,12 @@ fn static_method() {
|
|||
).unwrap();
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct StaticMethodWithArgs {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl StaticMethodWithArgs {
|
||||
#[staticmethod]
|
||||
fn method(_py: Python, input: i32) -> PyResult<String> {
|
||||
|
@ -204,12 +204,12 @@ fn static_method_with_args() {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct MethArgs {
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl MethArgs {
|
||||
#[args(test)]
|
||||
fn get_optional(&self, test: Option<i32>) -> PyResult<i32> {
|
||||
|
|
|
@ -5,19 +5,19 @@ extern crate pyo3;
|
|||
use pyo3::prelude::*;
|
||||
use std::isize;
|
||||
|
||||
use pyo3::py::class as pyclass;
|
||||
use pyo3::py::methods as pymethods;
|
||||
use pyo3::py::class;
|
||||
use pyo3::py::methods;
|
||||
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[class]
|
||||
struct MutRefArg {
|
||||
n: i32,
|
||||
token: PyToken,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
#[methods]
|
||||
impl MutRefArg {
|
||||
fn get(&self) -> PyResult<i32> {
|
||||
Ok(self.n)
|
||||
|
|
Loading…
Reference in New Issue