summaryrefslogtreecommitdiff
path: root/node_classes.py
diff options
context:
space:
mode:
authorEevee (Alex Munroe) <amunroe@yelp.com>2014-03-24 18:00:17 -0700
committerEevee (Alex Munroe) <amunroe@yelp.com>2014-07-01 17:33:00 -0700
commit53013fa01f45114c1b8ee87527eab4fbeef8a113 (patch)
treed4ea5d9e01a4e746ea48037443a62bdb40d250da /node_classes.py
parent2e13d989dbf9a097c679e4e1ca71e59fc9e5524f (diff)
downloadastroid-git-53013fa01f45114c1b8ee87527eab4fbeef8a113.tar.gz
Speed up rebuilder considerably by computing line numbers lazily.
Fetching the last child of each of hundreds of thousands of nodes is relatively expensive, and most of the time this information is never used, so don't actually figure it out until it's asked for. Saves the overhead of quite a few function calls, too.
Diffstat (limited to 'node_classes.py')
-rw-r--r--node_classes.py33
1 files changed, 17 insertions, 16 deletions
diff --git a/node_classes.py b/node_classes.py
index e247392e..45a00ba9 100644
--- a/node_classes.py
+++ b/node_classes.py
@@ -20,6 +20,8 @@
import sys
+from logilab.common.decorators import cachedproperty
+
from astroid.exceptions import NoDefault
from astroid.bases import (NodeNG, Statement, Instance, InferenceContext,
_infer_stmts, YES, BUILTINS)
@@ -267,6 +269,11 @@ class Arguments(NodeNG, AssignTypeMixin):
return name
return None
+ @cachedproperty
+ def fromlineno(self):
+ lineno = super(Arguments, self).fromlineno
+ return max(lineno, self.parent.fromlineno)
+
def format_args(self):
"""return arguments formatted as string"""
result = []
@@ -560,7 +567,8 @@ class ExceptHandler(Statement, AssignTypeMixin):
name = None
body = None
- def _blockstart_toline(self):
+ @cachedproperty
+ def blockstart_tolineno(self):
if self.name:
return self.name.tolineno
elif self.type:
@@ -568,11 +576,6 @@ class ExceptHandler(Statement, AssignTypeMixin):
else:
return self.lineno
- def set_line_info(self, lastchild):
- self.fromlineno = self.lineno
- self.tolineno = lastchild.tolineno
- self.blockstart_tolineno = self._blockstart_toline()
-
def catch(self, exceptions):
if self.type is None or exceptions is None:
return True
@@ -603,7 +606,8 @@ class For(BlockRangeMixIn, AssignTypeMixin, Statement):
orelse = None
optional_assign = True
- def _blockstart_toline(self):
+ @cachedproperty
+ def blockstart_tolineno(self):
return self.iter.tolineno
@@ -638,7 +642,8 @@ class If(BlockRangeMixIn, Statement):
body = None
orelse = None
- def _blockstart_toline(self):
+ @cachedproperty
+ def blockstart_tolineno(self):
return self.test.tolineno
def block_range(self, lineno):
@@ -789,9 +794,6 @@ class TryExcept(BlockRangeMixIn, Statement):
def _infer_name(self, frame, name):
return name
- def _blockstart_toline(self):
- return self.lineno
-
def block_range(self, lineno):
"""handle block line numbers range for try/except statements"""
last = None
@@ -811,9 +813,6 @@ class TryFinally(BlockRangeMixIn, Statement):
body = None
finalbody = None
- def _blockstart_toline(self):
- return self.lineno
-
def block_range(self, lineno):
"""handle block line numbers range for try/finally statements"""
child = self.body[0]
@@ -857,7 +856,8 @@ class While(BlockRangeMixIn, Statement):
body = None
orelse = None
- def _blockstart_toline(self):
+ @cachedproperty
+ def blockstart_tolineno(self):
return self.test.tolineno
def block_range(self, lineno):
@@ -871,7 +871,8 @@ class With(BlockRangeMixIn, AssignTypeMixin, Statement):
items = None
body = None
- def _blockstart_toline(self):
+ @cachedproperty
+ def blockstart_tolineno(self):
return self.items[-1][0].tolineno
def get_children(self):