Merge pull request #1498 from messense/func-name

Fix wrong class name in function call error message
This commit is contained in:
messense 2021-03-15 17:21:28 +08:00 committed by GitHub
commit b7376da739
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 34 deletions

View File

@ -232,7 +232,6 @@ fn function_c_wrapper(
_args: *mut pyo3::ffi::PyObject,
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#name), "()");
pyo3::callback::handle_panic(|_py| {
#slf_module
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);

View File

@ -93,7 +93,6 @@ pub fn impl_wrap_cfunction_with_keywords(
) -> Result<TokenStream> {
let body = impl_call(cls, &spec);
let slf = self_ty.receiver(cls);
let python_name = &spec.python_name;
let body = impl_arg_params(&spec, Some(cls), body)?;
Ok(quote! {
unsafe extern "C" fn __wrap(
@ -101,8 +100,6 @@ pub fn impl_wrap_cfunction_with_keywords(
_args: *mut pyo3::ffi::PyObject,
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(
stringify!(#cls), ".", stringify!(#python_name), "()");
pyo3::callback::handle_panic(|_py| {
#slf
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
@ -118,7 +115,6 @@ pub fn impl_wrap_cfunction_with_keywords(
pub fn impl_wrap_noargs(cls: &syn::Type, spec: &FnSpec<'_>, self_ty: &SelfType) -> TokenStream {
let body = impl_call(cls, &spec);
let slf = self_ty.receiver(cls);
let python_name = &spec.python_name;
assert!(spec.args.is_empty());
quote! {
unsafe extern "C" fn __wrap(
@ -126,8 +122,6 @@ pub fn impl_wrap_noargs(cls: &syn::Type, spec: &FnSpec<'_>, self_ty: &SelfType)
_args: *mut pyo3::ffi::PyObject,
) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(
stringify!(#cls), ".", stringify!(#python_name), "()");
pyo3::callback::handle_panic(|_py| {
#slf
#body
@ -139,7 +133,6 @@ pub fn impl_wrap_noargs(cls: &syn::Type, spec: &FnSpec<'_>, self_ty: &SelfType)
/// Generate class method wrapper (PyCFunction, PyCFunctionWithKeywords)
pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream> {
let name = &spec.name;
let python_name = &spec.python_name;
let names: Vec<syn::Ident> = get_arg_names(&spec);
let cb = quote! { #cls::#name(#(#names),*) };
let body = impl_arg_params(spec, Some(cls), cb)?;
@ -153,7 +146,6 @@ pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream>
{
use pyo3::callback::IntoPyCallbackOutput;
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
pyo3::callback::handle_panic(|_py| {
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@ -169,7 +161,6 @@ pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream>
/// Generate class method wrapper (PyCFunction, PyCFunctionWithKeywords)
pub fn impl_wrap_class(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream> {
let name = &spec.name;
let python_name = &spec.python_name;
let names: Vec<syn::Ident> = get_arg_names(&spec);
let cb = quote! { pyo3::callback::convert(_py, #cls::#name(&_cls, #(#names),*)) };
@ -182,7 +173,6 @@ pub fn impl_wrap_class(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream
_args: *mut pyo3::ffi::PyObject,
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
pyo3::callback::handle_panic(|_py| {
let _cls = pyo3::types::PyType::from_type_ptr(_py, _cls as *mut pyo3::ffi::PyTypeObject);
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
@ -197,7 +187,6 @@ pub fn impl_wrap_class(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream
/// Generate static method wrapper (PyCFunction, PyCFunctionWithKeywords)
pub fn impl_wrap_static(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream> {
let name = &spec.name;
let python_name = &spec.python_name;
let names: Vec<syn::Ident> = get_arg_names(&spec);
let cb = quote! { pyo3::callback::convert(_py, #cls::#name(#(#names),*)) };
@ -210,7 +199,6 @@ pub fn impl_wrap_static(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStrea
_args: *mut pyo3::ffi::PyObject,
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
pyo3::callback::handle_panic(|_py| {
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@ -258,17 +246,14 @@ pub(crate) fn impl_wrap_getter(
property_type: PropertyType,
self_ty: &SelfType,
) -> syn::Result<TokenStream> {
let (python_name, getter_impl) = match property_type {
let getter_impl = match property_type {
PropertyType::Descriptor(field) => {
let name = field.ident.as_ref().unwrap();
(
name.unraw(),
quote!({
_slf.#name.clone()
}),
)
quote!({
_slf.#name.clone()
})
}
PropertyType::Function(spec) => (spec.python_name.clone(), impl_call_getter(cls, spec)?),
PropertyType::Function(spec) => impl_call_getter(cls, spec)?,
};
let slf = self_ty.receiver(cls);
@ -276,7 +261,6 @@ pub(crate) fn impl_wrap_getter(
unsafe extern "C" fn __wrap(
_slf: *mut pyo3::ffi::PyObject, _: *mut std::os::raw::c_void) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
pyo3::callback::handle_panic(|_py| {
#slf
pyo3::callback::convert(_py, #getter_impl)
@ -313,12 +297,12 @@ pub(crate) fn impl_wrap_setter(
property_type: PropertyType,
self_ty: &SelfType,
) -> syn::Result<TokenStream> {
let (python_name, setter_impl) = match property_type {
let setter_impl = match property_type {
PropertyType::Descriptor(field) => {
let name = field.ident.as_ref().unwrap();
(name.unraw(), quote!({ _slf.#name = _val; }))
quote!({ _slf.#name = _val; })
}
PropertyType::Function(spec) => (spec.python_name.clone(), impl_call_setter(cls, spec)?),
PropertyType::Function(spec) => impl_call_setter(cls, spec)?,
};
let slf = self_ty.receiver(cls);
@ -328,7 +312,6 @@ pub(crate) fn impl_wrap_setter(
_slf: *mut pyo3::ffi::PyObject,
_value: *mut pyo3::ffi::PyObject, _: *mut std::os::raw::c_void) -> std::os::raw::c_int
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
pyo3::callback::handle_panic(|_py| {
#slf
let _value = _py.from_borrowed_ptr::<pyo3::types::PyAny>(_value);
@ -408,11 +391,19 @@ pub fn impl_arg_params(
}
}
let cls_name = if let Some(cls) = self_ {
quote! { Some(<#cls as pyo3::type_object::PyTypeInfo>::NAME) }
} else {
quote! { None }
};
let python_name = &spec.python_name;
// create array of arguments, and then parse
Ok(quote! {
{
const DESCRIPTION: pyo3::derive_utils::FunctionDescription = pyo3::derive_utils::FunctionDescription {
fname: _LOCATION,
cls_name: #cls_name,
func_name: stringify!(#python_name),
positional_parameter_names: &[#(#positional_parameter_names),*],
// TODO: https://github.com/PyO3/pyo3/issues/1439 - support specifying these
positional_only_parameters: 0,

View File

@ -22,7 +22,8 @@ pub struct KeywordOnlyParameterDescription {
/// Function argument specification for a `#[pyfunction]` or `#[pymethod]`.
#[derive(Debug)]
pub struct FunctionDescription {
pub fname: &'static str,
pub cls_name: Option<&'static str>,
pub func_name: &'static str,
pub positional_parameter_names: &'static [&'static str],
pub positional_only_parameters: usize,
pub required_positional_parameters: usize,
@ -32,6 +33,13 @@ pub struct FunctionDescription {
}
impl FunctionDescription {
fn full_name(&self) -> String {
if let Some(cls_name) = self.cls_name {
format!("{}.{}()", cls_name, self.func_name)
} else {
format!("{}()", self.func_name)
}
}
/// Extracts the `args` and `kwargs` provided into `output`, according to this function
/// definition.
///
@ -199,7 +207,7 @@ impl FunctionDescription {
let msg = if self.required_positional_parameters != self.positional_parameter_names.len() {
format!(
"{} takes from {} to {} positional arguments but {} {} given",
self.fname,
self.full_name(),
self.required_positional_parameters,
self.positional_parameter_names.len(),
args_provided,
@ -208,7 +216,7 @@ impl FunctionDescription {
} else {
format!(
"{} takes {} positional arguments but {} {} given",
self.fname,
self.full_name(),
self.positional_parameter_names.len(),
args_provided,
was
@ -220,21 +228,23 @@ impl FunctionDescription {
fn multiple_values_for_argument(&self, argument: &str) -> PyErr {
PyTypeError::new_err(format!(
"{} got multiple values for argument '{}'",
self.fname, argument
self.full_name(),
argument
))
}
fn unexpected_keyword_argument(&self, argument: &PyAny) -> PyErr {
PyTypeError::new_err(format!(
"{} got an unexpected keyword argument '{}'",
self.fname, argument
self.full_name(),
argument
))
}
fn positional_only_keyword_arguments(&self, parameter_names: &[&str]) -> PyErr {
let mut msg = format!(
"{} got some positional-only arguments passed as keyword arguments: ",
self.fname
self.full_name()
);
push_parameter_list(&mut msg, parameter_names);
PyTypeError::new_err(msg)
@ -248,7 +258,7 @@ impl FunctionDescription {
};
let mut msg = format!(
"{} missing {} required {} {}: ",
self.fname,
self.full_name(),
parameter_names.len(),
argument_type,
arguments,