Skip to content

Commit

Permalink
[WIP] Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Dec 12, 2019
1 parent 4d7dfaf commit 1723599
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pyo3-derive-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<'a> FnSpec<'a> {
ref pat, ref ty, ..
}) => {
// skip first argument (cls)
if (fn_type == FnType::FnClass || fn_type == FnType::FnNew) && !has_self {
if fn_type == FnType::FnClass && !has_self {
has_self = true;
continue;
}
Expand Down
18 changes: 18 additions & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ impl<T> IntoPyPointer for Py<T> {
}
}

impl<'a, T> std::convert::From<&PyClassShell<T>> for Py<T>
where
T: PyClass,
{
fn from(shell: &PyClassShell<T>) -> Self {
unsafe { Py::from_borrowed_ptr(shell.as_ptr()) }
}
}

impl<'a, T> std::convert::From<&mut PyClassShell<T>> for Py<T>
where
T: PyClass,
{
fn from(shell: &mut PyClassShell<T>) -> Self {
unsafe { Py::from_borrowed_ptr(shell.as_ptr()) }
}
}

impl<T> PartialEq for Py<T> {
#[inline]
fn eq(&self, o: &Py<T>) -> bool {
Expand Down
8 changes: 7 additions & 1 deletion src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ impl<T: PyClass> std::ops::DerefMut for PyClassShell<T> {
}
}

impl<T: PyClass> ToPyObject for PyClassShell<T> {
impl<T: PyClass> ToPyObject for &PyClassShell<T> {
fn to_object(&self, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}

impl<T: PyClass> ToPyObject for &mut PyClassShell<T> {
fn to_object(&self, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! py_assert {
macro_rules! py_expect_exception {
($py:expr, $val:ident, $code:expr, $err:ident) => {{
use pyo3::types::IntoPyDict;
let d = [(stringify!($val), &$val)].into_py_dict($py);
let d = [(stringify!($val), $val)].into_py_dict($py);

let res = $py.run($code, None, Some(d));
let err = res.unwrap_err();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_class_basics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::type_object::initialize_type;
use pyo3::pyclass::initialize_type;

mod common;

Expand Down
15 changes: 7 additions & 8 deletions tests/test_class_new.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use pyo3::prelude::*;
use pyo3::PyRawObject;

#[pyclass]
struct EmptyClassWithNew {}

#[pymethods]
impl EmptyClassWithNew {
#[new]
fn new(obj: &PyRawObject) {
obj.init(EmptyClassWithNew {});
fn new() -> EmptyClassWithNew {
EmptyClassWithNew {}
}
}

Expand All @@ -32,8 +31,8 @@ struct NewWithOneArg {
#[pymethods]
impl NewWithOneArg {
#[new]
fn new(obj: &PyRawObject, arg: i32) {
obj.init(NewWithOneArg { _data: arg })
fn new(arg: i32) -> NewWithOneArg {
NewWithOneArg { _data: arg }
}
}

Expand All @@ -56,11 +55,11 @@ struct NewWithTwoArgs {
#[pymethods]
impl NewWithTwoArgs {
#[new]
fn new(obj: &PyRawObject, arg1: i32, arg2: i32) {
obj.init(NewWithTwoArgs {
fn new(arg1: i32, arg2: i32) -> Self {
NewWithTwoArgs {
_data1: arg1,
_data2: arg2,
})
}
}
}

Expand Down
Empty file modified tests/test_datetime.rs
100644 → 100755
Empty file.
24 changes: 11 additions & 13 deletions tests/test_dunder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use pyo3::class::{
PyContextProtocol, PyIterProtocol, PyMappingProtocol, PyObjectProtocol, PySequenceProtocol,
};
use pyo3::exceptions::{IndexError, ValueError};
use pyo3::ffi;
use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::types::{IntoPyDict, PyAny, PyBytes, PySlice, PyType};
use pyo3::AsPyPointer;
use pyo3::{ffi, py_run, AsPyPointer, PyClassShell};
use std::convert::TryFrom;
use std::{isize, iter};

Expand Down Expand Up @@ -55,11 +53,11 @@ struct Iterator {

#[pyproto]
impl<'p> PyIterProtocol for Iterator {
fn __iter__(slf: PyRefMut<Self>) -> PyResult<Py<Iterator>> {
fn __iter__(slf: &mut PyClassShell<Self>) -> PyResult<Py<Iterator>> {
Ok(slf.into())
}

fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<i32>> {
fn __next__(mut slf: &mut PyClassShell<Self>) -> PyResult<Option<i32>> {
Ok(slf.iter.next())
}
}
Expand Down Expand Up @@ -252,7 +250,7 @@ fn setitem() {
let gil = Python::acquire_gil();
let py = gil.python();

let c = PyRef::new(py, SetItem { key: 0, val: 0 }).unwrap();
let c = PyClassShell::new_ref(py, SetItem { key: 0, val: 0 }).unwrap();
py_run!(py, c, "c[1] = 2");
assert_eq!(c.key, 1);
assert_eq!(c.val, 2);
Expand All @@ -277,7 +275,7 @@ fn delitem() {
let gil = Python::acquire_gil();
let py = gil.python();

let c = PyRef::new(py, DelItem { key: 0 }).unwrap();
let c = PyClassShell::new_ref(py, 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);
Expand Down Expand Up @@ -306,7 +304,7 @@ fn setdelitem() {
let gil = Python::acquire_gil();
let py = gil.python();

let c = PyRef::new(py, SetDelItem { val: None }).unwrap();
let c = PyClassShell::new_ref(py, SetDelItem { val: None }).unwrap();
py_run!(py, c, "c[1] = 2");
assert_eq!(c.val, Some(2));
py_run!(py, c, "del c[1]");
Expand Down Expand Up @@ -385,7 +383,7 @@ fn context_manager() {
let gil = Python::acquire_gil();
let py = gil.python();

let mut c = PyRefMut::new(py, ContextManager { exit_called: false }).unwrap();
let c = PyClassShell::new_mut(py, ContextManager { exit_called: false }).unwrap();
py_run!(py, c, "with c as x: assert x == 42");
assert!(c.exit_called);

Expand Down Expand Up @@ -457,7 +455,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 = PyClassShell::new_ref(py, DunderDictSupport {}).unwrap();
py_run!(
py,
inst,
Expand All @@ -472,7 +470,7 @@ fn dunder_dict_support() {
fn access_dunder_dict() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, DunderDictSupport {}).unwrap();
let inst = PyClassShell::new_ref(py, DunderDictSupport {}).unwrap();
py_run!(
py,
inst,
Expand All @@ -490,7 +488,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 = PyClassShell::new_ref(py, WeakRefDunderDictSupport {}).unwrap();
py_run!(
py,
inst,
Expand All @@ -515,7 +513,7 @@ impl PyObjectProtocol for ClassWithGetAttr {
fn getattr_doesnt_override_member() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, ClassWithGetAttr { data: 4 }).unwrap();
let inst = PyClassShell::new_ref(py, ClassWithGetAttr { data: 4 }).unwrap();
py_assert!(py, inst, "inst.data == 4");
py_assert!(py, inst, "inst.a == 8");
}
24 changes: 10 additions & 14 deletions tests/test_gc.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use pyo3::class::PyGCProtocol;
use pyo3::class::PyTraverseError;
use pyo3::class::PyVisit;
use pyo3::ffi;
use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::types::PyAny;
use pyo3::types::PyTuple;
use pyo3::AsPyPointer;
use pyo3::PyRawObject;
use pyo3::types::{PyAny, PyTuple};
use pyo3::{ffi, py_run, AsPyPointer, PyClassShell};
use std::cell::RefCell;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -157,7 +153,7 @@ fn gc_integration() {
{
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(
let inst = PyClassShell::new_ref(
py,
GCIntegration {
self_ref: RefCell::new(py.None()),
Expand Down Expand Up @@ -192,7 +188,7 @@ impl PyGCProtocol for GCIntegration2 {
fn gc_integration2() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, GCIntegration2 {}).unwrap();
let inst = PyClassShell::new_ref(py, GCIntegration2 {}).unwrap();
py_run!(py, inst, "import gc; assert inst in gc.get_objects()");
}

Expand All @@ -203,7 +199,7 @@ struct WeakRefSupport {}
fn weakref_support() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, WeakRefSupport {}).unwrap();
let inst = PyClassShell::new_ref(py, WeakRefSupport {}).unwrap();
py_run!(
py,
inst,
Expand All @@ -219,8 +215,8 @@ struct BaseClassWithDrop {
#[pymethods]
impl BaseClassWithDrop {
#[new]
fn new(obj: &PyRawObject) {
obj.init(BaseClassWithDrop { data: None })
fn new() -> BaseClassWithDrop {
BaseClassWithDrop { data: None }
}
}

Expand All @@ -239,10 +235,10 @@ struct SubClassWithDrop {

#[pymethods]
impl SubClassWithDrop {
// TODO(kngwyu): Implement baseclass initialization
#[new]
fn new(obj: &PyRawObject) {
obj.init(SubClassWithDrop { data: None });
BaseClassWithDrop::new(obj);
fn new() -> SubClassWithDrop {
SubClassWithDrop { data: None }
}
}

Expand Down
13 changes: 8 additions & 5 deletions tests/test_pyself.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use pyo3;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyString};
use pyo3::PyIterProtocol;
use pyo3::{PyClassShell, PyIterProtocol};
use std::collections::HashMap;

mod common;
Expand All @@ -17,21 +17,24 @@ struct Reader {

#[pymethods]
impl Reader {
fn clone_ref(slf: PyRef<Self>) -> PyRef<Self> {
fn clone_ref(slf: &PyClassShell<Self>) -> &PyClassShell<Self> {
slf
}
fn clone_ref_with_py<'py>(slf: PyRef<'py, Self>, _py: Python<'py>) -> PyRef<'py, Self> {
fn clone_ref_with_py<'py>(
slf: &'py PyClassShell<Self>,
_py: Python<'py>,
) -> &'py PyClassShell<Self> {
slf
}
fn get_iter(slf: PyRef<Self>, keys: Py<PyBytes>) -> PyResult<Iter> {
fn get_iter(slf: &PyClassShell<Self>, keys: Py<PyBytes>) -> PyResult<Iter> {
Ok(Iter {
reader: slf.into(),
keys,
idx: 0,
})
}
fn get_iter_and_reset(
mut slf: PyRefMut<Self>,
mut slf: &mut PyClassShell<Self>,
keys: Py<PyBytes>,
py: Python,
) -> PyResult<Iter> {
Expand Down
17 changes: 8 additions & 9 deletions tests/test_various.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use pyo3::prelude::*;
use pyo3::type_object::initialize_type;
use pyo3::types::IntoPyDict;
use pyo3::types::{PyDict, PyTuple};
use pyo3::{py_run, wrap_pyfunction};
use pyo3::{py_run, wrap_pyfunction, AsPyRef, PyClassShell};
use std::isize;

mod common;
Expand Down Expand Up @@ -83,8 +82,8 @@ fn intopytuple_pyclass() {
let py = gil.python();

let tup = (
PyRef::new(py, SimplePyClass {}).unwrap(),
PyRef::new(py, SimplePyClass {}).unwrap(),
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
);
py_assert!(py, tup, "type(tup[0]).__name__ == 'SimplePyClass'");
py_assert!(py, tup, "type(tup[0]).__name__ == type(tup[1]).__name__");
Expand All @@ -108,8 +107,8 @@ fn pytuple_pyclass_iter() {
let tup = PyTuple::new(
py,
[
PyRef::new(py, SimplePyClass {}).unwrap(),
PyRef::new(py, SimplePyClass {}).unwrap(),
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
]
.iter(),
);
Expand All @@ -124,12 +123,12 @@ struct PickleSupport {}
#[pymethods]
impl PickleSupport {
#[new]
fn new(obj: &PyRawObject) {
obj.init({ PickleSupport {} });
fn new() -> PickleSupport {
PickleSupport {}
}

pub fn __reduce__<'py>(
slf: PyRef<Self>,
slf: &'py PyClassShell<Self>,
py: Python<'py>,
) -> PyResult<(PyObject, &'py PyTuple, PyObject)> {
let cls = slf.to_object(py).getattr(py, "__class__")?;
Expand Down

0 comments on commit 1723599

Please sign in to comment.