Skip to content

Fix compilation on nightly #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ features = ["extension-module"]
extern crate pyo3;
use pyo3::prelude::*;

use pyo3::py::modinit as pymodinit;

// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
/// This module is implemented in Rust.
#[py::modinit(rust2py)]
#[pymodinit(rust2py)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

py::modinit doesn't work without the proc_macro_path_invoc feature, which isn't expected to be stabilized soon (unlike proc macros and specialization).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {

#[pyfn(m, "sum_as_string")]
Expand Down
12 changes: 7 additions & 5 deletions examples/word-count-cls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ use std::io::prelude::*;
use rayon::prelude::*;
use pyo3::prelude::*;

#[py::class]
use pyo3::py::methods as pymethods;
use pyo3::py::class as pyclass;
use pyo3::py::modinit as pymodinit;

#[pyclass]
struct WordCounter {
path: String,
token: PyToken,
}

#[py::methods]
#[pymethods]
impl WordCounter {

#[new]
Expand Down Expand Up @@ -81,9 +85,7 @@ fn wc_parallel(lines: &str, search: &str) -> i32 {
.sum()
}



#[py::modinit(_word_count)]
#[pymodinit(_word_count)]
fn init_mod(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<WordCounter>()?;

Expand Down
4 changes: 3 additions & 1 deletion examples/word-count/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::io::prelude::*;
use rayon::prelude::*;
use pyo3::prelude::*;

use pyo3::py::modinit as pymodinit;

fn matches(word: &str, search: &str) -> bool {
let mut search = search.chars();
for ch in word.chars().skip_while(|ch| !ch.is_alphabetic()) {
Expand Down Expand Up @@ -47,7 +49,7 @@ fn wc_parallel(lines: &str, search: &str) -> i32 {
.sum()
}

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

#[pyfn(m, "search")]
Expand Down
4 changes: 3 additions & 1 deletion guide/src/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ Now you can write your module, for example
extern crate pyo3;
use pyo3::{py, PyResult, Python, PyModule};

use pyo3::py::modinit as pymodinit;

// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
/// This module is implemented in Rust.
#[py::modinit(rust2py)]
#[pymodinit(rust2py)]
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {

// pyo3 aware function. All of our python interface could be declared in a separate module.
Expand Down
4 changes: 3 additions & 1 deletion guide/src/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ features = ["extension-module"]
extern crate pyo3;
use pyo3::{py, PyResult, Python, PyModule};

use pyo3::py::modinit as pymodinit;

// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
/// This module is implemented in Rust.
#[py::modinit(rust2py)]
#[pymodinit(rust2py)]
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {

#[pyfn(m, "sum_as_string")]
Expand Down
2 changes: 1 addition & 1 deletion guide/src/parallelism.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Then in the Python bridge, we have a function `search` exposed to Python runtime
`Python::allow_threads` method to enable true parallelism:

```rust,ignore
#[py::modinit(_word_count)]
#[pymodinit(_word_count)]
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {

#[pyfn(m, "search")]
Expand Down
23 changes: 16 additions & 7 deletions pyo3cls/src/py_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,20 +391,29 @@ fn is_python_token(field: &syn::Field) -> bool {
return false
}

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

// https://github.com/rust-lang/rust/pull/50120 removed the parantheses from
// the attr TokenStream, so we need to re-add them manually
// Old nightly (like 2018-04-05): ( name=CustomName )
// New nightly (like 2018-04-28): name=CustomName

if attr.len() > 0 && !attr.starts_with("(") {
attr = format!("({})", attr);
}

if let Ok(tts) = syn::parse_token_trees(&attr) {
let mut elem = Vec::new();
let mut elems = Vec::new();

for tt in tts.iter() {
match tt {
&syn::TokenTree::Token(_) => {
println!("Wrong format: {:?}", attr.to_string());
&syn::TokenTree::Token(ref token) => {
println!("Wrong format: Expected delimited, found token: {:?} {:?}", attr.to_string(), token);
}
&syn::TokenTree::Delimited(ref delimited) => {
for tt in delimited.tts.iter() {
Expand All @@ -429,7 +438,7 @@ fn parse_attribute(attr: String) -> (HashMap<&'static str, syn::Ident>,
ident.as_ref().to_owned().to_lowercase()
},
_ => {
println!("Wrong format: {:?}", attr.to_string());
println!("Wrong format: Expected Token: {:?}", attr.to_string());
continue
}
};
Expand Down Expand Up @@ -459,14 +468,14 @@ fn parse_attribute(attr: String) -> (HashMap<&'static str, syn::Ident>,
}

if elem.len() < 3 {
println!("Wrong format: {:?}", elem);
println!("Wrong format: Less than three elements{:?}", elem);
continue
}

match elem[1] {
syn::TokenTree::Token(syn::Token::Eq) => (),
_ => {
println!("Wrong format: {:?}", attr.to_string());
println!("Wrong format: Expected a Token as fist element: {:?}", attr.to_string());
continue
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
//! # Python extension
//!
//! To allow Python to load the rust code as a Python extension
//! module, you need provide initialization function and annotate it with `#[py::modinit(name)]`.
//! `py::modinit` expands to an `extern "C"` function.
//! module, you need provide initialization function and annotate it with `#[pymodinit(name)]`.
//! `pymodinit` expands to an `extern "C"` function.
//!
//! Macro syntax: `#[py::modinit(name)]`
//! Macro syntax: `#[pymodinit(name)]`
//!
//! 1. `name`: The module name as a Rust identifier
//! 2. Decorate init function `Fn(Python, &PyModule) -> PyResult<()>`.
Expand All @@ -80,11 +80,13 @@
//! extern crate pyo3;
//! use pyo3::{py, Python, PyResult, PyModule, PyString};
//!
//! use pyo3::py::modinit as pymodinit;
//!
//! // add bindings to the generated python module
//! // N.B: names: "libhello" must be the name of the `.so` or `.pyd` file
//!
//! /// Module documentation string
//! #[py::modinit(hello)]
//! #[pymodinit(hello)]
//! fn init_module(py: Python, m: &PyModule) -> PyResult<()> {
//!
//! // pyo3 aware function. All of our python interface could be declared
Expand Down
2 changes: 1 addition & 1 deletion src/pythonrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static START_PYO3: sync::Once = sync::ONCE_INIT;
/// thread (the thread which originally initialized Python) also initializes
/// threading.
///
/// When writing an extension module, the `#[py::modinit(..)]` macro
/// When writing an extension module, the `#[pymodinit(..)]` macro
/// will ensure that Python threading is initialized.
///
pub fn prepare_freethreaded_python() {
Expand Down
7 changes: 5 additions & 2 deletions tests/test_buffer_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ use std::os::raw::{c_int, c_void};

use pyo3::*;

use pyo3::py::class as pyclass;
use pyo3::py::proto as pyproto;

#[py::class]

#[pyclass]
struct TestClass {
vec: Vec<u8>,
token: PyToken,
}

#[py::proto]
#[pyproto]
impl class::PyBufferProtocol for TestClass {

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