Fix some clippy warnings (#49)

This commit is contained in:
messense 2017-07-18 19:28:49 +08:00 committed by GitHub
parent 6bbf3d7595
commit e69163344a
21 changed files with 70 additions and 70 deletions

View File

@ -99,7 +99,7 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
}
impl std::convert::AsRef<PyObjectRef> for #cls {
fn as_ref(&self) -> &_pyo3::PyObjectRef {
unsafe{std::mem::transmute(self.as_ptr())}
unsafe{&*(self.as_ptr() as *const pyo3::PyObjectRef)}
}
}
impl<'a> std::convert::From<&'a mut #cls> for &'a #cls
@ -235,13 +235,13 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
{
let offset = <#cls as _pyo3::typeob::PyTypeInfo>::offset();
let ptr = (ob.as_ptr() as *mut u8).offset(offset) as *mut #cls;
std::mem::transmute(ptr)
&*ptr
}
unsafe fn unchecked_mut_downcast_from(ob: &_pyo3::PyObjectRef) -> &mut Self
{
let offset = <#cls as _pyo3::typeob::PyTypeInfo>::offset();
let ptr = (ob.as_ptr() as *mut u8).offset(offset) as *mut #cls;
std::mem::transmute(ptr)
&mut *ptr
}
}
impl _pyo3::PyMutDowncastFrom for #cls

View File

@ -102,7 +102,7 @@ pub fn parse_args<'p>(py: Python<'p>,
#[inline]
#[doc(hidden)]
pub unsafe fn get_kwargs<'p>(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&PyDict> {
pub unsafe fn get_kwargs(py: Python, ptr: *mut ffi::PyObject) -> Option<&PyDict> {
if ptr.is_null() {
None
} else {

View File

@ -112,7 +112,7 @@ impl PySetterDef {
dst.name = CString::new(self.name).expect(
"Method name must not contain NULL byte").into_raw();
}
dst.set = Some(self.meth.clone());
dst.set = Some(self.meth);
}
}

View File

@ -197,7 +197,7 @@ impl PyErr {
}
fn new_helper(_py: Python, ty: &PyType, value: PyObject) -> PyErr {
assert!(unsafe { ffi::PyExceptionClass_Check(ty.as_ptr()) } != 0);
assert_ne!(unsafe { ffi::PyExceptionClass_Check(ty.as_ptr()) }, 0);
PyErr {
ptype: ty.into(),
pvalue: Some(value),
@ -214,7 +214,7 @@ impl PyErr {
PyErr::from_instance_helper(py, obj.into_object(py))
}
fn from_instance_helper<'p>(py: Python, obj: PyObject) -> PyErr {
fn from_instance_helper(py: Python, obj: PyObject) -> PyErr {
let ptr = obj.as_ptr();
if unsafe { ffi::PyExceptionInstance_Check(ptr) } != 0 {
@ -418,7 +418,7 @@ impl ToPyErr for io::Error {
let errno = self.raw_os_error().unwrap_or(0);
let errdesc = self.description();
PyErr::new_err(py, &tp, (errno, errdesc))
PyErr::new_err(py, tp, (errno, errdesc))
}
}

View File

@ -19,7 +19,7 @@ pub struct PyToken(PhantomData<Rc<()>>);
impl PyToken {
#[inline(always)]
pub fn py<'p>(&'p self) -> Python<'p> {
pub fn py(&self) -> Python {
unsafe { Python::assume_gil_acquired() }
}
}
@ -173,7 +173,7 @@ impl<T> Py<T> where T: PyTypeInfo,
/// Create new instance of `T` and move under python management.
/// Returns references to `T`
pub fn new_ref<'p, F>(py: Python<'p>, f: F) -> PyResult<&'p T>
pub fn new_ref<F>(py: Python, f: F) -> PyResult<&T>
where F: FnOnce(::PyToken) -> T,
T: PyObjectAlloc<T> + PyDowncastFrom
{
@ -187,7 +187,7 @@ impl<T> Py<T> where T: PyTypeInfo,
/// Create new instance of `T` and move under python management.
/// Returns mutable references to `T`
pub fn new_mut<'p, F>(py: Python<'p>, f: F) -> PyResult<&'p mut T>
pub fn new_mut<F>(py: Python, f: F) -> PyResult<&mut T>
where F: FnOnce(::PyToken) -> T,
T: PyObjectAlloc<T> + PyDowncastFrom
{
@ -229,7 +229,7 @@ impl<T> AsPyRef<T> for Py<T> where T: PyTypeInfo + PyNativeType {
}
#[inline]
fn as_mut(&self, _py: Python) -> &mut T {
unsafe {std::mem::transmute(self as *const _ as *mut T)}
unsafe { &mut *(self as *const _ as *mut T) }
}
}
@ -326,8 +326,8 @@ impl<'a, T> std::convert::From<&'a mut T> for PyObject
impl<T> PyDowncastInto for Py<T> where T: PyTypeInfo
{
fn downcast_into<'p, I>(py: Python<'p>, ob: I)
-> Result<Self, PyDowncastError<'p>>
fn downcast_into<I>(py: Python, ob: I)
-> Result<Self, PyDowncastError>
where I: IntoPyPointer
{
unsafe{
@ -341,8 +341,8 @@ impl<T> PyDowncastInto for Py<T> where T: PyTypeInfo
}
}
fn downcast_into_from_ptr<'p>(py: Python<'p>, ptr: *mut ffi::PyObject)
-> Result<Self, PyDowncastError<'p>>
fn downcast_into_from_ptr(py: Python, ptr: *mut ffi::PyObject)
-> Result<Self, PyDowncastError>
{
unsafe{
if T::is_instance(ptr) {
@ -354,7 +354,7 @@ impl<T> PyDowncastInto for Py<T> where T: PyTypeInfo
}
}
fn unchecked_downcast_into<'p, I>(ob: I) -> Self
fn unchecked_downcast_into<I>(ob: I) -> Self
where I: IntoPyPointer
{
unsafe{

View File

@ -112,8 +112,8 @@ impl PyObject {
/// Transmutes a slice of owned FFI pointers to `&[PyObject]`.
/// Undefined behavior if any pointer in the slice is NULL or invalid.
#[inline]
pub unsafe fn borrow_from_owned_ptr_slice<'a>(ptr: &'a [*mut ffi::PyObject])
-> &'a [PyObject] {
pub unsafe fn borrow_from_owned_ptr_slice(ptr: &[*mut ffi::PyObject])
-> &[PyObject] {
std::mem::transmute(ptr)
}

View File

@ -115,7 +115,7 @@ pub trait ObjectProtocol {
/// Takes an object and returns an iterator for it.
/// This is typically a new iterator but if the argument
/// is an iterator, this returns itself.
fn iter<'p>(&'p self) -> PyResult<PyIterator<'p>>;
fn iter(&self) -> PyResult<PyIterator>;
/// Gets the Python type object for this object's type.
fn get_type(&self) -> &PyType;

View File

@ -14,7 +14,7 @@ pyobject_nativetype!(PyBool, PyBool_Type, PyBool_Check);
impl PyBool {
/// Depending on `val`, returns `py.True()` or `py.False()`.
#[inline]
pub fn new<'p>(py: Python<'p>, val: bool) -> &'p PyBool {
pub fn new(py: Python, val: bool) -> &PyBool {
unsafe {
py.cast_from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() })
}

View File

@ -30,7 +30,7 @@ impl PyByteArray {
/// Creates a new Python bytearray object
/// from other PyObject, that implements the buffer protocol.
pub fn from<'p, I>(py: Python<'p>, src: I) -> PyResult<&'p PyByteArray>
pub fn from<I>(py: Python, src: I) -> PyResult<&PyByteArray>
where I: ToPyPointer
{
unsafe {

View File

@ -23,7 +23,7 @@ impl PyDict {
/// Creates a new empty dictionary.
///
/// May panic when running out of memory.
pub fn new<'p>(py: Python<'p>) -> &'p PyDict {
pub fn new(py: Python) -> &PyDict {
unsafe {
py.cast_from_ptr::<PyDict>(ffi::PyDict_New())
}

View File

@ -25,7 +25,7 @@ macro_rules! exc_type(
fn init_type(_py: Python) {}
#[inline]
fn type_object<'p>(py: $crate::python::Python<'p>) -> &'p $crate::PyType {
fn type_object(py: $crate::python::Python) -> &$crate::PyType {
unsafe { PyType::from_type_ptr(py, ffi::$exc_name as *mut ffi::PyTypeObject) }
}
}
@ -129,7 +129,7 @@ impl UnicodeDecodeError {
}
}
pub fn new_utf8<'p>(py: Python, input: &[u8], err: std::str::Utf8Error)
pub fn new_utf8(py: Python, input: &[u8], err: std::str::Utf8Error)
-> PyResult<PyObject>
{
let pos = err.valid_up_to();

View File

@ -30,7 +30,7 @@ impl PyList {
}
/// Construct a new empty list.
pub fn empty<'p>(py: Python<'p>) -> &'p PyList {
pub fn empty(py: Python) -> &PyList {
unsafe {
py.cast_from_ptr::<PyList>(ffi::PyList_New(0))
}

View File

@ -97,7 +97,7 @@ macro_rules! pyobject_nativetype(
}
impl $crate::PyObjectWithToken for $name {
#[inline(always)]
fn py<'p>(&'p self) -> $crate::Python<'p> {
fn py(&self) -> $crate::Python {
unsafe { $crate::Python::assume_gil_acquired() }
}
}
@ -165,7 +165,7 @@ macro_rules! pyobject_nativetype(
fn init_type(_py: $crate::Python) {}
#[inline]
fn type_object<'p>(py: $crate::Python<'p>) -> &'p $crate::PyType {
fn type_object(py: $crate::Python) -> &$crate::PyType {
unsafe { $crate::PyType::from_type_ptr(py, &mut $crate::ffi::$typeobject) }
}
}
@ -173,7 +173,7 @@ macro_rules! pyobject_nativetype(
impl $crate::ToPyObject for $name
{
#[inline]
fn to_object<'p>(&self, py: $crate::Python<'p>) -> $crate::PyObject {
fn to_object(&self, py: $crate::Python) -> $crate::PyObject {
unsafe {$crate::PyObject::from_borrowed_ptr(py, self.0.as_ptr())}
}
@ -188,7 +188,7 @@ macro_rules! pyobject_nativetype(
impl<'a> $crate::IntoPyObject for &'a $name
{
#[inline]
fn into_object<'p>(self, py: $crate::Python) -> $crate::PyObject {
fn into_object(self, py: $crate::Python) -> $crate::PyObject {
unsafe { $crate::PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}

View File

@ -51,7 +51,7 @@ impl PyModule {
}
}
unsafe fn str_from_ptr<'a>(&'a self, ptr: *const c_char) -> PyResult<&'a str> {
unsafe fn str_from_ptr(&self, ptr: *const c_char) -> PyResult<&str> {
if ptr.is_null() {
Err(PyErr::fetch(self.py()))
} else {
@ -68,14 +68,14 @@ impl PyModule {
/// Gets the module name.
///
/// May fail if the module does not have a `__name__` attribute.
pub fn name<'a>(&'a self) -> PyResult<&'a str> {
pub fn name(&self) -> PyResult<&str> {
unsafe { self.str_from_ptr(ffi::PyModule_GetName(self.as_ptr())) }
}
/// Gets the module filename.
///
/// May fail if the module does not have a `__file__` attribute.
pub fn filename<'a>(&'a self) -> PyResult<&'a str> {
pub fn filename(&self) -> PyResult<&str> {
unsafe { self.str_from_ptr(ffi::PyModule_GetFilename(self.as_ptr())) }
}

View File

@ -81,7 +81,7 @@ macro_rules! int_fits_larger_int(
);
fn err_if_invalid_value<'p, T: PartialEq>
fn err_if_invalid_value<T: PartialEq>
(py: Python, invalid_value: T, actual_value: T) -> PyResult<T>
{
if actual_value == invalid_value && PyErr::occurred(py) {

View File

@ -40,7 +40,7 @@ impl PySliceIndices {
impl PySlice {
/// Construct a new slice with the given elements.
pub fn new<'p>(py: Python<'p>, start: isize, stop: isize, step: isize) -> &'p PySlice {
pub fn new(py: Python, start: isize, stop: isize, step: isize) -> &PySlice {
unsafe {
let ptr = ffi::PySlice_New(ffi::PyLong_FromLong(start as i64),
ffi::PyLong_FromLong(stop as i64),

View File

@ -65,7 +65,7 @@ impl PyTuple {
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [PyObject] {
pub fn as_slice(&self) -> &[PyObject] {
// This is safe because PyObject has the same memory layout as *mut ffi::PyObject,
// and because tuples are immutable.
// (We don't even need a Python token, thanks to immutability)

View File

@ -30,13 +30,13 @@ impl PyType {
/// This increments the reference count on the type object.
/// Undefined behavior if the pointer is NULL or invalid.
#[inline]
pub unsafe fn from_type_ptr<'p>(py: Python<'p>, p: *mut ffi::PyTypeObject) -> &'p PyType
pub unsafe fn from_type_ptr(py: Python, p: *mut ffi::PyTypeObject) -> &PyType
{
py.cast_from_borrowed_ptr::<PyType>(p as *mut ffi::PyObject)
}
/// Gets the name of the PyType.
pub fn name<'a>(&'a self) -> Cow<'a, str> {
pub fn name(&self) -> Cow<str> {
unsafe {
CStr::from_ptr((*self.as_type_ptr()).tp_name).to_string_lossy()
}

View File

@ -54,15 +54,15 @@ pub trait PyMutDowncastFrom : Sized {
pub trait PyDowncastInto : Sized {
/// Cast Self to a concrete Python object type.
fn downcast_into<'p, I>(Python<'p>, I) -> Result<Self, PyDowncastError<'p>>
fn downcast_into<I>(Python, I) -> Result<Self, PyDowncastError>
where I: ToPyPointer + IntoPyPointer;
/// Cast from ffi::PyObject to a concrete Python object type.
fn downcast_into_from_ptr<'p>(py: Python<'p>, ptr: *mut ffi::PyObject)
-> Result<Self, PyDowncastError<'p>>;
fn downcast_into_from_ptr(py: Python, ptr: *mut ffi::PyObject)
-> Result<Self, PyDowncastError>;
/// Cast from ffi::PyObject to a concrete Python object type.
fn unchecked_downcast_into<'p, I>(I) -> Self where I: IntoPyPointer;
fn unchecked_downcast_into<I>(I) -> Self where I: IntoPyPointer;
}
/// This trait allows retrieving the underlying FFI pointer from Python objects.
@ -84,7 +84,7 @@ impl<'p, T> ToPyPointer for Option<&'p T> where T: ToPyPointer {
#[inline]
default fn as_ptr(&self) -> *mut ffi::PyObject {
match *self {
Some(ref t) => t.as_ptr(),
Some(t) => t.as_ptr(),
None => std::ptr::null_mut()
}
}

View File

@ -42,10 +42,10 @@ pub fn prepare_freethreaded_python() {
if ffi::Py_IsInitialized() != 0 {
// If Python is already initialized, we expect Python threading to also be initialized,
// as we can't make the existing Python main thread acquire the GIL.
assert!(ffi::PyEval_ThreadsInitialized() != 0);
assert_ne!(ffi::PyEval_ThreadsInitialized(), 0);
} else {
// If Python isn't initialized yet, we expect that Python threading isn't initialized either.
assert!(ffi::PyEval_ThreadsInitialized() == 0);
assert_eq!(ffi::PyEval_ThreadsInitialized(), 0);
// Initialize Python.
// We use Py_InitializeEx() with initsigs=0 to disable Python signal handling.
// Signal handling depends on the notion of a 'main thread', which doesn't exist in this case.
@ -99,7 +99,7 @@ pub struct GILGuard {
impl Drop for GILGuard {
fn drop(&mut self) {
unsafe {
let pool: &'static mut Pointers = mem::transmute(POINTERS);
let pool: &'static mut Pointers = &mut *POINTERS;
pool.drain(self.owned, self.borrowed, true);
ffi::PyGILState_Release(self.gstate);
@ -130,7 +130,7 @@ impl Pointers {
// vec of pointers
let ptr = *v;
let vec: &'static mut Vec<*mut ffi::PyObject> = mem::transmute(ptr);
let vec: &'static mut Vec<*mut ffi::PyObject> = &mut *ptr;
if vec.is_empty() {
return
}
@ -179,7 +179,7 @@ pub struct Pool {
impl Pool {
#[inline]
pub unsafe fn new() -> Pool {
let p: &'static mut Pointers = mem::transmute(POINTERS);
let p: &'static mut Pointers = &mut *POINTERS;
Pool {owned: p.owned.len(),
borrowed: p.borrowed.len(),
pointers: true,
@ -187,7 +187,7 @@ impl Pool {
}
#[inline]
pub unsafe fn new_no_pointers() -> Pool {
let p: &'static mut Pointers = mem::transmute(POINTERS);
let p: &'static mut Pointers = &mut *POINTERS;
Pool {owned: p.owned.len(),
borrowed: p.borrowed.len(),
pointers: false,
@ -198,7 +198,7 @@ impl Pool {
impl Drop for Pool {
fn drop(&mut self) {
unsafe {
let pool: &'static mut Pointers = mem::transmute(POINTERS);
let pool: &'static mut Pointers = &mut *POINTERS;
pool.drain(self.owned, self.borrowed, self.pointers);
}
}
@ -207,23 +207,23 @@ impl Drop for Pool {
pub unsafe fn register_pointer(obj: *mut ffi::PyObject)
{
let pool: &'static mut Pointers = mem::transmute(POINTERS);
let pool: &'static mut Pointers = &mut *POINTERS;
let v = pool.p.lock();
let pool: &'static mut Vec<*mut ffi::PyObject> = mem::transmute(*v);
let mut v = pool.p.lock();
let pool: &'static mut Vec<*mut ffi::PyObject> = &mut *(*v);
pool.push(obj);
}
pub unsafe fn register_owned<'p>(_py: Python<'p>, obj: *mut ffi::PyObject) -> &'p PyObjectRef
pub unsafe fn register_owned(_py: Python, obj: *mut ffi::PyObject) -> &PyObjectRef
{
let pool: &'static mut Pointers = mem::transmute(POINTERS);
pool.owned.push(obj);
mem::transmute(&pool.owned[pool.owned.len()-1])
}
pub unsafe fn register_borrowed<'p>(_py: Python<'p>, obj: *mut ffi::PyObject) -> &'p PyObjectRef
pub unsafe fn register_borrowed(_py: Python, obj: *mut ffi::PyObject) -> &PyObjectRef
{
let pool: &'static mut Pointers = mem::transmute(POINTERS);
let pool: &'static mut Pointers = &mut *POINTERS;
pool.borrowed.push(obj);
mem::transmute(&pool.borrowed[pool.borrowed.len()-1])
}
@ -238,7 +238,7 @@ impl GILGuard {
unsafe {
let gstate = ffi::PyGILState_Ensure(); // acquire GIL
let pool: &'static mut Pointers = mem::transmute(POINTERS);
let pool: &'static mut Pointers = &mut *POINTERS;
GILGuard { owned: pool.owned.len(),
borrowed: pool.borrowed.len(),
gstate: gstate,
@ -248,7 +248,7 @@ impl GILGuard {
/// Retrieves the marker type that proves that the GIL was acquired.
#[inline]
pub fn python<'p>(&'p self) -> Python<'p> {
pub fn python(&self) -> Python {
unsafe { Python::assume_gil_acquired() }
}
}

View File

@ -126,7 +126,7 @@ pub trait PyTypeObject {
fn init_type(py: Python);
/// Retrieves the type object for this Python object type.
fn type_object<'p>(py: Python<'p>) -> &'p PyType;
fn type_object(py: Python) -> &PyType;
}
@ -144,7 +144,7 @@ impl<T> PyTypeObject for T where T: PyObjectAlloc<T> + PyTypeInfo {
}
#[inline]
default fn type_object<'p>(py: Python<'p>) -> &'p PyType {
default fn type_object(py: Python) -> &PyType {
<T as PyTypeObject>::init_type(py);
unsafe { PyType::from_type_ptr(py, <T as PyTypeInfo>::type_object()) }
@ -305,26 +305,26 @@ fn py_class_method_defs<T>() -> PyResult<(Option<ffi::newfunc>,
let mut new = None;
for def in <T as class::methods::PyMethodsProtocolImpl>::py_methods() {
match def {
&PyMethodDefType::New(ref def) => {
match *def {
PyMethodDefType::New(ref def) => {
if let class::methods::PyMethodType::PyNewFunc(meth) = def.ml_meth {
new = Some(meth)
}
},
&PyMethodDefType::Call(ref def) => {
PyMethodDefType::Call(ref def) => {
if let class::methods::PyMethodType::PyCFunctionWithKeywords(meth) = def.ml_meth {
call = Some(meth)
} else {
panic!("Method type is not supoorted by tp_call slot")
}
}
&PyMethodDefType::Method(ref def) => {
PyMethodDefType::Method(ref def) => {
defs.push(def.as_method_def());
}
&PyMethodDefType::Class(ref def) => {
PyMethodDefType::Class(ref def) => {
defs.push(def.as_method_def());
}
&PyMethodDefType::Static(ref def) => {
PyMethodDefType::Static(ref def) => {
defs.push(def.as_method_def());
}
_ => (),
@ -366,8 +366,8 @@ fn py_class_properties<T>() -> Vec<ffi::PyGetSetDef> {
let mut defs = HashMap::new();
for def in <T as class::methods::PyMethodsProtocolImpl>::py_methods() {
match def {
&PyMethodDefType::Getter(ref getter) => {
match *def {
PyMethodDefType::Getter(ref getter) => {
let name = getter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
@ -375,7 +375,7 @@ fn py_class_properties<T>() -> Vec<ffi::PyGetSetDef> {
let def = defs.get_mut(&name).expect("Failed to call get_mut");
getter.copy_to(def);
},
&PyMethodDefType::Setter(ref setter) => {
PyMethodDefType::Setter(ref setter) => {
let name = setter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
@ -387,5 +387,5 @@ fn py_class_properties<T>() -> Vec<ffi::PyGetSetDef> {
}
}
defs.values().map(|i| i.clone()).collect()
defs.values().cloned().collect()
}