Fix usage of raw idents with #[pyo3(set)]

This commit is contained in:
David Hewitt 2020-01-27 08:13:55 +00:00
parent 11084e1433
commit a8ec946fc3
3 changed files with 13 additions and 6 deletions

View File

@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Clear error indicator when the exception is handled on the Rust side. [#719](https://github.com/PyO3/pyo3/pull/719) * Clear error indicator when the exception is handled on the Rust side. [#719](https://github.com/PyO3/pyo3/pull/719)
* Fixed unsoundness of subclassing. [#683](https://github.com/PyO3/pyo3/pull/683). * Fixed unsoundness of subclassing. [#683](https://github.com/PyO3/pyo3/pull/683).
* Usage of raw identifiers with `#[pyo3(set)]`. [#745](https://github.com/PyO3/pyo3/pull/745)
### Removed ### Removed

View File

@ -427,7 +427,7 @@ fn impl_descriptors(
.flat_map(|&(ref field, ref fns)| { .flat_map(|&(ref field, ref fns)| {
fns.iter() fns.iter()
.map(|desc| { .map(|desc| {
let name = field.ident.clone().unwrap(); let name = field.ident.as_ref().unwrap();
let field_ty = &field.ty; let field_ty = &field.ty;
match *desc { match *desc {
FnType::Getter => { FnType::Getter => {
@ -441,7 +441,7 @@ fn impl_descriptors(
} }
FnType::Setter => { FnType::Setter => {
let setter_name = let setter_name =
syn::Ident::new(&format!("set_{}", name), Span::call_site()); syn::Ident::new(&format!("set_{}", name.unraw()), Span::call_site());
quote! { quote! {
impl #cls { impl #cls {
fn #setter_name(&mut self, value: #field_ty) -> pyo3::PyResult<()> { fn #setter_name(&mut self, value: #field_ty) -> pyo3::PyResult<()> {
@ -463,7 +463,7 @@ fn impl_descriptors(
.flat_map(|&(ref field, ref fns)| { .flat_map(|&(ref field, ref fns)| {
fns.iter() fns.iter()
.map(|desc| { .map(|desc| {
let name = field.ident.clone().unwrap(); let name = field.ident.as_ref().unwrap();
// FIXME better doc? // FIXME better doc?
let doc = syn::LitStr::new(&name.to_string(), name.span()); let doc = syn::LitStr::new(&name.to_string(), name.span());
@ -483,8 +483,10 @@ fn impl_descriptors(
Ok(impl_py_getter_def(&spec, &impl_wrap_getter(&cls, &spec)?)) Ok(impl_py_getter_def(&spec, &impl_wrap_getter(&cls, &spec)?))
} }
FnType::Setter => { FnType::Setter => {
let setter_name = let setter_name = syn::Ident::new(
syn::Ident::new(&format!("set_{}", name), Span::call_site()); &format!("set_{}", name.unraw()),
Span::call_site(),
);
let spec = FnSpec { let spec = FnSpec {
tp: FnType::Setter, tp: FnType::Setter,
name: &setter_name, name: &setter_name,

View File

@ -69,7 +69,10 @@ fn custom_names() {
} }
#[pyclass] #[pyclass]
struct RawIdents {} struct RawIdents {
#[pyo3(get, set)]
r#type: i64,
}
#[pymethods] #[pymethods]
impl RawIdents { impl RawIdents {
@ -83,6 +86,7 @@ fn test_raw_idents() {
let typeobj = py.get_type::<RawIdents>(); let typeobj = py.get_type::<RawIdents>();
py_assert!(py, typeobj, "not hasattr(typeobj, 'r#fn')"); py_assert!(py, typeobj, "not hasattr(typeobj, 'r#fn')");
py_assert!(py, typeobj, "hasattr(typeobj, 'fn')"); py_assert!(py, typeobj, "hasattr(typeobj, 'fn')");
py_assert!(py, typeobj, "hasattr(typeobj, 'type')");
} }
#[pyclass] #[pyclass]