Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

* Fixed unsoundness of subclassing. [#683](https://github.com/PyO3/pyo3/pull/683).
* Clear error indicator when the exception is handled on the Rust side. [#719](https://github.com/PyO3/pyo3/pull/719)
* 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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