classification
Title: ast.fix_missing_locations() breaks if node doesn't have "_attributes" variable
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, aronacher, marcin.bachry
Priority: normal Keywords: patch

Created on 2008-10-07 14:37 by marcin.bachry, last changed 2008-10-19 08:31 by aronacher. This issue is now closed.

Files
File name Uploaded Description Edit
ast.diff marcin.bachry, 2008-10-07 14:37 simple fix
asdl_c.diff aronacher, 2008-10-07 17:42
Messages (4)
msg74453 - (view) Author: Marcin Bachry (marcin.bachry) Date: 2008-10-07 14:37
ast.fix_missing_locations() fails if any node is missing "_attributes"
instance variable - but it's the case of some fundamental nodes like
"alias" or "identifier".  When I run simple test:

  import ast
  with open(__file__) as fp:
      tree = ast.parse(fp.read())
  ast.fix_missing_locations(tree)

I get:

  $ python2.6 /tmp/test.py 
  Traceback (most recent call last):
    File "/tmp/test.py", line 5, in <module>
      ast.fix_missing_locations(tree)
    File "/usr/local/lib/python2.6/ast.py", line 133, in
fix_missing_locations
      _fix(node, 1, 0)
    File "/usr/local/lib/python2.6/ast.py", line 132, in _fix
      _fix(child, lineno, col_offset)
    File "/usr/local/lib/python2.6/ast.py", line 132, in _fix
      _fix(child, lineno, col_offset)
    File "/usr/local/lib/python2.6/ast.py", line 121, in _fix
      if 'lineno' in node._attributes:
  AttributeError: 'alias' object has no attribute '_attributes'
msg74465 - (view) Author: Armin Ronacher (aronacher) * (Python committer) Date: 2008-10-07 17:42
The root of the problem is that ast.AST doesn't have _fields or
_attributes.  I think the better solution is to add these attributes to
the root class which makes it easier to work with these objects.

I attached a diff for asdl_c.py which fixes that problem by adding
attributes to the AST class.

Run `python Parser/asdl_c.py -h Python Parser/Python.asdl` to regenerate
the Python-ast.c file.

Can someone review the diff?
msg74470 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-10-07 19:43
empty_tuple should be DECREF'ed at the end of the function.
Otherwise the patch is fine.
msg74968 - (view) Author: Armin Ronacher (aronacher) * (Python committer) Date: 2008-10-19 08:30
Fixed in changeset 66973 for trunk.
History
Date User Action Args
2008-10-19 08:31:09aronachersetstatus: open -> closed
resolution: fixed
2008-10-19 08:30:40aronachersetmessages: + msg74968
2008-10-07 19:43:36amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg74470
2008-10-07 17:42:14aronachersetfiles: + asdl_c.diff
nosy: + aronacher
messages: + msg74465
2008-10-07 14:37:55marcin.bachrycreate