From b2675b11fe20e622a81ae5ca6d9b62b220d4baf0 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Thu, 11 Feb 2021 21:37:38 +0000 Subject: [PATCH] rust 1.50: clippy and lint fixes --- examples/rustapi_module/src/datetime.rs | 8 +-- examples/rustapi_module/src/misc.rs | 3 +- pyo3-macros-backend/src/pymethod.rs | 2 +- src/class/methods.rs | 67 +++++++++---------- src/ffi/codecs.rs | 4 +- src/ffi/compile.rs | 4 +- src/ffi/complexobject.rs | 4 +- src/ffi/context.rs | 4 +- src/ffi/descrobject.rs | 2 +- src/ffi/dictobject.rs | 2 +- src/ffi/eval.rs | 4 +- src/ffi/fileobject.rs | 2 +- src/ffi/genobject.rs | 6 +- src/ffi/intrcheck.rs | 4 +- src/pyclass.rs | 67 +++++++++---------- tests/test_getter_setter.rs | 15 ++--- tests/test_methods.rs | 85 ++++++++++++------------- tests/test_module.rs | 12 ++-- tests/test_pyself.rs | 6 +- tests/test_string.rs | 4 +- tests/test_variable_arguments.rs | 8 +-- tests/test_various.rs | 13 ++-- 22 files changed, 154 insertions(+), 172 deletions(-) diff --git a/examples/rustapi_module/src/datetime.rs b/examples/rustapi_module/src/datetime.rs index 2148ea8f..b7231841 100644 --- a/examples/rustapi_module/src/datetime.rs +++ b/examples/rustapi_module/src/datetime.rs @@ -192,12 +192,12 @@ impl TzClass { PyDelta::new(py, 0, 3600, 0, true) } - fn tzname(&self, _py: Python<'_>, _dt: &PyDateTime) -> PyResult { - Ok(String::from("+01:00")) + fn tzname(&self, _py: Python<'_>, _dt: &PyDateTime) -> String { + String::from("+01:00") } - fn dst(&self, _py: Python<'_>, _dt: &PyDateTime) -> PyResult> { - Ok(None) + fn dst(&self, _py: Python<'_>, _dt: &PyDateTime) -> Option<&PyDelta> { + None } } diff --git a/examples/rustapi_module/src/misc.rs b/examples/rustapi_module/src/misc.rs index badab06e..33ad86d0 100644 --- a/examples/rustapi_module/src/misc.rs +++ b/examples/rustapi_module/src/misc.rs @@ -2,11 +2,10 @@ use pyo3::prelude::*; use pyo3::wrap_pyfunction; #[pyfunction] -fn issue_219() -> PyResult<()> { +fn issue_219() { // issue 219: acquiring GIL inside #[pyfunction] deadlocks. let gil = Python::acquire_gil(); let _py = gil.python(); - Ok(()) } #[pymodule] diff --git a/pyo3-macros-backend/src/pymethod.rs b/pyo3-macros-backend/src/pymethod.rs index 1eb16e05..f78d3607 100644 --- a/pyo3-macros-backend/src/pymethod.rs +++ b/pyo3-macros-backend/src/pymethod.rs @@ -663,7 +663,7 @@ pub fn impl_py_method_def_call(spec: &FnSpec, wrapper: &TokenStream) -> TokenStr pyo3::class::PyMethodDefType::Call({ #wrapper - pyo3::class::PyMethodDef::cfunction_with_keywords( + pyo3::class::PyMethodDef::call_func( concat!(stringify!(#python_name), "\0"), __wrap, pyo3::ffi::METH_STATIC, diff --git a/src/class/methods.rs b/src/class/methods.rs index bce6e1ef..9874a609 100644 --- a/src/class/methods.rs +++ b/src/class/methods.rs @@ -10,15 +10,15 @@ use std::os::raw::c_int; #[derive(Debug)] pub enum PyMethodDefType { /// Represents class `__new__` method - New(PyMethodDef), + New(PyMethodDef), /// Represents class `__call__` method - Call(PyMethodDef), + Call(PyMethodDef), /// Represents class method - Class(PyMethodDef), + Class(PyMethodDef), /// Represents static method - Static(PyMethodDef), + Static(PyMethodDef), /// Represents normal method - Method(PyMethodDef), + Method(PyMethodDef), /// Represents class attribute, used by `#[attribute]` ClassAttribute(PyClassAttributeDef), /// Represents getter descriptor, used by `#[getter]` @@ -31,14 +31,12 @@ pub enum PyMethodDefType { pub enum PyMethodType { PyCFunction(ffi::PyCFunction), PyCFunctionWithKeywords(ffi::PyCFunctionWithKeywords), - PyNewFunc(ffi::newfunc), - PyInitFunc(ffi::initproc), } #[derive(Clone, Debug)] -pub struct PyMethodDef { +pub struct PyMethodDef { pub(crate) ml_name: &'static CStr, - pub(crate) ml_meth: PyMethodType, + pub(crate) ml_meth: MethodT, pub(crate) ml_flags: c_int, pub(crate) ml_doc: &'static CStr, } @@ -63,7 +61,9 @@ pub struct PySetterDef { doc: &'static CStr, } -unsafe impl Sync for PyMethodDef {} +// Safe because ml_meth (the T) cannot be accessed outside of the crate, so only safe-to-sync values +// are stored in this structure. +unsafe impl Sync for PyMethodDef {} unsafe impl Sync for ffi::PyMethodDef {} @@ -82,23 +82,36 @@ fn get_doc(doc: &str) -> &CStr { CStr::from_bytes_with_nul(doc.as_bytes()).expect("Document must be terminated with NULL byte") } -impl PyMethodDef { - pub(crate) fn get_new_func(&self) -> Option { - if let PyMethodType::PyNewFunc(new_func) = self.ml_meth { - Some(new_func) - } else { - None +impl PyMethodDef { + /// Define a `__new__` function. + pub fn new_func(name: &'static str, newfunc: ffi::newfunc, doc: &'static str) -> Self { + Self { + ml_name: get_name(name), + ml_meth: newfunc, + ml_flags: ffi::METH_VARARGS | ffi::METH_KEYWORDS, + ml_doc: get_doc(doc), } } +} - pub(crate) fn get_cfunction_with_keywords(&self) -> Option { - if let PyMethodType::PyCFunctionWithKeywords(func) = self.ml_meth { - Some(func) - } else { - None +impl PyMethodDef { + /// Define a `__call__` function. + pub fn call_func( + name: &'static str, + callfunc: ffi::PyCFunctionWithKeywords, + flags: c_int, + doc: &'static str, + ) -> Self { + Self { + ml_name: get_name(name), + ml_meth: callfunc, + ml_flags: flags | ffi::METH_VARARGS | ffi::METH_KEYWORDS, + ml_doc: get_doc(doc), } } +} +impl PyMethodDef { /// Define a function with no `*args` and `**kwargs`. pub fn cfunction(name: &'static str, cfunction: ffi::PyCFunction, doc: &'static str) -> Self { Self { @@ -109,16 +122,6 @@ impl PyMethodDef { } } - /// Define a `__new__` function. - pub fn new_func(name: &'static str, newfunc: ffi::newfunc, doc: &'static str) -> Self { - Self { - ml_name: get_name(name), - ml_meth: PyMethodType::PyNewFunc(newfunc), - ml_flags: ffi::METH_VARARGS | ffi::METH_KEYWORDS, - ml_doc: get_doc(doc), - } - } - /// Define a function that can take `*args` and `**kwargs`. pub fn cfunction_with_keywords( name: &'static str, @@ -139,8 +142,6 @@ impl PyMethodDef { let meth = match self.ml_meth { PyMethodType::PyCFunction(meth) => meth, PyMethodType::PyCFunctionWithKeywords(meth) => unsafe { std::mem::transmute(meth) }, - PyMethodType::PyNewFunc(meth) => unsafe { std::mem::transmute(meth) }, - PyMethodType::PyInitFunc(meth) => unsafe { std::mem::transmute(meth) }, }; ffi::PyMethodDef { diff --git a/src/ffi/codecs.rs b/src/ffi/codecs.rs index 873686e9..303da177 100644 --- a/src/ffi/codecs.rs +++ b/src/ffi/codecs.rs @@ -51,6 +51,6 @@ extern "C" { pub fn PyCodec_ReplaceErrors(exc: *mut PyObject) -> *mut PyObject; pub fn PyCodec_XMLCharRefReplaceErrors(exc: *mut PyObject) -> *mut PyObject; pub fn PyCodec_BackslashReplaceErrors(exc: *mut PyObject) -> *mut PyObject; -// skipped non-limited PyCodec_NameReplaceErrors from Include/codecs.h -// skipped non-limited Py_hexdigits from Include/codecs.h + // skipped non-limited PyCodec_NameReplaceErrors from Include/codecs.h + // skipped non-limited Py_hexdigits from Include/codecs.h } diff --git a/src/ffi/compile.rs b/src/ffi/compile.rs index 8627dce1..4ed17590 100644 --- a/src/ffi/compile.rs +++ b/src/ffi/compile.rs @@ -80,8 +80,8 @@ extern "C" { #[cfg(Py_3_8)] pub fn PyCompile_OpcodeStackEffectWithJump(opcode: c_int, oparg: c_int, jump: c_int) -> c_int; -// skipped non-limited _PyASTOptimizeState -// skipped non-limited _PyAST_Optimize + // skipped non-limited _PyASTOptimizeState + // skipped non-limited _PyAST_Optimize } pub const Py_single_input: c_int = 256; diff --git a/src/ffi/complexobject.rs b/src/ffi/complexobject.rs index 4ec2adab..beecb421 100644 --- a/src/ffi/complexobject.rs +++ b/src/ffi/complexobject.rs @@ -56,6 +56,6 @@ extern "C" { pub fn PyComplex_RealAsDouble(op: *mut PyObject) -> c_double; #[cfg_attr(PyPy, link_name = "PyPyComplex_ImagAsDouble")] pub fn PyComplex_ImagAsDouble(op: *mut PyObject) -> c_double; -// skipped non-limited PyComplex_AsCComplex -// skipped non-limited _PyComplex_FormatAdvancedWriter + // skipped non-limited PyComplex_AsCComplex + // skipped non-limited _PyComplex_FormatAdvancedWriter } diff --git a/src/ffi/context.rs b/src/ffi/context.rs index cbb58f39..f3872a69 100644 --- a/src/ffi/context.rs +++ b/src/ffi/context.rs @@ -7,7 +7,7 @@ extern "C" { pub static mut PyContextVar_Type: PyTypeObject; // skipped non-limited opaque PyContextVar pub static mut PyContextToken_Type: PyTypeObject; -// skipped non-limited opaque PyContextToken + // skipped non-limited opaque PyContextToken } #[inline] @@ -41,5 +41,5 @@ extern "C" { ) -> c_int; pub fn PyContextVar_Set(var: *mut PyObject, value: *mut PyObject) -> *mut PyObject; pub fn PyContextVar_Reset(var: *mut PyObject, token: *mut PyObject) -> c_int; -// skipped non-limited _PyContext_NewHamtForTests + // skipped non-limited _PyContext_NewHamtForTests } diff --git a/src/ffi/descrobject.rs b/src/ffi/descrobject.rs index 17738772..41eec1cb 100644 --- a/src/ffi/descrobject.rs +++ b/src/ffi/descrobject.rs @@ -48,7 +48,7 @@ extern "C" { pub static mut PyWrapperDescr_Type: PyTypeObject; #[cfg_attr(PyPy, link_name = "PyPyDictProxy_Type")] pub static mut PyDictProxy_Type: PyTypeObject; -// skipped non-limited _PyMethodWrapper_Type + // skipped non-limited _PyMethodWrapper_Type } extern "C" { diff --git a/src/ffi/dictobject.rs b/src/ffi/dictobject.rs index cfb57995..a0d2b2b3 100644 --- a/src/ffi/dictobject.rs +++ b/src/ffi/dictobject.rs @@ -64,7 +64,7 @@ extern "C" { ) -> c_int; #[cfg_attr(PyPy, link_name = "PyPyDict_DelItemString")] pub fn PyDict_DelItemString(dp: *mut PyObject, key: *const c_char) -> c_int; -// skipped 3.10 / ex-non-limited PyObject_GenericGetDict + // skipped 3.10 / ex-non-limited PyObject_GenericGetDict } #[cfg_attr(windows, link(name = "pythonXY"))] diff --git a/src/ffi/eval.rs b/src/ffi/eval.rs index b554cda3..1ce62c07 100644 --- a/src/ffi/eval.rs +++ b/src/ffi/eval.rs @@ -22,6 +22,6 @@ extern "C" { closure: *mut PyObject, ) -> *mut PyObject; -// skipped non-limited _PyEval_EvalCodeWithName -// skipped non-limited _PyEval_CallTracing + // skipped non-limited _PyEval_EvalCodeWithName + // skipped non-limited _PyEval_CallTracing } diff --git a/src/ffi/fileobject.rs b/src/ffi/fileobject.rs index 029304eb..a72db985 100644 --- a/src/ffi/fileobject.rs +++ b/src/ffi/fileobject.rs @@ -29,7 +29,7 @@ extern "C" { pub static mut Py_FileSystemDefaultEncoding: *const c_char; pub static mut Py_FileSystemDefaultEncodeErrors: *const c_char; pub static mut Py_HasFileSystemDefaultEncoding: c_int; -// skipped Python 3.7 / ex-non-limited Py_UTF8Mode + // skipped Python 3.7 / ex-non-limited Py_UTF8Mode } // skipped _PyIsSelectable_fd diff --git a/src/ffi/genobject.rs b/src/ffi/genobject.rs index 76d70f9b..aa6b6607 100644 --- a/src/ffi/genobject.rs +++ b/src/ffi/genobject.rs @@ -79,9 +79,9 @@ pub unsafe fn PyCoroWrapper_Check(op: *mut PyObject) -> c_int { #[cfg_attr(windows, link(name = "pythonXY"))] extern "C" { pub static mut PyAsyncGen_Type: PyTypeObject; -// skipped _PyAsyncGenASend_Type -// skipped _PyAsyncGenWrappedValue_Type -// skipped _PyAsyncGenAThrow_Type + // skipped _PyAsyncGenASend_Type + // skipped _PyAsyncGenWrappedValue_Type + // skipped _PyAsyncGenAThrow_Type } // skipped PyAsyncGen_New diff --git a/src/ffi/intrcheck.rs b/src/ffi/intrcheck.rs index 74d3dc1f..ffd6c79c 100644 --- a/src/ffi/intrcheck.rs +++ b/src/ffi/intrcheck.rs @@ -20,6 +20,6 @@ extern "C" { #[cfg_attr(PyPy, link_name = "PyPyOS_AfterFork")] pub fn PyOS_AfterFork(); -// skipped non-limited _PyOS_IsMainThread -// skipped non-limited Windows _PyOS_SigintEvent + // skipped non-limited _PyOS_IsMainThread + // skipped non-limited Windows _PyOS_SigintEvent } diff --git a/src/pyclass.rs b/src/pyclass.rs index 71ae7dcb..2c2fe45d 100644 --- a/src/pyclass.rs +++ b/src/pyclass.rs @@ -102,16 +102,13 @@ pub trait PyClassAlloc: PyTypeInfo + Sized { } } -fn tp_dealloc() -> Option { - unsafe extern "C" fn dealloc(obj: *mut ffi::PyObject) - where - T: PyClassAlloc, - { - let pool = crate::GILPool::new(); - let py = pool.python(); - ::dealloc(py, (obj as *mut T::Layout) as _) - } - Some(dealloc::) +unsafe extern "C" fn tp_dealloc(obj: *mut ffi::PyObject) +where + T: PyClassAlloc, +{ + let pool = crate::GILPool::new(); + let py = pool.python(); + ::dealloc(py, (obj as *mut T::Layout) as _) } pub(crate) unsafe fn tp_free_fallback(ty: *mut ffi::PyTypeObject) -> ffi::freefunc { @@ -152,11 +149,6 @@ impl TypeSlots { fn push(&mut self, slot: c_int, pfunc: *mut c_void) { self.0.push(ffi::PyType_Slot { slot, pfunc }); } - pub(crate) fn maybe_push(&mut self, slot: c_int, value: Option<*mut c_void>) { - if let Some(pfunc) = value { - self.push(slot, pfunc); - } - } } fn tp_doc() -> PyResult> { @@ -189,12 +181,16 @@ where let mut slots = TypeSlots::default(); slots.push(ffi::Py_tp_base, T::BaseType::type_object_raw(py) as _); - slots.maybe_push(ffi::Py_tp_doc, tp_doc::()?); - slots.maybe_push(ffi::Py_tp_dealloc, tp_dealloc::().map(|v| v as _)); + slots.push(ffi::Py_tp_dealloc, tp_dealloc:: as _); + if let Some(doc) = tp_doc::()? { + slots.push(ffi::Py_tp_doc, doc); + } let (new, call, methods) = py_class_method_defs::(); - slots.maybe_push(ffi::Py_tp_new, new.map(|v| v as _)); - slots.maybe_push(ffi::Py_tp_call, call.map(|v| v as _)); + slots.push(ffi::Py_tp_new, new as _); + if let Some(call_meth) = call { + slots.push(ffi::Py_tp_call, call_meth as _); + } if cfg!(Py_3_9) { let members = py_class_members::(); @@ -315,39 +311,34 @@ pub(crate) fn py_class_attributes() -> impl Iterator Option { - unsafe extern "C" fn fallback_new( - _subtype: *mut ffi::PyTypeObject, - _args: *mut ffi::PyObject, - _kwds: *mut ffi::PyObject, - ) -> *mut ffi::PyObject { - crate::callback_body!(py, { - Err::<(), _>(crate::exceptions::PyTypeError::new_err( - "No constructor defined", - )) - }) - } - Some(fallback_new) +unsafe extern "C" fn fallback_new( + _subtype: *mut ffi::PyTypeObject, + _args: *mut ffi::PyObject, + _kwds: *mut ffi::PyObject, +) -> *mut ffi::PyObject { + crate::callback_body!(py, { + Err::<(), _>(crate::exceptions::PyTypeError::new_err( + "No constructor defined", + )) + }) } fn py_class_method_defs() -> ( - Option, + ffi::newfunc, Option, Vec, ) { let mut defs = Vec::new(); let mut call = None; - let mut new = fallback_new(); + let mut new = fallback_new as ffi::newfunc; for def in T::py_methods() { match def { PyMethodDefType::New(def) => { - new = def.get_new_func(); - debug_assert!(new.is_some()); + new = def.ml_meth; } PyMethodDefType::Call(def) => { - call = def.get_cfunction_with_keywords(); - debug_assert!(call.is_some()); + call = Some(def.ml_meth); } PyMethodDefType::Method(def) | PyMethodDefType::Class(def) diff --git a/tests/test_getter_setter.rs b/tests/test_getter_setter.rs index 747a9014..e3814ac7 100644 --- a/tests/test_getter_setter.rs +++ b/tests/test_getter_setter.rs @@ -11,19 +11,18 @@ struct ClassWithProperties { #[pymethods] impl ClassWithProperties { - fn get_num(&self) -> PyResult { - Ok(self.num) + fn get_num(&self) -> i32 { + self.num } #[getter(DATA)] /// a getter for data - fn get_data(&self) -> PyResult { - Ok(self.num) + fn get_data(&self) -> i32 { + self.num } #[setter(DATA)] - fn set_data(&mut self, value: i32) -> PyResult<()> { + fn set_data(&mut self, value: i32) { self.num = value; - Ok(()) } #[getter] @@ -79,8 +78,8 @@ struct GetterSetter { #[pymethods] impl GetterSetter { - fn get_num2(&self) -> PyResult { - Ok(self.num) + fn get_num2(&self) -> i32 { + self.num } } diff --git a/tests/test_methods.rs b/tests/test_methods.rs index e2d49f1d..7414e2e5 100644 --- a/tests/test_methods.rs +++ b/tests/test_methods.rs @@ -13,8 +13,8 @@ struct InstanceMethod { #[pymethods] impl InstanceMethod { /// Test method - fn method(&self) -> PyResult { - Ok(self.member) + fn method(&self) -> i32 { + self.member } // Checks that &Self works @@ -30,7 +30,7 @@ fn instance_method() { let obj = PyCell::new(py, InstanceMethod { member: 42 }).unwrap(); let obj_ref = obj.borrow(); - assert_eq!(obj_ref.method().unwrap(), 42); + assert_eq!(obj_ref.method(), 42); py_assert!(py, obj, "obj.method() == 42"); py_assert!(py, obj, "obj.add_other(obj) == 84"); py_assert!(py, obj, "obj.method.__doc__ == 'Test method'"); @@ -43,8 +43,8 @@ struct InstanceMethodWithArgs { #[pymethods] impl InstanceMethodWithArgs { - fn method(&self, multiplier: i32) -> PyResult { - Ok(self.member * multiplier) + fn method(&self, multiplier: i32) -> i32 { + self.member * multiplier } } @@ -56,7 +56,7 @@ fn instance_method_with_args() { let obj = PyCell::new(py, InstanceMethodWithArgs { member: 7 }).unwrap(); let obj_ref = obj.borrow(); - assert_eq!(obj_ref.method(6).unwrap(), 42); + assert_eq!(obj_ref.method(6), 42); let d = [("obj", obj)].into_py_dict(py); py.run("assert obj.method(3) == 21", None, Some(d)).unwrap(); py.run("assert obj.method(multiplier=6) == 42", None, Some(d)) @@ -134,8 +134,8 @@ impl StaticMethod { #[staticmethod] /// Test static method. - fn method(_py: Python) -> PyResult<&'static str> { - Ok("StaticMethod.method()!") + fn method(_py: Python) -> &'static str { + "StaticMethod.method()!" } } @@ -144,7 +144,7 @@ fn static_method() { let gil = Python::acquire_gil(); let py = gil.python(); - assert_eq!(StaticMethod::method(py).unwrap(), "StaticMethod.method()!"); + assert_eq!(StaticMethod::method(py), "StaticMethod.method()!"); let d = [("C", py.get_type::())].into_py_dict(py); let run = |code| { @@ -164,8 +164,8 @@ struct StaticMethodWithArgs {} #[pymethods] impl StaticMethodWithArgs { #[staticmethod] - fn method(_py: Python, input: i32) -> PyResult { - Ok(format!("0x{:x}", input)) + fn method(_py: Python, input: i32) -> String { + format!("0x{:x}", input) } } @@ -174,7 +174,7 @@ fn static_method_with_args() { let gil = Python::acquire_gil(); let py = gil.python(); - assert_eq!(StaticMethodWithArgs::method(py, 1234).unwrap(), "0x4d2"); + assert_eq!(StaticMethodWithArgs::method(py, 1234), "0x4d2"); let d = [("C", py.get_type::())].into_py_dict(py); py.run("assert C.method(1337) == '0x539'", None, Some(d)) @@ -187,41 +187,36 @@ struct MethArgs {} #[pymethods] impl MethArgs { #[args(test)] - fn get_optional(&self, test: Option) -> PyResult { - Ok(test.unwrap_or(10)) + fn get_optional(&self, test: Option) -> i32 { + test.unwrap_or(10) } - fn get_optional2(&self, test: Option) -> PyResult> { - Ok(test) + fn get_optional2(&self, test: Option) -> Option { + test } #[args(test = "None")] - fn get_optional3(&self, test: Option) -> PyResult> { - Ok(test) + fn get_optional3(&self, test: Option) -> Option { + test } fn get_optional_positional( &self, _t1: Option, t2: Option, _t3: Option, - ) -> PyResult> { - Ok(t2) + ) -> Option { + t2 } #[args(test = "10")] - fn get_default(&self, test: i32) -> PyResult { - Ok(test) + fn get_default(&self, test: i32) -> i32 { + test } #[args("*", test = 10)] - fn get_kwarg(&self, test: i32) -> PyResult { - Ok(test) + fn get_kwarg(&self, test: i32) -> i32 { + test } #[args(args = "*", kwargs = "**")] - fn get_kwargs( - &self, - py: Python, - args: &PyTuple, - kwargs: Option<&PyDict>, - ) -> PyResult { - Ok([args.into(), kwargs.to_object(py)].to_object(py)) + fn get_kwargs(&self, py: Python, args: &PyTuple, kwargs: Option<&PyDict>) -> PyObject { + [args.into(), kwargs.to_object(py)].to_object(py) } #[args(args = "*", kwargs = "**")] @@ -236,28 +231,28 @@ impl MethArgs { } #[args("*", a = 2, b = 3)] - fn get_kwargs_only_with_defaults(&self, a: i32, b: i32) -> PyResult { - Ok(a + b) + fn get_kwargs_only_with_defaults(&self, a: i32, b: i32) -> i32 { + a + b } #[args("*", a, b)] - fn get_kwargs_only(&self, a: i32, b: i32) -> PyResult { - Ok(a + b) + fn get_kwargs_only(&self, a: i32, b: i32) -> i32 { + a + b } #[args("*", a = 1, b)] - fn get_kwargs_only_with_some_default(&self, a: i32, b: i32) -> PyResult { - Ok(a + b) + fn get_kwargs_only_with_some_default(&self, a: i32, b: i32) -> i32 { + a + b } #[args(a, b = 2, "*", c = 3)] - fn get_pos_arg_kw_sep1(&self, a: i32, b: i32, c: i32) -> PyResult { - Ok(a + b + c) + fn get_pos_arg_kw_sep1(&self, a: i32, b: i32, c: i32) -> i32 { + a + b + c } #[args(a, "*", b = 2, c = 3)] - fn get_pos_arg_kw_sep2(&self, a: i32, b: i32, c: i32) -> PyResult { - Ok(a + b + c) + fn get_pos_arg_kw_sep2(&self, a: i32, b: i32, c: i32) -> i32 { + a + b + c } #[args(kwargs = "**")] @@ -417,14 +412,14 @@ struct MethDocs { #[pymethods] impl MethDocs { /// A method with "documentation" as well. - fn method(&self) -> PyResult { - Ok(0) + fn method(&self) -> i32 { + 0 } #[getter] /// `int`: a very "important" member of 'this' instance. - fn get_x(&self) -> PyResult { - Ok(self.x) + fn get_x(&self) -> i32 { + self.x } } diff --git a/tests/test_module.rs b/tests/test_module.rs index 5ed75bf4..68e01af2 100644 --- a/tests/test_module.rs +++ b/tests/test_module.rs @@ -39,14 +39,13 @@ fn module_with_functions(_py: Python, m: &PyModule) -> PyResult<()> { use pyo3::wrap_pyfunction; #[pyfn(m, "sum_as_string")] - fn sum_as_string_py(_py: Python, a: i64, b: i64) -> PyResult { - let out = sum_as_string(a, b); - Ok(out) + fn sum_as_string_py(_py: Python, a: i64, b: i64) -> String { + sum_as_string(a, b) } #[pyfn(m, "no_parameters")] - fn no_parameters() -> PyResult { - Ok(42) + fn no_parameters() -> usize { + 42 } #[pyfn(m, "with_module", pass_module)] @@ -106,7 +105,8 @@ fn test_module_with_functions() { } #[pymodule(other_name)] -fn some_name(_: Python, _: &PyModule) -> PyResult<()> { +fn some_name(_: Python, m: &PyModule) -> PyResult<()> { + m.add("other_name", "other_name")?; Ok(()) } diff --git a/tests/test_pyself.rs b/tests/test_pyself.rs index ee51524d..31dec01a 100644 --- a/tests/test_pyself.rs +++ b/tests/test_pyself.rs @@ -22,12 +22,12 @@ impl Reader { fn clone_ref_with_py<'py>(slf: &'py PyCell, _py: Python<'py>) -> &'py PyCell { slf } - fn get_iter(slf: &PyCell, keys: Py) -> PyResult { - Ok(Iter { + fn get_iter(slf: &PyCell, keys: Py) -> Iter { + Iter { reader: slf.into(), keys, idx: 0, - }) + } } fn get_iter_and_reset( mut slf: PyRefMut, diff --git a/tests/test_string.rs b/tests/test_string.rs index cde88592..63f78661 100644 --- a/tests/test_string.rs +++ b/tests/test_string.rs @@ -4,9 +4,7 @@ use pyo3::wrap_pyfunction; mod common; #[pyfunction] -fn take_str(_s: &str) -> PyResult<()> { - Ok(()) -} +fn take_str(_s: &str) {} #[test] fn test_unicode_encode_error() { diff --git a/tests/test_variable_arguments.rs b/tests/test_variable_arguments.rs index efaecef0..10670614 100644 --- a/tests/test_variable_arguments.rs +++ b/tests/test_variable_arguments.rs @@ -10,14 +10,14 @@ struct MyClass {} impl MyClass { #[staticmethod] #[args(args = "*")] - fn test_args(args: &PyTuple) -> PyResult<&PyTuple> { - Ok(args) + fn test_args(args: &PyTuple) -> &PyTuple { + args } #[staticmethod] #[args(kwargs = "**")] - fn test_kwargs(kwargs: Option<&PyDict>) -> PyResult> { - Ok(kwargs) + fn test_kwargs(kwargs: Option<&PyDict>) -> Option<&PyDict> { + kwargs } } diff --git a/tests/test_various.rs b/tests/test_various.rs index 3c900372..3b9722b8 100644 --- a/tests/test_various.rs +++ b/tests/test_various.rs @@ -14,12 +14,11 @@ struct MutRefArg { #[pymethods] impl MutRefArg { - fn get(&self) -> PyResult { - Ok(self.n) + fn get(&self) -> i32 { + self.n } - fn set_other(&self, mut other: PyRefMut) -> PyResult<()> { + fn set_other(&self, mut other: PyRefMut) { other.n = 100; - Ok(()) } } @@ -44,8 +43,8 @@ struct PyUsize { } #[pyfunction] -fn get_zero() -> PyResult { - Ok(PyUsize { value: 0 }) +fn get_zero() -> PyUsize { + PyUsize { value: 0 } } #[test] @@ -56,7 +55,7 @@ fn return_custom_class() { let py = gil.python(); // Using from rust - assert_eq!(get_zero().unwrap().value, 0); + assert_eq!(get_zero().value, 0); // Using from python let get_zero = wrap_pyfunction!(get_zero)(py).unwrap();