From 8026f3521e6b1e9877f81283e5da4a7932565a4c Mon Sep 17 00:00:00 2001 From: Sunyeop Lee Date: Sat, 14 Jan 2023 04:02:20 +0900 Subject: [PATCH] Improve derive(FromPyObject) to apply intern! when applicable --- newsfragments/2879.changed.md | 1 + pyo3-macros-backend/src/frompyobject.rs | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 newsfragments/2879.changed.md diff --git a/newsfragments/2879.changed.md b/newsfragments/2879.changed.md new file mode 100644 index 00000000000..42c5287a3e9 --- /dev/null +++ b/newsfragments/2879.changed.md @@ -0,0 +1 @@ +Improve `derive(FromPyObject)` to apply `intern!` when applicable to `#[pyo3(item)]`. diff --git a/pyo3-macros-backend/src/frompyobject.rs b/pyo3-macros-backend/src/frompyobject.rs index 9693805f5bf..ba365efe265 100644 --- a/pyo3-macros-backend/src/frompyobject.rs +++ b/pyo3-macros-backend/src/frompyobject.rs @@ -315,8 +315,13 @@ impl<'a> Container<'a> { FieldGetter::GetAttr(None) => { quote!(getattr(_pyo3::intern!(obj.py(), #field_name))) } + FieldGetter::GetItem(Some(syn::Lit::Str(key))) => { + quote!(get_item(_pyo3::intern!(obj.py(), #key))) + } FieldGetter::GetItem(Some(key)) => quote!(get_item(#key)), - FieldGetter::GetItem(None) => quote!(get_item(#field_name)), + FieldGetter::GetItem(None) => { + quote!(get_item(_pyo3::intern!(obj.py(), #field_name))) + } }; let extractor = match &field.from_py_with { None => {