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

Reverse argument assignment order #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Winand
Copy link
Contributor

@Winand Winand commented Oct 3, 2018

Fixes #23
Problem:

def partial2(a1, a2, *args, **kwargs):
    print(a1, a2)
    print(args)
    print(kwargs)

The original order of assignment is **kwargs, a1, a2, *args.
But after assigning a1 (which is arguments[0]) we cannot get more arguments.

Solution:
Assign everything in reverse order: **kwargs, *args, a2, a1.
So we "break" arguments[0] at the very end when we don't need it anymore.

# Apply values of positional args
# inside if, because standard arguments are invalid
args_var = 'arguments[0].flx_args'
if len(argnames) > 1:
args_var = self.dummy('args')
code.append(self.lf('%s = arguments[0].flx_args;' % args_var))
for i, name in enumerate(argnames):
for i, name in reversed(list(enumerate(argnames))):
code.append(self.lf('%s = %s[%i];' % (name, args_var, i)))
Copy link
Member

Choose a reason for hiding this comment

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

I'm afraid that I don't follow. As far as I can see, the order should not matter, since we do not change arguments[0].flx_args. What am I missing?

Could you add a test for the problem you were having? That would help in understanding this change, and make sure that it's not accidentally broken at a later time :)

Copy link
Contributor Author

@Winand Winand Oct 4, 2018

Choose a reason for hiding this comment

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

@almarklein when you call a function like this one def partial2(a1, a2, *args, **kwargs): your "real" arguments dictionary is in a1 (which is the same with arguments[0]).

The following happens (at least in Chrome):

  1. You extract **kwargs from arguments[0]
  2. You extract and assign positional arguments starting with the 1st one (a1)
  3. So you've just cleared original arguments[0] (a1) by assigning a new value to a1
  4. You cannot access original arguments[0] anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants