Make the init methods use a value instead of a function

This commit is contained in:
konstin 2019-02-07 19:07:09 +01:00
parent 94cb3e7625
commit 1a489d2829
23 changed files with 125 additions and 111 deletions

View file

@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Renamed `add_function` to `add_wrapped` as it now also supports modules.
* Renamed `#[pymodinit]` to `#[pymodule]`.
* `init`, `init_ref` and `init_mut` now take a value instead of a function that returns the value. (Migrating normally just removing `||`)
* Renamed `py_exception` to `create_exception` and refactored the error macros.
* Renamed `wrap_function!` to `wrap_pyfunction!`
* Migrated to the 2018 edition

View file

@ -191,7 +191,7 @@ pub struct TzClass {}
impl TzClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| TzClass {})
obj.init(TzClass {})
}
fn utcoffset(&self, py: Python<'_>, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> {

View file

@ -17,7 +17,7 @@ pub struct DictSize {
impl DictSize {
#[new]
fn __new__(obj: &PyRawObject, expected: u32) -> PyResult<()> {
obj.init(|| DictSize { expected })
obj.init(DictSize { expected })
}
fn iter_dict(&mut self, _py: Python<'_>, dict: &PyDict) -> PyResult<u32> {

View file

@ -14,7 +14,7 @@ pub struct ModClass {
impl ModClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| ModClass {
obj.init(ModClass {
_somefield: String::from("contents"),
})
}

View file

@ -9,7 +9,7 @@ pub struct Subclassable {}
impl Subclassable {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| Subclassable {})
obj.init(Subclassable {})
}
}

View file

@ -17,7 +17,7 @@ struct WordCounter {
impl WordCounter {
#[new]
fn __new__(obj: &PyRawObject, path: String) -> PyResult<()> {
obj.init(|| WordCounter {
obj.init(WordCounter {
path: PathBuf::from(path),
})
}

View file

@ -42,7 +42,7 @@ struct MyClass {
}
let gil = Python::acquire_gil();
let py = gil.python();
let obj = PyRef::new(py, || MyClass { num: 3, debug: true }).unwrap();
let obj = PyRef::new(py, MyClass { num: 3, debug: true }).unwrap();
assert_eq!(obj.num, 3);
let dict = PyDict::new();
// You can treat a `PyRef` as a Python object
@ -60,7 +60,7 @@ struct MyClass {
}
let gil = Python::acquire_gil();
let py = gil.python();
let mut obj = PyRefMut::new(py, || MyClass { num: 3, debug: true }).unwrap();
let mut obj = PyRefMut::new(py, MyClass { num: 3, debug: true }).unwrap();
obj.num = 5;
```
@ -119,7 +119,7 @@ impl MyClass {
#[new]
fn __new__(obj: &PyRawObject, num: i32) -> PyResult<()> {
obj.init(|| {
obj.init({
MyClass {
num,
}
@ -159,7 +159,7 @@ struct BaseClass {
impl BaseClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| BaseClass{ val1: 10 })
Ok(obj.init(BaseClass{ val1: 10 }))
}
pub fn method(&self) -> PyResult<()> {
@ -176,7 +176,7 @@ struct SubClass {
impl SubClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| SubClass{ val2: 10 });
obj.init(SubClass{ val2: 10 });
BaseClass::__new__(obj)
}

View file

@ -37,7 +37,7 @@ struct MyClass {
impl MyClass {
#[new]
fn __new__(obj: &PyRawObject, num: u32) -> PyResult<()> {
obj.init(|| {
obj.init({
MyClass {
num,
}

View file

@ -130,7 +130,7 @@ pub fn impl_method_proto(
});
let tmp2 = extract_decl(syn::parse_quote! {
fn test( &self, arg: Option<<#cls as #p<'p>>::#arg_name>) -> <#cls as #p<'p>>::Result {}
fn test(&self, arg: Option<<#cls as #p<'p>>::#arg_name>) -> <#cls as #p<'p>>::Result {}
});
modify_arg_ty(sig, 1, &tmp, &tmp2);

View file

@ -201,12 +201,9 @@ fn impl_class(
}
}
// TBH I'm not sure what exactely this does and I'm sure there's a better way,
// but for now it works and it only safe code and it is required to return custom
// objects, so for now I'm keeping it
impl ::pyo3::IntoPyObject for #cls {
fn into_object(self, py: ::pyo3::Python) -> ::pyo3::PyObject {
::pyo3::Py::new(py, || self).unwrap().into_object(py)
::pyo3::Py::new(py, self).unwrap().into_object(py)
}
}

View file

@ -52,7 +52,7 @@ pub trait PyNativeType: PyObjectWithGIL {}
/// }
/// let gil = Python::acquire_gil();
/// let py = gil.python();
/// let obj = PyRef::new(gil.python(), || Point { x: 3, y: 4 }).unwrap();
/// let obj = PyRef::new(gil.python(), Point { x: 3, y: 4 }).unwrap();
/// let d = vec![("p", obj)].into_py_dict(py);
/// py.run("assert p.length() == 12", None, Some(d)).unwrap();
/// ```
@ -69,12 +69,9 @@ impl<'a, T> PyRef<'a, T>
where
T: PyTypeInfo + PyTypeObject + PyTypeCreate,
{
pub fn new<F>(py: Python, f: F) -> PyResult<PyRef<T>>
where
F: FnOnce() -> T,
{
pub fn new(py: Python, value: T) -> PyResult<PyRef<T>> {
let obj = T::create(py)?;
obj.init(f)?;
obj.init(value)?;
let ref_ = unsafe { py.from_owned_ptr(obj.into_ptr()) };
Ok(PyRef::from_ref(ref_))
}
@ -117,7 +114,7 @@ impl<'a, T: PyTypeInfo> Deref for PyRef<'a, T> {
/// }
/// let gil = Python::acquire_gil();
/// let py = gil.python();
/// let mut obj = PyRefMut::new(gil.python(), || Point { x: 3, y: 4 }).unwrap();
/// let mut obj = PyRefMut::new(gil.python(), Point { x: 3, y: 4 }).unwrap();
/// let d = vec![("p", obj.to_object(py))].into_py_dict(py);
/// obj.x = 5; obj.y = 20;
/// py.run("assert p.length() == 100", None, Some(d)).unwrap();
@ -135,12 +132,9 @@ impl<'a, T> PyRefMut<'a, T>
where
T: PyTypeInfo + PyTypeObject + PyTypeCreate,
{
pub fn new<F>(py: Python, f: F) -> PyResult<PyRefMut<T>>
where
F: FnOnce() -> T,
{
pub fn new(py: Python, value: T) -> PyResult<PyRefMut<T>> {
let obj = T::create(py)?;
obj.init(f)?;
obj.init(value)?;
let ref_ = unsafe { py.mut_from_owned_ptr(obj.into_ptr()) };
Ok(PyRefMut::from_mut(ref_))
}
@ -350,17 +344,40 @@ where
{
/// Create new instance of T and move it under python management
/// Returns `Py<T>`.
pub fn new<F>(py: Python, f: F) -> PyResult<Py<T>>
pub fn new(py: Python, value: T) -> PyResult<Py<T>>
where
F: FnOnce() -> T,
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
ob.init(f)?;
ob.init(value)?;
let ob = unsafe { Py::from_owned_ptr(ob.into_ptr()) };
Ok(ob)
}
/// Create new instance of `T` and move it under python management.
/// Returns references to `T`
pub fn new_ref(py: Python, value: T) -> PyResult<&T>
where
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
ob.init(value)?;
unsafe { Ok(py.from_owned_ptr(ob.into_ptr())) }
}
/// Create new instance of `T` and move it under python management.
/// Returns mutable references to `T`
pub fn new_mut(py: Python, value: T) -> PyResult<&mut T>
where
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
ob.init(value)?;
unsafe { Ok(py.mut_from_owned_ptr(ob.into_ptr())) }
}
}
/// Specialization workaround

View file

@ -250,34 +250,31 @@ impl<'p> Python<'p> {
/// Create new instance of `T` and move it under python management.
/// Returns `Py<T>`.
#[inline]
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>>
pub fn init<T>(self, value: T) -> PyResult<Py<T>>
where
F: FnOnce() -> T,
T: PyTypeCreate,
{
Py::new(self, f)
Py::new(self, value)
}
/// Create new instance of `T` and move it under python management.
/// Created object get registered in release pool. Returns references to `T`
#[inline]
pub fn init_ref<T, F>(self, f: F) -> PyResult<PyRef<'p, T>>
pub fn init_ref<T>(self, value: T) -> PyResult<PyRef<'p, T>>
where
F: FnOnce() -> T,
T: PyTypeCreate,
{
PyRef::new(self, f)
PyRef::new(self, value)
}
/// Create new instance of `T` and move it under python management.
/// Created object get registered in release pool. Returns mutable references to `T`
#[inline]
pub fn init_mut<T, F>(self, f: F) -> PyResult<PyRefMut<'p, T>>
pub fn init_mut<T>(self, value: T) -> PyResult<PyRefMut<'p, T>>
where
F: FnOnce() -> T,
T: PyTypeCreate,
{
PyRefMut::new(self, f)
PyRefMut::new(self, value)
}
}

View file

@ -83,7 +83,7 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3;
/// impl MyClass {
/// #[new]
/// fn __new__(obj: &PyRawObject) -> PyResult<()> {
/// obj.init(|| MyClass { })
/// obj.init(MyClass { })
/// }
/// }
/// ```
@ -139,13 +139,10 @@ impl PyRawObject {
}
}
pub fn init<T, F>(&self, f: F) -> PyResult<()>
pub fn init<T>(&self, value: T) -> PyResult<()>
where
F: FnOnce() -> T,
T: PyTypeInfo,
{
let value = f();
unsafe {
// The `as *mut u8` part is required because the offset is in bytes
let ptr = (self.ptr as *mut u8).offset(T::OFFSET) as *mut T;

View file

@ -199,7 +199,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
let slice = t.as_slice();
if t.len() == $length {
Ok((
$( slice[$n].extract::<$T>(obj.py())?, )+
$(slice[$n].extract::<$T>(obj.py())?,)+
))
} else {
Err(wrong_tuple_length(t, $length))

View file

@ -32,7 +32,7 @@ fn unary_arithmetic() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init(|| UnaryArithmetic {}).unwrap();
let c = py.init(UnaryArithmetic {}).unwrap();
py_run!(py, c, "assert -c == 'neg'");
py_run!(py, c, "assert +c == 'pos'");
py_run!(py, c, "assert abs(c) == 'abs'");
@ -110,7 +110,7 @@ fn inplace_operations() {
let py = gil.python();
let init = |value, code| {
let c = py.init(|| InPlaceOperations { value }).unwrap();
let c = py.init(InPlaceOperations { value }).unwrap();
py_run!(py, c, code);
};
@ -164,7 +164,7 @@ fn binary_arithmetic() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init(|| BinaryArithmetic {}).unwrap();
let c = py.init(BinaryArithmetic {}).unwrap();
py_run!(py, c, "assert c + c == 'BA + BA'");
py_run!(py, c, "assert c + 1 == 'BA + 1'");
py_run!(py, c, "assert 1 + c == '1 + BA'");
@ -230,7 +230,7 @@ fn rich_comparisons() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init(|| RichComparisons {}).unwrap();
let c = py.init(RichComparisons {}).unwrap();
py_run!(py, c, "assert (c < c) == 'RC < RC'");
py_run!(py, c, "assert (c < 1) == 'RC < 1'");
py_run!(py, c, "assert (1 < c) == 'RC > 1'");
@ -257,7 +257,7 @@ fn rich_comparisons_python_3_type_error() {
let gil = Python::acquire_gil();
let py = gil.python();
let c2 = py.init(|| RichComparisons2 {}).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);

View file

@ -66,7 +66,7 @@ fn test_buffer() {
let py = gil.python();
let t = py
.init(|| TestClass {
.init(TestClass {
vec: vec![b' ', b'2', b'3'],
})
.unwrap();
@ -83,7 +83,7 @@ fn test_buffer() {
let py = gil.python();
let t = py
.init(|| TestClass {
.init(TestClass {
vec: vec![b' ', b'2', b'3'],
})
.unwrap();

View file

@ -8,7 +8,7 @@ struct EmptyClassWithNew {}
impl EmptyClassWithNew {
#[__new__]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| EmptyClassWithNew {})
obj.init(EmptyClassWithNew {})
}
}
@ -33,7 +33,7 @@ struct NewWithOneArg {
impl NewWithOneArg {
#[new]
fn __new__(obj: &PyRawObject, arg: i32) -> PyResult<()> {
obj.init(|| NewWithOneArg { _data: arg })
obj.init(NewWithOneArg { _data: arg })
}
}
@ -57,7 +57,7 @@ struct NewWithTwoArgs {
impl NewWithTwoArgs {
#[new]
fn __new__(obj: &PyRawObject, arg1: i32, arg2: i32) -> PyResult<()> {
obj.init(|| NewWithTwoArgs {
obj.init(NewWithTwoArgs {
_data1: arg1,
_data2: arg2,
})

View file

@ -31,16 +31,19 @@ fn len() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new(py, || Len { l: 10 }).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, || Len {
l: (isize::MAX as usize) + 1,
})
let inst = Py::new(
py,
Len {
l: (isize::MAX as usize) + 1,
},
)
.unwrap();
py_expect_exception!(py, inst, "len(inst)", OverflowError);
}
@ -66,9 +69,12 @@ fn iterator() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new(py, || Iterator {
iter: Box::new(5..8),
})
let inst = Py::new(
py,
Iterator {
iter: Box::new(5..8),
},
)
.unwrap();
py_assert!(py, inst, "iter(inst) is inst");
py_assert!(py, inst, "list(inst) == [5, 6, 7]");
@ -108,7 +114,7 @@ fn string_methods() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = Py::new(py, || StringMethods {}).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)'");
@ -121,7 +127,7 @@ fn string_methods() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = Py::new(py, || StringMethods {}).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'");
@ -148,10 +154,10 @@ fn comparisons() {
let gil = Python::acquire_gil();
let py = gil.python();
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();
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");
@ -182,7 +188,7 @@ fn sequence() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init(|| Sequence {}).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);
}
@ -203,11 +209,11 @@ fn callable() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init(|| Callable {}).unwrap();
let c = py.init(Callable {}).unwrap();
py_assert!(py, c, "callable(c)");
py_assert!(py, c, "c(7) == 42");
let nc = py.init(|| Comparisons { val: 0 }).unwrap();
let nc = py.init(Comparisons { val: 0 }).unwrap();
py_assert!(py, nc, "not callable(nc)");
}
@ -231,7 +237,7 @@ fn setitem() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init_ref(|| SetItem { key: 0, val: 0 }).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);
@ -256,7 +262,7 @@ fn delitem() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init_ref(|| DelItem { key: 0 }).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);
@ -285,7 +291,7 @@ fn setdelitem() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init_ref(|| SetDelItem { val: None }).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]");
@ -307,7 +313,7 @@ fn reversed() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init(|| Reversed {}).unwrap();
let c = py.init(Reversed {}).unwrap();
py_run!(py, c, "assert reversed(c) == 'I am reversed'");
}
@ -326,7 +332,7 @@ fn contains() {
let gil = Python::acquire_gil();
let py = gil.python();
let c = py.init(|| Contains {}).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,9 +370,7 @@ fn context_manager() {
let gil = Python::acquire_gil();
let py = gil.python();
let mut c = py
.init_mut(|| ContextManager { exit_called: false })
.unwrap();
let mut c = py.init_mut(ContextManager { exit_called: false }).unwrap();
py_run!(py, c, "with c as x: assert x == 42");
assert!(c.exit_called);
@ -423,7 +427,7 @@ fn test_cls_impl() {
let gil = Python::acquire_gil();
let py = gil.python();
let ob = py.init(|| Test {}).unwrap();
let ob = py.init(Test {}).unwrap();
let d = PyDict::new(py);
d.set_item("ob", ob).unwrap();
@ -439,7 +443,7 @@ struct DunderDictSupport {}
fn dunder_dict_support() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, || DunderDictSupport {}).unwrap();
let inst = PyRef::new(py, DunderDictSupport {}).unwrap();
py_run!(
py,
inst,
@ -457,7 +461,7 @@ struct WeakRefDunderDictSupport {}
fn weakref_dunder_dict_support() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, || WeakRefDunderDictSupport {}).unwrap();
let inst = PyRef::new(py, WeakRefDunderDictSupport {}).unwrap();
py_run!(
py,
inst,

View file

@ -24,8 +24,8 @@ fn class_with_freelist() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new(py, || ClassWithFreelist {}).unwrap();
let _inst2 = Py::new(py, || ClassWithFreelist {}).unwrap();
let inst = Py::new(py, ClassWithFreelist {}).unwrap();
let _inst2 = Py::new(py, ClassWithFreelist {}).unwrap();
ptr = inst.as_ptr();
drop(inst);
}
@ -34,10 +34,10 @@ fn class_with_freelist() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst3 = Py::new(py, || ClassWithFreelist {}).unwrap();
let inst3 = Py::new(py, ClassWithFreelist {}).unwrap();
assert_eq!(ptr, inst3.as_ptr());
let inst4 = Py::new(py, || ClassWithFreelist {}).unwrap();
let inst4 = Py::new(py, ClassWithFreelist {}).unwrap();
assert_ne!(ptr, inst4.as_ptr())
}
}
@ -68,7 +68,7 @@ fn data_is_dropped() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = py
.init(|| DataIsDropped {
.init(DataIsDropped {
member1: TestDropCall {
drop_called: Arc::clone(&drop_called1),
},
@ -114,7 +114,7 @@ fn create_pointers_in_drop() {
let empty = PyTuple::empty(py);
ptr = empty.as_ptr();
cnt = empty.get_refcnt() - 1;
let inst = py.init(|| ClassWithDrop {}).unwrap();
let inst = py.init(ClassWithDrop {}).unwrap();
drop(inst);
}
@ -157,12 +157,15 @@ fn gc_integration() {
{
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, || GCIntegration {
self_ref: RefCell::new(py.None()),
dropped: TestDropCall {
drop_called: Arc::clone(&drop_called),
let inst = PyRef::new(
py,
GCIntegration {
self_ref: RefCell::new(py.None()),
dropped: TestDropCall {
drop_called: Arc::clone(&drop_called),
},
},
})
)
.unwrap();
*inst.self_ref.borrow_mut() = inst.to_object(py);
@ -183,7 +186,7 @@ fn gc_integration2() {
let py = gil.python();
// Temporarily disable pythons garbage collector to avoid a race condition
py.run("import gc; gc.disable()", None, None).unwrap();
let inst = PyRef::new(py, || GCIntegration2 {}).unwrap();
let inst = PyRef::new(py, GCIntegration2 {}).unwrap();
py_run!(py, inst, "assert inst in gc.get_objects()");
py.run("gc.enable()", None, None).unwrap();
}
@ -195,7 +198,7 @@ struct WeakRefSupport {}
fn weakref_support() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, || WeakRefSupport {}).unwrap();
let inst = PyRef::new(py, WeakRefSupport {}).unwrap();
py_run!(
py,
inst,
@ -212,7 +215,7 @@ struct BaseClassWithDrop {
impl BaseClassWithDrop {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| BaseClassWithDrop { data: None })
obj.init(BaseClassWithDrop { data: None })
}
}
@ -233,7 +236,7 @@ struct SubClassWithDrop {
impl SubClassWithDrop {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| SubClassWithDrop { data: None })?;
obj.init(SubClassWithDrop { data: None })?;
BaseClassWithDrop::__new__(obj)
}
}

View file

@ -31,7 +31,7 @@ fn class_with_properties() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = py.init(|| ClassWithProperties { num: 10 }).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");
@ -61,7 +61,7 @@ fn getter_setter_autogen() {
let py = gil.python();
let inst = py
.init(|| GetterSetter {
.init(GetterSetter {
num: 10,
text: "Hello".to_string(),
})

View file

@ -35,7 +35,7 @@ fn subclass() {
impl BaseClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| BaseClass { val1: 10 })
obj.init(BaseClass { val1: 10 })
}
}
@ -49,7 +49,7 @@ struct SubClass {
impl SubClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| SubClass { val2: 5 })?;
obj.init(SubClass { val2: 5 })?;
BaseClass::__new__(obj)
}
}

View file

@ -23,7 +23,7 @@ fn instance_method() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = py.init_ref(|| InstanceMethod { member: 42 }).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();
@ -50,9 +50,7 @@ fn instance_method_with_args() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = py
.init_ref(|| InstanceMethodWithArgs { member: 7 })
.unwrap();
let obj = py.init_ref(InstanceMethodWithArgs { member: 7 }).unwrap();
assert_eq!(obj.method(6).unwrap(), 42);
let d = PyDict::new(py);
d.set_item("obj", obj).unwrap();
@ -68,7 +66,7 @@ struct ClassMethod {}
impl ClassMethod {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| ClassMethod {})
obj.init(ClassMethod {})
}
#[classmethod]
@ -132,7 +130,7 @@ struct StaticMethod {}
impl StaticMethod {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|| StaticMethod {})
obj.init(StaticMethod {})
}
#[staticmethod]
@ -221,7 +219,7 @@ impl MethArgs {
fn meth_args() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = py.init(|| MethArgs {}).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");

View file

@ -26,8 +26,8 @@ impl MutRefArg {
fn mut_ref_arg() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst1 = py.init(|| MutRefArg { n: 0 }).unwrap();
let inst2 = py.init(|| MutRefArg { 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();