Skip to content

Commit b0e0777

Browse files
committed
[WIP] Fix some tests
1 parent 4d7dfaf commit b0e0777

10 files changed

+65
-52
lines changed

pyo3-derive-backend/src/method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'a> FnSpec<'a> {
6565
ref pat, ref ty, ..
6666
}) => {
6767
// skip first argument (cls)
68-
if (fn_type == FnType::FnClass || fn_type == FnType::FnNew) && !has_self {
68+
if fn_type == FnType::FnClass && !has_self {
6969
has_self = true;
7070
continue;
7171
}

src/instance.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ impl<T> IntoPyPointer for Py<T> {
167167
}
168168
}
169169

170+
impl<'a, T> std::convert::From<&PyClassShell<T>> for Py<T>
171+
where
172+
T: PyClass,
173+
{
174+
fn from(shell: &PyClassShell<T>) -> Self {
175+
unsafe { Py::from_borrowed_ptr(shell.as_ptr()) }
176+
}
177+
}
178+
179+
impl<'a, T> std::convert::From<&mut PyClassShell<T>> for Py<T>
180+
where
181+
T: PyClass,
182+
{
183+
fn from(shell: &mut PyClassShell<T>) -> Self {
184+
unsafe { Py::from_borrowed_ptr(shell.as_ptr()) }
185+
}
186+
}
187+
170188
impl<T> PartialEq for Py<T> {
171189
#[inline]
172190
fn eq(&self, o: &Py<T>) -> bool {

tests/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ macro_rules! py_assert {
1313
macro_rules! py_expect_exception {
1414
($py:expr, $val:ident, $code:expr, $err:ident) => {{
1515
use pyo3::types::IntoPyDict;
16-
let d = [(stringify!($val), &$val)].into_py_dict($py);
16+
let d = [(stringify!($val), $val)].into_py_dict($py);
1717

1818
let res = $py.run($code, None, Some(d));
1919
let err = res.unwrap_err();

tests/test_class_basics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pyo3::prelude::*;
22
use pyo3::py_run;
3-
use pyo3::type_object::initialize_type;
3+
use pyo3::pyclass::initialize_type;
44

55
mod common;
66

tests/test_class_new.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use pyo3::prelude::*;
2-
use pyo3::PyRawObject;
32

43
#[pyclass]
54
struct EmptyClassWithNew {}
65

76
#[pymethods]
87
impl EmptyClassWithNew {
98
#[new]
10-
fn new(obj: &PyRawObject) {
11-
obj.init(EmptyClassWithNew {});
9+
fn new() -> EmptyClassWithNew {
10+
EmptyClassWithNew {}
1211
}
1312
}
1413

@@ -32,8 +31,8 @@ struct NewWithOneArg {
3231
#[pymethods]
3332
impl NewWithOneArg {
3433
#[new]
35-
fn new(obj: &PyRawObject, arg: i32) {
36-
obj.init(NewWithOneArg { _data: arg })
34+
fn new(arg: i32) -> NewWithOneArg {
35+
NewWithOneArg { _data: arg }
3736
}
3837
}
3938

@@ -56,11 +55,11 @@ struct NewWithTwoArgs {
5655
#[pymethods]
5756
impl NewWithTwoArgs {
5857
#[new]
59-
fn new(obj: &PyRawObject, arg1: i32, arg2: i32) {
60-
obj.init(NewWithTwoArgs {
58+
fn new(arg1: i32, arg2: i32) -> Self {
59+
NewWithTwoArgs {
6160
_data1: arg1,
6261
_data2: arg2,
63-
})
62+
}
6463
}
6564
}
6665

tests/test_datetime.rs

100644100755
File mode changed.

tests/test_dunder.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ use pyo3::class::{
44
PyContextProtocol, PyIterProtocol, PyMappingProtocol, PyObjectProtocol, PySequenceProtocol,
55
};
66
use pyo3::exceptions::{IndexError, ValueError};
7-
use pyo3::ffi;
87
use pyo3::prelude::*;
9-
use pyo3::py_run;
108
use pyo3::types::{IntoPyDict, PyAny, PyBytes, PySlice, PyType};
11-
use pyo3::AsPyPointer;
9+
use pyo3::{ffi, py_run, AsPyPointer, PyClassShell};
1210
use std::convert::TryFrom;
1311
use std::{isize, iter};
1412

@@ -55,11 +53,11 @@ struct Iterator {
5553

5654
#[pyproto]
5755
impl<'p> PyIterProtocol for Iterator {
58-
fn __iter__(slf: PyRefMut<Self>) -> PyResult<Py<Iterator>> {
56+
fn __iter__(slf: &mut PyClassShell<Self>) -> PyResult<Py<Iterator>> {
5957
Ok(slf.into())
6058
}
6159

62-
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<i32>> {
60+
fn __next__(mut slf: &mut PyClassShell<Self>) -> PyResult<Option<i32>> {
6361
Ok(slf.iter.next())
6462
}
6563
}
@@ -252,7 +250,7 @@ fn setitem() {
252250
let gil = Python::acquire_gil();
253251
let py = gil.python();
254252

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

280-
let c = PyRef::new(py, DelItem { key: 0 }).unwrap();
278+
let c = PyClassShell::new_ref(py, DelItem { key: 0 }).unwrap();
281279
py_run!(py, c, "del c[1]");
282280
assert_eq!(c.key, 1);
283281
py_expect_exception!(py, c, "c[1] = 2", NotImplementedError);
@@ -306,7 +304,7 @@ fn setdelitem() {
306304
let gil = Python::acquire_gil();
307305
let py = gil.python();
308306

309-
let c = PyRef::new(py, SetDelItem { val: None }).unwrap();
307+
let c = PyClassShell::new_ref(py, SetDelItem { val: None }).unwrap();
310308
py_run!(py, c, "c[1] = 2");
311309
assert_eq!(c.val, Some(2));
312310
py_run!(py, c, "del c[1]");
@@ -385,7 +383,7 @@ fn context_manager() {
385383
let gil = Python::acquire_gil();
386384
let py = gil.python();
387385

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

@@ -457,7 +455,7 @@ struct DunderDictSupport {}
457455
fn dunder_dict_support() {
458456
let gil = Python::acquire_gil();
459457
let py = gil.python();
460-
let inst = PyRef::new(py, DunderDictSupport {}).unwrap();
458+
let inst = PyClassShell::new_ref(py, DunderDictSupport {}).unwrap();
461459
py_run!(
462460
py,
463461
inst,
@@ -472,7 +470,7 @@ fn dunder_dict_support() {
472470
fn access_dunder_dict() {
473471
let gil = Python::acquire_gil();
474472
let py = gil.python();
475-
let inst = PyRef::new(py, DunderDictSupport {}).unwrap();
473+
let inst = PyClassShell::new_ref(py, DunderDictSupport {}).unwrap();
476474
py_run!(
477475
py,
478476
inst,
@@ -490,7 +488,7 @@ struct WeakRefDunderDictSupport {}
490488
fn weakref_dunder_dict_support() {
491489
let gil = Python::acquire_gil();
492490
let py = gil.python();
493-
let inst = PyRef::new(py, WeakRefDunderDictSupport {}).unwrap();
491+
let inst = PyClassShell::new_ref(py, WeakRefDunderDictSupport {}).unwrap();
494492
py_run!(
495493
py,
496494
inst,
@@ -515,7 +513,7 @@ impl PyObjectProtocol for ClassWithGetAttr {
515513
fn getattr_doesnt_override_member() {
516514
let gil = Python::acquire_gil();
517515
let py = gil.python();
518-
let inst = PyRef::new(py, ClassWithGetAttr { data: 4 }).unwrap();
516+
let inst = PyClassShell::new_ref(py, ClassWithGetAttr { data: 4 }).unwrap();
519517
py_assert!(py, inst, "inst.data == 4");
520518
py_assert!(py, inst, "inst.a == 8");
521519
}

tests/test_gc.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
use pyo3::class::PyGCProtocol;
22
use pyo3::class::PyTraverseError;
33
use pyo3::class::PyVisit;
4-
use pyo3::ffi;
54
use pyo3::prelude::*;
6-
use pyo3::py_run;
7-
use pyo3::types::PyAny;
8-
use pyo3::types::PyTuple;
9-
use pyo3::AsPyPointer;
10-
use pyo3::PyRawObject;
5+
use pyo3::types::{PyAny, PyTuple};
6+
use pyo3::{ffi, py_run, AsPyPointer, PyClassShell};
117
use std::cell::RefCell;
128
use std::sync::atomic::{AtomicBool, Ordering};
139
use std::sync::Arc;
@@ -157,7 +153,7 @@ fn gc_integration() {
157153
{
158154
let gil = Python::acquire_gil();
159155
let py = gil.python();
160-
let inst = PyRef::new(
156+
let inst = PyClassShell::new_ref(
161157
py,
162158
GCIntegration {
163159
self_ref: RefCell::new(py.None()),
@@ -192,7 +188,7 @@ impl PyGCProtocol for GCIntegration2 {
192188
fn gc_integration2() {
193189
let gil = Python::acquire_gil();
194190
let py = gil.python();
195-
let inst = PyRef::new(py, GCIntegration2 {}).unwrap();
191+
let inst = PyClassShell::new_ref(py, GCIntegration2 {}).unwrap();
196192
py_run!(py, inst, "import gc; assert inst in gc.get_objects()");
197193
}
198194

@@ -203,7 +199,7 @@ struct WeakRefSupport {}
203199
fn weakref_support() {
204200
let gil = Python::acquire_gil();
205201
let py = gil.python();
206-
let inst = PyRef::new(py, WeakRefSupport {}).unwrap();
202+
let inst = PyClassShell::new_ref(py, WeakRefSupport {}).unwrap();
207203
py_run!(
208204
py,
209205
inst,
@@ -219,8 +215,8 @@ struct BaseClassWithDrop {
219215
#[pymethods]
220216
impl BaseClassWithDrop {
221217
#[new]
222-
fn new(obj: &PyRawObject) {
223-
obj.init(BaseClassWithDrop { data: None })
218+
fn new() -> BaseClassWithDrop {
219+
BaseClassWithDrop { data: None }
224220
}
225221
}
226222

@@ -239,10 +235,10 @@ struct SubClassWithDrop {
239235

240236
#[pymethods]
241237
impl SubClassWithDrop {
238+
// TODO(kngwyu): Implement baseclass initialization
242239
#[new]
243-
fn new(obj: &PyRawObject) {
244-
obj.init(SubClassWithDrop { data: None });
245-
BaseClassWithDrop::new(obj);
240+
fn new() -> SubClassWithDrop {
241+
SubClassWithDrop { data: None }
246242
}
247243
}
248244

tests/test_pyself.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use pyo3;
33
use pyo3::prelude::*;
44
use pyo3::types::{PyBytes, PyString};
5-
use pyo3::PyIterProtocol;
5+
use pyo3::{PyClassShell, PyIterProtocol};
66
use std::collections::HashMap;
77

88
mod common;
@@ -17,21 +17,24 @@ struct Reader {
1717

1818
#[pymethods]
1919
impl Reader {
20-
fn clone_ref(slf: PyRef<Self>) -> PyRef<Self> {
20+
fn clone_ref(slf: &PyClassShell<Self>) -> &PyClassShell<Self> {
2121
slf
2222
}
23-
fn clone_ref_with_py<'py>(slf: PyRef<'py, Self>, _py: Python<'py>) -> PyRef<'py, Self> {
23+
fn clone_ref_with_py<'py>(
24+
slf: &'py PyClassShell<Self>,
25+
_py: Python<'py>,
26+
) -> &'py PyClassShell<Self> {
2427
slf
2528
}
26-
fn get_iter(slf: PyRef<Self>, keys: Py<PyBytes>) -> PyResult<Iter> {
29+
fn get_iter(slf: &PyClassShell<Self>, keys: Py<PyBytes>) -> PyResult<Iter> {
2730
Ok(Iter {
2831
reader: slf.into(),
2932
keys,
3033
idx: 0,
3134
})
3235
}
3336
fn get_iter_and_reset(
34-
mut slf: PyRefMut<Self>,
37+
mut slf: &mut PyClassShell<Self>,
3538
keys: Py<PyBytes>,
3639
py: Python,
3740
) -> PyResult<Iter> {

tests/test_various.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use pyo3::prelude::*;
2-
use pyo3::type_object::initialize_type;
32
use pyo3::types::IntoPyDict;
43
use pyo3::types::{PyDict, PyTuple};
5-
use pyo3::{py_run, wrap_pyfunction};
4+
use pyo3::{py_run, wrap_pyfunction, AsPyRef, PyClassShell};
65
use std::isize;
76

87
mod common;
@@ -83,8 +82,8 @@ fn intopytuple_pyclass() {
8382
let py = gil.python();
8483

8584
let tup = (
86-
PyRef::new(py, SimplePyClass {}).unwrap(),
87-
PyRef::new(py, SimplePyClass {}).unwrap(),
85+
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
86+
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
8887
);
8988
py_assert!(py, tup, "type(tup[0]).__name__ == 'SimplePyClass'");
9089
py_assert!(py, tup, "type(tup[0]).__name__ == type(tup[1]).__name__");
@@ -108,8 +107,8 @@ fn pytuple_pyclass_iter() {
108107
let tup = PyTuple::new(
109108
py,
110109
[
111-
PyRef::new(py, SimplePyClass {}).unwrap(),
112-
PyRef::new(py, SimplePyClass {}).unwrap(),
110+
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
111+
PyClassShell::new_ref(py, SimplePyClass {}).unwrap(),
113112
]
114113
.iter(),
115114
);
@@ -124,12 +123,12 @@ struct PickleSupport {}
124123
#[pymethods]
125124
impl PickleSupport {
126125
#[new]
127-
fn new(obj: &PyRawObject) {
128-
obj.init({ PickleSupport {} });
126+
fn new() -> PickleSupport {
127+
PickleSupport {}
129128
}
130129

131130
pub fn __reduce__<'py>(
132-
slf: PyRef<Self>,
131+
slf: &'py PyClassShell<Self>,
133132
py: Python<'py>,
134133
) -> PyResult<(PyObject, &'py PyTuple, PyObject)> {
135134
let cls = slf.to_object(py).getattr(py, "__class__")?;

0 commit comments

Comments
 (0)