finder docstring and naming improvements

This commit is contained in:
Dave Halter
2014-05-20 15:17:07 +02:00
parent ad762f674e
commit 79556a7935
+28 -23
View File
@@ -80,9 +80,9 @@ class NameFinder(object):
`scope_names_generator`), until the name fits. `scope_names_generator`), until the name fits.
""" """
result = [] result = []
for nscope, name_list in scope_names_generator: for name_list_scope, name_list in scope_names_generator:
break_scopes = [] break_scopes = []
if not isinstance(nscope, compiled.CompiledObject): if not isinstance(name_list_scope, compiled.CompiledObject):
# Here is the position stuff happening (sorting of variables). # Here is the position stuff happening (sorting of variables).
# Compiled objects don't need that, because there's only one # Compiled objects don't need that, because there's only one
# reference. # reference.
@@ -96,27 +96,21 @@ class NameFinder(object):
if scope in break_scopes: if scope in break_scopes:
continue continue
# Exclude `arr[1] =` from the result set.
if not self._name_is_array_assignment(name): if not self._name_is_array_assignment(name):
result.append(name) # `arr[1] =` is not the definition result.append(name)
# for comparison we need the raw class
# this means that a definition was found and is not e.g. if result and self._is_name_break_scope(name):
# in if/else. if self._does_scope_break_immediately(scope, name_list_scope):
if result and self._name_is_break_scope(name):
if isinstance(scope, pr.Flow) \
or isinstance(scope, pr.KeywordStatement) \
and scope.name == 'global':
s = nscope.base if isinstance(nscope, er.Class) else nscope
if scope == s:
break
else:
break break
break_scopes.append(scope) else:
break_scopes.append(scope)
if result: if result:
break break
self._last_filter_name_scope = nscope self._last_filter_name_scope = name_list_scope
debug.dbg('finder.filter_name "%s" in (%s-%s): %s@%s', self.name_str, debug.dbg('finder.filter_name "%s" in (%s-%s): %s@%s', self.name_str,
self.scope, nscope, u(result), self.position) self.scope, name_list_scope, u(result), self.position)
return result return result
def _check_getattr(self, inst): def _check_getattr(self, inst):
@@ -135,21 +129,32 @@ class NameFinder(object):
result = inst.execute_subscope_by_name('__getattribute__', [name]) result = inst.execute_subscope_by_name('__getattribute__', [name])
return result return result
def _name_is_break_scope(self, name): def _is_name_break_scope(self, name):
""" """
Returns the parent of a name, which means the element which stands Returns True except for nested imports and instance variables.
behind a name.
""" """
par = name.parent par = name.parent
if par.isinstance(pr.Statement): if par.isinstance(pr.Statement):
if isinstance(name, er.InstanceElement) and not name.is_class_var: if isinstance(name, er.InstanceElement) and not name.is_class_var:
return False return False
elif isinstance(par, pr.Import) and len(par.namespace) > 1: elif isinstance(par, pr.Import) and par.is_nested():
# TODO multi-level import non-breakable
return False return False
return True return True
def _does_scope_break_immediately(self, scope, name_list_scope):
"""
In comparison to everthing else, if/while/etc doesn't break directly,
because there are multiple different places in which a variable can be
defined.
"""
if isinstance(scope, pr.Flow) \
or isinstance(scope, pr.KeywordStatement) and scope.name == 'global':
if isinstance(name_list_scope, er.Class):
name_list_scope = name_list_scope.base
return scope == name_list_scope
else:
return True
def _name_is_array_assignment(self, name): def _name_is_array_assignment(self, name):
if name.parent.isinstance(pr.Statement): if name.parent.isinstance(pr.Statement):
def is_execution(calls): def is_execution(calls):