Skip to content

Commit

Permalink
Merge pull request #1383 from davidhewitt/raw-idents
Browse files Browse the repository at this point in the history
macros-backend: fix raw idents in pymethods
  • Loading branch information
davidhewitt authored Jan 13, 2021
2 parents 4a3c4b0 + 9807ef8 commit 8f4ed55
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
- `prepare_freethreaded_python` will no longer register an `atexit` handler to call `Py_Finalize`. [#1355](https://github.com/PyO3/pyo3/pull/1355)

### Fixed
- Fix support for using `r#raw_idents` as argument names in pyfunctions. [#1383](https://github.com/PyO3/pyo3/pull/1383)

## [0.13.1] - 2021-01-10
### Added
- Add support for `#[pyclass(dict)]` and `#[pyclass(weakref)]` with the `abi3` feature on Python 3.9 and up. [#1342](https://github.com/PyO3/pyo3/pull/1342)
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl<'a> FnSpec<'a> {
}

let ty = get_return_info(&sig.output);
let python_name = python_name.unwrap_or_else(|| name.unraw());
let python_name = python_name.as_ref().unwrap_or(name).unraw();

let mut parse_erroneous_text_signature = |error_msg: &str| {
// try to parse anyway to give better error messages
Expand Down
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,13 @@ pub fn impl_arg_params(
if arg.py || spec.is_args(&arg.name) || spec.is_kwargs(&arg.name) {
continue;
}
let name = arg.name;
let name = arg.name.unraw().to_string();
let kwonly = spec.is_kw_only(&arg.name);
let opt = arg.optional.is_some() || spec.default_value(&arg.name).is_some();

params.push(quote! {
pyo3::derive_utils::ParamDescription {
name: stringify!(#name),
name: #name,
is_optional: #opt,
kw_only: #kwonly
}
Expand Down
103 changes: 103 additions & 0 deletions tests/test_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,106 @@ fn test_from_sequence() {
let typeobj = py.get_type::<FromSequence>();
py_assert!(py, typeobj, "typeobj(range(0, 4)).numbers == [0, 1, 2, 3]")
}

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

#[pymethods]
impl r#RawIdents {
#[new]
pub fn r#new(
r#_py: Python,
r#type: PyObject,
r#subtype: PyObject,
r#subsubtype: PyObject,
) -> PyResult<Self> {
Ok(Self {
r#type,
r#subtype,
r#subsubtype,
})
}

#[getter(r#subtype)]
pub fn r#get_subtype(&self) -> PyObject {
self.r#subtype.clone()
}

#[setter(r#subtype)]
pub fn r#set_subtype(&mut self, r#subtype: PyObject) {
self.r#subtype = r#subtype;
}

#[getter]
pub fn r#get_subsubtype(&self) -> PyObject {
self.r#subsubtype.clone()
}

#[setter]
pub fn r#set_subsubtype(&mut self, r#subsubtype: PyObject) {
self.r#subsubtype = r#subsubtype;
}

#[call]
pub fn r#call(&mut self, r#type: PyObject) {
self.r#type = r#type;
}

#[staticmethod]
pub fn r#static_method(r#type: PyObject) -> PyObject {
r#type
}

#[classmethod]
pub fn r#class_method(_: &PyType, r#type: PyObject) -> PyObject {
r#type
}

#[classattr]
pub fn r#class_attr_fn() -> i32 {
5
}

#[classattr]
const r#CLASS_ATTR_CONST: i32 = 6;
}

#[test]
fn test_raw_idents() {
Python::with_gil(|py| {
let raw_idents_type = py.get_type::<r#RawIdents>();
py_run!(
py,
raw_idents_type,
r#"
instance = raw_idents_type(type=None, subtype=5, subsubtype="foo")
assert instance.type is None
assert instance.subtype == 5
assert instance.subsubtype == "foo"
instance.type = 1
instance.subtype = 2
instance.subsubtype = 3
assert instance.type == 1
assert instance.subtype == 2
assert instance.subsubtype == 3
assert raw_idents_type.static_method(type=30) == 30
assert instance.class_method(type=40) == 40
instance(type=50)
assert instance.type == 50
assert raw_idents_type.class_attr_fn == 5
assert raw_idents_type.CLASS_ATTR_CONST == 6
"#
);
})
}

0 comments on commit 8f4ed55

Please sign in to comment.