Skip to content

Commit

Permalink
py.init returns ptr, init_ref and init_mut ref and mut ref
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jun 23, 2017
1 parent 8238a14 commit 14c6aaa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
31 changes: 16 additions & 15 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,24 @@ impl<T> Py<T> {

impl<T> Py<T> where T: PyTypeInfo,
{
/// Create new instance of T and move under python management
/// Returns `Py<T>`.
pub fn new<F>(py: Python, f: F) -> PyResult<Py<T>>
where F: FnOnce(::PyToken) -> T,
T: PyObjectAlloc<T>
{
let ob = f(PyToken(PhantomData));

let ob = unsafe {
let ob = try!(<T as PyObjectAlloc<T>>::alloc(py, ob));
Py::from_owned_ptr(ob)
};
Ok(ob)
}

/// Create new instance of `T` and move under python management.
/// Returns references to `T`
pub fn new<'p, F>(py: Python<'p>, f: F) -> PyResult<&'p T>
pub fn new_ref<'p, F>(py: Python<'p>, f: F) -> PyResult<&'p T>
where F: FnOnce(::PyToken) -> T,
T: PyObjectAlloc<T> + PyDowncastFrom
{
Expand All @@ -189,20 +204,6 @@ impl<T> Py<T> where T: PyTypeInfo,
}
}

/// Create new instance of T and move under python management
/// Returns `Py<T>`.
pub fn new_ptr<F>(py: Python, f: F) -> PyResult<Py<T>>
where F: FnOnce(::PyToken) -> T,
T: PyObjectAlloc<T>
{
let ob = f(PyToken(PhantomData));

let ob = unsafe {
let ob = try!(<T as PyObjectAlloc<T>>::alloc(py, ob));
Py::from_owned_ptr(ob)
};
Ok(ob)
}
}

impl<T> AsPyRef<T> for Py<T> where T: PyTypeInfo {
Expand Down
20 changes: 10 additions & 10 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,33 +237,33 @@ impl<'p> Python<'p> {
impl<'p> Python<'p> {

/// Create new instance of T and move under python management.
/// Created object get registered in release pool. Returns references to `T`
/// Returns `Py<T>`.
#[inline]
pub fn init<T, F>(self, f: F) -> PyResult<&'p T>
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>>
where F: FnOnce(PyToken) -> T,
T: PyTypeInfo + PyObjectAlloc<T> + PyDowncastFrom
T: PyTypeInfo + PyObjectAlloc<T>
{
Py::new(self, f)
}

/// Create new instance of T and move under python management.
/// Created object get registered in release pool. Returns mutable references to `T`
/// Created object get registered in release pool. Returns references to `T`
#[inline]
pub fn init_mut<T, F>(self, f: F) -> PyResult<&'p mut T>
pub fn init_ref<T, F>(self, f: F) -> PyResult<&'p T>
where F: FnOnce(PyToken) -> T,
T: PyTypeInfo + PyObjectAlloc<T> + PyDowncastFrom
{
Py::new_mut(self, f)
Py::new_ref(self, f)
}

/// Create new instance of T and move under python management.
/// Returns `Py<T>`.
/// Created object get registered in release pool. Returns mutable references to `T`
#[inline]
pub fn init_ptr<T, F>(self, f: F) -> PyResult<Py<T>>
pub fn init_mut<T, F>(self, f: F) -> PyResult<&'p mut T>
where F: FnOnce(PyToken) -> T,
T: PyTypeInfo + PyObjectAlloc<T>
T: PyTypeInfo + PyObjectAlloc<T> + PyDowncastFrom
{
Py::new_ptr(self, f)
Py::new_mut(self, f)
}
}

Expand Down
24 changes: 12 additions & 12 deletions tests/test_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl NewWithTwoArgs {
#[new]
fn __new__(cls: &PyType, arg1: i32, arg2: i32) -> PyResult<Py<NewWithTwoArgs>>
{
Py::new_ptr(
Py::new(
cls.token(),
|t| NewWithTwoArgs{_data1: arg1, _data2: arg2, token: t})
}
Expand All @@ -180,12 +180,12 @@ fn class_with_freelist() {
let gil = Python::acquire_gil();
let py = gil.python();

let inst = Py::new_ptr(py, |t| ClassWithFreelist{token: t}).unwrap();
let inst2 = Py::new_ptr(py, |t| ClassWithFreelist{token: t}).unwrap();
let inst = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
let inst2 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
let ptr = inst.as_ptr();
drop(inst);

let inst3 = Py::new_ptr(py, |t| ClassWithFreelist{token: t}).unwrap();
let inst3 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
assert_eq!(ptr, inst3.as_ptr());
}

Expand All @@ -212,7 +212,7 @@ fn data_is_dropped() {

let drop_called1 = Arc::new(AtomicBool::new(false));
let drop_called2 = Arc::new(AtomicBool::new(false));
let inst = py.init_ptr(|t| DataIsDropped{
let inst = py.init(|t| DataIsDropped{
member1: TestDropCall { drop_called: drop_called1.clone() },
member2: TestDropCall { drop_called: drop_called2.clone() },
token: t
Expand Down Expand Up @@ -244,7 +244,7 @@ fn instance_method() {
let gil = Python::acquire_gil();
let py = gil.python();

let obj = Py::new(py, |t| InstanceMethod{member: 42, token: t}).unwrap();
let obj = py.init_ref(|t| InstanceMethod{member: 42, token: t}).unwrap();
assert!(obj.method().unwrap() == 42);
let d = PyDict::new(py);
d.set_item("obj", obj).unwrap();
Expand All @@ -270,7 +270,7 @@ fn instance_method_with_args() {
let gil = Python::acquire_gil();
let py = gil.python();

let obj = Py::new(py, |t| InstanceMethodWithArgs{member: 7, token: t}).unwrap();
let obj = py.init_ref(|t| InstanceMethodWithArgs{member: 7, token: t}).unwrap();
assert!(obj.method(6).unwrap() == 42);
let d = PyDict::new(py);
d.set_item("obj", obj).unwrap();
Expand All @@ -286,7 +286,7 @@ struct ClassMethod {token: PyToken}
impl ClassMethod {
#[new]
fn __new__(cls: &PyType) -> PyResult<Py<ClassMethod>> {
Py::new_ptr(cls.token(), |t| ClassMethod{token: t})
cls.token().init(|t| ClassMethod{token: t})
}

#[classmethod]
Expand Down Expand Up @@ -407,7 +407,7 @@ fn gc_integration() {
{
let gil = Python::acquire_gil();
let py = gil.python();
let inst = Py::new(py, |t| GCIntegration{
let inst = Py::new_ref(py, |t| GCIntegration{
self_ref: RefCell::new(py.None().into()),
dropped: TestDropCall { drop_called: drop_called.clone() },
token: t}).unwrap();
Expand Down Expand Up @@ -642,7 +642,7 @@ fn setitem() {
let gil = Python::acquire_gil();
let py = gil.python();

let c = py.init(|t| SetItem{key: 0, val: 0, token: t}).unwrap();
let c = py.init_ref(|t| SetItem{key: 0, val: 0, token: t}).unwrap();
py_run!(py, c, "c[1] = 2");
assert_eq!(c.key, 1);
assert_eq!(c.val, 2);
Expand All @@ -668,7 +668,7 @@ fn delitem() {
let gil = Python::acquire_gil();
let py = gil.python();

let c = py.init(|t| DelItem{key:0, token:t}).unwrap();
let c = py.init_ref(|t| DelItem{key:0, token:t}).unwrap();
py_run!(py, c, "del c[1]");
assert_eq!(c.key, 1);
py_expect_exception!(py, c, "c[1] = 2", NotImplementedError);
Expand Down Expand Up @@ -698,7 +698,7 @@ fn setdelitem() {
let gil = Python::acquire_gil();
let py = gil.python();

let c = py.init(|t| SetDelItem{val: None, token: t}).unwrap();
let c = py.init_ref(|t| SetDelItem{val: None, token: t}).unwrap();
py_run!(py, c, "c[1] = 2");
assert_eq!(c.val, Some(2));
py_run!(py, c, "del c[1]");
Expand Down

0 comments on commit 14c6aaa

Please sign in to comment.