Issue2108

classification
Title: Cannot set attribute to instances of AST/PythonTree (blocks pyflakes)
Type: Severity: normal
Components: Core Versions: Jython 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: fwierzbicki, tlecomte
Priority: Keywords:

Created on 2013-12-24.15:19:07 by tlecomte, last changed 2014-05-17.20:54:58 by fwierzbicki.

Messages
msg8213 (view) Author: Timothée Lecomte (tlecomte) Date: 2013-12-24.15:19:05
Dear all,

I would like to run pyflakes under Jython (to do live syntax analysis on Python files inside an editor written in Java).

Unfortunately, pyflakes does not run with Jython. When running the following inside Jython:

    from pyflakes.api import check
    check("a = 1", "untitled.py")

I get the following:

Traceback (most recent call last):
  File "/Users/tlecomte/Documents/20131224_pyflakes/test_pyflakes.py", line 2, in <module>
    check("a = 1", "untitled.py")
  File "/Users/tlecomte/Src/icy-git/Icy-App/plugins/Lib/site-packages/pyflakes/api.py", line 58, in check
    w = checker.Checker(tree, filename)
  File "/Users/tlecomte/Src/icy-git/Icy-App/plugins/Lib/site-packages/pyflakes/checker.py", line 253, in __init__
    self.handleChildren(tree)
  File "/Users/tlecomte/Src/icy-git/Icy-App/plugins/Lib/site-packages/pyflakes/checker.py", line 520, in handleChildren
    self.handleNode(node, tree)
  File "/Users/tlecomte/Src/icy-git/Icy-App/plugins/Lib/site-packages/pyflakes/checker.py", line 551, in handleNode
    node.level = self.nodeDepth
AttributeError: '_ast.Assign' object has no attribute 'level'



Note that pyflakes tries to set a new attribute on 'node', which is a '_ast.Assign' object, i.e. subclass of _ast.AST and PythonTree. These are java classes deriving from PyObject and do not have a __dict__.

Under CPython, _ast.AST() shows the same behaviour, BUT all its subclasses (including '_ast.Assign') seem to allow the setting of new attributes. So it would be great if it was the same under Jython.

I am willing to help, but I am afraid I do not understand well enough the inner workings of PyObject in Jython...

Best regards,

Timothée
msg8214 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2013-12-25.18:07:57
Hi Timothée - this is definitely my area :) -- is there any chance that you could provide an example that narrows things down to a simple test that doesn't depend on pyflakes? That would definitely speed me up on looking at this.

I have a fix in mind that amounts to a partial re-write of the whole ast implementation (something that needs to happen someday), but I'm hoping to find a simpler path.

The ast code generation has rotted a bit since I touched it last, so I'm doing some cleaning before I can really start on this. I really want to find a way to solve this and related problems - you are not the first to run into trouble here.
msg8215 (view) Author: Timothée Lecomte (tlecomte) Date: 2013-12-26.08:55:26
Hi Frank,

Thanks for your answer ! Here is a narrowed-down example:

This is on CPython, for reference:

$ python
Python 2.7.5 (default, Aug 28 2013, 10:09:22) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _ast
>>> m = _ast.Module()
>>> m.level = 1
>>> m.level
1

and now with Jython:

>>> import _ast
>>> m = _ast.Module()
>>> m.level = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_ast.Module' object has no attribute 'level'


P.S. : I also noticed another difference with _ast.AST(), but it is not a problem with pyflakes:

On CPython:
Python 2.7.5 (default, Aug 28 2013, 10:09:22) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _ast
>>> a = _ast.AST()
>>> a
<_ast.AST object at 0x1003430a0>
>>>

On Jython:
>>> import _ast
>>> a = _ast.AST()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create '_ast.AST' instances
>>>
msg8216 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2013-12-27.04:14:12
Thanks for the example, that will help!
msg8416 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2014-05-16.00:08:51
Fixed in trunk as of http://hg.python.org/jython/rev/ecb9475b7808
History
Date User Action Args
2014-05-17 20:54:58fwierzbickisetresolution: fixed
2014-05-16 00:08:51fwierzbickisetstatus: open -> closed
messages: + msg8416
2013-12-27 04:14:13fwierzbickisetmessages: + msg8216
2013-12-26 08:55:27tlecomtesetmessages: + msg8215
2013-12-25 18:07:58fwierzbickisetassignee: fwierzbicki
messages: + msg8214
nosy: + fwierzbicki
2013-12-24 15:19:07tlecomtecreate