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 varargs handling when main is called directly #49

Merged
merged 1 commit into from Jun 25, 2012

Conversation

oscarbenjamin
Copy link
Contributor

This is a patch for GH-48.

This patch fixes call_cmd_regular to work with varargs when the varargs parameter is placed after the option list, so that this script:

@opster()
def main(arg, opt=('o', False, 'option help'), *args):
    print 'arg', arg
    print 'args', args
    print 'opt', opt

main('a', 'b', 'c')

will output:

arg a
args ('b', 'c')
opt False

The patch also adds support for function siw th keyword only arguments in call_cmd_regular.

@piranha
Copy link
Owner

piranha commented Jun 25, 2012

Oh well, that's an elaborate solution. Python, Y U SO OBSCURE.

Merging it, I guess, what else can be done? :)

piranha added a commit that referenced this pull request Jun 25, 2012
Fix varargs handling when main is called directly
@piranha piranha merged commit e2b6a0e into piranha:master Jun 25, 2012
@oscarbenjamin
Copy link
Contributor Author

Thanks!

You're right that it's elaborate. The problem is that we need to wrap a function

def main(a, b=c, d=('d', False, ''), *varargs): pass

so that it works like the keyword-only arguments function

def main(a, b=c, *varargs, d=('d', False, '')): pass

but this is not totally trivial to do given all the different ways that someone can call the function from python.
I think the main thing is just that we have tests for this behaviour, so that the bugs discovered can't crop up again. I will make a set of tests to go with the positional.rst documentation that will try to be more systematic about testing the behaviour of all the different ways an opster wrapped function can be written and called.

I suspect that there are still bugs in call_cmd and call_cmd_regular for situations not yet considered or tested for. Notice that call_cmd_regular_py3k, which is the version for functions with keyword-only arguments, is much simpler and probably works perfectly. I think the simplicity, and the reduced likelihood of bugs is a good reason for eventually dropping support for the non-keyword-only Python 2.x syntax.

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.

None yet

2 participants