diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2014-07-24 16:16:46 +0200 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2014-07-24 16:16:46 +0200 |
commit | c4bfde2863540a9260d81c094bab20d29b4ff612 (patch) | |
tree | dbed658f10f3e73e783e2109688185b7a8ee8433 | |
parent | 662ca1ce434ff233b2afcb0b4f2eb855f18b547e (diff) | |
download | astroid-c4bfde2863540a9260d81c094bab20d29b4ff612.tar.gz |
Fix names grabed using wildcard import in "absolute import mode"
(ie with absolute_import activated from the __future__ or with python 3).
To do so, refactor do_import_module a bit so it may be easily used for wildcard
import as well.
Fix pylint issue #58.
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | builder.py | 6 | ||||
-rw-r--r-- | inference.py | 2 | ||||
-rw-r--r-- | mixins.py | 4 | ||||
-rw-r--r-- | test/data/module1abs/__init__.py | 4 | ||||
-rw-r--r-- | test/data/module1abs/core.py | 1 | ||||
-rw-r--r-- | test/unittest_manager.py | 4 | ||||
-rw-r--r-- | test/unittest_nodes.py | 8 |
8 files changed, 27 insertions, 6 deletions
@@ -21,6 +21,10 @@ Change log for the astroid package (used to be astng) most things there are for pylint/astroid only and we want to be able to fix them without requiring a new logilab.common release + * Fix names grabed using wildcard import in "absolute import mode" + (ie with absolute_import activated from the __future__ or with + python 3). Fix pylint issue #58. + 2014-04-30 -- 1.1.1 * `Class.metaclass()` looks in ancestors when the current class does not define explicitly a metaclass. @@ -1,4 +1,4 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of astroid. @@ -186,8 +186,8 @@ class AstroidBuilder(InspectBuilder): for (name, asname) in node.names: if name == '*': try: - imported = node.root().import_module(node.modname) - except AstroidBuildingException: + imported = node.do_import_module() + except InferenceError: continue for name in imported.wildcard_import_names(): node.parent.set_local(name, node) diff --git a/inference.py b/inference.py index 35cce33..549e9a1 100644 --- a/inference.py +++ b/inference.py @@ -197,7 +197,7 @@ def infer_from(self, context=None, asname=True): raise InferenceError() if asname: name = self.real_name(name) - module = self.do_import_module(self.modname) + module = self.do_import_module() try: context = copy_context(context) context.lookupname = name @@ -85,7 +85,7 @@ class FromImportMixIn(FilterStmtsMixin): def _infer_name(self, frame, name): return name - def do_import_module(self, modname): + def do_import_module(self, modname=None): """return the ast for a module whose name is <modname> imported by <self> """ # handle special case where we are on a package node importing a module @@ -94,6 +94,8 @@ class FromImportMixIn(FilterStmtsMixin): # XXX: no more needed ? mymodule = self.root() level = getattr(self, 'level', None) # Import as no level + if modname is None: + modname = self.modname # XXX we should investigate deeper if we really want to check # importing itself: modname and mymodule.name be relative or absolute if mymodule.relative_to_absolute_name(modname, level) == mymodule.name: diff --git a/test/data/module1abs/__init__.py b/test/data/module1abs/__init__.py new file mode 100644 index 0000000..9eb5902 --- /dev/null +++ b/test/data/module1abs/__init__.py @@ -0,0 +1,4 @@ +from __future__ import absolute_import +from . import core +from .core import * +print sys.version diff --git a/test/data/module1abs/core.py b/test/data/module1abs/core.py new file mode 100644 index 0000000..de10111 --- /dev/null +++ b/test/data/module1abs/core.py @@ -0,0 +1 @@ +import sys diff --git a/test/unittest_manager.py b/test/unittest_manager.py index 3e7bd62..6b3c489 100644 --- a/test/unittest_manager.py +++ b/test/unittest_manager.py @@ -194,7 +194,9 @@ class AstroidManagerTC(TestCase): 'data.format', 'data.lmfp', 'data.lmfp.foo', - 'data.module', 'data.module2', 'data.noendingnewline', + 'data.module', + 'data.module1abs', 'data.module1abs.core', + 'data.module2', 'data.noendingnewline', 'data.nonregr', 'data.notall'] self.assertListEqual(sorted(k for k in obj.keys()), expected) diff --git a/test/unittest_nodes.py b/test/unittest_nodes.py index 8fa1c0c..790c3b2 100644 --- a/test/unittest_nodes.py +++ b/test/unittest_nodes.py @@ -315,6 +315,14 @@ except PickleError: m = astroid['email'].infer(ctx).next() self.assertFalse(m.file.startswith(self.datapath('email.py'))) + def test_more_absolute_import(self): + sys.path.insert(0, self.datapath('moreabsimport')) + try: + astroid = abuilder.file_build(self.datapath('module1abs/__init__.py')) + self.assertIn('sys', astroid.locals) + finally: + sys.path.pop(0) + class CmpNodeTC(testlib.TestCase): def test_as_string(self): |