Sven Brauch added the comment:
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?
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue16795>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com