From 2c95b3abb4189226dc3d19f231662e1a599c3d3c Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Mon, 4 Apr 2022 19:10:28 +0200 Subject: [PATCH] Intern the attribute names used by the derive macro for FromPyObject. --- pyo3-macros-backend/src/frompyobject.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pyo3-macros-backend/src/frompyobject.rs b/pyo3-macros-backend/src/frompyobject.rs index c55f14b3..2af52079 100644 --- a/pyo3-macros-backend/src/frompyobject.rs +++ b/pyo3-macros-backend/src/frompyobject.rs @@ -294,8 +294,10 @@ impl<'a> Container<'a> { let mut fields: Punctuated = Punctuated::new(); for (ident, attrs) in tups { let getter = match &attrs.getter { - FieldGetter::GetAttr(Some(name)) => quote!(getattr(#name)), - FieldGetter::GetAttr(None) => quote!(getattr(stringify!(#ident))), + FieldGetter::GetAttr(Some(name)) => quote!(getattr(_pyo3::intern!(py, #name))), + FieldGetter::GetAttr(None) => { + quote!(getattr(_pyo3::intern!(py, stringify!(#ident)))) + } FieldGetter::GetItem(Some(key)) => quote!(get_item(#key)), FieldGetter::GetItem(None) => quote!(get_item(stringify!(#ident))), }; @@ -303,13 +305,14 @@ impl<'a> Container<'a> { format!("failed to extract field {}.{}", quote!(#self_ty), ident); let get_field = quote!(obj.#getter?); let extractor = match &attrs.from_py_with { - None => quote!( - #get_field.extract().map_err(|inner| { + None => quote!({ let py = _pyo3::PyNativeType::py(obj); - let new_err = _pyo3::exceptions::PyTypeError::new_err(#conversion_error_msg); - new_err.set_cause(py, ::std::option::Option::Some(inner)); - new_err - })?), + #get_field.extract().map_err(|inner| { + let new_err = _pyo3::exceptions::PyTypeError::new_err(#conversion_error_msg); + new_err.set_cause(py, ::std::option::Option::Some(inner)); + new_err + })? + }), Some(FromPyWithAttribute { value: expr_path, .. }) => quote! (