Skip to content

Commit d59bebc

Browse files
committed
Upgrade from proc_macro to use_extern_macros
1 parent dbd7440 commit d59bebc

27 files changed

+78
-107
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
* Upgraded to syn 0.14 which means much better error messages :tada:
66
* 128 bit integer support by [kngwyu](https://github.com/kngwyu) ([#137](https://github.com/PyO3/pyo3/pull/173))
7-
* Added `py` prefixes to the proc macros and moved them into the root module. You should just use the plain proc macros, i.e. `#[pyclass]`, `#[pymethods]`, `#[pyproto]`, `#[pyfunction]` and `#[pymodinit]`. This is important because `proc_macro_path_invoc` isn't going to be stabilized soon.
7+
* `proc_macro` has been stabilized on nightly ([rust-lang/rust#52081](https://github.com/rust-lang/rust/pull/52081)). This means that we can remove the `proc_macro` feature, but now we need the `use_extern_macros` from the 2018 edition instead.
8+
* All proc macro are now prefixed with `py` and live in the prelude. This means you can use `#[pyclass]`, `#[pymethods]`, `#[pyproto]`, `#[pyfunction]` and `#[pymodinit]` directly, at least after a `use pyo3::prelude::*`. They were also moved into a module called `proc_macro`. You shouldn't use `#[pyo3::proc_macro::pyclass]` or other longer paths in attributes because `proc_macro_path_invoc` isn't going to be stabilized soon.
89
* Renamed the `base` option in the `pyclass` macro to `extends`.
910
* `#[pymodinit]` uses the function name as module name, unless the name is overrriden with `#[pymodinit(name)]`
1011
* The guide is now properly versioned.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pyo3 = "0.2"
2525
Example program displaying the value of `sys.version`:
2626

2727
```rust
28-
#![feature(proc_macro, specialization)]
28+
#![feature(use_extern_macros, specialization)]
2929

3030
extern crate pyo3;
3131

@@ -69,12 +69,12 @@ features = ["extension-module"]
6969
**`src/lib.rs`**
7070

7171
```rust
72-
#![feature(proc_macro, specialization)]
72+
#![feature(use_extern_macros, specialization)]
7373

7474
extern crate pyo3;
7575
use pyo3::prelude::*;
7676

77-
use pyo3::pymodinit;
77+
7878

7979
// Add bindings to the generated python module
8080
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file

build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use std::process::Command;
99
use version_check::{is_min_date, is_min_version, supports_features};
1010

1111
// Specifies the minimum nightly version needed to compile pyo3.
12-
const MIN_DATE: &'static str = "2018-05-01";
13-
const MIN_VERSION: &'static str = "1.27.0-nightly";
12+
// This requirement is due to https://github.com/rust-lang/rust/pull/52081
13+
const MIN_DATE: &'static str = "2018-07-16";
14+
const MIN_VERSION: &'static str = "1.29.0-nightly";
1415

1516
#[derive(Debug)]
1617
struct PythonVersion {

examples/word-count-cls/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Source adopted from
22
// https://github.com/tildeio/helix-website/blob/master/crates/word_count/src/lib.rs
3-
#![feature(proc_macro, specialization)]
3+
#![feature(use_extern_macros, specialization)]
44

55
extern crate pyo3;
66
extern crate rayon;
77

88
use pyo3::prelude::*;
9-
use pyo3::{pyclass, pymethods, pymodinit};
109
use rayon::prelude::*;
1110
use std::fs::File;
1211
use std::io::prelude::*;

examples/word-count/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Source adopted from
22
// https://github.com/tildeio/helix-website/blob/master/crates/word_count/src/lib.rs
3-
#![feature(proc_macro, specialization)]
3+
#![feature(use_extern_macros, specialization)]
44
extern crate pyo3;
55
extern crate rayon;
66

@@ -10,8 +10,6 @@ use std::io::prelude::*;
1010
use pyo3::prelude::*;
1111
use rayon::prelude::*;
1212

13-
use pyo3::pymodinit;
14-
1513
fn matches(word: &str, search: &str) -> bool {
1614
let mut search = search.chars();
1715
for ch in word.chars().skip_while(|ch| !ch.is_alphabetic()) {

guide/src/class.md

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
To define python custom class, rust struct needs to be annotated with `#[pyclass]` attribute.
66

77
```rust
8-
# #![feature(proc_macro, specialization)]
8+
# #![feature(use_extern_macros, specialization)]
99
# extern crate pyo3;
1010
# use pyo3::prelude::*;
11-
use pyo3::pyclass;
11+
1212

1313
#[pyclass]
1414
struct MyClass {
@@ -45,11 +45,11 @@ To declare a constructor, you need to define a class method and annotate it with
4545
attribute. Only the python `__new__` method can be specified, `__init__` is not available.
4646

4747
```rust
48-
# #![feature(proc_macro, specialization)]
48+
# #![feature(use_extern_macros, specialization)]
4949
#
5050
# extern crate pyo3;
5151
# use pyo3::prelude::*;
52-
use pyo3::{pyclass, pymethods};
52+
5353

5454
#[pyclass]
5555
struct MyClass {
@@ -92,10 +92,9 @@ By default `PyObject` is used as default base class. To override default base cl
9292
with value of custom class struct. Subclass must call parent's `__new__` method.
9393

9494
```rust
95-
# #![feature(proc_macro, specialization)]
95+
# #![feature(use_extern_macros, specialization)]
9696
# extern crate pyo3;
9797
# use pyo3::prelude::*;
98-
# use pyo3::{pyclass, pymethods};
9998
#
10099
#[pyclass]
101100
struct BaseClass {
@@ -146,10 +145,9 @@ Descriptor methods can be defined in
146145
attributes. i.e.
147146

148147
```rust
149-
# #![feature(proc_macro, specialization)]
148+
# #![feature(use_extern_macros, specialization)]
150149
# extern crate pyo3;
151150
# use pyo3::prelude::*;
152-
# use pyo3::{pyclass, pymethods};
153151
# #[pyclass]
154152
# struct MyClass {
155153
# num: i32,
@@ -174,10 +172,9 @@ Descriptor name becomes function name with prefix removed. This is useful in cas
174172
rust's special keywords like `type`.
175173

176174
```rust
177-
# #![feature(proc_macro, specialization)]
175+
# #![feature(use_extern_macros, specialization)]
178176
# extern crate pyo3;
179177
# use pyo3::prelude::*;
180-
# use pyo3::{pyclass, pymethods};
181178
# #[pyclass]
182179
# struct MyClass {
183180
# num: i32,
@@ -206,10 +203,9 @@ Also both `#[getter]` and `#[setter]` attributes accepts one parameter.
206203
If parameter is specified, it is used and property name. i.e.
207204

208205
```rust
209-
# #![feature(proc_macro, specialization)]
206+
# #![feature(use_extern_macros, specialization)]
210207
# extern crate pyo3;
211208
# use pyo3::prelude::*;
212-
# use pyo3::{pyclass, pymethods};
213209
# #[pyclass]
214210
# struct MyClass {
215211
# num: i32,
@@ -237,10 +233,9 @@ In this case property `number` is defined. And it is available from python code
237233
For simple cases you can also define getters and setters in your Rust struct field definition, for example:
238234

239235
```rust
240-
# #![feature(proc_macro, specialization)]
236+
# #![feature(use_extern_macros, specialization)]
241237
# extern crate pyo3;
242238
# use pyo3::prelude::*;
243-
# use pyo3::{pyclass, pymethods};
244239
#[pyclass]
245240
struct MyClass {
246241
#[prop(get, set)]
@@ -258,10 +253,9 @@ wrappers for all functions in this block with some variations, like descriptors,
258253
class method static methods, etc.
259254

260255
```rust
261-
# #![feature(proc_macro, specialization)]
256+
# #![feature(use_extern_macros, specialization)]
262257
# extern crate pyo3;
263258
# use pyo3::prelude::*;
264-
# use pyo3::{pyclass, pymethods};
265259
# #[pyclass]
266260
# struct MyClass {
267261
# num: i32,
@@ -289,10 +283,9 @@ The return type must be `PyResult<T>` for some `T` that implements `IntoPyObject
289283
get injected by method wrapper. i.e
290284

291285
```rust
292-
# #![feature(proc_macro, specialization)]
286+
# #![feature(use_extern_macros, specialization)]
293287
# extern crate pyo3;
294288
# use pyo3::prelude::*;
295-
# use pyo3::{pyclass, pymethods};
296289
# #[pyclass]
297290
# struct MyClass {
298291
# num: i32,
@@ -316,10 +309,9 @@ To specify class method for custom class, method needs to be annotated
316309
with`#[classmethod]` attribute.
317310

318311
```rust
319-
# #![feature(proc_macro, specialization)]
312+
# #![feature(use_extern_macros, specialization)]
320313
# extern crate pyo3;
321314
# use pyo3::prelude::*;
322-
# use pyo3::{pyclass, pymethods};
323315
# #[pyclass]
324316
# struct MyClass {
325317
# num: i32,
@@ -351,10 +343,9 @@ with `#[staticmethod]` attribute. The return type must be `PyResult<T>`
351343
for some `T` that implements `IntoPyObject`.
352344

353345
```rust
354-
# #![feature(proc_macro, specialization)]
346+
# #![feature(use_extern_macros, specialization)]
355347
# extern crate pyo3;
356348
# use pyo3::prelude::*;
357-
# use pyo3::{pyclass, pymethods};
358349
# #[pyclass]
359350
# struct MyClass {
360351
# num: i32,
@@ -377,10 +368,9 @@ To specify custom `__call__` method for custom class, call method needs to be an
377368
with `#[call]` attribute. Arguments of the method are specified same as for instance method.
378369

379370
```rust
380-
# #![feature(proc_macro, specialization)]
371+
# #![feature(use_extern_macros, specialization)]
381372
# extern crate pyo3;
382373
# use pyo3::prelude::*;
383-
# use pyo3::{pyclass, pymethods};
384374
# #[pyclass]
385375
# struct MyClass {
386376
# num: i32,
@@ -423,10 +413,10 @@ Each parameter could one of following type:
423413

424414
Example:
425415
```rust
426-
# #![feature(proc_macro, specialization)]
416+
# #![feature(use_extern_macros, specialization)]
427417
# extern crate pyo3;
428418
# use pyo3::prelude::*;
429-
# use pyo3::{pyclass, pymethods};
419+
#
430420
# #[pyclass]
431421
# struct MyClass {
432422
# num: i32,
@@ -527,11 +517,11 @@ These correspond to the slots `tp_traverse` and `tp_clear` in the Python C API.
527517
as every cycle must contain at least one mutable reference.
528518
Example:
529519
```rust
530-
#![feature(proc_macro, specialization)]
520+
#![feature(use_extern_macros, specialization)]
531521
extern crate pyo3;
532522

533523
use pyo3::prelude::*;
534-
use pyo3::{pyclass, pyproto};
524+
535525

536526
#[pyclass]
537527
struct ClassWithGCSupport {
@@ -576,11 +566,11 @@ It includes two methods `__iter__` and `__next__`:
576566
Example:
577567

578568
```rust
579-
#![feature(proc_macro, specialization)]
569+
#![feature(use_extern_macros, specialization)]
580570
extern crate pyo3;
581571

582572
use pyo3::prelude::*;
583-
use pyo3::{pyclass, pyproto};
573+
584574

585575
#[pyclass]
586576
struct MyIterator {

guide/src/exception.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ trait can be implemented. In that case actual exception arguments creation get d
131131
until `Python` object is available.
132132

133133
```rust,ignore
134-
#![feature(proc_macro, specialization)]
134+
#![feature(use_extern_macros, specialization)]
135135
extern crate pyo3;
136136
137137
use std::net::TcpListener;

guide/src/function.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ One way is defining the function in the module definition.
1010

1111
extern crate pyo3;
1212
use pyo3::prelude::*;
13-
use pyo3::pymodinit;
13+
1414

1515
#[pymodinit]
1616
fn rust2py(py: Python, m: &PyModule) -> PyResult<()> {
@@ -34,13 +34,13 @@ as first parameter, the function name as second and an instance of `Python`
3434
as third.
3535

3636
```rust
37-
#![feature(proc_macro, concat_idents)]
37+
#![feature(use_extern_macros, concat_idents)]
3838

3939
#[macro_use]
4040
extern crate pyo3;
4141
use pyo3::prelude::*;
4242

43-
use pyo3::{pyfunction, pymodinit};
43+
4444

4545
#[pyfunction]
4646
fn double(x: usize) -> usize {

guide/src/module.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ As shown in the Getting Started chapter, you can create a module as follows:
88
extern crate pyo3;
99
use pyo3::{PyResult, Python, PyModule};
1010

11-
use pyo3::pymodinit;
11+
1212

1313
// add bindings to the generated python module
1414
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file

guide/src/overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ features = ["extension-module"]
5959
**`src/lib.rs`**
6060

6161
```rust
62-
#![feature(proc_macro, specialization)]
62+
#![feature(use_extern_macros, specialization)]
6363

6464
extern crate pyo3;
6565
use pyo3::prelude::*;
6666

67-
use pyo3::pymodinit;
67+
6868

6969
// Add bindings to the generated python module
7070
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file

0 commit comments

Comments
 (0)