Skip to content

Commit

Permalink
Fix usage of raw idents with #[pyo3(set)]
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jan 27, 2020
1 parent 11084e1 commit a8ec946
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* 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

Expand Down
12 changes: 7 additions & 5 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ fn impl_descriptors(
.flat_map(|&(ref field, ref fns)| {
fns.iter()
.map(|desc| {
let name = field.ident.clone().unwrap();
let name = field.ident.as_ref().unwrap();
let field_ty = &field.ty;
match *desc {
FnType::Getter => {
Expand All @@ -441,7 +441,7 @@ fn impl_descriptors(
}
FnType::Setter => {
let setter_name =
syn::Ident::new(&format!("set_{}", name), Span::call_site());
syn::Ident::new(&format!("set_{}", name.unraw()), Span::call_site());
quote! {
impl #cls {
fn #setter_name(&mut self, value: #field_ty) -> pyo3::PyResult<()> {
Expand All @@ -463,7 +463,7 @@ fn impl_descriptors(
.flat_map(|&(ref field, ref fns)| {
fns.iter()
.map(|desc| {
let name = field.ident.clone().unwrap();
let name = field.ident.as_ref().unwrap();

// FIXME better doc?
let doc = syn::LitStr::new(&name.to_string(), name.span());
Expand All @@ -483,8 +483,10 @@ fn impl_descriptors(
Ok(impl_py_getter_def(&spec, &impl_wrap_getter(&cls, &spec)?))
}
FnType::Setter => {
let setter_name =
syn::Ident::new(&format!("set_{}", name), Span::call_site());
let setter_name = syn::Ident::new(
&format!("set_{}", name.unraw()),
Span::call_site(),
);
let spec = FnSpec {
tp: FnType::Setter,
name: &setter_name,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_class_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ fn custom_names() {
}

#[pyclass]
struct RawIdents {}
struct RawIdents {
#[pyo3(get, set)]
r#type: i64,
}

#[pymethods]
impl RawIdents {
Expand All @@ -83,6 +86,7 @@ fn test_raw_idents() {
let typeobj = py.get_type::<RawIdents>();
py_assert!(py, typeobj, "not hasattr(typeobj, 'r#fn')");
py_assert!(py, typeobj, "hasattr(typeobj, 'fn')");
py_assert!(py, typeobj, "hasattr(typeobj, 'type')");
}

#[pyclass]
Expand Down

0 comments on commit a8ec946

Please sign in to comment.