diff --git a/guide/src/class.md b/guide/src/class.md index b52b46fe..99b176a3 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -95,14 +95,13 @@ with value of custom class struct. Subclass must call parent's `__new__` method. #[pyclass] struct BaseClass { val1: usize, - token: PyToken, } #[pymethods] impl BaseClass { #[new] fn __new__(obj: &PyRawObject) -> PyResult<()> { - obj.init(|token| BaseClass{val1: 10, token}) + obj.init(|_| BaseClass{ val1: 10 }) } pub fn method(&self) -> PyResult<()> { @@ -113,14 +112,13 @@ impl BaseClass { #[pyclass(extends=BaseClass)] struct SubClass { val2: usize, - token: PyToken, } #[pymethods] impl SubClass { #[new] fn __new__(obj: &PyRawObject) -> PyResult<()> { - obj.init(|token| SubClass{val2: 10, token}); + obj.init(|_| SubClass{ val2: 10 }); BaseClass::__new__(obj) } @@ -513,7 +511,6 @@ use pyo3::prelude::*; #[pyclass] struct ClassWithGCSupport { obj: Option, - token: PyToken, } #[pyproto] @@ -562,7 +559,6 @@ use pyo3::prelude::*; #[pyclass] struct MyIterator { iter: Box + Send>, - token: PyToken, } #[pyproto] diff --git a/pyo3-derive-backend/src/module.rs b/pyo3-derive-backend/src/module.rs index e6a08332..53965382 100644 --- a/pyo3-derive-backend/src/module.rs +++ b/pyo3-derive-backend/src/module.rs @@ -86,8 +86,8 @@ fn wrap_fn_argument<'a>(input: &'a syn::FnArg, name: &'a syn::Ident) -> Option PyResult<()> { -/// obj.init(|token| MyClass { token }) +/// obj.init(|_| MyClass { }) /// } /// } /// ``` diff --git a/tests/test_arithmetics.rs b/tests/test_arithmetics.rs index c59f5f7c..4c8cf3ff 100644 --- a/tests/test_arithmetics.rs +++ b/tests/test_arithmetics.rs @@ -5,7 +5,6 @@ extern crate pyo3; use pyo3::class::*; use pyo3::prelude::*; use pyo3::types::PyObjectRef; -use pyo3::PyObjectWithToken; #[macro_use] mod common; @@ -212,9 +211,7 @@ impl PyObjectProtocol for RichComparisons { } #[pyclass] -struct RichComparisons2 { - py: PyToken, -} +struct RichComparisons2 {} #[pyproto] impl PyObjectProtocol for RichComparisons2 { @@ -223,10 +220,11 @@ impl PyObjectProtocol for RichComparisons2 { } fn __richcmp__(&self, _other: &PyObjectRef, op: CompareOp) -> PyResult { + let gil = GILGuard::acquire(); match op { - CompareOp::Eq => Ok(true.to_object(self.py())), - CompareOp::Ne => Ok(false.to_object(self.py())), - _ => Ok(self.py().NotImplemented()), + CompareOp::Eq => Ok(true.to_object(gil.python())), + CompareOp::Ne => Ok(false.to_object(gil.python())), + _ => Ok(gil.python().NotImplemented()), } } } @@ -263,7 +261,7 @@ fn rich_comparisons_python_3_type_error() { let gil = Python::acquire_gil(); let py = gil.python(); - let c2 = py.init(|t| RichComparisons2 { py: t }).unwrap(); + let c2 = py.init(|_| RichComparisons2 {}).unwrap(); py_expect_exception!(py, c2, "c2 < c2", TypeError); py_expect_exception!(py, c2, "c2 < 1", TypeError); py_expect_exception!(py, c2, "1 < c2", TypeError); diff --git a/tests/test_buffer_protocol.rs b/tests/test_buffer_protocol.rs index 2c384094..7a26e99a 100644 --- a/tests/test_buffer_protocol.rs +++ b/tests/test_buffer_protocol.rs @@ -14,7 +14,6 @@ use pyo3::types::PyDict; #[pyclass] struct TestClass { vec: Vec, - token: PyToken, } #[pyproto] @@ -72,9 +71,8 @@ fn test_buffer() { let py = gil.python(); let t = py - .init(|t| TestClass { + .init(|_| TestClass { vec: vec![b' ', b'2', b'3'], - token: t, }) .unwrap(); @@ -90,9 +88,8 @@ fn test_buffer() { let py = gil.python(); let t = py - .init(|t| TestClass { + .init(|_| TestClass { vec: vec![b' ', b'2', b'3'], - token: t, }) .unwrap(); diff --git a/tests/test_dunder.rs b/tests/test_dunder.rs index 9582bbea..8d6d92a8 100644 --- a/tests/test_dunder.rs +++ b/tests/test_dunder.rs @@ -2,6 +2,8 @@ extern crate pyo3; +use std::{isize, iter}; + use pyo3::class::{ PyContextProtocol, PyIterProtocol, PyMappingProtocol, PyObjectProtocol, PySequenceProtocol, }; @@ -10,8 +12,6 @@ use pyo3::ffi; use pyo3::prelude::*; use pyo3::python::ToPyPointer; use pyo3::types::{PyBytes, PyDict, PyObjectRef, PySlice, PyString, PyType}; -use pyo3::PyObjectWithToken; -use std::{isize, iter}; #[macro_use] mod common; @@ -19,7 +19,6 @@ mod common; #[pyclass] pub struct Len { l: usize, - token: PyToken, } #[pyproto] @@ -34,16 +33,15 @@ fn len() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new(py, |t| Len { l: 10, token: t }).unwrap(); + let inst = Py::new(py, |_| Len { l: 10 }).unwrap(); py_assert!(py, inst, "len(inst) == 10"); unsafe { assert_eq!(ffi::PyObject_Size(inst.as_ptr()), 10); assert_eq!(ffi::PyMapping_Size(inst.as_ptr()), 10); } - let inst = Py::new(py, |t| Len { + let inst = Py::new(py, |_| Len { l: (isize::MAX as usize) + 1, - token: t, }) .unwrap(); py_expect_exception!(py, inst, "len(inst)", OverflowError); @@ -52,7 +50,6 @@ fn len() { #[pyclass] struct Iterator { iter: Box + Send>, - token: PyToken, } #[pyproto] @@ -71,9 +68,8 @@ fn iterator() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new(py, |t| Iterator { + let inst = Py::new(py, |_| Iterator { iter: Box::new(5..8), - token: t, }) .unwrap(); py_assert!(py, inst, "iter(inst) is inst"); @@ -81,9 +77,7 @@ fn iterator() { } #[pyclass] -struct StringMethods { - token: PyToken, -} +struct StringMethods {} #[pyproto] impl<'p> PyObjectProtocol<'p> for StringMethods { @@ -100,11 +94,13 @@ impl<'p> PyObjectProtocol<'p> for StringMethods { } fn __bytes__(&self) -> PyResult { - Ok(PyBytes::new(self.py(), b"bytes").into()) + let gil = GILGuard::acquire(); + Ok(PyBytes::new(gil.python(), b"bytes").into()) } fn __unicode__(&self) -> PyResult { - Ok(PyString::new(self.py(), "unicode").into()) + let gil = GILGuard::acquire(); + Ok(PyString::new(gil.python(), "unicode").into()) } } @@ -114,7 +110,7 @@ fn string_methods() { let gil = Python::acquire_gil(); let py = gil.python(); - let obj = Py::new(py, |t| StringMethods { token: t }).unwrap(); + let obj = Py::new(py, |_| StringMethods {}).unwrap(); py_assert!(py, obj, "str(obj) == 'str'"); py_assert!(py, obj, "repr(obj) == 'repr'"); py_assert!(py, obj, "'{0:x}'.format(obj) == 'format(x)'"); @@ -127,7 +123,7 @@ fn string_methods() { let gil = Python::acquire_gil(); let py = gil.python(); - let obj = Py::new(py, |t| StringMethods { token: t }).unwrap(); + let obj = Py::new(py, |_| StringMethods {}).unwrap(); py_assert!(py, obj, "str(obj) == 'str'"); py_assert!(py, obj, "repr(obj) == 'repr'"); py_assert!(py, obj, "unicode(obj) == 'unicode'"); @@ -137,7 +133,6 @@ fn string_methods() { #[pyclass] struct Comparisons { val: i32, - token: PyToken, } #[pyproto] @@ -155,10 +150,10 @@ fn comparisons() { let gil = Python::acquire_gil(); let py = gil.python(); - let zero = Py::new(py, |t| Comparisons { val: 0, token: t }).unwrap(); - let one = Py::new(py, |t| Comparisons { val: 1, token: t }).unwrap(); - let ten = Py::new(py, |t| Comparisons { val: 10, token: t }).unwrap(); - let minus_one = Py::new(py, |t| Comparisons { val: -1, token: t }).unwrap(); + let zero = Py::new(py, |_| Comparisons { val: 0 }).unwrap(); + let one = Py::new(py, |_| Comparisons { val: 1 }).unwrap(); + let ten = Py::new(py, |_| Comparisons { val: 10 }).unwrap(); + let minus_one = Py::new(py, |_| Comparisons { val: -1 }).unwrap(); py_assert!(py, one, "hash(one) == 1"); py_assert!(py, ten, "hash(ten) == 10"); py_assert!(py, minus_one, "hash(minus_one) == -2"); @@ -168,9 +163,7 @@ fn comparisons() { } #[pyclass] -struct Sequence { - token: PyToken, -} +struct Sequence {} #[pyproto] impl PySequenceProtocol for Sequence { @@ -191,15 +184,13 @@ fn sequence() { let gil = Python::acquire_gil(); let py = gil.python(); - let c = py.init(|t| Sequence { token: t }).unwrap(); + let c = py.init(|_| Sequence {}).unwrap(); py_assert!(py, c, "list(c) == [0, 1, 2, 3, 4]"); py_expect_exception!(py, c, "c['abc']", TypeError); } #[pyclass] -struct Callable { - token: PyToken, -} +struct Callable {} #[pymethods] impl Callable { @@ -214,11 +205,11 @@ fn callable() { let gil = Python::acquire_gil(); let py = gil.python(); - let c = py.init(|t| Callable { token: t }).unwrap(); + let c = py.init(|_| Callable {}).unwrap(); py_assert!(py, c, "callable(c)"); py_assert!(py, c, "c(7) == 42"); - let nc = py.init(|t| Comparisons { val: 0, token: t }).unwrap(); + let nc = py.init(|_| Comparisons { val: 0 }).unwrap(); py_assert!(py, nc, "not callable(nc)"); } @@ -226,7 +217,6 @@ fn callable() { struct SetItem { key: i32, val: i32, - token: PyToken, } #[pyproto] @@ -243,13 +233,7 @@ fn setitem() { let gil = Python::acquire_gil(); let py = gil.python(); - let c = py - .init_ref(|t| SetItem { - key: 0, - val: 0, - token: t, - }) - .unwrap(); + let c = py.init_ref(|_| SetItem { key: 0, val: 0 }).unwrap(); py_run!(py, c, "c[1] = 2"); assert_eq!(c.key, 1); assert_eq!(c.val, 2); @@ -259,7 +243,6 @@ fn setitem() { #[pyclass] struct DelItem { key: i32, - token: PyToken, } #[pyproto] @@ -275,7 +258,7 @@ fn delitem() { let gil = Python::acquire_gil(); let py = gil.python(); - let c = py.init_ref(|t| DelItem { key: 0, token: t }).unwrap(); + let c = py.init_ref(|_| DelItem { key: 0 }).unwrap(); py_run!(py, c, "del c[1]"); assert_eq!(c.key, 1); py_expect_exception!(py, c, "c[1] = 2", NotImplementedError); @@ -284,7 +267,6 @@ fn delitem() { #[pyclass] struct SetDelItem { val: Option, - token: PyToken, } #[pyproto] @@ -305,12 +287,7 @@ fn setdelitem() { let gil = Python::acquire_gil(); let py = gil.python(); - let c = py - .init_ref(|t| SetDelItem { - val: None, - token: t, - }) - .unwrap(); + let c = py.init_ref(|_| SetDelItem { val: None }).unwrap(); py_run!(py, c, "c[1] = 2"); assert_eq!(c.val, Some(2)); py_run!(py, c, "del c[1]"); @@ -318,9 +295,7 @@ fn setdelitem() { } #[pyclass] -struct Reversed { - token: PyToken, -} +struct Reversed {} #[pyproto] impl PyMappingProtocol for Reversed { @@ -334,14 +309,12 @@ fn reversed() { let gil = Python::acquire_gil(); let py = gil.python(); - let c = py.init(|t| Reversed { token: t }).unwrap(); + let c = py.init(|_| Reversed {}).unwrap(); py_run!(py, c, "assert reversed(c) == 'I am reversed'"); } #[pyclass] -struct Contains { - token: PyToken, -} +struct Contains {} #[pyproto] impl PySequenceProtocol for Contains { @@ -355,7 +328,7 @@ fn contains() { let gil = Python::acquire_gil(); let py = gil.python(); - let c = py.init(|t| Contains { token: t }).unwrap(); + let c = py.init(|_| Contains {}).unwrap(); py_run!(py, c, "assert 1 in c"); py_run!(py, c, "assert -1 not in c"); py_expect_exception!(py, c, "assert 'wrong type' not in c", TypeError); @@ -364,7 +337,6 @@ fn contains() { #[pyclass] struct ContextManager { exit_called: bool, - token: PyToken, } #[pyproto] @@ -379,8 +351,9 @@ impl<'p> PyContextProtocol<'p> for ContextManager { _value: Option<&'p PyObjectRef>, _traceback: Option<&'p PyObjectRef>, ) -> PyResult { + let gil = GILGuard::acquire(); self.exit_called = true; - if ty == Some(self.py().get_type::()) { + if ty == Some(gil.python().get_type::()) { Ok(true) } else { Ok(false) @@ -394,10 +367,7 @@ fn context_manager() { let py = gil.python(); let c = py - .init_mut(|t| ContextManager { - exit_called: false, - token: t, - }) + .init_mut(|_| ContextManager { exit_called: false }) .unwrap(); py_run!(py, c, "with c as x: assert x == 42"); assert!(c.exit_called); @@ -430,21 +400,20 @@ fn test_basics() { } #[pyclass] -struct Test { - token: PyToken, -} +struct Test {} #[pyproto] impl<'p> PyMappingProtocol<'p> for Test { fn __getitem__(&self, idx: &PyObjectRef) -> PyResult { + let gil = GILGuard::acquire(); if let Ok(slice) = idx.cast_as::() { let indices = slice.indices(1000)?; if indices.start == 100 && indices.stop == 200 && indices.step == 1 { - return Ok("slice".into_object(self.py())); + return Ok("slice".into_object(gil.python())); } } else if let Ok(idx) = idx.extract::() { if idx == 1 { - return Ok("int".into_object(self.py())); + return Ok("int".into_object(gil.python())); } } Err(PyErr::new::("error")) @@ -456,7 +425,7 @@ fn test_cls_impl() { let gil = Python::acquire_gil(); let py = gil.python(); - let ob = py.init(|t| Test { token: t }).unwrap(); + let ob = py.init(|_| Test {}).unwrap(); let d = PyDict::new(py); d.set_item("ob", ob).unwrap(); @@ -466,15 +435,13 @@ fn test_cls_impl() { } #[pyclass(dict)] -struct DunderDictSupport { - token: PyToken, -} +struct DunderDictSupport {} #[test] fn dunder_dict_support() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new_ref(py, |t| DunderDictSupport { token: t }).unwrap(); + let inst = Py::new_ref(py, |_| DunderDictSupport {}).unwrap(); py_run!( py, inst, @@ -486,15 +453,13 @@ fn dunder_dict_support() { } #[pyclass(weakref, dict)] -struct WeakRefDunderDictSupport { - token: PyToken, -} +struct WeakRefDunderDictSupport {} #[test] fn weakref_dunder_dict_support() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new_ref(py, |t| WeakRefDunderDictSupport { token: t }).unwrap(); + let inst = Py::new_ref(py, |_| WeakRefDunderDictSupport {}).unwrap(); py_run!( py, inst, diff --git a/tests/test_gc.rs b/tests/test_gc.rs index fbf4a533..a459120d 100644 --- a/tests/test_gc.rs +++ b/tests/test_gc.rs @@ -2,6 +2,10 @@ extern crate pyo3; +use std::cell::RefCell; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; + use pyo3::class::PyGCProtocol; use pyo3::class::PyTraverseError; use pyo3::class::PyVisit; @@ -10,19 +14,13 @@ use pyo3::prelude::*; use pyo3::python::ToPyPointer; use pyo3::types::PyObjectRef; use pyo3::types::PyTuple; -use pyo3::PyObjectWithToken; use pyo3::PyRawObject; -use std::cell::RefCell; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; #[macro_use] mod common; #[pyclass(freelist = 2)] -struct ClassWithFreelist { - token: PyToken, -} +struct ClassWithFreelist {} #[test] fn class_with_freelist() { @@ -31,8 +29,8 @@ fn class_with_freelist() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap(); - let _inst2 = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap(); + let inst = Py::new(py, |_| ClassWithFreelist {}).unwrap(); + let _inst2 = Py::new(py, |_| ClassWithFreelist {}).unwrap(); ptr = inst.as_ptr(); drop(inst); } @@ -41,10 +39,10 @@ fn class_with_freelist() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst3 = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap(); + let inst3 = Py::new(py, |_| ClassWithFreelist {}).unwrap(); assert_eq!(ptr, inst3.as_ptr()); - let inst4 = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap(); + let inst4 = Py::new(py, |_| ClassWithFreelist {}).unwrap(); assert_ne!(ptr, inst4.as_ptr()) } } @@ -64,7 +62,6 @@ impl Drop for TestDropCall { struct DataIsDropped { member1: TestDropCall, member2: TestDropCall, - token: PyToken, } #[test] @@ -76,14 +73,13 @@ fn data_is_dropped() { let gil = Python::acquire_gil(); let py = gil.python(); let inst = py - .init(|t| DataIsDropped { + .init(|_| DataIsDropped { member1: TestDropCall { drop_called: Arc::clone(&drop_called1), }, member2: TestDropCall { drop_called: Arc::clone(&drop_called2), }, - token: t, }) .unwrap(); assert!(!drop_called1.load(Ordering::Relaxed)); @@ -96,9 +92,7 @@ fn data_is_dropped() { } #[pyclass] -struct ClassWithDrop { - token: PyToken, -} +struct ClassWithDrop {} impl Drop for ClassWithDrop { fn drop(&mut self) { @@ -125,7 +119,7 @@ fn create_pointers_in_drop() { let empty = PyTuple::empty(py); ptr = empty.as_ptr(); cnt = empty.get_refcnt() - 1; - let inst = py.init(|t| ClassWithDrop { token: t }).unwrap(); + let inst = py.init(|_| ClassWithDrop {}).unwrap(); drop(inst); } @@ -147,7 +141,6 @@ fn create_pointers_in_drop() { struct GCIntegration { self_ref: RefCell, dropped: TestDropCall, - token: PyToken, } #[pyproto] @@ -157,7 +150,8 @@ impl PyGCProtocol for GCIntegration { } fn __clear__(&mut self) { - *self.self_ref.borrow_mut() = self.py().None(); + let gil = GILGuard::acquire(); + *self.self_ref.borrow_mut() = gil.python().None(); } } @@ -168,12 +162,11 @@ fn gc_integration() { { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new_ref(py, |t| GCIntegration { + let inst = Py::new_ref(py, |_| GCIntegration { self_ref: RefCell::new(py.None()), dropped: TestDropCall { drop_called: Arc::clone(&drop_called), }, - token: t, }) .unwrap(); @@ -187,28 +180,24 @@ fn gc_integration() { } #[pyclass(gc)] -struct GCIntegration2 { - token: PyToken, -} +struct GCIntegration2 {} #[test] fn gc_integration2() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new_ref(py, |t| GCIntegration2 { token: t }).unwrap(); + let inst = Py::new_ref(py, |_| GCIntegration2 {}).unwrap(); py_run!(py, inst, "import gc; assert inst in gc.get_objects()"); } #[pyclass(weakref)] -struct WeakRefSupport { - token: PyToken, -} +struct WeakRefSupport {} #[test] fn weakref_support() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = Py::new_ref(py, |t| WeakRefSupport { token: t }).unwrap(); + let inst = Py::new_ref(py, |_| WeakRefSupport {}).unwrap(); py_run!( py, inst, @@ -218,7 +207,6 @@ fn weakref_support() { #[pyclass] struct BaseClassWithDrop { - token: PyToken, data: Option>, } @@ -226,10 +214,7 @@ struct BaseClassWithDrop { impl BaseClassWithDrop { #[new] fn __new__(obj: &PyRawObject) -> PyResult<()> { - obj.init(|t| BaseClassWithDrop { - token: t, - data: None, - }) + obj.init(|_| BaseClassWithDrop { data: None }) } } @@ -243,7 +228,6 @@ impl Drop for BaseClassWithDrop { #[pyclass(extends = BaseClassWithDrop)] struct SubClassWithDrop { - token: PyToken, data: Option>, } @@ -251,10 +235,7 @@ struct SubClassWithDrop { impl SubClassWithDrop { #[new] fn __new__(obj: &PyRawObject) -> PyResult<()> { - obj.init(|t| SubClassWithDrop { - token: t, - data: None, - })?; + obj.init(|_| SubClassWithDrop { data: None })?; BaseClassWithDrop::__new__(obj) } } @@ -282,7 +263,8 @@ fn inheritance_with_new_methods_with_drop() { let obj = SubClassWithDrop::try_from_mut(inst).unwrap(); obj.data = Some(Arc::clone(&drop_called1)); - let base = obj.get_mut_base(); + let base: &mut ::BaseType = + unsafe { py.mut_from_borrowed_ptr(obj.as_ptr()) }; base.data = Some(Arc::clone(&drop_called2)); } diff --git a/tests/test_getter_setter.rs b/tests/test_getter_setter.rs index bf0b3474..0bf6dec3 100644 --- a/tests/test_getter_setter.rs +++ b/tests/test_getter_setter.rs @@ -11,7 +11,6 @@ mod common; #[pyclass] struct ClassWithProperties { num: i32, - token: PyToken, } #[pymethods] @@ -36,9 +35,7 @@ fn class_with_properties() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = py - .init(|t| ClassWithProperties { num: 10, token: t }) - .unwrap(); + let inst = py.init(|_| ClassWithProperties { num: 10 }).unwrap(); py_run!(py, inst, "assert inst.get_num() == 10"); py_run!(py, inst, "assert inst.get_num() == inst.DATA"); @@ -49,7 +46,6 @@ fn class_with_properties() { #[pyclass] struct GetterSetter { - token: PyToken, #[prop(get, set)] num: i32, #[prop(get, set)] @@ -69,9 +65,8 @@ fn getter_setter_autogen() { let py = gil.python(); let inst = py - .init(|t| GetterSetter { + .init(|_| GetterSetter { num: 10, - token: t, text: "Hello".to_string(), }) .unwrap(); diff --git a/tests/test_methods.rs b/tests/test_methods.rs index b4bdea3f..d76b6706 100644 --- a/tests/test_methods.rs +++ b/tests/test_methods.rs @@ -4,7 +4,6 @@ extern crate pyo3; use pyo3::prelude::*; use pyo3::types::{PyDict, PyString, PyTuple, PyType}; -use pyo3::PyObjectWithToken; use pyo3::PyRawObject; #[macro_use] @@ -13,7 +12,6 @@ mod common; #[pyclass] struct InstanceMethod { member: i32, - token: PyToken, } #[pymethods] @@ -29,12 +27,7 @@ fn instance_method() { let gil = Python::acquire_gil(); let py = gil.python(); - let obj = py - .init_ref(|t| InstanceMethod { - member: 42, - token: t, - }) - .unwrap(); + let obj = py.init_ref(|_| InstanceMethod { member: 42 }).unwrap(); assert_eq!(obj.method().unwrap(), 42); let d = PyDict::new(py); d.set_item("obj", obj).unwrap(); @@ -46,7 +39,6 @@ fn instance_method() { #[pyclass] struct InstanceMethodWithArgs { member: i32, - token: PyToken, } #[pymethods] @@ -63,12 +55,9 @@ fn instance_method_with_args() { let py = gil.python(); let obj = py - .init_ref(|t| InstanceMethodWithArgs { - member: 7, - token: t, - }) + .init_ref(|_| InstanceMethodWithArgs { member: 7 }) .unwrap(); - assert!(obj.method(6).unwrap() == 42); + assert_eq!(obj.method(6).unwrap(), 42); let d = PyDict::new(py); d.set_item("obj", obj).unwrap(); py.run("assert obj.method(3) == 21", None, Some(d)).unwrap(); @@ -114,9 +103,7 @@ fn class_method() { } #[pyclass] -struct ClassMethodWithArgs { - token: PyToken, -} +struct ClassMethodWithArgs {} #[pymethods] impl ClassMethodWithArgs { @@ -143,15 +130,13 @@ fn class_method_with_args() { } #[pyclass] -struct StaticMethod { - token: PyToken, -} +struct StaticMethod {} #[pymethods] impl StaticMethod { #[new] fn __new__(obj: &PyRawObject) -> PyResult<()> { - obj.init(|t| StaticMethod { token: t }) + obj.init(|_| StaticMethod {}) } #[staticmethod] @@ -183,9 +168,7 @@ fn static_method() { } #[pyclass] -struct StaticMethodWithArgs { - token: PyToken, -} +struct StaticMethodWithArgs {} #[pymethods] impl StaticMethodWithArgs { @@ -210,9 +193,7 @@ fn static_method_with_args() { } #[pyclass] -struct MethArgs { - token: PyToken, -} +struct MethArgs {} #[pymethods] impl MethArgs { @@ -230,8 +211,13 @@ impl MethArgs { Ok(test) } #[args(args = "*", kwargs = "**")] - fn get_kwargs(&self, args: &PyTuple, kwargs: Option<&PyDict>) -> PyResult { - Ok([args.into(), kwargs.to_object(self.py())].to_object(self.py())) + fn get_kwargs( + &self, + py: Python, + args: &PyTuple, + kwargs: Option<&PyDict>, + ) -> PyResult { + Ok([args.into(), kwargs.to_object(py)].to_object(py)) } } @@ -239,7 +225,7 @@ impl MethArgs { fn meth_args() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = py.init(|t| MethArgs { token: t }).unwrap(); + let inst = py.init(|_| MethArgs {}).unwrap(); py_run!(py, inst, "assert inst.get_optional() == 10"); py_run!(py, inst, "assert inst.get_optional(100) == 100"); diff --git a/tests/test_various.rs b/tests/test_various.rs index b599da2d..5f640d2c 100644 --- a/tests/test_various.rs +++ b/tests/test_various.rs @@ -13,7 +13,6 @@ mod common; #[pyclass] struct MutRefArg { n: i32, - token: PyToken, } #[pymethods] @@ -31,8 +30,8 @@ impl MutRefArg { fn mut_ref_arg() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst1 = py.init(|t| MutRefArg { token: t, n: 0 }).unwrap(); - let inst2 = py.init(|t| MutRefArg { token: t, n: 0 }).unwrap(); + let inst1 = py.init(|_| MutRefArg { n: 0 }).unwrap(); + let inst2 = py.init(|_| MutRefArg { n: 0 }).unwrap(); let d = PyDict::new(py); d.set_item("inst1", &inst1).unwrap();