Clippy stuff

This commit is contained in:
konstin 2018-08-26 21:35:53 +02:00
parent 7293cdbc6f
commit 8de0574aad
14 changed files with 93 additions and 180 deletions

View file

@ -54,7 +54,6 @@ where
T: PyBufferProtocol<'p>,
{
#[inline]
fn tp_as_buffer() -> Option<ffi::PyBufferProcs> {
Some(ffi::PyBufferProcs {
bf_getbuffer: Self::cb_bf_getbuffer(),

View file

@ -327,23 +327,22 @@ macro_rules! py_func_set {
let py = $crate::Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<$generic>(slf);
let result;
if value.is_null() {
result = Err($crate::PyErr::new::<exc::NotImplementedError, _>(format!(
let result = if value.is_null() {
Err($crate::PyErr::new::<exc::NotImplementedError, _>(format!(
"Subscript deletion not supported by {:?}",
stringify!($generic)
)));
)))
} else {
let name = py.mut_from_borrowed_ptr::<$crate::PyObjectRef>(name);
let value = py.from_borrowed_ptr::<$crate::PyObjectRef>(value);
result = match name.extract() {
match name.extract() {
Ok(name) => match value.extract() {
Ok(value) => slf.$fn_set(name, value).into(),
Err(e) => Err(e.into()),
},
Err(e) => Err(e.into()),
};
}
};
match result {
Ok(_) => 0,
Err(e) => {
@ -373,22 +372,20 @@ macro_rules! py_func_del {
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let result;
if value.is_null() {
let result = if value.is_null() {
let slf = py.mut_from_borrowed_ptr::<$generic>(slf);
let name = py.from_borrowed_ptr::<$crate::PyObjectRef>(name);
result = match name.extract() {
match name.extract() {
Ok(name) => slf.$fn_del(name).into(),
Err(e) => Err(e.into()),
};
}
} else {
result = Err(PyErr::new::<exc::NotImplementedError, _>(format!(
Err(PyErr::new::<exc::NotImplementedError, _>(format!(
"Subscript assignment not supported by {:?}",
stringify!($generic)
)));
}
)))
};
match result {
Ok(_) => 0,
Err(e) => {
@ -420,22 +417,21 @@ macro_rules! py_func_set_del {
let slf = py.mut_from_borrowed_ptr::<$generic>(slf);
let name = py.from_borrowed_ptr::<$crate::PyObjectRef>(name);
let result;
if value.is_null() {
result = match name.extract() {
let result = if value.is_null() {
match name.extract() {
Ok(name) => slf.$fn_del(name).into(),
Err(e) => Err(e.into()),
};
}
} else {
let value = py.from_borrowed_ptr::<$crate::PyObjectRef>(value);
result = match name.extract() {
match name.extract() {
Ok(name) => match value.extract() {
Ok(value) => slf.$fn_set(name, value).into(),
Err(e) => Err(e.into()),
},
Err(e) => Err(e.into()),
};
}
};
match result {
Ok(_) => 0,
Err(e) => {

View file

@ -234,19 +234,18 @@ where
let py = Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<T>(slf);
let result;
if value.is_null() {
result = Err(PyErr::new::<exc::NotImplementedError, _>(format!(
let result = if value.is_null() {
Err(PyErr::new::<exc::NotImplementedError, _>(format!(
"Item deletion not supported by {:?}",
stringify!(T)
)));
)))
} else {
let value = py.from_borrowed_ptr::<PyObjectRef>(value);
result = match value.extract() {
match value.extract() {
Ok(value) => slf.__setitem__(key as isize, value).into(),
Err(e) => Err(e),
};
}
};
match result {
Ok(_) => 0,
Err(e) => {
@ -309,15 +308,14 @@ mod sq_ass_item_impl {
let py = Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<T>(slf);
let result;
if value.is_null() {
result = slf.__delitem__(key as isize).into();
let result = if value.is_null() {
slf.__delitem__(key as isize).into()
} else {
result = Err(PyErr::new::<exc::NotImplementedError, _>(format!(
Err(PyErr::new::<exc::NotImplementedError, _>(format!(
"Item assignment not supported by {:?}",
stringify!(T)
)));
}
)))
};
match result {
Ok(_) => 0,
@ -356,16 +354,15 @@ mod sq_ass_item_impl {
let py = Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<T>(slf);
let result;
if value.is_null() {
result = slf.__delitem__(key as isize).into();
let result = if value.is_null() {
slf.__delitem__(key as isize).into()
} else {
let value = py.from_borrowed_ptr::<PyObjectRef>(value);
result = match value.extract() {
match value.extract() {
Ok(value) => slf.__setitem__(key as isize, value).into(),
Err(e) => Err(e),
};
}
};
match result {
Ok(_) => 0,
Err(e) => {

View file

@ -28,7 +28,9 @@ pub trait ToBorrowedObject: ToPyObject {
{
let ptr = self.to_object(py).into_ptr();
let result = f(ptr);
py.xdecref(ptr);
unsafe {
ffi::Py_XDECREF(ptr);
}
result
}
}

View file

@ -5,7 +5,10 @@ use std::os::raw::{
c_char, c_double, c_int, c_long, c_longlong, c_uchar, c_ulong, c_ulonglong, c_void,
};
pub enum PyLongObject {}
#[repr(C)]
pub struct PyLongObject {
_private: [u8; 0],
}
#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {

View file

@ -565,7 +565,7 @@ mod typeobject {
etype: *mut PyHeapTypeObject,
) -> *mut ffi3::structmember::PyMemberDef {
let py_type = ffi3::object::Py_TYPE(etype as *mut ffi3::object::PyObject);
let ptr = (etype as *mut c_char).offset((*py_type).tp_basicsize);
let ptr = etype.offset((*py_type).tp_basicsize);
ptr as *mut ffi3::structmember::PyMemberDef
}
}

View file

@ -90,5 +90,5 @@ pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int {
#[cfg(not(Py_LIMITED_API))]
pub unsafe fn PyObject_GET_WEAKREFS_LISTPTR(o: *mut PyObject) -> *mut *mut PyObject {
let weaklistoffset = (*Py_TYPE(o)).tp_weaklistoffset as isize;
(o as *mut u8).offset(weaklistoffset) as *mut *mut PyObject
o.offset(weaklistoffset) as *mut *mut PyObject
}

View file

@ -76,7 +76,7 @@ pub trait AsPyRef<T>: Sized {
let py = gil.python();
let result = f(py, self.as_ref(py));
py.xdecref(self.into_ptr());
py.xdecref(self);
result
}
@ -89,7 +89,7 @@ pub trait AsPyRef<T>: Sized {
let py = gil.python();
let result = f(py, self.as_mut(py));
py.xdecref(self.into_ptr());
py.xdecref(self);
result
}
}

View file

@ -172,29 +172,21 @@ impl PyObject {
A: IntoPyTuple,
{
let args = args.into_tuple(py).into_ptr();
let kwargs = kwargs.into_ptr();
let result = unsafe {
PyObject::from_owned_ptr_or_err(
py,
ffi::PyObject_Call(self.as_ptr(), args, kwargs.into_ptr()),
)
PyObject::from_owned_ptr_or_err(py, ffi::PyObject_Call(self.as_ptr(), args, kwargs))
};
py.xdecref(args);
py.xdecref(kwargs.into_ptr());
unsafe {
ffi::Py_XDECREF(args);
ffi::Py_XDECREF(kwargs);
}
result
}
/// Calls the object without arguments.
/// This is equivalent to the Python expression: 'self()'
pub fn call0(&self, py: Python) -> PyResult<PyObject> {
let args = PyTuple::empty(py).into_ptr();
let result = unsafe {
PyObject::from_owned_ptr_or_err(
py,
ffi::PyObject_Call(self.as_ptr(), args, std::ptr::null_mut()),
)
};
py.xdecref(args);
result
self.call(py, PyTuple::empty(py), None)
}
/// Calls the object.
@ -203,25 +195,17 @@ impl PyObject {
where
A: IntoPyTuple,
{
let args = args.into_tuple(py).into_ptr();
let result = unsafe {
PyObject::from_owned_ptr_or_err(
py,
ffi::PyObject_Call(self.as_ptr(), args, std::ptr::null_mut()),
)
};
py.xdecref(args);
result
self.call(py, args, None)
}
/// Calls a method on the object.
/// This is equivalent to the Python expression: 'self.name(*args, **kwargs)'
pub fn call_method<A, K>(
pub fn call_method<A>(
&self,
py: Python,
name: &str,
args: A,
kwargs: PyDict,
kwargs: Option<PyDict>,
) -> PyResult<PyObject>
where
A: IntoPyTuple,
@ -232,8 +216,8 @@ impl PyObject {
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
let result = PyObject::from_owned_ptr_or_err(py, ffi::PyObject_Call(ptr, args, kwargs));
ffi::Py_DECREF(ptr);
py.xdecref(args);
py.xdecref(kwargs);
ffi::Py_XDECREF(args);
ffi::Py_XDECREF(kwargs);
result
})
}
@ -241,17 +225,7 @@ impl PyObject {
/// Calls a method on the object.
/// This is equivalent to the Python expression: 'self.name()'
pub fn call_method0(&self, py: Python, name: &str) -> PyResult<PyObject> {
name.with_borrowed_ptr(py, |name| unsafe {
let args = PyTuple::empty(py).into_ptr();
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
let result = PyObject::from_owned_ptr_or_err(
py,
ffi::PyObject_Call(ptr, args, std::ptr::null_mut()),
);
ffi::Py_DECREF(ptr);
py.xdecref(args);
result
})
self.call_method(py, name, PyTuple::empty(py), None)
}
/// Calls a method on the object.
@ -260,17 +234,7 @@ impl PyObject {
where
A: IntoPyTuple,
{
name.with_borrowed_ptr(py, |name| unsafe {
let args = args.into_tuple(py).into_ptr();
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
let result = PyObject::from_owned_ptr_or_err(
py,
ffi::PyObject_Call(ptr, args, std::ptr::null_mut()),
);
ffi::Py_DECREF(ptr);
py.xdecref(args);
result
})
self.call_method(py, name, args, None)
}
}
@ -287,7 +251,7 @@ impl AsPyRef<PyObjectRef> for PyObject {
impl ToPyObject for PyObject {
#[inline]
fn to_object<'p>(&self, py: Python<'p>) -> PyObject {
fn to_object(&self, py: Python) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}
@ -324,7 +288,7 @@ impl IntoPyPointer for PyObject {
#[must_use]
fn into_ptr(self) -> *mut ffi::PyObject {
let ptr = self.0;
std::mem::forget(self);
std::mem::forget(self); // Avoid Drop
ptr
}
}

View file

@ -316,42 +316,27 @@ where
A: IntoPyTuple,
{
let args = args.into_tuple(self.py()).into_ptr();
let kwargs = kwargs.into_ptr();
let result = unsafe {
let return_value = ffi::PyObject_Call(self.as_ptr(), args, kwargs.into_ptr());
let return_value = ffi::PyObject_Call(self.as_ptr(), args, kwargs);
self.py().from_owned_ptr_or_err(return_value)
};
self.py().xdecref(args);
self.py().xdecref(kwargs.into_ptr());
unsafe {
ffi::Py_XDECREF(args);
ffi::Py_XDECREF(kwargs);
}
result
}
fn call0(&self) -> PyResult<&PyObjectRef> {
let args = PyTuple::empty(self.py()).into_ptr();
let result = unsafe {
self.py().from_owned_ptr_or_err(ffi::PyObject_Call(
self.as_ptr(),
args,
std::ptr::null_mut(),
))
};
self.py().xdecref(args);
result
self.call(PyTuple::empty(self.py()), None)
}
fn call1<A>(&self, args: A) -> PyResult<&PyObjectRef>
where
A: IntoPyTuple,
{
let args = args.into_tuple(self.py()).into_ptr();
let result = unsafe {
self.py().from_owned_ptr_or_err(ffi::PyObject_Call(
self.as_ptr(),
args,
std::ptr::null_mut(),
))
};
self.py().xdecref(args);
result
self.call(args, None)
}
fn call_method<A>(&self, name: &str, args: A, kwargs: Option<PyDict>) -> PyResult<&PyObjectRef>
@ -368,46 +353,18 @@ where
let result_ptr = ffi::PyObject_Call(ptr, args, kwargs);
let result = self.py().from_owned_ptr_or_err(result_ptr);
ffi::Py_DECREF(ptr);
self.py().xdecref(args);
self.py().xdecref(kwargs);
ffi::Py_XDECREF(args);
ffi::Py_XDECREF(kwargs);
result
})
}
fn call_method0(&self, name: &str) -> PyResult<&PyObjectRef> {
name.with_borrowed_ptr(self.py(), |name| unsafe {
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
if ptr.is_null() {
return Err(PyErr::fetch(self.py()));
}
let args = PyTuple::empty(self.py()).into_ptr();
let result = self.py().from_owned_ptr_or_err(ffi::PyObject_Call(
ptr,
args,
std::ptr::null_mut(),
));
ffi::Py_DECREF(ptr);
self.py().xdecref(args);
result
})
self.call_method(name, PyTuple::empty(self.py()), None)
}
fn call_method1<A: IntoPyTuple>(&self, name: &str, args: A) -> PyResult<&PyObjectRef> {
name.with_borrowed_ptr(self.py(), |name| unsafe {
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
if ptr.is_null() {
return Err(PyErr::fetch(self.py()));
}
let args = args.into_tuple(self.py()).into_ptr();
let result = self.py().from_owned_ptr_or_err(ffi::PyObject_Call(
ptr,
args,
std::ptr::null_mut(),
));
ffi::Py_DECREF(ptr);
self.py().xdecref(args);
result
})
self.call_method(name, args, None)
}
fn hash(&self) -> PyResult<isize> {

View file

@ -57,9 +57,9 @@ impl PyDate {
pub fn new(py: Python, year: i32, month: u8, day: u8) -> PyResult<Py<PyDate>> {
unsafe {
let ptr = (PyDateTimeAPI.Date_FromDate)(
year as c_int,
month as c_int,
day as c_int,
year,
c_int::from(month),
c_int::from(day),
PyDateTimeAPI.DateType,
);
Py::from_owned_ptr_or_err(py, ptr)
@ -106,12 +106,12 @@ impl PyDateTime {
) -> PyResult<Py<PyDateTime>> {
unsafe {
let ptr = (PyDateTimeAPI.DateTime_FromDateAndTime)(
year as c_int,
month as c_int,
day as c_int,
hour as c_int,
minute as c_int,
second as c_int,
year,
c_int::from(month),
c_int::from(day),
c_int::from(hour),
c_int::from(minute),
c_int::from(second),
microsecond as c_int,
opt_to_pyobj(py, tzinfo),
PyDateTimeAPI.DateTimeType,
@ -188,9 +188,9 @@ impl PyTime {
) -> PyResult<Py<PyTime>> {
unsafe {
let ptr = (PyDateTimeAPI.Time_FromTime)(
hour as c_int,
minute as c_int,
second as c_int,
c_int::from(hour),
c_int::from(minute),
c_int::from(second),
microsecond as c_int,
opt_to_pyobj(py, tzinfo),
PyDateTimeAPI.TimeType,
@ -211,9 +211,9 @@ impl PyTime {
) -> PyResult<Py<PyTime>> {
unsafe {
let ptr = (PyDateTimeAPI.Time_FromTimeAndFold)(
hour as c_int,
minute as c_int,
second as c_int,
c_int::from(hour),
c_int::from(minute),
c_int::from(second),
microsecond as c_int,
opt_to_pyobj(py, tzinfo),
fold as c_int,

View file

@ -279,7 +279,7 @@ impl PyTryFrom for PySequence {
fn try_from(value: &PyObjectRef) -> Result<&PySequence, PyDowncastError> {
unsafe {
if ffi::PySequence_Check(value.as_ptr()) != 0 {
let ptr = value as *const _ as *mut u8 as *mut PySequence;
let ptr = value as *const _ as *mut PySequence;
Ok(&*ptr)
} else {
Err(PyDowncastError)
@ -294,7 +294,7 @@ impl PyTryFrom for PySequence {
fn try_from_mut(value: &PyObjectRef) -> Result<&mut PySequence, PyDowncastError> {
unsafe {
if ffi::PySequence_Check(value.as_ptr()) != 0 {
let ptr = value as *const _ as *mut u8 as *mut PySequence;
let ptr = value as *const _ as *mut PySequence;
Ok(&mut *ptr)
} else {
Err(PyDowncastError)

View file

@ -96,7 +96,7 @@ where
#[inline]
fn into_ptr(self) -> *mut ffi::PyObject {
let ptr = self.as_ptr();
if ptr != std::ptr::null_mut() {
if !ptr.is_null() {
unsafe {
ffi::Py_INCREF(ptr);
}
@ -474,12 +474,9 @@ impl<'p> Python<'p> {
}
/// Release `ffi::PyObject` pointer.
/// Undefined behavior if the pointer is invalid.
#[inline]
pub fn xdecref(self, ptr: *mut ffi::PyObject) {
if !ptr.is_null() {
unsafe { ffi::Py_DECREF(ptr) };
}
pub fn xdecref<T: IntoPyPointer>(self, ptr: T) {
unsafe { ffi::Py_XDECREF(ptr.into_ptr()) };
}
}

View file

@ -508,8 +508,6 @@ fn py_class_method_defs<T>() -> PyResult<(
let mut new = None;
let mut init = None;
//<T as class::methods::PyPropMethodsProtocolImpl>::py_methods()
for def in <T as class::methods::PyMethodsProtocolImpl>::py_methods() {
match *def {
PyMethodDefType::New(ref def) => {