This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author scummos
Recipients benjamin.peterson, brett.cannon, eric.snow, meador.inge, scummos
Date 2013-01-16.23:43:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1358379805.57.0.926338638456.issue16795@psf.upfronthosting.co.za>
In-reply-to
Content
I think I got it mostly working now (it was quite simple in fact), but there's one issue which I can't seem to solve. This fails:

>>> compile(ast.parse("def fun(): pass"), "<file>", "exec")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: required field "arg" missing from arg

However, this succeeds:
>>> compile(ast.parse("def fun(*va, **kwa): pass"), "<file>", "exec")
<code object <module> at 0x7fb390323780, file "<file>", line 1>

The reason is quite simple: vararg and kwarg are optional in arguments, but they're of type arg, and arg has mandatory attributes ("arg", the name of the argument). Still, when calling ast.parse(), Python creates attributes called vararg, kwarg on the "attributes" object, which are set to None:

>>> ast.parse('def fun(): pass').body[0].args.vararg.__repr__()
'None'

Thus, when in compile(), the code in Python_ast.c does "if ( _PyObject_HasAttrId(obj, &PyId_vararg) ) { ... }" this check says "yes there's a vararg" altough there really is none, which leads to the above error message.

I checked the asdl file, and in fact I think this is a general issue, which was not noticed so far, since only things without mandatory attributes are used in conjunction with the question mark "?" operator there (expr and the integral types identifier, int...). Is this correct?

An easy way to solve this problem would be to check whether the attribute is None in Python_ast.c, but I'm everything but sure this is the correct way to fix this. Alternatively, one could not create the attributes on the ast objects when they're not present in the parsed code (i.e. leave the "vararg" attribute nonexistent instead of setting it to none). What should I do about this?
History
Date User Action Args
2013-01-16 23:43:25scummossetrecipients: + scummos, brett.cannon, benjamin.peterson, meador.inge, eric.snow
2013-01-16 23:43:25scummossetmessageid: <1358379805.57.0.926338638456.issue16795@psf.upfronthosting.co.za>
2013-01-16 23:43:25scummoslinkissue16795 messages
2013-01-16 23:43:24scummoscreate