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)
This commit is contained in:
konstin 2018-07-15 11:58:33 +02:00
parent eb613c64d9
commit dbd74401eb
7 changed files with 9 additions and 8 deletions

View File

@ -5,6 +5,7 @@
* Upgraded to syn 0.14 which means much better error messages :tada: * 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)) * 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. * 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)]` * `#[pymodinit]` uses the function name as module name, unless the name is overrriden with `#[pymodinit(name)]`
* The guide is now properly versioned. * 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)) * 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))

View File

@ -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 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. python object that can be collected, the `PyGCProtocol` trait has to be implemented.
* `weakref` - adds support for python weak references * `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 * `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)) * `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 { struct SubClass {
val2: usize, val2: usize,
token: PyToken, token: PyToken,

View File

@ -180,6 +180,7 @@ mod test {
use args::{parse_arguments, Argument}; use args::{parse_arguments, Argument};
use syn; use syn;
use proc_macro::TokenStream;
fn items(s: TokenStream) -> Vec<syn::NestedMeta> { fn items(s: TokenStream) -> Vec<syn::NestedMeta> {
let dummy: syn::ItemFn = parse_quote!{#s fn dummy() {}}; let dummy: syn::ItemFn = parse_quote!{#s fn dummy() {}};

View File

@ -17,11 +17,10 @@ pub fn py3_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS
quote! { quote! {
#[no_mangle] #[no_mangle]
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[doc(hidden)]
/// This autogenerated function is called by the python interpreter when importing /// This autogenerated function is called by the python interpreter when importing
/// the module. /// the module.
pub unsafe extern "C" fn #cb_name() -> *mut ::pyo3::ffi::PyObject { pub unsafe extern "C" fn #cb_name() -> *mut ::pyo3::ffi::PyObject {
use ::pyo3::{IntoPyPointer, ObjectProtocol};
::pyo3::init_once(); ::pyo3::init_once();
static mut MODULE_DEF: ::pyo3::ffi::PyModuleDef = ::pyo3::ffi::PyModuleDef_INIT; 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"); _module.add("__doc__", #doc).expect("Failed to add doc for module");
match #fnname(_py, _module) { match #fnname(_py, _module) {
Ok(_) => _module.into_ptr(), Ok(_) => ::pyo3::IntoPyPointer::into_ptr(_module),
Err(e) => { Err(e) => {
e.restore(_py); e.restore(_py);
::std::ptr::null_mut() ::std::ptr::null_mut()

View File

@ -445,7 +445,7 @@ fn parse_attribute(
} }
_ => println!("Wrong 'name' format: {:?}", *ass.right), _ => println!("Wrong 'name' format: {:?}", *ass.right),
}, },
"base" => match *ass.right { "extends" => match *ass.right {
syn::Expr::Path(ref exp) => { syn::Expr::Path(ref exp) => {
base = syn::TypePath { base = syn::TypePath {
path: exp.path.clone(), path: exp.path.clone(),

View File

@ -231,7 +231,7 @@ impl Drop for BaseClassWithDrop {
} }
} }
#[pyclass(base=BaseClassWithDrop)] #[pyclass(extends=BaseClassWithDrop)]
struct SubClassWithDrop { struct SubClassWithDrop {
token: PyToken, token: PyToken,
data: Option<Arc<AtomicBool>>, data: Option<Arc<AtomicBool>>,

View File

@ -43,7 +43,7 @@ impl BaseClass {
} }
} }
#[pyclass(base=BaseClass)] #[pyclass(extends=BaseClass)]
struct SubClass { struct SubClass {
#[prop(get)] #[prop(get)]
val2: usize, val2: usize,