Changes from PR#760

This commit is contained in:
David Hewitt 2020-02-09 11:06:44 +00:00
parent 50bb41f398
commit de9698e7a5
2 changed files with 16 additions and 27 deletions

View file

@ -277,27 +277,23 @@ fn impl_call_getter(spec: &FnSpec) -> syn::Result<TokenStream> {
Ok(fncall)
}
/// Generate functiona wrapper (PyCFunction, PyCFunctionWithKeywords)
/// Generate a function wrapper called `__wrap` for a property getter
pub(crate) fn impl_wrap_getter(
cls: &syn::Type,
property_type: PropertyType,
) -> syn::Result<TokenStream> {
let python_name;
let getter_impl;
match property_type {
let (python_name, getter_impl) = match property_type {
PropertyType::Descriptor(field) => {
let name = field.ident.as_ref().unwrap();
python_name = name.unraw();
getter_impl = quote!({
use pyo3::derive_utils::GetPropertyValue;
(&_slf.#name).get_property_value(_py)
});
}
PropertyType::Function(spec) => {
python_name = spec.python_name.clone();
getter_impl = impl_call_getter(&spec)?;
(
name.unraw(),
quote!({
use pyo3::derive_utils::GetPropertyValue;
(&_slf.#name).get_property_value(_py)
})
)
}
PropertyType::Function(spec) => (spec.python_name.clone(), impl_call_getter(&spec)?)
};
Ok(quote! {
@ -350,24 +346,17 @@ fn impl_call_setter(spec: &FnSpec) -> syn::Result<TokenStream> {
Ok(fncall)
}
/// Generate functiona wrapper (PyCFunction, PyCFunctionWithKeywords)
/// Generate a function wrapper called `__wrap` for a property setter
pub(crate) fn impl_wrap_setter(
cls: &syn::Type,
property_type: PropertyType,
) -> syn::Result<TokenStream> {
let python_name;
let setter_impl;
match property_type {
let (python_name, setter_impl) = match property_type {
PropertyType::Descriptor(field) => {
let name = field.ident.as_ref().unwrap();
python_name = name.unraw();
setter_impl = quote!({ _slf.#name = _val; Ok(()) });
}
PropertyType::Function(spec) => {
python_name = spec.python_name.clone();
setter_impl = impl_call_setter(&spec)?;
(name.unraw(), quote!({ _slf.#name = _val; Ok(()) }))
}
PropertyType::Function(spec) => (spec.python_name.clone(), impl_call_setter(&spec)?)
};
Ok(quote! {

View file

@ -11,7 +11,7 @@ use crate::instance::PyNativeType;
use crate::pyclass::PyClass;
use crate::pyclass_init::PyClassInitializer;
use crate::types::{PyAny, PyDict, PyModule, PyTuple};
use crate::{ffi, GILPool, IntoPy, PyObject, Python, ToPyObject};
use crate::{ffi, GILPool, IntoPy, PyObject, Python};
use std::ptr;
/// Description of a python parameter; used for `parse_args()`.
@ -215,6 +215,6 @@ where
impl GetPropertyValue for PyObject {
fn get_property_value(&self, py: Python) -> PyObject {
self.to_object(py)
self.clone_ref(py)
}
}