summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2014-07-24 16:16:46 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2014-07-24 16:16:46 +0200
commitc4bfde2863540a9260d81c094bab20d29b4ff612 (patch)
treedbed658f10f3e73e783e2109688185b7a8ee8433
parent662ca1ce434ff233b2afcb0b4f2eb855f18b547e (diff)
downloadastroid-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--ChangeLog4
-rw-r--r--builder.py6
-rw-r--r--inference.py2
-rw-r--r--mixins.py4
-rw-r--r--test/data/module1abs/__init__.py4
-rw-r--r--test/data/module1abs/core.py1
-rw-r--r--test/unittest_manager.py4
-rw-r--r--test/unittest_nodes.py8
8 files changed, 27 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 7261cac..ae4d7bc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
diff --git a/builder.py b/builder.py
index f419ae3..83c2ba5 100644
--- a/builder.py
+++ b/builder.py
@@ -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
diff --git a/mixins.py b/mixins.py
index 5e7b787..e462313 100644
--- a/mixins.py
+++ b/mixins.py
@@ -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):