use error_on_minusone in more cases

This commit is contained in:
David Hewitt 2023-07-04 20:34:23 +01:00
parent 7613f65c89
commit 43477a8e30
5 changed files with 34 additions and 38 deletions

View File

@ -1,6 +1,4 @@
use crate::types::PyString;
#[cfg(windows)]
use crate::PyErr;
use crate::{
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject,
};
@ -92,9 +90,7 @@ impl FromPyObject<'_> for OsString {
// ourselves
let size =
unsafe { ffi::PyUnicode_AsWideChar(pystring.as_ptr(), std::ptr::null_mut(), 0) };
if size == -1 {
return Err(PyErr::fetch(ob.py()));
}
crate::err::error_on_minusone(ob.py(), size)?;
let mut buffer = vec![0; size as usize];
let bytes_read =

View File

@ -9,7 +9,6 @@ use crate::{AsPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyObject, Python, ToP
use std::borrow::Cow;
use std::cell::UnsafeCell;
use std::ffi::CString;
use std::os::raw::c_int;
mod err_state;
mod impls;
@ -792,14 +791,33 @@ pub fn panic_after_error(_py: Python<'_>) -> ! {
/// Returns Ok if the error code is not -1.
#[inline]
pub fn error_on_minusone(py: Python<'_>, result: c_int) -> PyResult<()> {
if result != -1 {
pub(crate) fn error_on_minusone<T: SignedInteger>(py: Python<'_>, result: T) -> PyResult<()> {
if result != T::MINUS_ONE {
Ok(())
} else {
Err(PyErr::fetch(py))
}
}
pub(crate) trait SignedInteger: Eq {
const MINUS_ONE: Self;
}
macro_rules! impl_signed_integer {
($t:ty) => {
impl SignedInteger for $t {
const MINUS_ONE: Self = -1;
}
};
}
impl_signed_integer!(i8);
impl_signed_integer!(i16);
impl_signed_integer!(i32);
impl_signed_integer!(i64);
impl_signed_integer!(i128);
impl_signed_integer!(isize);
#[inline]
fn exceptions_must_derive_from_base_exception(py: Python<'_>) -> PyErr {
PyErr::from_state(PyErrState::exceptions_must_derive_from_base_exception(py))

View File

@ -947,24 +947,18 @@ impl PyAny {
/// This is equivalent to the Python expression `hash(self)`.
pub fn hash(&self) -> PyResult<isize> {
let v = unsafe { ffi::PyObject_Hash(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.py()))
} else {
crate::err::error_on_minusone(self.py(), v)?;
Ok(v)
}
}
/// Returns the length of the sequence or mapping.
///
/// This is equivalent to the Python expression `len(self)`.
pub fn len(&self) -> PyResult<usize> {
let v = unsafe { ffi::PyObject_Size(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.py()))
} else {
crate::err::error_on_minusone(self.py(), v)?;
Ok(v as usize)
}
}
/// Returns the list of attributes of this object.
///

View File

@ -17,12 +17,9 @@ impl PyMapping {
#[inline]
pub fn len(&self) -> PyResult<usize> {
let v = unsafe { ffi::PyMapping_Size(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.py()))
} else {
crate::err::error_on_minusone(self.py(), v)?;
Ok(v as usize)
}
}
/// Returns whether the mapping is empty.
#[inline]

View File

@ -23,12 +23,9 @@ impl PySequence {
#[inline]
pub fn len(&self) -> PyResult<usize> {
let v = unsafe { ffi::PySequence_Size(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.py()))
} else {
crate::err::error_on_minusone(self.py(), v)?;
Ok(v as usize)
}
}
/// Returns whether the sequence is empty.
#[inline]
@ -191,12 +188,9 @@ impl PySequence {
{
fn inner(seq: &PySequence, value: PyObject) -> PyResult<usize> {
let r = unsafe { ffi::PySequence_Count(seq.as_ptr(), value.as_ptr()) };
if r == -1 {
Err(PyErr::fetch(seq.py()))
} else {
crate::err::error_on_minusone(seq.py(), r)?;
Ok(r as usize)
}
}
inner(self, value.to_object(self.py()))
}
@ -231,12 +225,9 @@ impl PySequence {
{
fn inner(seq: &PySequence, value: PyObject) -> PyResult<usize> {
let r = unsafe { ffi::PySequence_Index(seq.as_ptr(), value.as_ptr()) };
if r == -1 {
Err(PyErr::fetch(seq.py()))
} else {
crate::err::error_on_minusone(seq.py(), r)?;
Ok(r as usize)
}
}
inner(self, value.to_object(self.py()))
}