1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#![allow(clippy::transmute_ptr_to_ptr)]
pub use self::module::PyModule;
pub use self::object::PyObject;
pub use self::typeobject::PyType;
#[cfg(feature = "python3-sys")]
pub use self::string::PyString as PyUnicode;
#[cfg(feature = "python27-sys")]
pub use self::string::PyUnicode;
pub use self::string::{PyBytes, PyString, PyStringData};
pub use self::boolobject::PyBool;
pub use self::capsule::PyCapsule;
pub use self::dict::PyDict;
pub use self::iterator::PyIterator;
pub use self::list::PyList;
pub use self::none::PyNone;
#[cfg(feature = "python27-sys")]
pub use self::num::PyInt;
#[cfg(feature = "python3-sys")]
pub use self::num::PyLong as PyInt;
pub use self::num::{PyFloat, PyLong};
pub use self::sequence::PySequence;
pub use self::set::PySet;
pub use self::tuple::{NoArgs, PyTuple};
#[macro_export]
macro_rules! pyobject_newtype(
($name: ident) => (
$crate::py_impl_to_py_object_for_python_object!($name);
$crate::py_impl_from_py_object_for_python_object!($name);
impl $crate::PythonObject for $name {
#[inline]
fn as_object(&self) -> &$crate::PyObject {
&self.0
}
#[inline]
fn into_object(self) -> $crate::PyObject {
self.0
}
#[inline]
unsafe fn unchecked_downcast_from(obj: $crate::PyObject) -> Self {
$name(obj)
}
#[inline]
unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a $crate::PyObject) -> &'a Self {
std::mem::transmute(obj)
}
}
);
($name: ident, $checkfunction: ident) => (
pyobject_newtype!($name);
impl crate::python::PythonObjectWithCheckedDowncast for $name {
#[inline]
fn downcast_from<'p>(py: crate::python::Python<'p>, obj: crate::objects::object::PyObject) -> Result<$name, crate::python::PythonObjectDowncastError<'p>> {
unsafe {
if crate::ffi::$checkfunction(obj.as_ptr()) != 0 {
Ok($name(obj))
} else {
Err(crate::python::PythonObjectDowncastError::new(
py,
stringify!($name),
obj.get_type(py)
))
}
}
}
#[inline]
fn downcast_borrow_from<'a, 'p>(py: crate::python::Python<'p>, obj: &'a crate::objects::object::PyObject) -> Result<&'a $name, crate::python::PythonObjectDowncastError<'p>> {
unsafe {
if crate::ffi::$checkfunction(obj.as_ptr()) != 0 {
Ok(std::mem::transmute(obj))
} else {
Err(crate::python::PythonObjectDowncastError::new(
py,
stringify!($name),
obj.get_type(py)
))
}
}
}
}
);
($name: ident, $checkfunction: ident, $typeobject: ident) => (
pyobject_newtype!($name, $checkfunction);
impl crate::python::PythonObjectWithTypeObject for $name {
#[inline]
fn type_object(py: crate::python::Python) -> crate::objects::typeobject::PyType {
unsafe { crate::objects::typeobject::PyType::from_type_ptr(py, &mut crate::ffi::$typeobject) }
}
}
);
);
macro_rules! extract(
($obj:ident to $t:ty; $(#[$meta:meta])* $py:ident => $body: block) => {
impl <'s> crate::conversion::FromPyObject<'s>
for $t
{
$(#[$meta])*
fn extract($py: Python, $obj: &'s PyObject) -> PyResult<Self> {
$body
}
}
}
);
mod boolobject;
mod capsule;
mod dict;
pub mod exc;
mod iterator;
mod list;
mod module;
mod none;
mod num;
mod object;
mod sequence;
mod set;
mod string;
mod tuple;
mod typeobject;
#[cfg(feature = "python27-sys")]
pub mod oldstyle;
mod tests;