This commit is contained in:
konstin 2018-07-03 21:55:43 +02:00
commit 68c14a5707
18 changed files with 50 additions and 43 deletions

View file

@ -100,9 +100,15 @@ fn sum_as_string(a:i64, b:i64) -> String {
```
**To build**: `cargo rustc --release`
On windows and linux, you can build normally with `cargo build --release`. On Mac Os, you need to set additional linker arguments. One option is to compile with `cargo rustc --release -- -C link-arg=-undefined -C link-arg=dynamic_lookup`, the other is to create a `.cargo/config` with the following content:
**On a Mac**: `cargo rustc --release -- -C link-arg=-undefined -C link-arg=dynamic_lookup`
```toml
[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
```
Also on macOS, you will need to rename the output from \*.dylib to \*.so. On Windows, you will need to rename the output from \*.dll to \*.pyd.

View file

@ -204,7 +204,7 @@ macro_rules! wrap_function (
};
);
mod python;
pub mod python;
mod err;
mod conversion;
mod instance;

View file

@ -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 {

View file

@ -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.

View file

@ -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 {

View file

@ -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 {

View file

@ -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.

View file

@ -35,7 +35,7 @@ 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`.
@ -43,7 +43,7 @@ macro_rules! pyobject_downcast(
fn extract(ob: &'a $crate::PyObjectRef) -> $crate::PyResult<Self>
{
unsafe {
if $crate::ffi::$checkfunction(ob.as_ptr()) != 0 {
if $checkfunction(ob.as_ptr()) != 0 {
Ok(&*(ob as *const $crate::objects::PyObjectRef as *const $name))
} else {
Err($crate::PyDowncastError.into())
@ -59,7 +59,7 @@ 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{&*(self as *const $name as *const $crate::objects::PyObjectRef)}
@ -92,12 +92,12 @@ macro_rules! pyobject_native_type_named(
#[macro_export]
macro_rules! pyobject_native_type(
($name: ident, $typeobject: ident, $checkfunction: ident) => {
($name: ident, $typeobject: expr, $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{&*(ob as *const $name as *const $crate::objects::PyObjectRef)}
}
@ -107,24 +107,24 @@ macro_rules! pyobject_native_type(
#[macro_export]
macro_rules! pyobject_native_type_convert(
($name: ident, $typeobject: ident, $checkfunction: ident) => {
($name: ident, $typeobject: expr, $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 }
}
}
@ -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())
}
}
@ -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;
@ -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;

View file

@ -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 {

View file

@ -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.
@ -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.

View file

@ -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) => (

View file

@ -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))]

View file

@ -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.

View file

@ -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

View file

@ -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.
@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {