Remove dependency on interpolate_idents.

We now use the generic <DUMMY> hack to avoid duplicate extern "C" symbols.
See rust-lang/rust#26201.

py_module_initializer!() calls now need to manually concatenate the module
name with the prefixes "init" and "PyInit_".
This commit is contained in:
Daniel Grunwald 2016-03-05 23:20:53 +01:00
parent a1654d5d65
commit 53353d374b
8 changed files with 34 additions and 52 deletions

View File

@ -27,7 +27,6 @@ build = "build.rs"
[dependencies]
libc = "0.2"
num = "0.1"
interpolate_idents = ">=0.0.7"
abort_on_panic = "1.0"
# These features are both optional, but you must pick one to

View File

@ -1,7 +1,5 @@
#![crate_type = "dylib"]
#![feature(const_fn)]
#![feature(plugin)]
#![plugin(interpolate_idents)]
#[macro_use] extern crate cpython;
@ -10,7 +8,7 @@ use std::cell::RefCell;
static MY_TYPE: GILProtected<RefCell<Option<PyRustType<i32>>>> = GILProtected::new(RefCell::new(None));
py_module_initializer!(custom_type, |py, m| {
py_module_initializer!(custom_type, initcustom_type, PyInit_custom_type, |py, m| {
try!(m.add(py, "__doc__", "Module documentation string"));
*MY_TYPE.get(py).borrow_mut() = Some(try!(m.add_type::<i32>(py, "MyType")
.add("a", py_method!(a()))

View File

@ -1,12 +1,10 @@
#![crate_type = "dylib"]
#![feature(plugin)]
#![plugin(interpolate_idents)]
#[macro_use] extern crate cpython;
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
py_module_initializer!(hello, |py, m| {
py_module_initializer!(hello, inithello, PyInit_hello, |py, m| {
try!(m.add(py, "__doc__", "Module documentation string"));
try!(m.add(py, "run", py_fn!(run)));
try!(m.add(py, "val", py_fn!(val())));

View File

@ -1,10 +1,8 @@
#![crate_type = "dylib"]
#![feature(plugin)]
#![plugin(interpolate_idents)]
#[macro_use] extern crate cpython;
py_module_initializer!(inheritance, |py, m| {
py_module_initializer!(inheritance, initinheritance, PyInit_inheritance, |py, m| {
try!(m.add(py, "__doc__", "Module documentation string"));
let base_class = try!(
m.add_type::<()>(py, "BaseClass")

View File

@ -27,21 +27,21 @@ use err::{self, PyResult};
#[macro_export]
#[doc(hidden)]
macro_rules! py_method_def {
($f: ident, $flags: expr, $wrap: expr) => ( interpolate_idents! {{
static mut [ method_def_ $f ]: $crate::_detail::ffi::PyMethodDef = $crate::_detail::ffi::PyMethodDef {
($f: ident, $flags: expr, $wrap: expr) => {{
static mut method_def: $crate::_detail::ffi::PyMethodDef = $crate::_detail::ffi::PyMethodDef {
//ml_name: bytes!(stringify!($f), "\0"),
ml_name: 0 as *const $crate::_detail::libc::c_char,
ml_meth: None,
ml_flags: $crate::_detail::ffi::METH_VARARGS | $crate::_detail::ffi::METH_KEYWORDS | $flags,
ml_doc: 0 as *const $crate::_detail::libc::c_char
};
[ method_def_ $f ].ml_name = concat!(stringify!($f), "\0").as_ptr() as *const _;
[ method_def_ $f ].ml_meth = Some(
method_def.ml_name = concat!(stringify!($f), "\0").as_ptr() as *const _;
method_def.ml_meth = Some(
std::mem::transmute::<$crate::_detail::ffi::PyCFunctionWithKeywords,
$crate::_detail::ffi::PyCFunction>($wrap)
);
&mut [ method_def_ $f ]
}})
&mut method_def
}}
}
#[macro_export]
@ -49,8 +49,8 @@ macro_rules! py_method_def {
macro_rules! py_fn_wrap {
// * $f: function name, used as part of wrapper function name
// * |py, args, kwargs| { body }
($f: ident, | $py: ident, $args: ident, $kwargs: ident | $body: block) => ( interpolate_idents! {{
unsafe extern "C" fn [ wrap_ $f ](
($f: ident, | $py: ident, $args: ident, $kwargs: ident | $body: block) => {{
unsafe extern "C" fn wrap<DUMMY>(
_slf: *mut $crate::_detail::ffi::PyObject,
args: *mut $crate::_detail::ffi::PyObject,
kwargs: *mut $crate::_detail::ffi::PyObject)
@ -63,8 +63,8 @@ macro_rules! py_fn_wrap {
let $kwargs: Option<&$crate::PyDict> = $crate::_detail::get_kwargs(&kwargs);
$crate::_detail::result_to_ptr($py, $body)
}
[ wrap_ $f ]
}});
wrap::<()>
}};
}
#[inline]
@ -110,8 +110,6 @@ pub fn result_to_ptr<T>(py: Python, result: PyResult<T>) -> *mut ffi::PyObject
///
/// # Example
/// ```
/// #![feature(plugin)]
/// #![plugin(interpolate_idents)]
/// #[macro_use] extern crate cpython;
/// use cpython::{Python, PyResult, PyErr, PyDict};
/// use cpython::{exc};

View File

@ -23,9 +23,6 @@
#![feature(const_fn)] // for GILProtected::new (#24111)
#![feature(shared)] // for std::ptr::Shared (#27730)
#![feature(plugin)] // necessary because `fn concat_idents!(...)()` is
#![plugin(interpolate_idents)] // not supported by the current macro system.
#![allow(unused_imports)] // because some imports are only necessary with python 2.x or 3.x
//! Rust bindings to the Python interpreter.
@ -156,22 +153,22 @@ pub mod _detail {
/// Expands to an `extern "C"` function that allows Python to load
/// the rust code as a Python extension module.
///
/// Macro syntax: `py_module_initializer!($name, |$py, $m| $body)`
/// Macro syntax: `py_module_initializer!($name, $py2_init, $py3_init, |$py, $m| $body)`
///
/// 1. `name`: The module name as a Rust identifier.
/// 2. A lambda of type `Fn(Python, &PyModule) -> PyResult<()>`.
/// 2. `py2_init`: "init" + $name. Necessary because macros can't use concat_idents!().
/// 3. `py3_init`: "PyInit_" + $name. Necessary because macros can't use concat_idents!().
/// 4. A lambda of type `Fn(Python, &PyModule) -> PyResult<()>`.
/// This function will be called when the module is imported, and is responsible
/// for adding the module's members.
///
/// # Example
/// ```
/// #![crate_type = "dylib"]
/// #![feature(plugin)]
/// #![plugin(interpolate_idents)]
/// #[macro_use] extern crate cpython;
/// use cpython::{Python, PyResult, PyObject};
///
/// py_module_initializer!(example, |py, m| {
/// py_module_initializer!(example, initexample, PyInit_example, |py, m| {
/// try!(m.add(py, "__doc__", "Module documentation string"));
/// try!(m.add(py, "run", py_fn!(run())));
/// Ok(())
@ -199,10 +196,10 @@ pub mod _detail {
#[macro_export]
#[cfg(feature="python27-sys")]
macro_rules! py_module_initializer {
($name: ident, |$py_id: ident, $m_id: ident| $body: expr) => ( interpolate_idents! {
#[[no_mangle]]
($name: ident, $py2: ident, $py3: ident, |$py_id: ident, $m_id: ident| $body: expr) => {
#[no_mangle]
#[allow(non_snake_case)]
pub unsafe extern "C" fn [ init $name ]() {
pub unsafe extern "C" fn $py2() {
// Nest init function so that $body isn't in unsafe context
fn init($py_id: $crate::Python, $m_id: &$crate::PyModule) -> $crate::PyResult<()> {
$body
@ -210,7 +207,7 @@ macro_rules! py_module_initializer {
let name = concat!(stringify!($name), "\0").as_ptr() as *const _;
$crate::py_module_initializer_impl(name, init)
}
})
}
}
@ -243,10 +240,10 @@ pub unsafe fn py_module_initializer_impl(
#[macro_export]
#[cfg(feature="python3-sys")]
macro_rules! py_module_initializer {
($name: ident, |$py_id: ident, $m_id: ident| $body: expr) => ( interpolate_idents! {
#[[no_mangle]]
($name: ident, $py2: ident, $py3: ident, |$py_id: ident, $m_id: ident| $body: expr) => {
#[no_mangle]
#[allow(non_snake_case)]
pub unsafe extern "C" fn [ PyInit_ $name ]() -> *mut $crate::_detail::ffi::PyObject {
pub unsafe extern "C" fn $py3() -> *mut $crate::_detail::ffi::PyObject {
// Nest init function so that $body isn't in unsafe context
fn init($py_id: $crate::Python, $m_id: &$crate::PyModule) -> $crate::PyResult<()> {
$body
@ -267,7 +264,7 @@ macro_rules! py_module_initializer {
module_def.m_name = concat!(stringify!($name), "\0").as_ptr() as *const _;
$crate::py_module_initializer_impl(&mut module_def, init)
}
})
}
}

View File

@ -28,8 +28,8 @@ use err;
macro_rules! py_method_wrap {
// * $f: function name, used as part of wrapper function name
// * |py, slf, args, kwargs| { body }
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => ( interpolate_idents! {{
unsafe extern "C" fn [ wrap_ $f ](
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => {{
unsafe extern "C" fn wrap<DUMMY>(
slf: *mut $crate::_detail::ffi::PyObject,
args: *mut $crate::_detail::ffi::PyObject,
kwargs: *mut $crate::_detail::ffi::PyObject)
@ -43,8 +43,8 @@ macro_rules! py_method_wrap {
let $kwargs: Option<&$crate::PyDict> = $crate::_detail::get_kwargs(&kwargs);
$crate::_detail::result_to_ptr($py, $body)
}
[ wrap_ $f ]
}});
wrap::<()>
}};
}
/// Creates a Python instance method descriptor that invokes a Rust function.
@ -66,8 +66,6 @@ macro_rules! py_method_wrap {
///
/// # Example
/// ```
/// #![feature(plugin)]
/// #![plugin(interpolate_idents)]
/// #[macro_use] extern crate cpython;
/// use cpython::{Python, PythonObject, PyResult, PyErr, ObjectProtocol,
/// PyRustObject, PyRustTypeBuilder};
@ -203,8 +201,8 @@ impl <T> typebuilder::TypeMember<T> for MethodDescriptor<T> where T: PythonObjec
macro_rules! py_class_method_wrap {
// * $f: function name, used as part of wrapper function name
// * |py, cls, args, kwargs| { body }
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => ( interpolate_idents! {{
unsafe extern "C" fn [ wrap_ $f ](
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => {{
unsafe extern "C" fn wrap<DUMMY>(
slf: *mut $crate::_detail::ffi::PyObject,
args: *mut $crate::_detail::ffi::PyObject,
kwargs: *mut $crate::_detail::ffi::PyObject)
@ -218,8 +216,8 @@ macro_rules! py_class_method_wrap {
let $kwargs: Option<&$crate::PyDict> = $crate::_detail::get_kwargs(&kwargs);
$crate::_detail::result_to_ptr($py, $body)
}
[ wrap_ $f ]
}});
wrap::<()>
}};
}
/// Creates a Python class method descriptor that invokes a Rust function.
@ -241,8 +239,6 @@ macro_rules! py_class_method_wrap {
///
/// # Example
/// ```
/// #![feature(plugin)]
/// #![plugin(interpolate_idents)]
/// #[macro_use] extern crate cpython;
/// use cpython::{Python, PythonObject, PyResult, ObjectProtocol, PyType,
/// PyRustTypeBuilder, NoArgs};

View File

@ -1,5 +1,3 @@
#![feature(plugin)]
#![plugin(interpolate_idents)]
#[macro_use] extern crate cpython;
use cpython::{PyResult, Python, NoArgs, ToPyObject, ObjectProtocol, PyDict, PyTuple};