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 index error in parameter conversion codes #631

Merged
merged 1 commit into from
Oct 19, 2019
Merged
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
29 changes: 12 additions & 17 deletions pyo3-derive-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,8 @@ pub(crate) fn impl_wrap_setter(

/// This function abstracts away some copied code and can propably be simplified itself
pub fn get_arg_names(spec: &FnSpec) -> Vec<syn::Ident> {
spec.args
.iter()
.enumerate()
.map(|(pos, _)| syn::Ident::new(&format!("arg{}", pos), Span::call_site()))
(0..spec.args.len())
Copy link
Member Author

Choose a reason for hiding this comment

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

This is just refactoring, not related to the core of this PR.

.map(|pos| syn::Ident::new(&format!("arg{}", pos), Span::call_site()))
.collect()
}

Expand Down Expand Up @@ -448,10 +446,6 @@ pub fn impl_arg_params(spec: &FnSpec<'_>, body: TokenStream) -> TokenStream {
}
});
}
let placeholders: Vec<syn::Ident> = params
.iter()
.map(|_| syn::Ident::new("None", Span::call_site()))
.collect();

let mut param_conversion = Vec::new();
let mut option_pos = 0;
Expand All @@ -461,15 +455,15 @@ pub fn impl_arg_params(spec: &FnSpec<'_>, body: TokenStream) -> TokenStream {

let accept_args = bool_to_ident(spec.accept_args());
let accept_kwargs = bool_to_ident(spec.accept_kwargs());

let num_normal_params = params.len();
Copy link
Member Author

Choose a reason for hiding this comment

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

This is just refactoring, too.

// create array of arguments, and then parse
quote! {
use pyo3::ObjectProtocol;
const PARAMS: &'static [pyo3::derive_utils::ParamDescription] = &[
#(#params),*
];

let mut output = [#(#placeholders),*];
let mut output = [None; #num_normal_params];
let mut _args = _args;
let mut _kwargs = _kwargs;

Expand Down Expand Up @@ -507,21 +501,22 @@ fn impl_arg_param(
let #arg_name = _py;
};
}
let arg_value = quote!(output[#option_pos]);
*option_pos += 1;

let ty = arg.ty;
let name = arg.name;

if spec.is_args(&name) {
quote! {
return quote! {
let #arg_name = <#ty as pyo3::FromPyObject>::extract(_args.as_ref())?;
}
};
} else if spec.is_kwargs(&name) {
quote! {
return quote! {
let #arg_name = _kwargs;
}
} else if arg.optional.is_some() {
};
}
let arg_value = quote!(output[#option_pos]);
*option_pos += 1;
if arg.optional.is_some() {
let default = if let Some(d) = spec.default_value(name) {
if d.to_string() == "None" {
quote! { None }
Expand Down