Skip to content

Commit 53353d3

Browse files
committed
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_".
1 parent a1654d5 commit 53353d3

File tree

8 files changed

+34
-52
lines changed

8 files changed

+34
-52
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ build = "build.rs"
2727
[dependencies]
2828
libc = "0.2"
2929
num = "0.1"
30-
interpolate_idents = ">=0.0.7"
3130
abort_on_panic = "1.0"
3231

3332
# These features are both optional, but you must pick one to

extensions/custom_type.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![crate_type = "dylib"]
22
#![feature(const_fn)]
3-
#![feature(plugin)]
4-
#![plugin(interpolate_idents)]
53

64
#[macro_use] extern crate cpython;
75

@@ -10,7 +8,7 @@ use std::cell::RefCell;
108

119
static MY_TYPE: GILProtected<RefCell<Option<PyRustType<i32>>>> = GILProtected::new(RefCell::new(None));
1210

13-
py_module_initializer!(custom_type, |py, m| {
11+
py_module_initializer!(custom_type, initcustom_type, PyInit_custom_type, |py, m| {
1412
try!(m.add(py, "__doc__", "Module documentation string"));
1513
*MY_TYPE.get(py).borrow_mut() = Some(try!(m.add_type::<i32>(py, "MyType")
1614
.add("a", py_method!(a()))

extensions/hello.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#![crate_type = "dylib"]
2-
#![feature(plugin)]
3-
#![plugin(interpolate_idents)]
42

53
#[macro_use] extern crate cpython;
64

75
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
86

9-
py_module_initializer!(hello, |py, m| {
7+
py_module_initializer!(hello, inithello, PyInit_hello, |py, m| {
108
try!(m.add(py, "__doc__", "Module documentation string"));
119
try!(m.add(py, "run", py_fn!(run)));
1210
try!(m.add(py, "val", py_fn!(val())));

extensions/inheritance.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#![crate_type = "dylib"]
2-
#![feature(plugin)]
3-
#![plugin(interpolate_idents)]
42

53
#[macro_use] extern crate cpython;
64

7-
py_module_initializer!(inheritance, |py, m| {
5+
py_module_initializer!(inheritance, initinheritance, PyInit_inheritance, |py, m| {
86
try!(m.add(py, "__doc__", "Module documentation string"));
97
let base_class = try!(
108
m.add_type::<()>(py, "BaseClass")

src/function.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ use err::{self, PyResult};
2727
#[macro_export]
2828
#[doc(hidden)]
2929
macro_rules! py_method_def {
30-
($f: ident, $flags: expr, $wrap: expr) => ( interpolate_idents! {{
31-
static mut [ method_def_ $f ]: $crate::_detail::ffi::PyMethodDef = $crate::_detail::ffi::PyMethodDef {
30+
($f: ident, $flags: expr, $wrap: expr) => {{
31+
static mut method_def: $crate::_detail::ffi::PyMethodDef = $crate::_detail::ffi::PyMethodDef {
3232
//ml_name: bytes!(stringify!($f), "\0"),
3333
ml_name: 0 as *const $crate::_detail::libc::c_char,
3434
ml_meth: None,
3535
ml_flags: $crate::_detail::ffi::METH_VARARGS | $crate::_detail::ffi::METH_KEYWORDS | $flags,
3636
ml_doc: 0 as *const $crate::_detail::libc::c_char
3737
};
38-
[ method_def_ $f ].ml_name = concat!(stringify!($f), "\0").as_ptr() as *const _;
39-
[ method_def_ $f ].ml_meth = Some(
38+
method_def.ml_name = concat!(stringify!($f), "\0").as_ptr() as *const _;
39+
method_def.ml_meth = Some(
4040
std::mem::transmute::<$crate::_detail::ffi::PyCFunctionWithKeywords,
4141
$crate::_detail::ffi::PyCFunction>($wrap)
4242
);
43-
&mut [ method_def_ $f ]
44-
}})
43+
&mut method_def
44+
}}
4545
}
4646

4747
#[macro_export]
4848
#[doc(hidden)]
4949
macro_rules! py_fn_wrap {
5050
// * $f: function name, used as part of wrapper function name
5151
// * |py, args, kwargs| { body }
52-
($f: ident, | $py: ident, $args: ident, $kwargs: ident | $body: block) => ( interpolate_idents! {{
53-
unsafe extern "C" fn [ wrap_ $f ](
52+
($f: ident, | $py: ident, $args: ident, $kwargs: ident | $body: block) => {{
53+
unsafe extern "C" fn wrap<DUMMY>(
5454
_slf: *mut $crate::_detail::ffi::PyObject,
5555
args: *mut $crate::_detail::ffi::PyObject,
5656
kwargs: *mut $crate::_detail::ffi::PyObject)
@@ -63,8 +63,8 @@ macro_rules! py_fn_wrap {
6363
let $kwargs: Option<&$crate::PyDict> = $crate::_detail::get_kwargs(&kwargs);
6464
$crate::_detail::result_to_ptr($py, $body)
6565
}
66-
[ wrap_ $f ]
67-
}});
66+
wrap::<()>
67+
}};
6868
}
6969

7070
#[inline]
@@ -110,8 +110,6 @@ pub fn result_to_ptr<T>(py: Python, result: PyResult<T>) -> *mut ffi::PyObject
110110
///
111111
/// # Example
112112
/// ```
113-
/// #![feature(plugin)]
114-
/// #![plugin(interpolate_idents)]
115113
/// #[macro_use] extern crate cpython;
116114
/// use cpython::{Python, PyResult, PyErr, PyDict};
117115
/// use cpython::{exc};

src/lib.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
#![feature(const_fn)] // for GILProtected::new (#24111)
2424
#![feature(shared)] // for std::ptr::Shared (#27730)
2525

26-
#![feature(plugin)] // necessary because `fn concat_idents!(...)()` is
27-
#![plugin(interpolate_idents)] // not supported by the current macro system.
28-
2926
#![allow(unused_imports)] // because some imports are only necessary with python 2.x or 3.x
3027

3128
//! Rust bindings to the Python interpreter.
@@ -156,22 +153,22 @@ pub mod _detail {
156153
/// Expands to an `extern "C"` function that allows Python to load
157154
/// the rust code as a Python extension module.
158155
///
159-
/// Macro syntax: `py_module_initializer!($name, |$py, $m| $body)`
156+
/// Macro syntax: `py_module_initializer!($name, $py2_init, $py3_init, |$py, $m| $body)`
160157
///
161158
/// 1. `name`: The module name as a Rust identifier.
162-
/// 2. A lambda of type `Fn(Python, &PyModule) -> PyResult<()>`.
159+
/// 2. `py2_init`: "init" + $name. Necessary because macros can't use concat_idents!().
160+
/// 3. `py3_init`: "PyInit_" + $name. Necessary because macros can't use concat_idents!().
161+
/// 4. A lambda of type `Fn(Python, &PyModule) -> PyResult<()>`.
163162
/// This function will be called when the module is imported, and is responsible
164163
/// for adding the module's members.
165164
///
166165
/// # Example
167166
/// ```
168167
/// #![crate_type = "dylib"]
169-
/// #![feature(plugin)]
170-
/// #![plugin(interpolate_idents)]
171168
/// #[macro_use] extern crate cpython;
172169
/// use cpython::{Python, PyResult, PyObject};
173170
///
174-
/// py_module_initializer!(example, |py, m| {
171+
/// py_module_initializer!(example, initexample, PyInit_example, |py, m| {
175172
/// try!(m.add(py, "__doc__", "Module documentation string"));
176173
/// try!(m.add(py, "run", py_fn!(run())));
177174
/// Ok(())
@@ -199,18 +196,18 @@ pub mod _detail {
199196
#[macro_export]
200197
#[cfg(feature="python27-sys")]
201198
macro_rules! py_module_initializer {
202-
($name: ident, |$py_id: ident, $m_id: ident| $body: expr) => ( interpolate_idents! {
203-
#[[no_mangle]]
199+
($name: ident, $py2: ident, $py3: ident, |$py_id: ident, $m_id: ident| $body: expr) => {
200+
#[no_mangle]
204201
#[allow(non_snake_case)]
205-
pub unsafe extern "C" fn [ init $name ]() {
202+
pub unsafe extern "C" fn $py2() {
206203
// Nest init function so that $body isn't in unsafe context
207204
fn init($py_id: $crate::Python, $m_id: &$crate::PyModule) -> $crate::PyResult<()> {
208205
$body
209206
}
210207
let name = concat!(stringify!($name), "\0").as_ptr() as *const _;
211208
$crate::py_module_initializer_impl(name, init)
212209
}
213-
})
210+
}
214211
}
215212

216213

@@ -243,10 +240,10 @@ pub unsafe fn py_module_initializer_impl(
243240
#[macro_export]
244241
#[cfg(feature="python3-sys")]
245242
macro_rules! py_module_initializer {
246-
($name: ident, |$py_id: ident, $m_id: ident| $body: expr) => ( interpolate_idents! {
247-
#[[no_mangle]]
243+
($name: ident, $py2: ident, $py3: ident, |$py_id: ident, $m_id: ident| $body: expr) => {
244+
#[no_mangle]
248245
#[allow(non_snake_case)]
249-
pub unsafe extern "C" fn [ PyInit_ $name ]() -> *mut $crate::_detail::ffi::PyObject {
246+
pub unsafe extern "C" fn $py3() -> *mut $crate::_detail::ffi::PyObject {
250247
// Nest init function so that $body isn't in unsafe context
251248
fn init($py_id: $crate::Python, $m_id: &$crate::PyModule) -> $crate::PyResult<()> {
252249
$body
@@ -267,7 +264,7 @@ macro_rules! py_module_initializer {
267264
module_def.m_name = concat!(stringify!($name), "\0").as_ptr() as *const _;
268265
$crate::py_module_initializer_impl(&mut module_def, init)
269266
}
270-
})
267+
}
271268
}
272269

273270

src/rustobject/method.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use err;
2828
macro_rules! py_method_wrap {
2929
// * $f: function name, used as part of wrapper function name
3030
// * |py, slf, args, kwargs| { body }
31-
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => ( interpolate_idents! {{
32-
unsafe extern "C" fn [ wrap_ $f ](
31+
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => {{
32+
unsafe extern "C" fn wrap<DUMMY>(
3333
slf: *mut $crate::_detail::ffi::PyObject,
3434
args: *mut $crate::_detail::ffi::PyObject,
3535
kwargs: *mut $crate::_detail::ffi::PyObject)
@@ -43,8 +43,8 @@ macro_rules! py_method_wrap {
4343
let $kwargs: Option<&$crate::PyDict> = $crate::_detail::get_kwargs(&kwargs);
4444
$crate::_detail::result_to_ptr($py, $body)
4545
}
46-
[ wrap_ $f ]
47-
}});
46+
wrap::<()>
47+
}};
4848
}
4949

5050
/// Creates a Python instance method descriptor that invokes a Rust function.
@@ -66,8 +66,6 @@ macro_rules! py_method_wrap {
6666
///
6767
/// # Example
6868
/// ```
69-
/// #![feature(plugin)]
70-
/// #![plugin(interpolate_idents)]
7169
/// #[macro_use] extern crate cpython;
7270
/// use cpython::{Python, PythonObject, PyResult, PyErr, ObjectProtocol,
7371
/// PyRustObject, PyRustTypeBuilder};
@@ -203,8 +201,8 @@ impl <T> typebuilder::TypeMember<T> for MethodDescriptor<T> where T: PythonObjec
203201
macro_rules! py_class_method_wrap {
204202
// * $f: function name, used as part of wrapper function name
205203
// * |py, cls, args, kwargs| { body }
206-
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => ( interpolate_idents! {{
207-
unsafe extern "C" fn [ wrap_ $f ](
204+
($f: ident, | $py: ident, $slf: ident, $args: ident, $kwargs: ident | $body: block) => {{
205+
unsafe extern "C" fn wrap<DUMMY>(
208206
slf: *mut $crate::_detail::ffi::PyObject,
209207
args: *mut $crate::_detail::ffi::PyObject,
210208
kwargs: *mut $crate::_detail::ffi::PyObject)
@@ -218,8 +216,8 @@ macro_rules! py_class_method_wrap {
218216
let $kwargs: Option<&$crate::PyDict> = $crate::_detail::get_kwargs(&kwargs);
219217
$crate::_detail::result_to_ptr($py, $body)
220218
}
221-
[ wrap_ $f ]
222-
}});
219+
wrap::<()>
220+
}};
223221
}
224222

225223
/// Creates a Python class method descriptor that invokes a Rust function.
@@ -241,8 +239,6 @@ macro_rules! py_class_method_wrap {
241239
///
242240
/// # Example
243241
/// ```
244-
/// #![feature(plugin)]
245-
/// #![plugin(interpolate_idents)]
246242
/// #[macro_use] extern crate cpython;
247243
/// use cpython::{Python, PythonObject, PyResult, ObjectProtocol, PyType,
248244
/// PyRustTypeBuilder, NoArgs};

tests/test_function.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(plugin)]
2-
#![plugin(interpolate_idents)]
31
#[macro_use] extern crate cpython;
42

53
use cpython::{PyResult, Python, NoArgs, ToPyObject, ObjectProtocol, PyDict, PyTuple};

0 commit comments

Comments
 (0)