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 // Copyright (c) 2017-present PyO3 Project and Contributors
use crate::conversion::{PyTryFrom, ToBorrowedObject}; use crate::conversion::{PyTryFrom, ToBorrowedObject};
use crate::err::{PyDowncastError, PyErr, PyResult}; use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::gil; use crate::gil;
use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell}; use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell};
use crate::types::{PyDict, PyTuple}; use crate::types::{PyDict, PyTuple};
@ -458,11 +458,8 @@ impl<T> Py<T> {
/// This is equivalent to the Python expression `bool(self)`. /// This is equivalent to the Python expression `bool(self)`.
pub fn is_true(&self, py: Python) -> PyResult<bool> { pub fn is_true(&self, py: Python) -> PyResult<bool> {
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) }; let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
if v == -1 { err::error_on_minusone(py, v)?;
Err(PyErr::api_call_failed(py)) Ok(v != 0)
} else {
Ok(v != 0)
}
} }
/// Extracts some type from the Python object. /// Extracts some type from the Python object.

View File

@ -2,7 +2,7 @@
// //
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython // 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::gil::{self, GILGuard, GILPool};
use crate::type_object::{PyTypeInfo, PyTypeObject}; use crate::type_object::{PyTypeInfo, PyTypeObject};
use crate::types::{PyAny, PyDict, PyModule, PyType}; use crate::types::{PyAny, PyDict, PyModule, PyType};
@ -622,11 +622,7 @@ impl<'p> Python<'p> {
/// [2]: https://docs.python.org/3/library/signal.html /// [2]: https://docs.python.org/3/library/signal.html
pub fn check_signals(self) -> PyResult<()> { pub fn check_signals(self) -> PyResult<()> {
let v = unsafe { ffi::PyErr_CheckSignals() }; let v = unsafe { ffi::PyErr_CheckSignals() };
if v == -1 { err::error_on_minusone(self, v)
Err(PyErr::api_call_failed(self))
} else {
Ok(())
}
} }
/// Retrieves a Python instance under the assumption that the GIL is already /// 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::pyclass::PyClass;
use crate::types::{PyAny, PyType}; use crate::types::{PyAny, PyType};
use crate::{conversion::IntoPyPointer, PyMethodDefType}; 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 parking_lot::{const_mutex, Mutex};
use std::thread::{self, ThreadId}; use std::thread::{self, ThreadId};
@ -190,9 +190,7 @@ fn initialize_tp_dict(
// the POV of other threads. // the POV of other threads.
for (key, val) in items { for (key, val) in items {
let ret = unsafe { ffi::PyObject_SetAttrString(type_object, key.as_ptr(), val.into_ptr()) }; let ret = unsafe { ffi::PyObject_SetAttrString(type_object, key.as_ptr(), val.into_ptr()) };
if ret < 0 { crate::err::error_on_minusone(py, ret)?;
return Err(PyErr::api_call_failed(py));
}
} }
Ok(()) Ok(())
} }

View File

@ -516,11 +516,8 @@ impl PyAny {
/// This is equivalent to the Python expression `bool(self)`. /// This is equivalent to the Python expression `bool(self)`.
pub fn is_true(&self) -> PyResult<bool> { pub fn is_true(&self) -> PyResult<bool> {
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) }; let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
if v == -1 { err::error_on_minusone(self.py(), v)?;
Err(PyErr::api_call_failed(self.py())) Ok(v != 0)
} else {
Ok(v != 0)
}
} }
/// Returns whether the object is considered to be None. /// 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> { pub fn from_sequence(py: Python, seq: PyObject) -> PyResult<&PyDict> {
unsafe { unsafe {
let dict = py.from_owned_ptr::<PyDict>(ffi::PyDict_New()); let dict = py.from_owned_ptr::<PyDict>(ffi::PyDict_New());
match ffi::PyDict_MergeFromSeq2(dict.into_ptr(), seq.into_ptr(), 1i32) { err::error_on_minusone(
0 => Ok(dict), py,
-1 => Err(PyErr::api_call_failed(py)), ffi::PyDict_MergeFromSeq2(dict.into_ptr(), seq.into_ptr(), 1),
_ => unreachable!(), )?;
} Ok(dict)
} }
} }

View File

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

View File

@ -2,7 +2,7 @@
// //
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython // 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::instance::PyNativeType;
use crate::type_object::PyTypeObject; use crate::type_object::PyTypeObject;
use crate::{ffi, AsPyPointer, PyAny, Python}; use crate::{ffi, AsPyPointer, PyAny, Python};
@ -50,13 +50,8 @@ impl PyType {
{ {
let result = let result =
unsafe { ffi::PyObject_IsSubclass(self.as_ptr(), T::type_object(self.py()).as_ptr()) }; unsafe { ffi::PyObject_IsSubclass(self.as_ptr(), T::type_object(self.py()).as_ptr()) };
if result == -1 { err::error_on_minusone(self.py(), result)?;
Err(PyErr::api_call_failed(self.py())) Ok(result == 1)
} else if result == 1 {
Ok(true)
} else {
Ok(false)
}
} }
/// Check whether `obj` is an instance of `self`. /// Check whether `obj` is an instance of `self`.
@ -64,12 +59,7 @@ impl PyType {
/// Equivalent to Python's `isinstance` function. /// Equivalent to Python's `isinstance` function.
pub fn is_instance<T: AsPyPointer>(&self, obj: &T) -> PyResult<bool> { pub fn is_instance<T: AsPyPointer>(&self, obj: &T) -> PyResult<bool> {
let result = unsafe { ffi::PyObject_IsInstance(obj.as_ptr(), self.as_ptr()) }; let result = unsafe { ffi::PyObject_IsInstance(obj.as_ptr(), self.as_ptr()) };
if result == -1 { err::error_on_minusone(self.py(), result)?;
Err(PyErr::api_call_failed(self.py())) Ok(result == 1)
} else if result == 1 {
Ok(true)
} else {
Ok(false)
}
} }
} }