Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and Export Pyobject_* macros #155

Merged
merged 2 commits into from
May 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/objects/boolobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use conversion::{ToPyObject, IntoPyObject, ToBorrowedObject, PyTryFrom};
/// Represents a Python `bool`.
pub struct PyBool(PyObject);

pyobject_convert!(PyBool);
pyobject_nativetype!(PyBool, PyBool_Type, PyBool_Check);
pyobject_native_type!(PyBool, PyBool_Type, PyBool_Check);


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

pyobject_convert!(PyByteArray);
pyobject_nativetype!(PyByteArray, PyByteArray_Type, PyByteArray_Check);
pyobject_native_type!(PyByteArray, PyByteArray_Type, PyByteArray_Check);

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

pyobject_convert!(PyDict);
pyobject_nativetype!(PyDict, PyDict_Type, PyDict_Check);
pyobject_native_type!(PyDict, PyDict_Type, PyDict_Check);


impl PyDict {
Expand Down
3 changes: 1 addition & 2 deletions src/objects/floatob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use conversion::{ToPyObject, IntoPyObject};
/// with `f32`/`f64`.
pub struct PyFloat(PyObject);

pyobject_convert!(PyFloat);
pyobject_nativetype!(PyFloat, PyFloat_Type, PyFloat_Check);
pyobject_native_type!(PyFloat, PyFloat_Type, PyFloat_Check);


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

pyobject_convert!(PyList);
pyobject_nativetype!(PyList, PyList_Type, PyList_Check);
pyobject_native_type!(PyList, PyList_Type, PyList_Check);

impl PyList {
/// Construct a new list with the given elements.
Expand Down
46 changes: 30 additions & 16 deletions src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub use self::num3::PyLong as PyInt;
pub use self::num2::{PyInt, PyLong};


/// Implements typesafe conversions from a PyObjectRef, given a typecheck function as second
/// parameter
#[macro_export]
macro_rules! pyobject_downcast(
($name: ident, $checkfunction: ident) => (
impl<'a> $crate::FromPyObject<'a> for &'a $name
Expand All @@ -51,17 +54,8 @@ macro_rules! pyobject_downcast(
);
);

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

macro_rules! pyobject_nativetype(
#[macro_export]
macro_rules! pyobject_native_type_named(
($name: ident) => {
impl $crate::PyNativeType for $name {}

Expand All @@ -71,30 +65,49 @@ macro_rules! pyobject_nativetype(
unsafe{$crate::std::mem::transmute(self)}
}
}

impl $crate::PyObjectWithToken for $name {
#[inline(always)]
fn py(&self) -> $crate::Python {
unsafe { $crate::Python::assume_gil_acquired() }
}
}

impl $crate::python::ToPyPointer for $name {
/// Gets the underlying FFI pointer, returns a borrowed pointer.
#[inline]
fn as_ptr(&self) -> *mut $crate::ffi::PyObject {
self.0.as_ptr()
}
}

impl PartialEq for $name {
#[inline]
fn eq(&self, o: &$name) -> bool {
self.as_ptr() == o.as_ptr()
}
}
};
);

#[macro_export]
macro_rules! pyobject_native_type(
($name: ident, $typeobject: ident, $checkfunction: ident) => {
pyobject_nativetype!($name);
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 {
fn from(ob: &'a $name) -> Self {
unsafe{$crate::std::mem::transmute(ob)}
}
}
};
);

#[macro_export]
macro_rules! pyobject_native_type_convert(
($name: ident, $typeobject: ident, $checkfunction: ident) => {
impl $crate::typeob::PyTypeInfo for $name {
type Type = ();
type BaseType = $crate::PyObjectRef;
Expand Down Expand Up @@ -162,11 +175,10 @@ macro_rules! pyobject_nativetype(
f.write_str(&s.to_string_lossy())
}
}

pyobject_downcast!($name, $checkfunction);
};
};
);

#[macro_export]
macro_rules! pyobject_extract(
($obj:ident to $t:ty => $body: block) => {
impl<'source> $crate::FromPyObject<'source> for $t
Expand Down Expand Up @@ -201,7 +213,9 @@ use python::ToPyPointer;

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

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

pyobject_convert!(PyModule);
pyobject_nativetype!(PyModule, PyModule_Type, PyModule_Check);
pyobject_native_type!(PyModule, PyModule_Type, PyModule_Check);


impl PyModule {
Expand Down
6 changes: 2 additions & 4 deletions src/objects/num2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use conversion::{ToPyObject, IntoPyObject, FromPyObject};
/// with the primitive Rust integer types.
pub struct PyInt(PyObject);

pyobject_convert!(PyInt);
pyobject_nativetype!(PyInt, PyInt_Type, PyInt_Check);
pyobject_native_type!(PyInt, PyInt_Type, 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,8 +36,7 @@ pyobject_nativetype!(PyInt, PyInt_Type, PyInt_Check);
/// with the primitive Rust integer types.
pub struct PyLong(PyObject);

pyobject_convert!(PyLong);
pyobject_nativetype!(PyLong, PyLong_Type, PyLong_Check);
pyobject_native_type!(PyLong, PyLong_Type, PyLong_Check);

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

pyobject_convert!(PyLong);
pyobject_nativetype!(PyLong, PyLong_Type, PyLong_Check);
pyobject_native_type!(PyLong, PyLong_Type, PyLong_Check);


macro_rules! int_fits_c_long(
Expand Down
3 changes: 1 addition & 2 deletions src/objects/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use objectprotocol::ObjectProtocol;

/// Represents a reference to a python object supporting the sequence protocol.
pub struct PySequence(PyObject);

pyobject_nativetype!(PySequence);
pyobject_native_type_named!(PySequence);
pyobject_downcast!(PySequence, PySequence_Check);


Expand Down
6 changes: 2 additions & 4 deletions src/objects/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub struct PySet(PyObject);
/// Represents a Python `frozenset`
pub struct PyFrozenSet(PyObject);

pyobject_convert!(PySet);
pyobject_convert!(PyFrozenSet);
pyobject_nativetype!(PySet, PySet_Type, PySet_Check);
pyobject_nativetype!(PyFrozenSet, PyFrozenSet_Type, PyFrozenSet_Check);

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

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

pyobject_convert!(PySlice);
pyobject_nativetype!(PySlice, PySlice_Type, PySlice_Check);
pyobject_native_type!(PySlice, PySlice_Type, PySlice_Check);


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

pyobject_convert!(PyString);
pyobject_nativetype!(PyString, PyUnicode_Type, PyUnicode_Check);
pyobject_native_type!(PyString, PyUnicode_Type, PyUnicode_Check);

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

pyobject_convert!(PyBytes);
pyobject_nativetype!(PyBytes, PyBytes_Type, PyBytes_Check);
pyobject_native_type!(PyBytes, PyBytes_Type, PyBytes_Check);


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

pyobject_convert!(PyString);
pyobject_nativetype!(PyString, PyBaseString_Type, PyBaseString_Check);
pyobject_native_type!(PyString, PyBaseString_Type, PyBaseString_Check);

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

pyobject_convert!(PyUnicode);
pyobject_nativetype!(PyUnicode, PyUnicode_Type, PyUnicode_Check);
pyobject_native_type!(PyUnicode, PyUnicode_Type, PyUnicode_Check);

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

pyobject_convert!(PyBytes);
pyobject_nativetype!(PyBytes, PyBaseString_Type, PyString_Check);
pyobject_native_type!(PyBytes, PyBaseString_Type, PyString_Check);


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

pyobject_convert!(PyTuple);
pyobject_nativetype!(PyTuple, PyTuple_Type, PyTuple_Check);
pyobject_native_type!(PyTuple, PyTuple_Type, PyTuple_Check);


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

pyobject_convert!(PyType);
pyobject_nativetype!(PyType, PyType_Type, PyType_Check);
pyobject_native_type!(PyType, PyType_Type, PyType_Check);


impl PyType {
Expand Down