Use `errror_on_minusone` more often

This commit is contained in:
messense 2021-07-05 21:23:56 +08:00 committed by David Hewitt
parent 9d0b92bf32
commit 93b25edba1
7 changed files with 21 additions and 46 deletions

View File

@ -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,12 +458,9 @@ impl<T> Py<T> {
/// This is equivalent to the Python expression `bool(self)`.
pub fn is_true(&self, py: Python) -> PyResult<bool> {
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
if v == -1 {
Err(PyErr::api_call_failed(py))
} else {
err::error_on_minusone(py, v)?;
Ok(v != 0)
}
}
/// Extracts some type from the Python object.
///

View File

@ -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

View File

@ -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(())
}

View File

@ -516,12 +516,9 @@ impl PyAny {
/// This is equivalent to the Python expression `bool(self)`.
pub fn is_true(&self) -> PyResult<bool> {
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
if v == -1 {
Err(PyErr::api_call_failed(self.py()))
} else {
err::error_on_minusone(self.py(), v)?;
Ok(v != 0)
}
}
/// Returns whether the object is considered to be None.
///

View File

@ -41,11 +41,11 @@ impl PyDict {
pub fn from_sequence(py: Python, seq: PyObject) -> PyResult<&PyDict> {
unsafe {
let dict = py.from_owned_ptr::<PyDict>(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)
}
}

View File

@ -200,14 +200,11 @@ mod fast_128bit_int_conversion {
$is_signed,
);
ffi::Py_DECREF(num);
if ok == -1 {
Err(PyErr::api_call_failed(ob.py()))
} else {
crate::err::error_on_minusone(ob.py(), ok)?;
Ok(<$rust_type>::from_le_bytes(buffer))
}
}
}
}
};
}

View File

@ -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<T: AsPyPointer>(&self, obj: &T) -> PyResult<bool> {
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)
}
}