macros: fix raw-ident pyclasses having r# at the start of the Python name

This commit is contained in:
David Hewitt 2022-05-24 07:38:41 +01:00
parent 48690525e1
commit 71abeeff8b
6 changed files with 19 additions and 7 deletions

View File

@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed incorrectly disabled FFI definition `PyThreadState_DeleteCurrent`. [#2357](https://github.com/PyO3/pyo3/pull/2357) - Fixed incorrectly disabled FFI definition `PyThreadState_DeleteCurrent`. [#2357](https://github.com/PyO3/pyo3/pull/2357)
- Correct FFI definition `PyEval_EvalCodeEx` to take `*const *mut PyObject` array arguments instead of `*mut *mut PyObject` (this was changed in CPython 3.6). [#2368](https://github.com/PyO3/pyo3/pull/2368) - Correct FFI definition `PyEval_EvalCodeEx` to take `*const *mut PyObject` array arguments instead of `*mut *mut PyObject` (this was changed in CPython 3.6). [#2368](https://github.com/PyO3/pyo3/pull/2368)
- Fix "raw-ident" structs (e.g. `#[pyclass] struct r#RawName`) incorrectly having `r#` at the start of the class name created in Python. [#2395](https://github.com/PyO3/pyo3/pull/2395)
## [0.16.5] - 2022-05-15 ## [0.16.5] - 2022-05-15

View File

@ -1,5 +1,7 @@
// Copyright (c) 2017-present PyO3 Project and Contributors // Copyright (c) 2017-present PyO3 Project and Contributors
use std::borrow::Cow;
use crate::attributes::TextSignatureAttribute; use crate::attributes::TextSignatureAttribute;
use crate::deprecations::Deprecation; use crate::deprecations::Deprecation;
use crate::params::{accept_args_kwargs, impl_arg_params}; use crate::params::{accept_args_kwargs, impl_arg_params};
@ -287,7 +289,9 @@ impl<'a> FnSpec<'a> {
let doc = utils::get_doc( let doc = utils::get_doc(
meth_attrs, meth_attrs,
text_signature.as_ref().map(|attr| (&python_name, attr)), text_signature
.as_ref()
.map(|attr| (Cow::Borrowed(&python_name), attr)),
); );
let arguments: Vec<_> = if skip_first_arg { let arguments: Vec<_> = if skip_first_arg {

View File

@ -1,5 +1,7 @@
// Copyright (c) 2017-present PyO3 Project and Contributors // Copyright (c) 2017-present PyO3 Project and Contributors
use std::borrow::Cow;
use crate::attributes::{ use crate::attributes::{
self, kw, take_pyo3_options, CrateAttribute, ExtendsAttribute, FreelistAttribute, self, kw, take_pyo3_options, CrateAttribute, ExtendsAttribute, FreelistAttribute,
ModuleAttribute, NameAttribute, NameLitStr, TextSignatureAttribute, ModuleAttribute, NameAttribute, NameLitStr, TextSignatureAttribute,
@ -282,12 +284,12 @@ impl FieldPyO3Options {
} }
} }
fn get_class_python_name<'a>(cls: &'a syn::Ident, args: &'a PyClassArgs) -> &'a syn::Ident { fn get_class_python_name<'a>(cls: &'a syn::Ident, args: &'a PyClassArgs) -> Cow<'a, syn::Ident> {
args.options args.options
.name .name
.as_ref() .as_ref()
.map(|name_attr| &name_attr.value.0) .map(|name_attr| Cow::Borrowed(&name_attr.value.0))
.unwrap_or(cls) .unwrap_or_else(|| Cow::Owned(cls.unraw()))
} }
fn impl_class( fn impl_class(

View File

@ -1,5 +1,7 @@
// Copyright (c) 2017-present PyO3 Project and Contributors // Copyright (c) 2017-present PyO3 Project and Contributors
use std::borrow::Cow;
use crate::{ use crate::{
attributes::{ attributes::{
self, get_pyo3_options, take_attributes, take_pyo3_options, CrateAttribute, self, get_pyo3_options, take_attributes, take_pyo3_options, CrateAttribute,
@ -408,7 +410,7 @@ pub fn impl_wrap_pyfunction(
options options
.text_signature .text_signature
.as_ref() .as_ref()
.map(|attr| (&python_name, attr)), .map(|attr| (Cow::Borrowed(&python_name), attr)),
); );
let krate = get_pyo3_crate(&options.krate); let krate = get_pyo3_crate(&options.krate);

View File

@ -1,7 +1,9 @@
use std::borrow::Cow;
// Copyright (c) 2017-present PyO3 Project and Contributors // Copyright (c) 2017-present PyO3 Project and Contributors
use proc_macro2::{Span, TokenStream}; use proc_macro2::{Span, TokenStream};
use quote::ToTokens; use quote::ToTokens;
use syn::spanned::Spanned; use syn::{spanned::Spanned, Ident};
use crate::attributes::{CrateAttribute, TextSignatureAttribute}; use crate::attributes::{CrateAttribute, TextSignatureAttribute};
@ -66,7 +68,7 @@ pub struct PythonDoc(TokenStream);
/// e.g. concat!("...", "\n", "\0") /// e.g. concat!("...", "\n", "\0")
pub fn get_doc( pub fn get_doc(
attrs: &[syn::Attribute], attrs: &[syn::Attribute],
text_signature: Option<(&syn::Ident, &TextSignatureAttribute)>, text_signature: Option<(Cow<'_, Ident>, &TextSignatureAttribute)>,
) -> PythonDoc { ) -> PythonDoc {
let mut tokens = TokenStream::new(); let mut tokens = TokenStream::new();
let comma = syn::token::Comma(Span::call_site()); let comma = syn::token::Comma(Span::call_site());

View File

@ -902,6 +902,7 @@ impl r#RawIdents {
fn test_raw_idents() { fn test_raw_idents() {
Python::with_gil(|py| { Python::with_gil(|py| {
let raw_idents_type = py.get_type::<r#RawIdents>(); let raw_idents_type = py.get_type::<r#RawIdents>();
assert_eq!(raw_idents_type.name().unwrap(), "RawIdents");
py_run!( py_run!(
py, py,
raw_idents_type, raw_idents_type,