Remove PyToken completely; Fixes #94

This commit is contained in:
konstin 2018-11-12 14:15:11 +01:00
parent fb2349b6ec
commit 57afb51604
21 changed files with 122 additions and 191 deletions

View File

@ -186,7 +186,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

@ -13,7 +13,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

@ -21,7 +21,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

@ -59,7 +59,7 @@ impl MyClass {
#[new]
fn __new__(obj: &PyRawObject, num: i32) -> PyResult<()> {
obj.init(|_| {
obj.init(|| {
MyClass {
num,
}
@ -101,7 +101,7 @@ struct BaseClass {
impl BaseClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| BaseClass{ val1: 10 })
obj.init(|| BaseClass{ val1: 10 })
}
pub fn method(&self) -> PyResult<()> {
@ -118,7 +118,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

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

View File

@ -10,29 +10,20 @@ use utils;
pub fn build_py_class(class: &mut syn::ItemStruct, attr: &Vec<syn::Expr>) -> TokenStream {
let (params, flags, base) = parse_attribute(attr);
let doc = utils::get_doc(&class.attrs, true);
let mut token: Option<syn::Ident> = None;
let mut descriptors = Vec::new();
if let syn::Fields::Named(ref mut fields) = class.fields {
for field in fields.named.iter_mut() {
if is_python_token(field) {
if token.is_none() {
token = field.ident.clone();
} else {
panic!("You can only have one PyToken per class");
}
} else {
let field_descs = parse_descriptors(field);
if !field_descs.is_empty() {
descriptors.push((field.clone(), field_descs));
}
let field_descs = parse_descriptors(field);
if !field_descs.is_empty() {
descriptors.push((field.clone(), field_descs));
}
}
} else {
panic!("#[pyclass] can only be used with C-style structs")
}
impl_class(&class.ident, &base, token, doc, params, flags, descriptors)
impl_class(&class.ident, &base, doc, params, flags, descriptors)
}
fn parse_descriptors(item: &mut syn::Field) -> Vec<FnType> {
@ -72,7 +63,6 @@ fn parse_descriptors(item: &mut syn::Field) -> Vec<FnType> {
fn impl_class(
cls: &syn::Ident,
base: &syn::TypePath,
token: Option<syn::Ident>,
doc: syn::Lit,
params: HashMap<&'static str, syn::Expr>,
flags: Vec<syn::Expr>,
@ -83,38 +73,6 @@ fn impl_class(
None => quote! { #cls }.to_string(),
};
let extra = if let Some(token) = token {
Some(quote! {
impl ::pyo3::PyObjectWithToken for #cls {
fn py<'p>(&'p self) -> ::pyo3::Python<'p> {
self.#token.py()
}
}
impl<'a> ::std::convert::From<&'a mut #cls> for &'a #cls
{
fn from(ob: &'a mut #cls) -> Self {
unsafe{std::mem::transmute(ob)}
}
}
impl ::std::fmt::Debug for #cls {
fn fmt(&self, f : &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
use ::pyo3::ObjectProtocol;
let s = self.repr().map_err(|_| ::std::fmt::Error)?;
f.write_str(&s.to_string_lossy())
}
}
impl ::std::fmt::Display for #cls {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
use ::pyo3::ObjectProtocol;
let s = self.str().map_err(|_| ::std::fmt::Error)?;
f.write_str(&s.to_string_lossy())
}
}
})
} else {
None
};
let extra = {
if let Some(freelist) = params.get("freelist") {
Some(quote! {
@ -133,11 +91,9 @@ fn impl_class(
}
}
}
#extra
})
} else {
extra
None
}
};
@ -209,7 +165,7 @@ fn impl_class(
// 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)
}
}
@ -337,18 +293,6 @@ fn impl_descriptors(cls: &syn::Type, descriptors: Vec<(syn::Field, Vec<FnType>)>
}
}
fn is_python_token(field: &syn::Field) -> bool {
match field.ty {
syn::Type::Path(ref typath) => {
if let Some(segment) = typath.path.segments.last() {
return segment.value().ident.to_string() == "PyToken";
}
}
_ => (),
}
return false;
}
fn parse_attribute(
args: &Vec<syn::Expr>,
) -> (

View File

@ -1,9 +1,7 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use std;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::rc::Rc;
use crate::conversion::{FromPyObject, IntoPyObject, ToPyObject};
use crate::err::{PyErr, PyResult};
@ -17,19 +15,6 @@ use crate::typeob::PyTypeCreate;
use crate::typeob::{PyTypeInfo, PyTypeObject};
use crate::types::PyObjectRef;
pub struct PyToken(PhantomData<Rc<()>>);
impl PyToken {
pub(crate) fn new() -> PyToken {
PyToken(PhantomData)
}
#[inline]
pub fn py(&self) -> Python {
unsafe { Python::assume_gil_acquired() }
}
}
/// Any instance that is managed Python can have access to `gil`.
pub trait PyObjectWithToken: Sized {
fn py(&self) -> Python;
@ -174,7 +159,7 @@ where
/// Returns `Py<T>`.
pub fn new<F>(py: Python, f: F) -> PyResult<Py<T>>
where
F: FnOnce(crate::PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
@ -188,7 +173,7 @@ where
/// Returns references to `T`
pub fn new_ref<F>(py: Python, f: F) -> PyResult<&T>
where
F: FnOnce(crate::PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
@ -201,7 +186,7 @@ where
/// Returns mutable references to `T`
pub fn new_mut<F>(py: Python, f: F) -> PyResult<&mut T>
where
F: FnOnce(crate::PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;

View File

@ -143,7 +143,7 @@ pub use crate::conversion::{
ToBorrowedObject, ToPyObject,
};
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyErrValue, PyResult};
pub use crate::instance::{AsPyRef, Py, PyNativeType, PyObjectWithToken, PyToken};
pub use crate::instance::{AsPyRef, Py, PyNativeType, PyObjectWithToken};
pub use crate::noargs::NoArgs;
pub use crate::object::PyObject;
pub use crate::objectprotocol::ObjectProtocol;

View File

@ -12,7 +12,7 @@
pub use crate::conversion::{FromPyObject, IntoPyObject, PyTryFrom, PyTryInto, ToPyObject};
pub use crate::err::{PyErr, PyResult};
pub use crate::instance::{AsPyRef, Py, PyToken};
pub use crate::instance::{AsPyRef, Py};
pub use crate::noargs::NoArgs;
pub use crate::object::PyObject;
pub use crate::objectprotocol::ObjectProtocol;

View File

@ -5,7 +5,7 @@
use crate::conversion::PyTryFrom;
use crate::err::{PyDowncastError, PyErr, PyResult};
use crate::ffi;
use crate::instance::{AsPyRef, Py, PyToken};
use crate::instance::{AsPyRef, Py};
use crate::object::PyObject;
use crate::pythonrun::{self, GILGuard};
use crate::typeob::PyTypeCreate;
@ -256,7 +256,7 @@ impl<'p> Python<'p> {
#[inline]
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>>
where
F: FnOnce(PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeCreate,
{
Py::new(self, f)
@ -267,7 +267,7 @@ impl<'p> Python<'p> {
#[inline]
pub fn init_ref<T, F>(self, f: F) -> PyResult<&'p T>
where
F: FnOnce(PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeCreate,
{
Py::new_ref(self, f)
@ -278,7 +278,7 @@ impl<'p> Python<'p> {
#[inline]
pub fn init_mut<T, F>(self, f: F) -> PyResult<&'p mut T>
where
F: FnOnce(PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeCreate,
{
Py::new_mut(self, f)

View File

@ -8,14 +8,14 @@ use std::ffi::CString;
use std::mem;
use std::os::raw::c_void;
use crate::{class, ffi, pythonrun};
use crate::class::methods::PyMethodDefType;
use crate::err::{PyErr, PyResult};
use crate::instance::{Py, PyObjectWithToken, PyToken};
use crate::python::{IntoPyPointer, Python};
use crate::instance::{Py, PyObjectWithToken};
use crate::python::ToPyPointer;
use crate::python::{IntoPyPointer, Python};
use crate::types::PyObjectRef;
use crate::types::PyType;
use crate::{class, ffi, pythonrun};
/// Python type information.
pub trait PyTypeInfo {
@ -85,7 +85,7 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3;
/// impl MyClass {
/// #[new]
/// fn __new__(obj: &PyRawObject) -> PyResult<()> {
/// obj.init(|_| MyClass { })
/// obj.init(|| MyClass { })
/// }
/// }
/// ```
@ -142,11 +142,11 @@ impl PyRawObject {
}
pub fn init<T, F>(&self, f: F) -> PyResult<()>
where
F: FnOnce(PyToken) -> T,
T: PyTypeInfo,
where
F: FnOnce() -> T,
T: PyTypeInfo,
{
let value = f(PyToken::new());
let value = f();
unsafe {
let ptr = (self.ptr as *mut u8).offset(T::OFFSET) as *mut T;
@ -208,7 +208,9 @@ pub trait PyObjectAlloc: PyTypeInfo {
unsafe fn drop(_py: Python, _obj: *mut ffi::PyObject) {}
}
impl<T> PyObjectAlloc for T where T: PyTypeInfo
impl<T> PyObjectAlloc for T
where
T: PyTypeInfo,
{
#[allow(unconditional_recursion)]
/// Calls the rust destructor for the object.
@ -231,11 +233,11 @@ impl<T> PyObjectAlloc for T where T: PyTypeInfo
Self::drop(py, obj);
#[cfg(Py_3)]
{
if ffi::PyObject_CallFinalizerFromDealloc(obj) < 0 {
return;
}
{
if ffi::PyObject_CallFinalizerFromDealloc(obj) < 0 {
return;
}
}
match Self::type_object().tp_free {
Some(free) => free(obj as *mut c_void),
@ -310,8 +312,8 @@ pub trait PyTypeCreate: PyObjectAlloc + PyTypeInfo + Sized {
impl<T> PyTypeCreate for T where T: PyObjectAlloc + PyTypeInfo + Sized {}
impl<T> PyTypeObject for T
where
T: PyTypeCreate,
where
T: PyTypeCreate,
{
fn init_type() {
<T as PyTypeCreate>::init_type()
@ -325,8 +327,8 @@ impl<T> PyTypeObject for T
/// Register new type in python object system.
#[cfg(not(Py_LIMITED_API))]
pub fn initialize_type<T>(py: Python, module_name: Option<&str>) -> PyResult<()>
where
T: PyObjectAlloc + PyTypeInfo,
where
T: PyObjectAlloc + PyTypeInfo,
{
// type name
let name = match module_name {
@ -463,8 +465,8 @@ fn async_methods<T>(type_info: &mut ffi::PyTypeObject) {
fn async_methods<T>(_type_info: &mut ffi::PyTypeObject) {}
unsafe extern "C" fn tp_dealloc_callback<T>(obj: *mut ffi::PyObject)
where
T: PyObjectAlloc,
where
T: PyObjectAlloc,
{
let _pool = pythonrun::GILPool::new_no_pointers();
let py = Python::assume_gil_acquired();
@ -476,9 +478,9 @@ fn py_class_flags<T: PyTypeInfo>(type_object: &mut ffi::PyTypeObject) {
if type_object.tp_traverse != None
|| type_object.tp_clear != None
|| T::FLAGS & PY_TYPE_FLAG_GC != 0
{
type_object.tp_flags = ffi::Py_TPFLAGS_DEFAULT | ffi::Py_TPFLAGS_HAVE_GC;
} else {
{
type_object.tp_flags = ffi::Py_TPFLAGS_DEFAULT | ffi::Py_TPFLAGS_HAVE_GC;
} else {
type_object.tp_flags = ffi::Py_TPFLAGS_DEFAULT;
}
if T::FLAGS & PY_TYPE_FLAG_BASETYPE != 0 {
@ -491,10 +493,10 @@ fn py_class_flags<T: PyTypeInfo>(type_object: &mut ffi::PyTypeObject) {
if type_object.tp_traverse != None
|| type_object.tp_clear != None
|| T::FLAGS & PY_TYPE_FLAG_GC != 0
{
type_object.tp_flags =
ffi::Py_TPFLAGS_DEFAULT | ffi::Py_TPFLAGS_CHECKTYPES | ffi::Py_TPFLAGS_HAVE_GC;
} else {
{
type_object.tp_flags =
ffi::Py_TPFLAGS_DEFAULT | ffi::Py_TPFLAGS_CHECKTYPES | ffi::Py_TPFLAGS_HAVE_GC;
} else {
type_object.tp_flags = ffi::Py_TPFLAGS_DEFAULT | ffi::Py_TPFLAGS_CHECKTYPES;
}
if !type_object.tp_as_buffer.is_null() {
@ -583,27 +585,27 @@ fn py_class_properties<T>() -> Vec<ffi::PyGetSetDef> {
for def in <T as class::methods::PyMethodsProtocolImpl>::py_methods()
.iter()
.chain(<T as class::methods::PyPropMethodsProtocolImpl>::py_methods().iter())
{
match *def {
PyMethodDefType::Getter(ref getter) => {
let name = getter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
}
let def = defs.get_mut(&name).expect("Failed to call get_mut");
getter.copy_to(def);
{
match *def {
PyMethodDefType::Getter(ref getter) => {
let name = getter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
}
PyMethodDefType::Setter(ref setter) => {
let name = setter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
}
let def = defs.get_mut(&name).expect("Failed to call get_mut");
setter.copy_to(def);
}
_ => (),
let def = defs.get_mut(&name).expect("Failed to call get_mut");
getter.copy_to(def);
}
PyMethodDefType::Setter(ref setter) => {
let name = setter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
}
let def = defs.get_mut(&name).expect("Failed to call get_mut");
setter.copy_to(def);
}
_ => (),
}
}
defs.values().cloned().collect()
}

View File

@ -36,7 +36,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'");
@ -114,7 +114,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);
};
@ -168,7 +168,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'");
@ -234,7 +234,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'");
@ -261,7 +261,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

@ -71,7 +71,7 @@ fn test_buffer() {
let py = gil.python();
let t = py
.init(|_| TestClass {
.init(|| TestClass {
vec: vec![b' ', b'2', b'3'],
})
.unwrap();
@ -88,7 +88,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

@ -12,7 +12,7 @@ struct EmptyClassWithNew {}
impl EmptyClassWithNew {
#[__new__]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| EmptyClassWithNew {})
obj.init(|| EmptyClassWithNew {})
}
}
@ -37,7 +37,7 @@ struct NewWithOneArg {
impl NewWithOneArg {
#[new]
fn __new__(obj: &PyRawObject, arg: i32) -> PyResult<()> {
obj.init(|_| NewWithOneArg { _data: arg })
obj.init(|| NewWithOneArg { _data: arg })
}
}
@ -61,7 +61,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

@ -33,14 +33,14 @@ 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 {
let inst = Py::new(py, || Len {
l: (isize::MAX as usize) + 1,
})
.unwrap();
@ -68,7 +68,7 @@ fn iterator() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new(py, |_| Iterator {
let inst = Py::new(py, || Iterator {
iter: Box::new(5..8),
})
.unwrap();
@ -110,7 +110,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)'");
@ -123,7 +123,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'");
@ -150,10 +150,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");
@ -184,7 +184,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);
}
@ -205,11 +205,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)");
}
@ -233,7 +233,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);
@ -258,7 +258,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);
@ -287,7 +287,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]");
@ -309,7 +309,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'");
}
@ -328,7 +328,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);
@ -367,7 +367,7 @@ fn context_manager() {
let py = gil.python();
let c = py
.init_mut(|_| ContextManager { exit_called: false })
.init_mut(|| ContextManager { exit_called: false })
.unwrap();
py_run!(py, c, "with c as x: assert x == 42");
assert!(c.exit_called);
@ -425,7 +425,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();
@ -441,7 +441,7 @@ struct DunderDictSupport {}
fn dunder_dict_support() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new_ref(py, |_| DunderDictSupport {}).unwrap();
let inst = Py::new_ref(py, || DunderDictSupport {}).unwrap();
py_run!(
py,
inst,
@ -459,7 +459,7 @@ struct WeakRefDunderDictSupport {}
fn weakref_dunder_dict_support() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new_ref(py, |_| WeakRefDunderDictSupport {}).unwrap();
let inst = Py::new_ref(py, || WeakRefDunderDictSupport {}).unwrap();
py_run!(
py,
inst,

View File

@ -29,8 +29,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);
}
@ -39,10 +39,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())
}
}
@ -73,7 +73,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),
},
@ -119,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(|_| ClassWithDrop {}).unwrap();
let inst = py.init(|| ClassWithDrop {}).unwrap();
drop(inst);
}
@ -162,7 +162,7 @@ fn gc_integration() {
{
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new_ref(py, |_| GCIntegration {
let inst = Py::new_ref(py, || GCIntegration {
self_ref: RefCell::new(py.None()),
dropped: TestDropCall {
drop_called: Arc::clone(&drop_called),
@ -186,7 +186,7 @@ struct GCIntegration2 {}
fn gc_integration2() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new_ref(py, |_| GCIntegration2 {}).unwrap();
let inst = Py::new_ref(py, || GCIntegration2 {}).unwrap();
py_run!(py, inst, "import gc; assert inst in gc.get_objects()");
}
@ -197,7 +197,7 @@ struct WeakRefSupport {}
fn weakref_support() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new_ref(py, |_| WeakRefSupport {}).unwrap();
let inst = Py::new_ref(py, || WeakRefSupport {}).unwrap();
py_run!(
py,
inst,
@ -214,7 +214,7 @@ struct BaseClassWithDrop {
impl BaseClassWithDrop {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| BaseClassWithDrop { data: None })
obj.init(|| BaseClassWithDrop { data: None })
}
}
@ -235,7 +235,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

@ -35,7 +35,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");
@ -65,7 +65,7 @@ fn getter_setter_autogen() {
let py = gil.python();
let inst = py
.init(|_| GetterSetter {
.init(|| GetterSetter {
num: 10,
text: "Hello".to_string(),
})

View File

@ -39,7 +39,7 @@ fn subclass() {
impl BaseClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| BaseClass { val1: 10 })
obj.init(|| BaseClass { val1: 10 })
}
}
@ -53,7 +53,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

@ -27,7 +27,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();
@ -55,7 +55,7 @@ fn instance_method_with_args() {
let py = gil.python();
let obj = py
.init_ref(|_| InstanceMethodWithArgs { member: 7 })
.init_ref(|| InstanceMethodWithArgs { member: 7 })
.unwrap();
assert_eq!(obj.method(6).unwrap(), 42);
let d = PyDict::new(py);
@ -72,7 +72,7 @@ struct ClassMethod {}
impl ClassMethod {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| ClassMethod {})
obj.init(|| ClassMethod {})
}
#[classmethod]
@ -136,7 +136,7 @@ struct StaticMethod {}
impl StaticMethod {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| StaticMethod {})
obj.init(|| StaticMethod {})
}
#[staticmethod]
@ -225,7 +225,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

@ -30,8 +30,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();