Skip to content

Commit 4735c2c

Browse files
authored
Merge pull request #147 from konstin/proc_macro_path_invoc
Fix compilation on nightly
2 parents 14ab12b + d0c42df commit 4735c2c

File tree

12 files changed

+140
-110
lines changed

12 files changed

+140
-110
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ features = ["extension-module"]
8383
extern crate pyo3;
8484
use pyo3::prelude::*;
8585

86+
use pyo3::py::modinit as pymodinit;
87+
8688
// add bindings to the generated python module
8789
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
8890
/// This module is implemented in Rust.
89-
#[py::modinit(rust2py)]
91+
#[pymodinit(rust2py)]
9092
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
9193

9294
#[pyfn(m, "sum_as_string")]

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ use std::io::prelude::*;
1212
use rayon::prelude::*;
1313
use pyo3::prelude::*;
1414

15-
#[py::class]
15+
use pyo3::py::methods as pymethods;
16+
use pyo3::py::class as pyclass;
17+
use pyo3::py::modinit as pymodinit;
18+
19+
#[pyclass]
1620
struct WordCounter {
1721
path: String,
1822
token: PyToken,
1923
}
2024

21-
#[py::methods]
25+
#[pymethods]
2226
impl WordCounter {
2327

2428
#[new]
@@ -81,9 +85,7 @@ fn wc_parallel(lines: &str, search: &str) -> i32 {
8185
.sum()
8286
}
8387

84-
85-
86-
#[py::modinit(_word_count)]
88+
#[pymodinit(_word_count)]
8789
fn init_mod(_py: Python, m: &PyModule) -> PyResult<()> {
8890
m.add_class::<WordCounter>()?;
8991

examples/word-count/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use std::io::prelude::*;
1010
use rayon::prelude::*;
1111
use pyo3::prelude::*;
1212

13+
use pyo3::py::modinit as pymodinit;
14+
1315
fn matches(word: &str, search: &str) -> bool {
1416
let mut search = search.chars();
1517
for ch in word.chars().skip_while(|ch| !ch.is_alphabetic()) {
@@ -47,7 +49,7 @@ fn wc_parallel(lines: &str, search: &str) -> i32 {
4749
.sum()
4850
}
4951

50-
#[py::modinit(_word_count)]
52+
#[pymodinit(_word_count)]
5153
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
5254

5355
#[pyfn(m, "search")]

guide/src/module.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ Now you can write your module, for example
2828
extern crate pyo3;
2929
use pyo3::{py, PyResult, Python, PyModule};
3030

31+
use pyo3::py::modinit as pymodinit;
32+
3133
// add bindings to the generated python module
3234
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
3335
/// This module is implemented in Rust.
34-
#[py::modinit(rust2py)]
36+
#[pymodinit(rust2py)]
3537
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
3638

3739
// pyo3 aware function. All of our python interface could be declared in a separate module.

guide/src/overview.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ features = ["extension-module"]
7878
extern crate pyo3;
7979
use pyo3::{py, PyResult, Python, PyModule};
8080

81+
use pyo3::py::modinit as pymodinit;
82+
8183
// add bindings to the generated python module
8284
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
8385
/// This module is implemented in Rust.
84-
#[py::modinit(rust2py)]
86+
#[pymodinit(rust2py)]
8587
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
8688

8789
#[pyfn(m, "sum_as_string")]

guide/src/parallelism.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Then in the Python bridge, we have a function `search` exposed to Python runtime
2828
`Python::allow_threads` method to enable true parallelism:
2929

3030
```rust,ignore
31-
#[py::modinit(_word_count)]
31+
#[pymodinit(_word_count)]
3232
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
3333
3434
#[pyfn(m, "search")]

pyo3cls/src/py_class.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,20 +391,29 @@ fn is_python_token(field: &syn::Field) -> bool {
391391
return false
392392
}
393393

394-
fn parse_attribute(attr: String) -> (HashMap<&'static str, syn::Ident>,
395-
Vec<syn::Ident>, syn::Ident) {
394+
fn parse_attribute(mut attr: String) -> (HashMap<&'static str, syn::Ident>,
395+
Vec<syn::Ident>, syn::Ident) {
396396
let mut params = HashMap::new();
397397
let mut flags = vec![syn::Ident::from("0")];
398398
let mut base = syn::Ident::from("_pyo3::PyObjectRef");
399399

400+
// https://github.com/rust-lang/rust/pull/50120 removed the parantheses from
401+
// the attr TokenStream, so we need to re-add them manually
402+
// Old nightly (like 2018-04-05): ( name=CustomName )
403+
// New nightly (like 2018-04-28): name=CustomName
404+
405+
if attr.len() > 0 && !attr.starts_with("(") {
406+
attr = format!("({})", attr);
407+
}
408+
400409
if let Ok(tts) = syn::parse_token_trees(&attr) {
401410
let mut elem = Vec::new();
402411
let mut elems = Vec::new();
403412

404413
for tt in tts.iter() {
405414
match tt {
406-
&syn::TokenTree::Token(_) => {
407-
println!("Wrong format: {:?}", attr.to_string());
415+
&syn::TokenTree::Token(ref token) => {
416+
println!("Wrong format: Expected delimited, found token: {:?} {:?}", attr.to_string(), token);
408417
}
409418
&syn::TokenTree::Delimited(ref delimited) => {
410419
for tt in delimited.tts.iter() {
@@ -429,7 +438,7 @@ fn parse_attribute(attr: String) -> (HashMap<&'static str, syn::Ident>,
429438
ident.as_ref().to_owned().to_lowercase()
430439
},
431440
_ => {
432-
println!("Wrong format: {:?}", attr.to_string());
441+
println!("Wrong format: Expected Token: {:?}", attr.to_string());
433442
continue
434443
}
435444
};
@@ -459,14 +468,14 @@ fn parse_attribute(attr: String) -> (HashMap<&'static str, syn::Ident>,
459468
}
460469

461470
if elem.len() < 3 {
462-
println!("Wrong format: {:?}", elem);
471+
println!("Wrong format: Less than three elements{:?}", elem);
463472
continue
464473
}
465474

466475
match elem[1] {
467476
syn::TokenTree::Token(syn::Token::Eq) => (),
468477
_ => {
469-
println!("Wrong format: {:?}", attr.to_string());
478+
println!("Wrong format: Expected a Token as fist element: {:?}", attr.to_string());
470479
continue
471480
}
472481
}

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
//! # Python extension
5555
//!
5656
//! To allow Python to load the rust code as a Python extension
57-
//! module, you need provide initialization function and annotate it with `#[py::modinit(name)]`.
58-
//! `py::modinit` expands to an `extern "C"` function.
57+
//! module, you need provide initialization function and annotate it with `#[pymodinit(name)]`.
58+
//! `pymodinit` expands to an `extern "C"` function.
5959
//!
60-
//! Macro syntax: `#[py::modinit(name)]`
60+
//! Macro syntax: `#[pymodinit(name)]`
6161
//!
6262
//! 1. `name`: The module name as a Rust identifier
6363
//! 2. Decorate init function `Fn(Python, &PyModule) -> PyResult<()>`.
@@ -80,11 +80,13 @@
8080
//! extern crate pyo3;
8181
//! use pyo3::{py, Python, PyResult, PyModule, PyString};
8282
//!
83+
//! use pyo3::py::modinit as pymodinit;
84+
//!
8385
//! // add bindings to the generated python module
8486
//! // N.B: names: "libhello" must be the name of the `.so` or `.pyd` file
8587
//!
8688
//! /// Module documentation string
87-
//! #[py::modinit(hello)]
89+
//! #[pymodinit(hello)]
8890
//! fn init_module(py: Python, m: &PyModule) -> PyResult<()> {
8991
//!
9092
//! // pyo3 aware function. All of our python interface could be declared

src/pythonrun.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static START_PYO3: sync::Once = sync::ONCE_INIT;
2727
/// thread (the thread which originally initialized Python) also initializes
2828
/// threading.
2929
///
30-
/// When writing an extension module, the `#[py::modinit(..)]` macro
30+
/// When writing an extension module, the `#[pymodinit(..)]` macro
3131
/// will ensure that Python threading is initialized.
3232
///
3333
pub fn prepare_freethreaded_python() {

tests/test_buffer_protocol.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ use std::os::raw::{c_int, c_void};
88

99
use pyo3::*;
1010

11+
use pyo3::py::class as pyclass;
12+
use pyo3::py::proto as pyproto;
1113

12-
#[py::class]
14+
15+
#[pyclass]
1316
struct TestClass {
1417
vec: Vec<u8>,
1518
token: PyToken,
1619
}
1720

18-
#[py::proto]
21+
#[pyproto]
1922
impl class::PyBufferProtocol for TestClass {
2023

2124
fn bf_getbuffer(&self, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> {

0 commit comments

Comments
 (0)