From 29fdc05b604baddd5450c662a9e277661264e48f Mon Sep 17 00:00:00 2001 From: konstin Date: Sun, 1 Apr 2018 15:04:45 +0200 Subject: [PATCH 1/3] Move the codegen into its own crate This makes the code generatioin reusable from third party crates. --- pyo3-derive-backend/Cargo.toml | 18 ++++++++++++++++ {pyo3cls => pyo3-derive-backend}/src/args.rs | 0 {pyo3cls => pyo3-derive-backend}/src/defs.rs | 0 {pyo3cls => pyo3-derive-backend}/src/func.rs | 0 pyo3-derive-backend/src/lib.rs | 21 +++++++++++++++++++ .../src/method.rs | 0 .../src/module.rs | 0 .../src/py_class.rs | 0 .../src/py_impl.rs | 0 .../src/py_method.rs | 0 .../src/py_proto.rs | 0 {pyo3cls => pyo3-derive-backend}/src/utils.rs | 0 pyo3cls/Cargo.toml | 4 +++- pyo3cls/src/lib.rs | 16 +++----------- 14 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 pyo3-derive-backend/Cargo.toml rename {pyo3cls => pyo3-derive-backend}/src/args.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/defs.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/func.rs (100%) create mode 100644 pyo3-derive-backend/src/lib.rs rename {pyo3cls => pyo3-derive-backend}/src/method.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/module.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/py_class.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/py_impl.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/py_method.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/py_proto.rs (100%) rename {pyo3cls => pyo3-derive-backend}/src/utils.rs (100%) diff --git a/pyo3-derive-backend/Cargo.toml b/pyo3-derive-backend/Cargo.toml new file mode 100644 index 00000000..c1f5b738 --- /dev/null +++ b/pyo3-derive-backend/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "pyo3-derive-backend" +version = "0.2.1" +description = "Code generation for PyO3 package" +authors = ["PyO3 Project and Contributors Date: Sun, 1 Apr 2018 15:13:32 +0200 Subject: [PATCH 2/3] Expect the pyo3 crate to be in scope for the codegen This allows using generated code from crates that do not directly depend on pyo3. E.g.: ```rust extern crate my_pyo3_wrapper; use my_pyo3_wrapper::pyo3; #[py::modinit(rust2py)] fn init_mod(py: Python, m: &PyModule) -> PyResult<()> { // ... Ok(()) } --- pyo3-derive-backend/src/module.rs | 2 -- pyo3-derive-backend/src/py_class.rs | 4 ++-- pyo3-derive-backend/src/py_impl.rs | 2 +- pyo3-derive-backend/src/py_proto.rs | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pyo3-derive-backend/src/module.rs b/pyo3-derive-backend/src/module.rs index 24264641..dd9601ca 100644 --- a/pyo3-derive-backend/src/module.rs +++ b/pyo3-derive-backend/src/module.rs @@ -44,7 +44,6 @@ pub fn py3_init(fnname: &syn::Ident, name: &String, doc: syn::Lit) -> Tokens { #[no_mangle] #[allow(non_snake_case, unused_imports)] pub unsafe extern "C" fn #cb_name() -> *mut ::pyo3::ffi::PyObject { - extern crate pyo3; use std; use pyo3::{IntoPyPointer, ObjectProtocol}; @@ -121,7 +120,6 @@ pub fn py2_init(fnname: &syn::Ident, name: &String, doc: syn::Lit) -> Tokens { #[no_mangle] #[allow(non_snake_case, unused_imports)] pub unsafe extern "C" fn #cb_name() { - extern crate pyo3; use std; // initialize python diff --git a/pyo3-derive-backend/src/py_class.rs b/pyo3-derive-backend/src/py_class.rs index 487229d4..16f2812b 100644 --- a/pyo3-derive-backend/src/py_class.rs +++ b/pyo3-derive-backend/src/py_class.rs @@ -41,7 +41,7 @@ pub fn build_py_class(ast: &mut syn::DeriveInput, attr: String) -> Tokens { unused_qualifications, unused_variables, non_camel_case_types)] const #dummy_const: () = { use std; - extern crate pyo3 as _pyo3; + use pyo3 as _pyo3; #tokens }; @@ -372,7 +372,7 @@ fn impl_descriptors(cls: &syn::Ty, descriptors: Vec<(syn::Field, Vec)>) #[allow(non_upper_case_globals, unused_attributes, unused_qualifications, unused_variables, unused_imports)] const #dummy_const: () = { - extern crate pyo3 as _pyo3; + use pyo3 as _pyo3; #tokens }; diff --git a/pyo3-derive-backend/src/py_impl.rs b/pyo3-derive-backend/src/py_impl.rs index 025a0f7a..c53ac69b 100644 --- a/pyo3-derive-backend/src/py_impl.rs +++ b/pyo3-derive-backend/src/py_impl.rs @@ -57,7 +57,7 @@ fn impl_methods(ty: &Box, impls: &mut Vec) -> Tokens { #[allow(non_upper_case_globals, unused_attributes, unused_qualifications, unused_variables, unused_imports)] const #dummy_const: () = { - extern crate pyo3 as _pyo3; + use pyo3 as _pyo3; #tokens }; diff --git a/pyo3-derive-backend/src/py_proto.rs b/pyo3-derive-backend/src/py_proto.rs index b012606e..b18b8709 100644 --- a/pyo3-derive-backend/src/py_proto.rs +++ b/pyo3-derive-backend/src/py_proto.rs @@ -128,7 +128,7 @@ fn impl_proto_impl(ty: &Box, #[allow(non_upper_case_globals, unused_attributes, unused_qualifications, unused_variables)] const #dummy_const: () = { - extern crate pyo3 as _pyo3; + use pyo3 as _pyo3; #tokens From 2fcac850033fdfba58fb6f6feddc1a2ad7780790 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 1 May 2018 20:25:13 +0200 Subject: [PATCH 3/3] Add version in Cargo.toml Following https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies, we need a version for path dependencies --- Cargo.toml | 2 +- pyo3-derive-backend/Cargo.toml | 2 +- pyo3cls/Cargo.toml | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73497f60..2d1f0123 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 = { path = "pyo3cls", version = "^0.2.1" } +pyo3cls = { path = "pyo3cls", version = "0.2.5" } [build-dependencies] regex = "0.2" diff --git a/pyo3-derive-backend/Cargo.toml b/pyo3-derive-backend/Cargo.toml index c1f5b738..130b19df 100644 --- a/pyo3-derive-backend/Cargo.toml +++ b/pyo3-derive-backend/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pyo3-derive-backend" -version = "0.2.1" +version = "0.2.5" description = "Code generation for PyO3 package" authors = ["PyO3 Project and Contributors