Skip to content

Commit

Permalink
Rename the base option in the pyclass macro to extends
Browse files Browse the repository at this point in the history
"extends" is intuitive for people with java or ES6 experience, and it also aligns pyo3 with
wasm-bindgen (see rustwasm/rfcs#2)
  • Loading branch information
konstin committed Jul 15, 2018
1 parent eb613c6 commit dbd7440
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -115,7 +115,7 @@ impl BaseClass {
}
}

#[pyclass(base=BaseClass)]
#[pyclass(extends=BaseClass)]
struct SubClass {
val2: usize,
token: PyToken,
Expand Down
1 change: 1 addition & 0 deletions pyo3-derive-backend/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ mod test {

use args::{parse_arguments, Argument};
use syn;
use proc_macro::TokenStream;

fn items(s: TokenStream) -> Vec<syn::NestedMeta> {
let dummy: syn::ItemFn = parse_quote!{#s fn dummy() {}};
Expand Down
5 changes: 2 additions & 3 deletions pyo3-derive-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pyo3-derive-backend/src/py_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl Drop for BaseClassWithDrop {
}
}

#[pyclass(base=BaseClassWithDrop)]
#[pyclass(extends=BaseClassWithDrop)]
struct SubClassWithDrop {
token: PyToken,
data: Option<Arc<AtomicBool>>,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl BaseClass {
}
}

#[pyclass(base=BaseClass)]
#[pyclass(extends=BaseClass)]
struct SubClass {
#[prop(get)]
val2: usize,
Expand Down

0 comments on commit dbd7440

Please sign in to comment.