Skip to content

Commit dbaa2de

Browse files
committed
Small improvements around function calling
1 parent 625a3f6 commit dbaa2de

9 files changed

+66
-78
lines changed

src/ffi2/modsupport.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ extern "C" {
4343
value: *const c_char,
4444
) -> c_int;
4545

46-
#[cfg(all(
47-
target_pointer_width = "64",
48-
not(py_sys_config = "Py_TRACE_REFS")
49-
))]
46+
#[cfg(all(target_pointer_width = "64", not(py_sys_config = "Py_TRACE_REFS")))]
5047
fn Py_InitModule4_64(
5148
name: *const c_char,
5249
methods: *mut PyMethodDef,
@@ -64,10 +61,7 @@ extern "C" {
6461
apiver: c_int,
6562
) -> *mut PyObject;
6663

67-
#[cfg(all(
68-
not(target_pointer_width = "64"),
69-
not(py_sys_config = "Py_TRACE_REFS")
70-
))]
64+
#[cfg(all(not(target_pointer_width = "64"), not(py_sys_config = "Py_TRACE_REFS")))]
7165
pub fn Py_InitModule4(
7266
name: *const c_char,
7367
methods: *mut PyMethodDef,
@@ -76,10 +70,7 @@ extern "C" {
7670
apiver: c_int,
7771
) -> *mut PyObject;
7872

79-
#[cfg(all(
80-
not(target_pointer_width = "64"),
81-
py_sys_config = "Py_TRACE_REFS"
82-
))]
73+
#[cfg(all(not(target_pointer_width = "64"), py_sys_config = "Py_TRACE_REFS"))]
8374
fn Py_InitModule4TraceRefs(
8475
name: *const c_char,
8576
methods: *mut PyMethodDef,
@@ -91,10 +82,7 @@ extern "C" {
9182

9283
pub const PYTHON_API_VERSION: c_int = 1013;
9384

94-
#[cfg(all(
95-
target_pointer_width = "64",
96-
not(py_sys_config = "Py_TRACE_REFS")
97-
))]
85+
#[cfg(all(target_pointer_width = "64", not(py_sys_config = "Py_TRACE_REFS")))]
9886
#[inline]
9987
pub unsafe fn Py_InitModule4(
10088
name: *const c_char,
@@ -118,10 +106,7 @@ pub unsafe fn Py_InitModule4(
118106
Py_InitModule4TraceRefs_64(name, methods, doc, _self, apiver)
119107
}
120108

121-
#[cfg(all(
122-
not(target_pointer_width = "64"),
123-
py_sys_config = "Py_TRACE_REFS"
124-
))]
109+
#[cfg(all(not(target_pointer_width = "64"), py_sys_config = "Py_TRACE_REFS"))]
125110
#[inline]
126111
pub unsafe fn Py_InitModule4(
127112
name: *const c_char,

src/ffi2/objectabstract.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ pub unsafe fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int {
216216
#[inline]
217217
pub unsafe fn PyIter_Check(obj: *mut PyObject) -> c_int {
218218
let t = (*obj).ob_type;
219-
(PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER) != 0 && match (*t).tp_iternext {
220-
None => false,
221-
Some(f) => f as *const c_void != _PyObject_NextNotImplemented as *const c_void,
222-
}) as c_int
219+
(PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER) != 0
220+
&& match (*t).tp_iternext {
221+
None => false,
222+
Some(f) => f as *const c_void != _PyObject_NextNotImplemented as *const c_void,
223+
}) as c_int
223224
}
224225

225226
#[inline]

src/ffi2/objimpl.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int {
3939
/// Test if an object has a GC head
4040
#[inline]
4141
pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int {
42-
(PyType_IS_GC(Py_TYPE(o)) != 0 && match (*Py_TYPE(o)).tp_is_gc {
43-
Some(tp_is_gc) => tp_is_gc(o) != 0,
44-
None => true,
45-
}) as c_int
42+
(PyType_IS_GC(Py_TYPE(o)) != 0
43+
&& match (*Py_TYPE(o)).tp_is_gc {
44+
Some(tp_is_gc) => tp_is_gc(o) != 0,
45+
None => true,
46+
}) as c_int
4647
}
4748

4849
/* Test if a type supports weak references */

src/ffi3/objimpl.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int {
5858
#[inline]
5959
#[cfg(not(Py_LIMITED_API))]
6060
pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int {
61-
(PyType_IS_GC(Py_TYPE(o)) != 0 && match (*Py_TYPE(o)).tp_is_gc {
62-
Some(tp_is_gc) => tp_is_gc(o) != 0,
63-
None => true,
64-
}) as c_int
61+
(PyType_IS_GC(Py_TYPE(o)) != 0
62+
&& match (*Py_TYPE(o)).tp_is_gc {
63+
Some(tp_is_gc) => tp_is_gc(o) != 0,
64+
None => true,
65+
}) as c_int
6566
}
6667

6768
#[cfg_attr(windows, link(name = "pythonXY"))]

src/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use object::PyObject;
1212
use objectprotocol::ObjectProtocol;
1313
use python::{IntoPyPointer, Python, ToPyPointer};
1414
use pythonrun;
15+
use typeob::PyTypeCreate;
1516
use typeob::{PyTypeInfo, PyTypeObject};
1617
use types::PyObjectRef;
17-
use typeob::PyTypeCreate;
1818

1919
pub struct PyToken(PhantomData<Rc<()>>);
2020

src/object.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,13 @@ impl PyObject {
199199

200200
/// Calls a method on the object.
201201
/// This is equivalent to the Python expression: 'self.name(*args, **kwargs)'
202-
pub fn call_method<A>(
202+
pub fn call_method(
203203
&self,
204204
py: Python,
205205
name: &str,
206-
args: A,
207-
kwargs: Option<PyDict>,
208-
) -> PyResult<PyObject>
209-
where
210-
A: IntoPyTuple,
211-
{
206+
args: impl IntoPyTuple,
207+
kwargs: Option<&PyDict>,
208+
) -> PyResult<PyObject> {
212209
name.with_borrowed_ptr(py, |name| unsafe {
213210
let args = args.into_tuple(py).into_ptr();
214211
let kwargs = kwargs.into_ptr();
@@ -232,10 +229,12 @@ impl PyObject {
232229

233230
/// Calls a method on the object.
234231
/// This is equivalent to the Python expression: 'self.name(*args)'
235-
pub fn call_method1<A>(&self, py: Python, name: &str, args: A) -> PyResult<PyObject>
236-
where
237-
A: IntoPyTuple,
238-
{
232+
pub fn call_method1(
233+
&self,
234+
py: Python,
235+
name: &str,
236+
args: impl IntoPyTuple,
237+
) -> PyResult<PyObject> {
239238
self.call_method(py, name, args, None)
240239
}
241240
}
@@ -328,10 +327,9 @@ mod test {
328327
let py = gil.python();
329328
let obj: PyObject = PyDict::new(py).into();
330329
assert!(obj.call_method0(py, "asdf").is_err());
331-
assert!(
332-
obj.call_method(py, "nonexistent_method", (1,), None)
333-
.is_err()
334-
);
330+
assert!(obj
331+
.call_method(py, "nonexistent_method", (1,), None)
332+
.is_err());
335333
assert!(obj.call_method0(py, "nonexistent_method").is_err());
336334
assert!(obj.call_method1(py, "nonexistent_method", (1,)).is_err());
337335
}

src/objectprotocol.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,31 @@ pub trait ObjectProtocol {
102102
/// This is equivalent to the Python expression: `self.name(*args, **kwargs)`
103103
///
104104
/// # Example
105-
/// ```rust,ignore
106-
/// let obj = SomePyObject::new();
107-
/// let args = (arg1, arg2, arg3);
108-
/// let kwargs = ((key1, value1), (key2, value2));
109-
/// let pid = obj.call_method("do_something", args, kwargs.into_py_dict());
105+
/// ```rust
106+
/// # use pyo3::prelude::*;
107+
/// use pyo3::types::IntoPyDict;
108+
///
109+
/// let gil = Python::acquire_gil();
110+
/// let py = gil.python();
111+
/// let list = vec![3, 6, 5, 4, 7].to_object(py);
112+
/// let dict = vec![("reverse", true)].into_py_dict(py);
113+
/// list.call_method(py, "sort", (), Some(dict)).unwrap();
114+
/// assert_eq!(list.extract::<Vec<i32>>(py).unwrap(), vec![7, 6, 5, 4, 3]);
110115
/// ```
111-
fn call_method<A>(
116+
fn call_method(
112117
&self,
113118
name: &str,
114-
args: A,
119+
args: impl IntoPyTuple,
115120
kwargs: Option<&PyDict>,
116-
) -> PyResult<&PyObjectRef>
117-
where
118-
A: IntoPyTuple;
121+
) -> PyResult<&PyObjectRef>;
119122

120123
/// Calls a method on the object.
121124
/// This is equivalent to the Python expression: `self.name()`
122125
fn call_method0(&self, name: &str) -> PyResult<&PyObjectRef>;
123126

124127
/// Calls a method on the object with positional arguments only .
125128
/// This is equivalent to the Python expression: `self.name(*args)`
126-
fn call_method1<A: IntoPyTuple>(&self, name: &str, args: A) -> PyResult<&PyObjectRef>;
129+
fn call_method1(&self, name: &str, args: impl IntoPyTuple) -> PyResult<&PyObjectRef>;
127130

128131
/// Retrieves the hash code of the object.
129132
/// This is equivalent to the Python expression: `hash(self)`
@@ -346,10 +349,12 @@ where
346349
self.call(args, None)
347350
}
348351

349-
fn call_method<A>(&self, name: &str, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyObjectRef>
350-
where
351-
A: IntoPyTuple,
352-
{
352+
fn call_method(
353+
&self,
354+
name: &str,
355+
args: impl IntoPyTuple,
356+
kwargs: Option<&PyDict>,
357+
) -> PyResult<&PyObjectRef> {
353358
name.with_borrowed_ptr(self.py(), |name| unsafe {
354359
let py = self.py();
355360
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
@@ -371,7 +376,7 @@ where
371376
self.call_method(name, PyTuple::empty(self.py()), None)
372377
}
373378

374-
fn call_method1<A: IntoPyTuple>(&self, name: &str, args: A) -> PyResult<&PyObjectRef> {
379+
fn call_method1(&self, name: &str, args: impl IntoPyTuple) -> PyResult<&PyObjectRef> {
375380
self.call_method(name, args, None)
376381
}
377382

@@ -531,10 +536,10 @@ mod test {
531536
fn test_call_with_kwargs() {
532537
let gil = Python::acquire_gil();
533538
let py = gil.python();
534-
let list = py.eval("list([3, 6, 5, 4, 7])", None, None).unwrap();
539+
let list = vec![3, 6, 5, 4, 7].to_object(py);
535540
let dict = vec![("reverse", true)].into_py_dict(py);
536-
list.call_method("sort", (), Some(dict)).unwrap();
537-
assert_eq!(list.extract::<Vec<i32>>().unwrap(), vec![7, 6, 5, 4, 3]);
541+
list.call_method(py, "sort", (), Some(dict)).unwrap();
542+
assert_eq!(list.extract::<Vec<i32>>(py).unwrap(), vec![7, 6, 5, 4, 3]);
538543
}
539544

540545
#[test]

src/python.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,9 @@ mod test {
532532
fn test_is_instance() {
533533
let gil = Python::acquire_gil();
534534
let py = gil.python();
535-
assert!(
536-
py.is_instance::<PyBool, PyObjectRef>(PyBool::new(py, true).into())
537-
.unwrap()
538-
);
535+
assert!(py
536+
.is_instance::<PyBool, PyObjectRef>(PyBool::new(py, true).into())
537+
.unwrap());
539538
let list = PyList::new(py, &[1, 2, 3, 4]);
540539
assert!(!py.is_instance::<PyBool, _>(list.as_ref()).unwrap());
541540
assert!(py.is_instance::<PyList, _>(list.as_ref()).unwrap());

tests/test_class_new.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ fn empty_class_with_new() {
2121
let gil = Python::acquire_gil();
2222
let py = gil.python();
2323
let typeobj = py.get_type::<EmptyClassWithNew>();
24-
assert!(
25-
typeobj
26-
.call(NoArgs, None)
27-
.unwrap()
28-
.cast_as::<EmptyClassWithNew>()
29-
.is_ok()
30-
);
24+
assert!(typeobj
25+
.call(NoArgs, None)
26+
.unwrap()
27+
.cast_as::<EmptyClassWithNew>()
28+
.is_ok());
3129
}
3230

3331
#[pyclass]

0 commit comments

Comments
 (0)