Permalink
Browse files

fix: absolute imports from project level package. Fix recursion evalu…

…ator limit
  • Loading branch information...
1 parent 270f94d commit 2f56f9e57009110b016083676a52b81d3f5c5578 @baverman committed Oct 12, 2011
View
2 supplement/calls.py
@@ -112,6 +112,8 @@ def collect_calls(self, scope, skip_if_exists=False):
try:
func = s.eval(func, False)
+ except AssertionError:
+ raise
except:
continue
View
8 supplement/evaluator.py
@@ -188,10 +188,15 @@ def visit_Compare(self, node):
def process(self, tree, scope, skip_toplevel=True):
#from .tree import dump_tree; print '!!!', scope.filename; print dump_tree(tree); print
+ if getattr(tree, '_evaluating', False):
+ return UnknownObject()
+
self.scope = scope
self.ops = []
self.stack = []
+ tree._evaluating = True
+
try:
if skip_toplevel:
self.generic_visit(tree)
@@ -204,6 +209,7 @@ def process(self, tree, scope, skip_toplevel=True):
raise
except Exception, e:
if not getattr(e, '_processed', None):
+ logging.getLogger(__name__).exception('Boo')
from .tree import dump_tree
logger = logging.getLogger(__name__)
logger.exception('\n<<<<<<<<<<<<<<<<<')
@@ -212,5 +218,7 @@ def process(self, tree, scope, skip_toplevel=True):
e._processed = True
raise
+ finally:
+ tree._evaluating = False
return self.stack[0]
View
55 supplement/module.py
@@ -12,7 +12,6 @@ class ModuleProvider(object):
def __init__(self):
self.cache = {}
self.override = []
- self.root_modules = {}
def add_override(self, override):
self.override.append(override)
@@ -30,59 +29,32 @@ def on_file_change(self, filename, module_name):
else:
m.invalidate()
- def invalidate_root_module(self, filename, module_name):
- try:
- del self.root_modules[module_name]
- except KeyError:
- pass
-
- def get_root_module(self, project, name, path):
- key = (name, path)
- try:
- return self.root_modules[key]
- except KeyError:
- pass
-
- filename = join(path, name, '__init__.py')
- m = imp.new_module(name)
- m.__file__ = filename
- m.__package__ = name
- m.__path__ = [join(path, name)]
-
- execfile(filename, m.__dict__)
-
- self.root_modules[key] = m
-
- project.monitor.monitor(filename, self.invalidate_root_module, name)
- return m
-
def get_absolute_name(self, project, name, filename):
- package_path = None
if name[0] == '.':
assert filename, 'You should provide source filename to resolve relative imports'
- package_name, package_path = project.package_resolver.get(normpath(abspath(dirname(filename))))
+ package_name = project.package_resolver.get(normpath(abspath(dirname(filename))))
level = len(name) - len(name.lstrip('.')) - 1
parts = package_name.split('.')
name = '.'.join(parts[:len(parts)-level]) + (name[level:] if len(name) > level + 1 else '')
else:
if filename and exists(join(dirname(filename), '__init__.py')):
pkg_dir = dirname(filename)
if exists(join(pkg_dir, name+'.py')):
- package_name, package_path = project.package_resolver.get(normpath(abspath(pkg_dir)))
+ package_name = project.package_resolver.get(normpath(abspath(pkg_dir)))
name = package_name + '.' + name
- return name, package_path
+ return name
def get(self, project, name, filename=None):
- name, ppath = self.get_absolute_name(project, name, filename)
+ name = self.get_absolute_name(project, name, filename)
try:
return self.cache[name]
except KeyError:
pass
- m = Module(project, name, ppath)
+ m = Module(project, name)
for o in self.override:
m = o(project, m)
@@ -130,19 +102,10 @@ def load_module(project, name, package_path):
oldsyspath = sys.path
sys.path = project.paths
- try:
- if package_path and package_path not in sys.path:
- pkg_name, _, tail = name.partition('.')
-
- root_module = project.module_providers['default'].get_root_module(
- project, pkg_name, package_path)
-
- if tail:
- pi.add(pkg_name)
- sys.modules[pkg_name] = root_module
- else:
- return root_module
+ if exists(join(project.root, '__init__.py')):
+ sys.path.insert(1, dirname(project.root))
+ try:
__import__(name)
return sys.modules[name]
except ImportError:
@@ -188,7 +151,7 @@ def get(self, path):
ppath = newpath
- result = self.cache[path] = '.'.join(reversed(packages)), ppath
+ result = self.cache[path] = '.'.join(reversed(packages))
return result
View
1 supplement/names.py
@@ -333,7 +333,6 @@ def process(self, name, scope, node):
self.scope = scope
self.name = name
self.result = {}
-
self.generic_visit(node)
return self.result
View
4 supplement/objects.py
@@ -40,8 +40,8 @@ def __init__(self, node):
self.node = node
def get_object(self):
- name, inode = self.node[1], self.node[2]
- module = self.project.get_module(inode.module, self.filename)
+ name, mname = self.node[1:3]
+ module = self.project.get_module(mname, self.filename)
return module[name]
View
1 supplement/project.py
@@ -47,6 +47,7 @@ def _refresh_paths(self):
self.paths.extend(sys.path)
def get_module(self, name, filename=None):
+ assert name
ctx, sep, name = name.partition(':')
if not sep:
ctx, name = 'default', ctx
View
9 supplement/tree.py
@@ -59,8 +59,13 @@ def visit_FunctionDef(self, node):
def visit_ImportFrom(self, node):
for n in node.names:
- name = n.asname if n.asname else n.name
- self.attrs[name] = 'imported', n.name, node
+ module_name = '.' * node.level + (node.module if node.module else '')
+ if n.name == '*':
+ # TODO handle star names in imported objects
+ pass
+ else:
+ name = n.asname if n.asname else n.name
+ self.attrs[name] = 'imported', n.name, module_name
def visit_ClassDef(self, node):
self.attrs[node.name] = 'class', node
View
6 tests/helpers.py
@@ -21,7 +21,7 @@ def create_module(project, name, source):
exec code in module.__dict__
- m = TestModule(project, name, None)
+ m = TestModule(project, name)
m._module = module
m.source = source
module.__file__ = name + '.py'
@@ -33,8 +33,8 @@ def create_module(project, name, source):
if package_name:
module.__file__ = "%s/%s.py" % (package_path, module_name)
- project.package_resolver.cache[os.path.abspath(package_name)] = package_name, package_path
- p = TestModule(project, package_name, None)
+ project.package_resolver.cache[os.path.abspath(package_name)] = package_name
+ p = TestModule(project, package_name)
p._module = sys.modules[package_name] = types.ModuleType(package_name)
setattr(sys.modules[package_name], module_name, module)
p._module.__file__ = "%s/__init__.py" % package_path
View
11 tests/test_irl_cases.py
@@ -62,13 +62,10 @@ def fnc(file):
''')
#def test_snaked(project):
-# project.set_root('/home/bobrov/work/snaked')
-#
-# result = do_assist(project, '''
-# from . import window
-#
-# window.Window().|
-# ''', '/home/bobrov/work/snaked/snaked/core/test.py')
+# project.set_root('/home/bobrov/work/bonent')
+# fname = project.root + '/bparser/smscenter.py'
+# source, pos = open(fname).read(), 524
+# result = do_assist(project, source, fname)
#
# print result
# assert False

0 comments on commit 2f56f9e

Please sign in to comment.