From 93b25edba171a2d08358d5bbcf5483bcb59d13eb Mon Sep 17 00:00:00 2001 From: messense Date: Mon, 5 Jul 2021 21:23:56 +0800 Subject: [PATCH] Use `errror_on_minusone` more often --- src/instance.rs | 9 +++------ src/python.rs | 8 ++------ src/type_object.rs | 6 ++---- src/types/any.rs | 7 ++----- src/types/dict.rs | 10 +++++----- src/types/num.rs | 7 ++----- src/types/typeobject.rs | 20 +++++--------------- 7 files changed, 21 insertions(+), 46 deletions(-) diff --git a/src/instance.rs b/src/instance.rs index 7e098a96..b9351129 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -1,6 +1,6 @@ // Copyright (c) 2017-present PyO3 Project and Contributors use crate::conversion::{PyTryFrom, ToBorrowedObject}; -use crate::err::{PyDowncastError, PyErr, PyResult}; +use crate::err::{self, PyDowncastError, PyErr, PyResult}; use crate::gil; use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell}; use crate::types::{PyDict, PyTuple}; @@ -458,11 +458,8 @@ impl Py { /// This is equivalent to the Python expression `bool(self)`. pub fn is_true(&self, py: Python) -> PyResult { let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) }; - if v == -1 { - Err(PyErr::api_call_failed(py)) - } else { - Ok(v != 0) - } + err::error_on_minusone(py, v)?; + Ok(v != 0) } /// Extracts some type from the Python object. diff --git a/src/python.rs b/src/python.rs index a74f510c..95b02ebc 100644 --- a/src/python.rs +++ b/src/python.rs @@ -2,7 +2,7 @@ // // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython -use crate::err::{PyDowncastError, PyErr, PyResult}; +use crate::err::{self, PyDowncastError, PyErr, PyResult}; use crate::gil::{self, GILGuard, GILPool}; use crate::type_object::{PyTypeInfo, PyTypeObject}; use crate::types::{PyAny, PyDict, PyModule, PyType}; @@ -622,11 +622,7 @@ impl<'p> Python<'p> { /// [2]: https://docs.python.org/3/library/signal.html pub fn check_signals(self) -> PyResult<()> { let v = unsafe { ffi::PyErr_CheckSignals() }; - if v == -1 { - Err(PyErr::api_call_failed(self)) - } else { - Ok(()) - } + err::error_on_minusone(self, v) } /// Retrieves a Python instance under the assumption that the GIL is already diff --git a/src/type_object.rs b/src/type_object.rs index 4c4ead79..bcaed452 100644 --- a/src/type_object.rs +++ b/src/type_object.rs @@ -7,7 +7,7 @@ use crate::pyclass::create_type_object; use crate::pyclass::PyClass; use crate::types::{PyAny, PyType}; use crate::{conversion::IntoPyPointer, PyMethodDefType}; -use crate::{ffi, AsPyPointer, PyErr, PyNativeType, PyObject, PyResult, Python}; +use crate::{ffi, AsPyPointer, PyNativeType, PyObject, PyResult, Python}; use parking_lot::{const_mutex, Mutex}; use std::thread::{self, ThreadId}; @@ -190,9 +190,7 @@ fn initialize_tp_dict( // the POV of other threads. for (key, val) in items { let ret = unsafe { ffi::PyObject_SetAttrString(type_object, key.as_ptr(), val.into_ptr()) }; - if ret < 0 { - return Err(PyErr::api_call_failed(py)); - } + crate::err::error_on_minusone(py, ret)?; } Ok(()) } diff --git a/src/types/any.rs b/src/types/any.rs index e59f1a1b..a1c7b9b1 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -516,11 +516,8 @@ impl PyAny { /// This is equivalent to the Python expression `bool(self)`. pub fn is_true(&self) -> PyResult { let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) }; - if v == -1 { - Err(PyErr::api_call_failed(self.py())) - } else { - Ok(v != 0) - } + err::error_on_minusone(self.py(), v)?; + Ok(v != 0) } /// Returns whether the object is considered to be None. diff --git a/src/types/dict.rs b/src/types/dict.rs index 81c18a55..7d05d8a5 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -41,11 +41,11 @@ impl PyDict { pub fn from_sequence(py: Python, seq: PyObject) -> PyResult<&PyDict> { unsafe { let dict = py.from_owned_ptr::(ffi::PyDict_New()); - match ffi::PyDict_MergeFromSeq2(dict.into_ptr(), seq.into_ptr(), 1i32) { - 0 => Ok(dict), - -1 => Err(PyErr::api_call_failed(py)), - _ => unreachable!(), - } + err::error_on_minusone( + py, + ffi::PyDict_MergeFromSeq2(dict.into_ptr(), seq.into_ptr(), 1), + )?; + Ok(dict) } } diff --git a/src/types/num.rs b/src/types/num.rs index fbfc64d7..710d9fbb 100644 --- a/src/types/num.rs +++ b/src/types/num.rs @@ -200,11 +200,8 @@ mod fast_128bit_int_conversion { $is_signed, ); ffi::Py_DECREF(num); - if ok == -1 { - Err(PyErr::api_call_failed(ob.py())) - } else { - Ok(<$rust_type>::from_le_bytes(buffer)) - } + crate::err::error_on_minusone(ob.py(), ok)?; + Ok(<$rust_type>::from_le_bytes(buffer)) } } } diff --git a/src/types/typeobject.rs b/src/types/typeobject.rs index db00d60e..b16b9f3e 100644 --- a/src/types/typeobject.rs +++ b/src/types/typeobject.rs @@ -2,7 +2,7 @@ // // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython -use crate::err::{PyErr, PyResult}; +use crate::err::{self, PyResult}; use crate::instance::PyNativeType; use crate::type_object::PyTypeObject; use crate::{ffi, AsPyPointer, PyAny, Python}; @@ -50,13 +50,8 @@ impl PyType { { let result = unsafe { ffi::PyObject_IsSubclass(self.as_ptr(), T::type_object(self.py()).as_ptr()) }; - if result == -1 { - Err(PyErr::api_call_failed(self.py())) - } else if result == 1 { - Ok(true) - } else { - Ok(false) - } + err::error_on_minusone(self.py(), result)?; + Ok(result == 1) } /// Check whether `obj` is an instance of `self`. @@ -64,12 +59,7 @@ impl PyType { /// Equivalent to Python's `isinstance` function. pub fn is_instance(&self, obj: &T) -> PyResult { let result = unsafe { ffi::PyObject_IsInstance(obj.as_ptr(), self.as_ptr()) }; - if result == -1 { - Err(PyErr::api_call_failed(self.py())) - } else if result == 1 { - Ok(true) - } else { - Ok(false) - } + err::error_on_minusone(self.py(), result)?; + Ok(result == 1) } }