From 5a8f5034d6844e49af21a8fcbb0ca239c3eeef9f Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 1 May 2018 20:15:43 +0200 Subject: [PATCH] Fix make test This is a follow-up to #147 --- .gitignore | 2 ++ Cargo.toml | 2 +- pyo3cls/src/lib.rs | 6 +++--- pyo3cls/src/module.rs | 4 ++-- pyo3cls/src/utils.rs | 15 +++++++++++++++ tests/test_module.rs | 1 + 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9c6831e9..5810b5b8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ Cargo.lock build/ __pycache__/ .cache +.pytest_cache/ +dist/ *.so *.out diff --git a/Cargo.toml b/Cargo.toml index 4c4746cd..73497f60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ log = "0.4" libc = "0.2" spin = "0.4.6" num-traits = "0.2" -pyo3cls = { version = "^0.2.1" } +pyo3cls = { path = "pyo3cls", version = "^0.2.1" } [build-dependencies] regex = "0.2" diff --git a/pyo3cls/src/lib.rs b/pyo3cls/src/lib.rs index aea0529e..4ac5674d 100644 --- a/pyo3cls/src/lib.rs +++ b/pyo3cls/src/lib.rs @@ -34,7 +34,7 @@ pub fn mod2init(attr: TokenStream, input: TokenStream) -> TokenStream { let mut ast = syn::parse_item(&source).unwrap(); // Build the output - let init = module::build_py2_module_init(&mut ast, attr.to_string()); + let init = module::build_py2_module_init(&mut ast, utils::attr_with_parentheses(attr)); // Return the generated impl as a TokenStream let mut tokens = Tokens::new(); @@ -53,7 +53,7 @@ pub fn mod3init(attr: TokenStream, input: TokenStream) -> TokenStream { let mut ast = syn::parse_item(&source).unwrap(); // Build the output - let init = module::build_py3_module_init(&mut ast, attr.to_string()); + let init = module::build_py3_module_init(&mut ast, utils::attr_with_parentheses(attr)); // Return the generated impl as a TokenStream let mut tokens = Tokens::new(); @@ -91,7 +91,7 @@ pub fn class(attr: TokenStream, input: TokenStream) -> TokenStream { let mut ast = syn::parse_derive_input(&source).unwrap(); // Build the output - let expanded = py_class::build_py_class(&mut ast, attr.to_string()); + let expanded = py_class::build_py_class(&mut ast, utils::attr_with_parentheses(attr)); // Return the generated impl as a TokenStream let mut tokens = Tokens::new(); diff --git a/pyo3cls/src/module.rs b/pyo3cls/src/module.rs index 0aa9993d..24264641 100644 --- a/pyo3cls/src/module.rs +++ b/pyo3cls/src/module.rs @@ -10,7 +10,7 @@ use utils; pub fn build_py3_module_init(ast: &mut syn::Item, attr: String) -> Tokens { - let modname = &attr.to_string()[1..attr.to_string().len()-1].to_string(); + let modname = &attr[1..attr.len()-1].to_string(); match ast.node { syn::ItemKind::Fn(_, _, _, _, _, ref mut block) => { @@ -86,7 +86,7 @@ pub fn py3_init(fnname: &syn::Ident, name: &String, doc: syn::Lit) -> Tokens { } pub fn build_py2_module_init(ast: &mut syn::Item, attr: String) -> Tokens { - let modname = &attr.to_string()[1..attr.to_string().len()-1].to_string(); + let modname = &attr[1..attr.len()-1].to_string(); match ast.node { syn::ItemKind::Fn(_, _, _, _, _, ref mut block) => { diff --git a/pyo3cls/src/utils.rs b/pyo3cls/src/utils.rs index f0c10124..aed9ed20 100644 --- a/pyo3cls/src/utils.rs +++ b/pyo3cls/src/utils.rs @@ -1,8 +1,23 @@ // Copyright (c) 2017-present PyO3 Project and Contributors use syn; use quote::{Tokens, ToTokens}; +use proc_macro::TokenStream; +/// https://github.com/rust-lang/rust/pull/50120 removed the parantheses from +/// the attr TokenStream, so we need to re-add them manually. +/// +/// nightly-2018-04-05: ( name=CustomName ) +/// nightly-2018-04-28: name=CustomName +pub fn attr_with_parentheses(attr: TokenStream) -> String { + let attr = attr.to_string(); + if attr.len() > 0 && !attr.starts_with("(") { + return format!("({})", attr); + } else { + return attr; + } +} + pub fn print_err(msg: String, t: Tokens) { println!("Error: {} in '{}'", msg, t.to_string()); } diff --git a/tests/test_module.rs b/tests/test_module.rs index 730da0e0..95562864 100644 --- a/tests/test_module.rs +++ b/tests/test_module.rs @@ -36,6 +36,7 @@ fn init_mod(py: Python, m: &PyModule) -> PyResult<()> { } #[test] +#[cfg(Py_3)] fn test_module_with_functions() { let gil = Python::acquire_gil(); let py = gil.python();