From dbd74401eb29b0272fe5c967f2b03933fdb87fa2 Mon Sep 17 00:00:00 2001 From: konstin Date: Sun, 15 Jul 2018 11:58:33 +0200 Subject: [PATCH] Rename the `base` option in the `pyclass` macro to `extends` "extends" is intuitive for people with java or ES6 experience, and it also aligns pyo3 with wasm-bindgen (see https://github.com/rustwasm/rfcs/pull/2) --- CHANGELOG.md | 1 + guide/src/class.md | 4 ++-- pyo3-derive-backend/src/args.rs | 1 + pyo3-derive-backend/src/module.rs | 5 ++--- pyo3-derive-backend/src/py_class.rs | 2 +- tests/test_gc.rs | 2 +- tests/test_inheritance.rs | 2 +- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3cbcfcf..118705fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Upgraded to syn 0.14 which means much better error messages :tada: * 128 bit integer support by [kngwyu](https://github.com/kngwyu) ([#137](https://github.com/PyO3/pyo3/pull/173)) * 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. +* Renamed the `base` option in the `pyclass` macro to `extends`. * `#[pymodinit]` uses the function name as module name, unless the name is overrriden with `#[pymodinit(name)]` * The guide is now properly versioned. * A few internal macros became part of the public api ([#155](https://github.com/PyO3/pyo3/pull/155), [#186](https://github.com/PyO3/pyo3/pull/186)) diff --git a/guide/src/class.md b/guide/src/class.md index 0e5f31fc..d1ec3a92 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -33,7 +33,7 @@ so that they can benefit from a freelist. `XXX` is a number of items for free li participate in python garbage collector. If a custom class contains references to other python object that can be collected, the `PyGCProtocol` trait has to be implemented. * `weakref` - adds support for python weak references -* `base=BaseType` - use a custom base class. The base BaseType must implement `PyTypeInfo`. +* `extends=BaseType` - use a custom base class. The base BaseType must implement `PyTypeInfo`. * `subclass` - Allows Python classes to inherit from this class * `dict` - adds `__dict__` support, the instances of this type have a dictionary containing instance variables. (Incomplete, see [#123](https://github.com/PyO3/pyo3/issues/123)) @@ -115,7 +115,7 @@ impl BaseClass { } } -#[pyclass(base=BaseClass)] +#[pyclass(extends=BaseClass)] struct SubClass { val2: usize, token: PyToken, diff --git a/pyo3-derive-backend/src/args.rs b/pyo3-derive-backend/src/args.rs index 71027f6a..3522cb73 100644 --- a/pyo3-derive-backend/src/args.rs +++ b/pyo3-derive-backend/src/args.rs @@ -180,6 +180,7 @@ mod test { use args::{parse_arguments, Argument}; use syn; + use proc_macro::TokenStream; fn items(s: TokenStream) -> Vec { let dummy: syn::ItemFn = parse_quote!{#s fn dummy() {}}; diff --git a/pyo3-derive-backend/src/module.rs b/pyo3-derive-backend/src/module.rs index 8e57f9ac..d181c2d0 100644 --- a/pyo3-derive-backend/src/module.rs +++ b/pyo3-derive-backend/src/module.rs @@ -17,11 +17,10 @@ pub fn py3_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS quote! { #[no_mangle] #[allow(non_snake_case)] + #[doc(hidden)] /// This autogenerated function is called by the python interpreter when importing /// the module. pub unsafe extern "C" fn #cb_name() -> *mut ::pyo3::ffi::PyObject { - use ::pyo3::{IntoPyPointer, ObjectProtocol}; - ::pyo3::init_once(); static mut MODULE_DEF: ::pyo3::ffi::PyModuleDef = ::pyo3::ffi::PyModuleDef_INIT; @@ -48,7 +47,7 @@ pub fn py3_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS }; _module.add("__doc__", #doc).expect("Failed to add doc for module"); match #fnname(_py, _module) { - Ok(_) => _module.into_ptr(), + Ok(_) => ::pyo3::IntoPyPointer::into_ptr(_module), Err(e) => { e.restore(_py); ::std::ptr::null_mut() diff --git a/pyo3-derive-backend/src/py_class.rs b/pyo3-derive-backend/src/py_class.rs index 81cfd783..3d5e6b7a 100644 --- a/pyo3-derive-backend/src/py_class.rs +++ b/pyo3-derive-backend/src/py_class.rs @@ -445,7 +445,7 @@ fn parse_attribute( } _ => println!("Wrong 'name' format: {:?}", *ass.right), }, - "base" => match *ass.right { + "extends" => match *ass.right { syn::Expr::Path(ref exp) => { base = syn::TypePath { path: exp.path.clone(), diff --git a/tests/test_gc.rs b/tests/test_gc.rs index ef8f7b9b..3dac129b 100644 --- a/tests/test_gc.rs +++ b/tests/test_gc.rs @@ -231,7 +231,7 @@ impl Drop for BaseClassWithDrop { } } -#[pyclass(base=BaseClassWithDrop)] +#[pyclass(extends=BaseClassWithDrop)] struct SubClassWithDrop { token: PyToken, data: Option>, diff --git a/tests/test_inheritance.rs b/tests/test_inheritance.rs index 7702be84..ac368ee7 100644 --- a/tests/test_inheritance.rs +++ b/tests/test_inheritance.rs @@ -43,7 +43,7 @@ impl BaseClass { } } -#[pyclass(base=BaseClass)] +#[pyclass(extends=BaseClass)] struct SubClass { #[prop(get)] val2: usize,