Skip to content

Commit 45fdacc

Browse files
committed
use edition 2018 style macro export and import
1 parent d1d93c2 commit 45fdacc

29 files changed

+651
-877
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ cpython = "0.4"
3030
#### Example program displaying the value of `sys.version`:
3131

3232
```rust
33-
extern crate cpython;
34-
3533
use cpython::{Python, PyDict, PyResult};
3634

3735
fn main() {
@@ -78,9 +76,7 @@ features = ["extension-module"]
7876

7977
**`src/lib.rs`**
8078
```rust
81-
#[macro_use] extern crate cpython;
82-
83-
use cpython::{PyResult, Python};
79+
use cpython::{PyResult, Python, py_module_initializer, py_fn};
8480

8581
// add bindings to the generated python module
8682
// N.B: names: "rust2py" must be the name of the `.so` or `.pyd` file

examples/hello.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate cpython;
2-
31
use cpython::{PyDict, PyResult, Python};
42

53
fn main() {

extensions/hello/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "hello"
33
version = "0.4.0"
44
authors = ["Daniel Grunwald <[email protected]>"]
5+
edition = "2018"
56

67
[lib]
78
## Python extension modules should be compiled as 'cdylib'

extensions/hello/src/hello.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
#[macro_use]
2-
extern crate cpython;
3-
4-
use cpython::{PyDict, PyObject, PyResult, PyTuple, Python};
1+
use cpython::{py_fn, py_module_initializer, PyDict, PyObject, PyResult, PyTuple, Python};
52

63
// Our module is named 'hello', and can be imported using `import hello`.
74
// This requires that the output binary file is named `hello.so` (or Windows: `hello.pyd`).

extensions/tests/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ all:
2121
clean:
2222
$(RM) *.so
2323
$(RM) *.out
24-
$(RM) *_expanded.rs
2524
$(RM) -r stamps
2625

2726
stamps:
@@ -33,10 +32,10 @@ stamps/rust-cpython-$(PY): $(call rwildcard,../../src,*.rs) Makefile | stamps
3332
touch "$@"
3433

3534
%.so: %.rs stamps/rust-cpython-$(PY)
36-
rustc $< -g -L $(TARGETDIR) -L $(TARGETDIR)/deps -o $@
35+
rustc $< --edition 2018 -g -L $(TARGETDIR) -L $(TARGETDIR)/deps --extern cpython=$(TARGETDIR)/libcpython.rlib -o $@
3736

3837
%_expanded.rs: %.rs stamps/rust-cpython-$(PY)
39-
rustc $< -g -L $(TARGETDIR) -L $(TARGETDIR)/deps -Z unstable-options --pretty=expanded -o $@
38+
rustc $< --edition 2018 -g -L $(TARGETDIR) -L $(TARGETDIR)/deps -Z unstable-options --pretty=expanded --extern cpython=$(TARGETDIR)/libcpython.rlib -o $@
4039

4140
hello.out: hello.so
4241
python$(PY) -c "import hello; hello.run(hello.val())" 2>&1 | tee $@

extensions/tests/btree.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![crate_type = "dylib"]
22

3-
#[macro_use] extern crate cpython;
4-
5-
use cpython::{PyObject, PyResult};
3+
use cpython::{PyObject, PyResult, py_module_initializer, py_class};
64
use std::{cell, cmp, collections};
75

86
py_module_initializer!(btree, initbtree, PyInit_btree, |py, m| {

extensions/tests/custom_class.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![crate_type = "dylib"]
22

3-
#[macro_use] extern crate cpython;
4-
5-
use cpython::{PyObject, PyResult};
3+
use cpython::{PyObject, PyResult, py_module_initializer, py_class};
64

75
py_module_initializer!(custom_class, initcustom_class, PyInit_custom_class, |py, m| {
86
m.add(py, "__doc__", "Module documentation string")?;

extensions/tests/hello.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![crate_type = "dylib"]
22

3-
#[macro_use] extern crate cpython;
4-
5-
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
3+
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict, py_module_initializer, py_fn};
64

75
py_module_initializer!(hello, inithello, PyInit_hello, |py, m| {
86
m.add(py, "__doc__", "Module documentation string")?;

python27-sys/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,5 @@ For a safe high-level API, see [rust-cpython](https://github.com/dgrunwald/rust-
1919
version = "*"
2020
```
2121

22-
In Rust, import the crate like this:
23-
24-
```rust
25-
extern crate python27_sys as py;
26-
```
27-
2822
Documentation for the python API is available on [https://docs.python.org/2/c-api/].
2923

python27-sys/build.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// THIS FILE IS GENERATED FROM python3-sys/build.rs
22
// DO NOT MODIFY
33

4-
extern crate regex;
5-
64
use regex::Regex;
75
use std::collections::HashMap;
86
use std::env;

python27-sys/examples/version.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate libc;
2-
extern crate python27_sys;
3-
41
unsafe fn get_str<'a>(s: *const libc::c_char) -> &'a str {
52
let bytes = std::ffi::CStr::from_ptr(s).to_bytes();
63
std::str::from_utf8(bytes).unwrap()

python3-sys/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,5 @@ For a safe high-level API, see [rust-cpython](https://github.com/dgrunwald/rust-
2020
version = "*"
2121
```
2222

23-
In Rust, import the crate like this:
24-
25-
```rust
26-
extern crate python3_sys as py;
27-
```
28-
2923
Documentation for the python API is available on [https://docs.python.org/3/c-api/].
3024

python3-sys/build.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate regex;
2-
31
use regex::Regex;
42
use std::collections::HashMap;
53
use std::env;

python3-sys/examples/version.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate libc;
2-
extern crate python3_sys;
3-
41
unsafe fn get_str<'a>(s: *const libc::c_char) -> &'a str {
52
let bytes = std::ffi::CStr::from_ptr(s).to_bytes();
63
std::str::from_utf8(bytes).unwrap()

src/argparse.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ pub fn parse_args(
167167
/// so `return` statements might behave unexpectedly in this case. (this only affects direct use
168168
/// of `py_argparse!`; `py_fn!` is unaffected as the body there is always in a separate function
169169
/// from the generated argument-parsing code).
170-
#[macro_export(local_inner_macros)]
170+
#[macro_export]
171171
macro_rules! py_argparse {
172172
($py:expr, $fname:expr, $args:expr, $kwargs:expr, $plist:tt $body:block) => {
173-
py_argparse_parse_plist! { py_argparse_impl { $py, $fname, $args, $kwargs, $body, } $plist }
173+
$crate::py_argparse_parse_plist! { py_argparse_impl { $py, $fname, $args, $kwargs, $body, } $plist }
174174
};
175175
}
176176

177-
#[macro_export(local_inner_macros)]
177+
#[macro_export]
178178
#[doc(hidden)]
179179
macro_rules! py_argparse_parse_plist {
180180
// Parses a parameter-list into a format more suitable for consumption by Rust macros.
@@ -184,30 +184,30 @@ macro_rules! py_argparse_parse_plist {
184184

185185
// Special-case entry-point for empty parameter list:
186186
{ $callback:ident { $($initial_arg:tt)* } ( ) } => {
187-
$callback! { $($initial_arg)* [] }
187+
$crate::$callback! { $($initial_arg)* [] }
188188
};
189189
// Regular entry point for non-empty parameter list:
190190
{ $callback:ident $initial_args:tt ( $( $p:tt )+ ) } => {
191191
// add trailing comma to plist so that the parsing step can assume every
192192
// parameter ends with a comma.
193-
py_argparse_parse_plist_impl! { $callback $initial_args [] ( $($p)*, ) }
193+
$crate::py_argparse_parse_plist_impl! { $callback $initial_args [] ( $($p)*, ) }
194194
};
195195
}
196196

197-
#[macro_export(local_inner_macros)]
197+
#[macro_export]
198198
#[doc(hidden)]
199199
macro_rules! py_argparse_parse_plist_impl {
200200
// TT muncher macro that does the main work for py_argparse_parse_plist!.
201201

202202
// Base case: all parameters handled
203203
{ $callback:ident { $($initial_arg:tt)* } $output:tt ( ) } => {
204-
$callback! { $($initial_arg)* $output }
204+
$crate::$callback! { $($initial_arg)* $output }
205205
};
206206
// Kwargs parameter with reference extraction
207207
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
208208
( ** $name:ident : &$t:ty , $($tail:tt)* )
209209
} => {
210-
py_argparse_parse_plist_impl! {
210+
$crate::py_argparse_parse_plist_impl! {
211211
$callback $initial_args
212212
[ $($output)* { $name:&$t = [ {**} {} {$t} ] } ]
213213
($($tail)*)
@@ -217,7 +217,7 @@ macro_rules! py_argparse_parse_plist_impl {
217217
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
218218
( ** $name:ident : $t:ty , $($tail:tt)* )
219219
} => {
220-
py_argparse_parse_plist_impl! {
220+
$crate::py_argparse_parse_plist_impl! {
221221
$callback $initial_args
222222
[ $($output)* { $name:$t = [ {**} {} {} ] } ]
223223
($($tail)*)
@@ -227,7 +227,7 @@ macro_rules! py_argparse_parse_plist_impl {
227227
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
228228
( ** $name:ident , $($tail:tt)* )
229229
} => {
230-
py_argparse_parse_plist_impl! {
230+
$crate::py_argparse_parse_plist_impl! {
231231
$callback $initial_args
232232
[ $($output)* { $name:Option<&$crate::PyDict> = [ {**} {} {} ] } ]
233233
($($tail)*)
@@ -237,7 +237,7 @@ macro_rules! py_argparse_parse_plist_impl {
237237
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
238238
( * $name:ident : &$t:ty , $($tail:tt)* )
239239
} => {
240-
py_argparse_parse_plist_impl! {
240+
$crate::py_argparse_parse_plist_impl! {
241241
$callback $initial_args
242242
[ $($output)* { $name:&$t = [ {*} {} {$t} ] } ]
243243
($($tail)*)
@@ -247,7 +247,7 @@ macro_rules! py_argparse_parse_plist_impl {
247247
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
248248
( * $name:ident : $t:ty , $($tail:tt)* )
249249
} => {
250-
py_argparse_parse_plist_impl! {
250+
$crate::py_argparse_parse_plist_impl! {
251251
$callback $initial_args
252252
[ $($output)* { $name:$t = [ {*} {} {} ] } ]
253253
($($tail)*)
@@ -257,7 +257,7 @@ macro_rules! py_argparse_parse_plist_impl {
257257
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
258258
( * $name:ident , $($tail:tt)* )
259259
} => {
260-
py_argparse_parse_plist_impl! {
260+
$crate::py_argparse_parse_plist_impl! {
261261
$callback $initial_args
262262
[ $($output)* { $name:&$crate::PyTuple = [ {*} {} {} ] } ]
263263
($($tail)*)
@@ -267,7 +267,7 @@ macro_rules! py_argparse_parse_plist_impl {
267267
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
268268
( $name:ident : &$t:ty , $($tail:tt)* )
269269
} => {
270-
py_argparse_parse_plist_impl! {
270+
$crate::py_argparse_parse_plist_impl! {
271271
$callback $initial_args
272272
[ $($output)* { $name:&$t = [ {} {} {$t} ] } ]
273273
($($tail)*)
@@ -277,7 +277,7 @@ macro_rules! py_argparse_parse_plist_impl {
277277
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
278278
( $name:ident : $t:ty , $($tail:tt)* )
279279
} => {
280-
py_argparse_parse_plist_impl! {
280+
$crate::py_argparse_parse_plist_impl! {
281281
$callback $initial_args
282282
[ $($output)* { $name:$t = [ {} {} {} ] } ]
283283
($($tail)*)
@@ -287,7 +287,7 @@ macro_rules! py_argparse_parse_plist_impl {
287287
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
288288
( $name:ident , $($tail:tt)* )
289289
} => {
290-
py_argparse_parse_plist_impl! {
290+
$crate::py_argparse_parse_plist_impl! {
291291
$callback $initial_args
292292
[ $($output)* { $name:&$crate::PyObject = [ {} {} {} ] } ]
293293
($($tail)*)
@@ -297,7 +297,7 @@ macro_rules! py_argparse_parse_plist_impl {
297297
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
298298
( $name:ident : &$t:ty = $default:expr, $($tail:tt)* )
299299
} => {
300-
py_argparse_parse_plist_impl! {
300+
$crate::py_argparse_parse_plist_impl! {
301301
$callback $initial_args
302302
[ $($output)* { $name:&$t = [ {} {$default} {$t} ] } ]
303303
($($tail)*)
@@ -307,7 +307,7 @@ macro_rules! py_argparse_parse_plist_impl {
307307
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
308308
( $name:ident : $t:ty = $default:expr , $($tail:tt)* )
309309
} => {
310-
py_argparse_parse_plist_impl! {
310+
$crate::py_argparse_parse_plist_impl! {
311311
$callback $initial_args
312312
[ $($output)* { $name:$t = [ {} {$default} {} ] } ]
313313
($($tail)*)
@@ -317,7 +317,7 @@ macro_rules! py_argparse_parse_plist_impl {
317317

318318
// The main py_argparse!() macro, except that it expects the parameter-list
319319
// in the output format of py_argparse_parse_plist!().
320-
#[macro_export(local_inner_macros)]
320+
#[macro_export]
321321
#[doc(hidden)]
322322
macro_rules! py_argparse_impl {
323323
// special case: function signature is (*args, **kwargs),
@@ -341,11 +341,11 @@ macro_rules! py_argparse_impl {
341341
) => {{
342342
const PARAMS: &'static [$crate::argparse::ParamDescription<'static>] = &[
343343
$(
344-
py_argparse_param_description! { $pname : $ptype = $detail }
344+
$crate::py_argparse_param_description! { $pname : $ptype = $detail }
345345
),*
346346
];
347347
let py: $crate::Python = $py;
348-
let mut output = [$( py_replace_expr!($pname None) ),*];
348+
let mut output = [$( $crate::py_replace_expr!($pname None) ),*];
349349
match $crate::argparse::parse_args(py, $fname, PARAMS, $args, $kwargs, &mut output) {
350350
Ok(()) => {
351351
// Experimental slice pattern syntax would be really nice here (#23121)
@@ -355,7 +355,7 @@ macro_rules! py_argparse_impl {
355355
// We'll have to generate a bunch of nested `match` statements
356356
// (at least until we can use ? + catch, assuming that will be hygienic wrt. macros),
357357
// so use a recursive helper macro for that:
358-
py_argparse_extract!( py, _iter, $body,
358+
$crate::py_argparse_extract!( py, _iter, $body,
359359
[ $( { $pname : $ptype = $detail } )* ])
360360
},
361361
Err(e) => Err(e)
@@ -364,14 +364,14 @@ macro_rules! py_argparse_impl {
364364
}
365365

366366
// Like py_argparse_impl!(), but accepts `*mut ffi::PyObject` for $args and $kwargs.
367-
#[macro_export(local_inner_macros)]
367+
#[macro_export]
368368
#[doc(hidden)]
369369
macro_rules! py_argparse_raw {
370370
($py:ident, $fname:expr, $args:expr, $kwargs:expr, $plist:tt $body:block) => {{
371371
let args: $crate::PyTuple =
372372
$crate::PyObject::from_borrowed_ptr($py, $args).unchecked_cast_into();
373373
let kwargs: Option<$crate::PyDict> = $crate::argparse::get_kwargs($py, $kwargs);
374-
let ret = py_argparse_impl!($py, $fname, &args, kwargs.as_ref(), $body, $plist);
374+
let ret = $crate::py_argparse_impl!($py, $fname, &args, kwargs.as_ref(), $body, $plist);
375375
$crate::PyDrop::release_ref(args, $py);
376376
$crate::PyDrop::release_ref(kwargs, $py);
377377
ret
@@ -407,7 +407,7 @@ macro_rules! py_argparse_param_description {
407407
);
408408
}
409409

410-
#[macro_export(local_inner_macros)]
410+
#[macro_export]
411411
#[doc(hidden)]
412412
macro_rules! py_argparse_extract {
413413
// base case
@@ -419,7 +419,7 @@ macro_rules! py_argparse_extract {
419419
// First unwrap() asserts the iterated sequence is long enough (which should be guaranteed);
420420
// second unwrap() asserts the parameter was not missing (which fn parse_args already checked for).
421421
match <$ptype as $crate::FromPyObject>::extract($py, $iter.next().unwrap().as_ref().unwrap()) {
422-
Ok($pname) => py_argparse_extract!($py, $iter, $body, [$($tail)*]),
422+
Ok($pname) => $crate::py_argparse_extract!($py, $iter, $body, [$($tail)*]),
423423
Err(e) => Err(e)
424424
}
425425
};
@@ -431,7 +431,7 @@ macro_rules! py_argparse_extract {
431431
// second unwrap() asserts the parameter was not missing (which fn parse_args already checked for).
432432
match <$rtype as $crate::RefFromPyObject>::with_extracted($py,
433433
$iter.next().unwrap().as_ref().unwrap(),
434-
|$pname: $ptype| py_argparse_extract!($py, $iter, $body, [$($tail)*])
434+
|$pname: $ptype| $crate::py_argparse_extract!($py, $iter, $body, [$($tail)*])
435435
) {
436436
Ok(v) => v,
437437
Err(e) => Err(e)
@@ -442,7 +442,7 @@ macro_rules! py_argparse_extract {
442442
[ { $pname:ident : $ptype:ty = [ {} {$default:expr} {} ] } $($tail:tt)* ]
443443
) => {
444444
match $iter.next().unwrap().as_ref().map(|obj| obj.extract::<_>($py)).unwrap_or(Ok($default)) {
445-
Ok($pname) => py_argparse_extract!($py, $iter, $body, [$($tail)*]),
445+
Ok($pname) => $crate::py_argparse_extract!($py, $iter, $body, [$($tail)*]),
446446
Err(e) => Err(e)
447447
}
448448
};
@@ -453,7 +453,7 @@ macro_rules! py_argparse_extract {
453453
//unwrap() asserts the iterated sequence is long enough (which should be guaranteed);
454454
$crate::argparse::with_extracted_or_default($py,
455455
$iter.next().unwrap().as_ref(),
456-
|$pname: $ptype| py_argparse_extract!($py, $iter, $body, [$($tail)*]),
456+
|$pname: $ptype| $crate::py_argparse_extract!($py, $iter, $body, [$($tail)*]),
457457
$default)
458458
};
459459
}

0 commit comments

Comments
 (0)