Skip to content

Commit

Permalink
Merge pull request #186 from kngwyu/modify-pyobject-macro
Browse files Browse the repository at this point in the history
Modify pyobject_native_type to take path instead of ident
  • Loading branch information
konstin committed Jul 1, 2018
2 parents 9ce5e8c + 41f4d1d commit c22bec6
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ macro_rules! wrap_function (
};
);

mod python;
pub mod python;
mod err;
mod conversion;
mod instance;
Expand Down
2 changes: 1 addition & 1 deletion src/objects/boolobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use conversion::{ToPyObject, IntoPyObject, ToBorrowedObject, PyTryFrom};
/// Represents a Python `bool`.
pub struct PyBool(PyObject);

pyobject_native_type!(PyBool, PyBool_Type, PyBool_Check);
pyobject_native_type!(PyBool, ffi::PyBool_Type, ffi::PyBool_Check);


impl PyBool {
Expand Down
2 changes: 1 addition & 1 deletion src/objects/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use err::{PyResult, PyErr};
/// Represents a Python `bytearray`.
pub struct PyByteArray(PyObject);

pyobject_native_type!(PyByteArray, PyByteArray_Type, PyByteArray_Check);
pyobject_native_type!(PyByteArray, ffi::PyByteArray_Type, ffi::PyByteArray_Check);

impl PyByteArray {
/// Creates a new Python bytearray object.
Expand Down
2 changes: 1 addition & 1 deletion src/objects/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use err::{self, PyResult, PyErr};
/// Represents a Python `dict`.
pub struct PyDict(PyObject);

pyobject_native_type!(PyDict, PyDict_Type, PyDict_Check);
pyobject_native_type!(PyDict, ffi::PyDict_Type, ffi::PyDict_Check);


impl PyDict {
Expand Down
2 changes: 1 addition & 1 deletion src/objects/floatob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use objectprotocol::ObjectProtocol;
/// with `f32`/`f64`.
pub struct PyFloat(PyObject);

pyobject_native_type!(PyFloat, PyFloat_Type, PyFloat_Check);
pyobject_native_type!(PyFloat, ffi::PyFloat_Type, ffi::PyFloat_Check);


impl PyFloat {
Expand Down
2 changes: 1 addition & 1 deletion src/objects/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use conversion::{ToPyObject, IntoPyObject, ToBorrowedObject};
/// Represents a Python `list`.
pub struct PyList(PyObject);

pyobject_native_type!(PyList, PyList_Type, PyList_Check);
pyobject_native_type!(PyList, ffi::PyList_Type, ffi::PyList_Check);

impl PyList {
/// Construct a new list with the given elements.
Expand Down
48 changes: 24 additions & 24 deletions src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ pub use self::num2::{PyInt, PyLong};
/// parameter
#[macro_export]
macro_rules! pyobject_downcast(
($name: ident, $checkfunction: ident) => (
($name: ident, $checkfunction: path) => (
impl<'a> $crate::FromPyObject<'a> for &'a $name
{
/// Extracts `Self` from the source `PyObject`.
#[cfg_attr(feature = "cargo-clippy", allow(useless_transmute))]
fn extract(ob: &'a $crate::PyObjectRef) -> $crate::PyResult<Self>
{
unsafe {
if $crate::ffi::$checkfunction(ob.as_ptr()) != 0 {
Ok($crate::std::mem::transmute(ob))
if $checkfunction(ob.as_ptr()) != 0 {
Ok(::std::mem::transmute(ob))
} else {
Err($crate::PyDowncastError.into())
}
Expand All @@ -59,10 +59,10 @@ macro_rules! pyobject_native_type_named(
($name: ident) => {
impl $crate::PyNativeType for $name {}

impl $crate::std::convert::AsRef<$crate::PyObjectRef> for $name {
impl ::std::convert::AsRef<$crate::PyObjectRef> for $name {
#[cfg_attr(feature = "cargo-clippy", allow(useless_transmute))]
fn as_ref(&self) -> &$crate::PyObjectRef {
unsafe{$crate::std::mem::transmute(self)}
unsafe{::std::mem::transmute(self)}
}
}

Expand Down Expand Up @@ -92,39 +92,39 @@ macro_rules! pyobject_native_type_named(

#[macro_export]
macro_rules! pyobject_native_type(
($name: ident, $typeobject: ident, $checkfunction: ident) => {
($name: ident, $typeobject: path, $checkfunction: path) => {
pyobject_native_type_named!($name);
pyobject_native_type_convert!($name, $typeobject, $checkfunction);
pyobject_downcast!($name, $checkfunction);

impl<'a> $crate::std::convert::From<&'a $name> for &'a $crate::PyObjectRef {
impl<'a> ::std::convert::From<&'a $name> for &'a $crate::PyObjectRef {
fn from(ob: &'a $name) -> Self {
unsafe{$crate::std::mem::transmute(ob)}
unsafe{::std::mem::transmute(ob)}
}
}
};
);

#[macro_export]
macro_rules! pyobject_native_type_convert(
($name: ident, $typeobject: ident, $checkfunction: ident) => {
($name: ident, $typeobject: path, $checkfunction: path) => {
impl $crate::typeob::PyTypeInfo for $name {
type Type = ();
type BaseType = $crate::PyObjectRef;

const NAME: &'static str = stringify!($name);
const SIZE: usize = $crate::std::mem::size_of::<$crate::ffi::PyObject>();
const SIZE: usize = ::std::mem::size_of::<$crate::ffi::PyObject>();
const OFFSET: isize = 0;

#[inline]
unsafe fn type_object() -> &'static mut $crate::ffi::PyTypeObject {
&mut $crate::ffi::$typeobject
&mut $typeobject
}

#[cfg_attr(feature = "cargo-clippy", allow(not_unsafe_ptr_arg_deref))]
fn is_instance(ptr: *mut $crate::ffi::PyObject) -> bool {
#[allow(unused_unsafe)]
unsafe { $crate::ffi::$checkfunction(ptr) > 0 }
unsafe { $checkfunction(ptr) > 0 }
}
}

Expand Down Expand Up @@ -156,22 +156,22 @@ macro_rules! pyobject_native_type_convert(
}
}

impl $crate::std::fmt::Debug for $name {
fn fmt(&self, f: &mut $crate::std::fmt::Formatter)
-> Result<(), $crate::std::fmt::Error>
impl ::std::fmt::Debug for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter)
-> Result<(), ::std::fmt::Error>
{
use $crate::ObjectProtocol;
let s = try!(self.repr().map_err(|_| $crate::std::fmt::Error));
let s = try!(self.repr().map_err(|_| ::std::fmt::Error));
f.write_str(&s.to_string_lossy())
}
}

impl $crate::std::fmt::Display for $name {
fn fmt(&self, f: &mut $crate::std::fmt::Formatter)
-> Result<(), $crate::std::fmt::Error>
impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter)
-> Result<(), ::std::fmt::Error>
{
use $crate::ObjectProtocol;
let s = try!(self.str().map_err(|_| $crate::std::fmt::Error));
let s = try!(self.str().map_err(|_| ::std::fmt::Error));
f.write_str(&s.to_string_lossy())
}
}
Expand All @@ -193,7 +193,7 @@ macro_rules! pyobject_extract(
}

#[cfg(feature = "try_from")]
impl<'source> $crate::std::convert::TryFrom<&'source $crate::PyObjectRef> for $t
impl<'source> ::std::convert::TryFrom<&'source $crate::PyObjectRef> for $t
{
type Error = $crate::PyErr;

Expand All @@ -207,12 +207,12 @@ macro_rules! pyobject_extract(


use python::ToPyPointer;

use ffi;
/// Represents general python instance.
pub struct PyObjectRef(::PyObject);
pyobject_native_type_named!(PyObjectRef);
pyobject_native_type_convert!(PyObjectRef, PyBaseObject_Type, PyObject_Check);
pyobject_downcast!(PyObjectRef, PyObject_Check);
pyobject_native_type_convert!(PyObjectRef, ffi::PyBaseObject_Type, ffi::PyObject_Check);
pyobject_downcast!(PyObjectRef, ffi::PyObject_Check);

mod typeobject;
mod module;
Expand Down
2 changes: 1 addition & 1 deletion src/objects/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use err::{PyResult, PyErr};
/// Represents a Python `module` object.
pub struct PyModule(PyObject);

pyobject_native_type!(PyModule, PyModule_Type, PyModule_Check);
pyobject_native_type!(PyModule, ffi::PyModule_Type, ffi::PyModule_Check);


impl PyModule {
Expand Down
4 changes: 2 additions & 2 deletions src/objects/num2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use super::num_common::{err_if_invalid_value, IS_LITTLE_ENDIAN};
/// with the primitive Rust integer types.
pub struct PyInt(PyObject);

pyobject_native_type!(PyInt, PyInt_Type, PyInt_Check);
pyobject_native_type!(PyInt, ffi::PyInt_Type, ffi::PyInt_Check);

/// In Python 2.x, represents a Python `long` object.
/// Both `PyInt` and `PyLong` refer to the same type on Python 3.x.
Expand All @@ -37,7 +37,7 @@ pyobject_native_type!(PyInt, PyInt_Type, PyInt_Check);
/// with the primitive Rust integer types.
pub struct PyLong(PyObject);

pyobject_native_type!(PyLong, PyLong_Type, PyLong_Check);
pyobject_native_type!(PyLong, ffi::PyLong_Type, ffi::PyLong_Check);

impl PyInt {
/// Creates a new Python 2.7 `int` object.
Expand Down
2 changes: 1 addition & 1 deletion src/objects/num3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use super::num_common::{err_if_invalid_value, IS_LITTLE_ENDIAN};
/// with the primitive Rust integer types.
pub struct PyLong(PyObject);

pyobject_native_type!(PyLong, PyLong_Type, PyLong_Check);
pyobject_native_type!(PyLong, ffi::PyLong_Type, ffi::PyLong_Check);

macro_rules! int_fits_c_long(
($rust_type:ty) => (
Expand Down
2 changes: 1 addition & 1 deletion src/objects/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use objectprotocol::ObjectProtocol;
/// Represents a reference to a python object supporting the sequence protocol.
pub struct PySequence(PyObject);
pyobject_native_type_named!(PySequence);
pyobject_downcast!(PySequence, PySequence_Check);
pyobject_downcast!(PySequence, ffi::PySequence_Check);


#[cfg_attr(feature = "cargo-clippy", allow(len_without_is_empty))]
Expand Down
3 changes: 2 additions & 1 deletion src/objects/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub struct PySet(PyObject);
pub struct PyFrozenSet(PyObject);


pyobject_native_type!(PySet, PySet_Type, PySet_Check);pyobject_native_type!(PyFrozenSet, PyFrozenSet_Type, PyFrozenSet_Check);
pyobject_native_type!(PySet, ffi::PySet_Type, ffi::PySet_Check);
pyobject_native_type!(PyFrozenSet, ffi::PyFrozenSet_Type, ffi::PyFrozenSet_Check);

impl PySet {
/// Creates a new set.
Expand Down
2 changes: 1 addition & 1 deletion src/objects/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use conversion::ToPyObject;
/// Only `c_long` indeces supprted at the moment by `PySlice` object.
pub struct PySlice(PyObject);

pyobject_native_type!(PySlice, PySlice_Type, PySlice_Check);
pyobject_native_type!(PySlice, ffi::PySlice_Type, ffi::PySlice_Check);


/// Represents a Python `slice` indices
Expand Down
4 changes: 2 additions & 2 deletions src/objects/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::PyStringData;
/// Represents a Python `string`.
pub struct PyString(PyObject);

pyobject_native_type!(PyString, PyUnicode_Type, PyUnicode_Check);
pyobject_native_type!(PyString, ffi::PyUnicode_Type, ffi::PyUnicode_Check);

/// Represents a Python `unicode string`.
/// Corresponds to `unicode` in Python 2, and `str` in Python 3.
Expand All @@ -25,7 +25,7 @@ pub use PyString as PyUnicode;
/// Represents a Python `byte` string.
pub struct PyBytes(PyObject);

pyobject_native_type!(PyBytes, PyBytes_Type, PyBytes_Check);
pyobject_native_type!(PyBytes, ffi::PyBytes_Type, ffi::PyBytes_Check);


impl PyString {
Expand Down
6 changes: 3 additions & 3 deletions src/objects/string2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ use super::{PyObjectRef, PyStringData};
/// Represents a Python `string`.
pub struct PyString(PyObject);

pyobject_native_type!(PyString, PyBaseString_Type, PyBaseString_Check);
pyobject_native_type!(PyString, ffi::PyBaseString_Type, ffi::PyBaseString_Check);

/// Represents a Python `unicode string`.
pub struct PyUnicode(PyObject);

pyobject_native_type!(PyUnicode, PyUnicode_Type, PyUnicode_Check);
pyobject_native_type!(PyUnicode, ffi::PyUnicode_Type, ffi::PyUnicode_Check);

/// Represents a Python `byte` string. Corresponds to `str` in Python 2
pub struct PyBytes(PyObject);

pyobject_native_type!(PyBytes, PyBaseString_Type, PyString_Check);
pyobject_native_type!(PyBytes, ffi::PyBaseString_Type, ffi::PyString_Check);


impl PyString {
Expand Down
2 changes: 1 addition & 1 deletion src/objects/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::exc;
/// Represents a Python `tuple` object.
pub struct PyTuple(PyObject);

pyobject_native_type!(PyTuple, PyTuple_Type, PyTuple_Check);
pyobject_native_type!(PyTuple, ffi::PyTuple_Type, ffi::PyTuple_Check);


impl PyTuple {
Expand Down
2 changes: 1 addition & 1 deletion src/objects/typeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use typeob::{PyTypeInfo, PyTypeObject};
/// Represents a reference to a Python `type object`.
pub struct PyType(PyObject);

pyobject_native_type!(PyType, PyType_Type, PyType_Check);
pyobject_native_type!(PyType, ffi::PyType_Type, ffi::PyType_Check);


impl PyType {
Expand Down

0 comments on commit c22bec6

Please sign in to comment.