diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2016-01-25 00:24:38 +0200 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-01-25 00:24:38 +0200 |
| commit | 35ba13cf470ffca09cf97e65ce2c7fa75c0025e5 (patch) | |
| tree | dc7c6f3ea884cb067feb7b693a9400c83bac4f78 | |
| parent | eb48cf362e413e29c0ddaa7c7d0c88a7db0be95a (diff) | |
| download | astroid-git-35ba13cf470ffca09cf97e65ce2c7fa75c0025e5.tar.gz | |
Don't set the fromlineno of parents or children for nodes without it set
If a node had a .fromlineno as None, then a function would be invoked
which retrieved the first line number of the children or the parents which
wasn't none. This is wrong, because it's not a guarantee that all nodes should
have it set, especially those coming from raw_building, where it can be close
to impossible to determine the line number of an object coming from an
extension module. The functionality was removed and certain guards
were added in a couple of places in order to protect operations against
None.
Close #310 and #195
| -rw-r--r-- | astroid/interpreter/lookup.py | 2 | ||||
| -rw-r--r-- | astroid/tests/unittest_nodes.py | 8 | ||||
| -rw-r--r-- | astroid/tests/unittest_regrtest.py | 8 | ||||
| -rw-r--r-- | astroid/tree/base.py | 31 | ||||
| -rw-r--r-- | astroid/tree/node_classes.py | 7 |
5 files changed, 16 insertions, 40 deletions
diff --git a/astroid/interpreter/lookup.py b/astroid/interpreter/lookup.py index b47c1193..8a8eb2a0 100644 --- a/astroid/interpreter/lookup.py +++ b/astroid/interpreter/lookup.py @@ -291,7 +291,7 @@ def locals_import_from(node, locals_): # Sort the list for having the locals ordered by their first # appearance. def sort_locals(my_list): - my_list.sort(key=lambda node: node.fromlineno) + my_list.sort(key=lambda node: node.fromlineno or 0) for name, asname in node.names: if name == '*': diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index aeab26e2..fab82eef 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -509,14 +509,6 @@ class ArgumentsNodeTC(unittest.TestCase): self.skipTest('FIXME http://bugs.python.org/issue10445 ' '(no line number on function args)') - def test_builtin_fromlineno_missing(self): - cls = test_utils.extract_node(''' - class Foo(Exception): #@ - pass - ''') - new = cls.getattr('__new__')[-1] - self.assertEqual(new.args.fromlineno, 0) - class UnboundMethodNodeTest(unittest.TestCase): diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 9d36befc..21b93dc8 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -24,6 +24,7 @@ import six from astroid import MANAGER, Instance, nodes from astroid.builder import AstroidBuilder from astroid import exceptions +from astroid.interpreter import lookup from astroid.manager import AstroidManager from astroid import raw_building from astroid.test_utils import require_version, extract_node, bootstrap @@ -310,6 +311,13 @@ def test(): with self.assertRaises(AttributeError): node.qname + def test_null_fromlineno_does_not_crash_lookup(self): + node = nodes.ImportFrom('test', [('a', 'a')]) + function = nodes.FunctionDef() + locals_ = {'a': [function]} + lookup._get_locals(node, locals_) + + class Whatever(object): a = property(lambda x: x, lambda x: x) diff --git a/astroid/tree/base.py b/astroid/tree/base.py index 755e3b8b..de137560 100644 --- a/astroid/tree/base.py +++ b/astroid/tree/base.py @@ -231,10 +231,7 @@ class NodeNG(object): @decorators.cachedproperty def fromlineno(self): - if self.lineno is None: - return self._fixed_source_line() - else: - return self.lineno + return self.lineno @decorators.cachedproperty def tolineno(self): @@ -248,29 +245,6 @@ class NodeNG(object): else: return lastchild.tolineno - # TODO / FIXME: - assert self.fromlineno is not None, self - assert self.tolineno is not None, self - - def _fixed_source_line(self): - """return the line number where the given node appears - - we need this method since not all nodes have the lineno attribute - correctly set... - """ - line = self.lineno - _node = self - try: - while line is None: - _node = next(_node.get_children()) - line = _node.lineno - except StopIteration: - _node = self.parent - while _node and line is None: - line = _node.lineno - _node = _node.parent - return line - def block_range(self, lineno): """handle block line numbers range for non block opening statements """ @@ -592,7 +566,6 @@ class LookupMixIn(object): # take care node may be missing lineno information (this is the case for # nodes inserted for living objects) if myframe is frame and mystmt.fromlineno is not None: - assert mystmt.fromlineno is not None, mystmt mylineno = mystmt.fromlineno + offset else: # disabling lineno filtering @@ -602,7 +575,7 @@ class LookupMixIn(object): for node in stmts: stmt = node.statement() # line filtering is on and we have reached our location, break - if mylineno > 0 and stmt.fromlineno > mylineno: + if mylineno > 0 and stmt.fromlineno and stmt.fromlineno > mylineno: break assert hasattr(node, 'assign_type'), (node, node.scope(), node.scope().locals) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 78a9afc8..722e2b75 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -185,8 +185,11 @@ class Arguments(base.AssignTypeMixin, AssignedStmtsMixin, base.NodeNG): @decorators.cachedproperty def fromlineno(self): - lineno = super(Arguments, self).fromlineno - return max(lineno, self.parent.fromlineno or 0) + # Let the Function's lineno be the lineno for this. + if self.parent.fromlineno: + return self.parent.fromlineno + + return super(Arguments, self).fromlineno def format_args(self): """return arguments formatted as string""" |
