closes #70565: absolute imports treated as relative (patch by Jacek Konieczny)

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changesetc8f6995834aa
branchdefault
phasepublic
hiddenno
parent revision#607f472c60d9 closes #70381: IndendationError in import causes crash
child revision#77c1171bcace fix absolute import behaviour with python 2.7: it seems that absolute import are actually not activated by default, at least with 2.7.1
files modified by this revision
ChangeLog
scoped_nodes.py
test/regrtest_data/absimp/string.py
test/unittest_inference.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1310128256 -7200
# Fri Jul 08 14:30:56 2011 +0200
# Node ID c8f6995834aafcf01879ae1a8076d454b2305941
# Parent 607f472c60d9838fddc99687b63ef65b84e5e649
closes #70565: absolute imports treated as relative (patch by Jacek Konieczny)

diff --git a/ChangeLog b/ChangeLog
@@ -3,10 +3,11 @@
1 
2  --
3      * added column offset information on nodes (patch by fawce)
4      * #70497: Crash on AttributeError: 'NoneType' object has no attribute '_infer_name'
5      * #70381: IndendationError in import causes crash
6 +    * #70565: absolute imports treated as relative (patch by Jacek Konieczny)
7 
8  2011-01-11  --  0.21.1
9      * python3: handle file encoding; fix a lot of tests
10 
11      * fix #52006: "True" and "False" can be assigned as variable in Python2x
diff --git a/scoped_nodes.py b/scoped_nodes.py
@@ -322,10 +322,12 @@
12      else:
13          absolute_import_activated = lambda self: True
14 
15      def import_module(self, modname, relative_only=False, level=None):
16          """import the given module considering self as context"""
17 +        if relative_only and level is None:
18 +            level = 0
19          absmodname = self.relative_to_absolute_name(modname, level)
20          try:
21              return MANAGER.astng_from_module_name(absmodname)
22          except ASTNGBuildingException:
23              # we only want to import a sub module or package of this module,
@@ -340,10 +342,12 @@
24          The relative import can be implicit or explicit.
25          """
26          # XXX this returns non sens when called on an absolute import
27          # like 'pylint.checkers.logilab.astng.utils'
28          # XXX doesn't return absolute name if self.name isn't absolute name
29 +        if self.absolute_import_activated() and level is None:
30 +            return modname
31          if level:
32              if self.package:
33                  level = level - 1
34              package_name = self.name.rsplit('.', level)[0]
35          elif self.package:
diff --git a/test/regrtest_data/absimp/string.py b/test/regrtest_data/absimp/string.py
@@ -0,0 +1,3 @@
36 +from __future__ import absolute_import
37 +import string
38 +print string
diff --git a/test/unittest_inference.py b/test/unittest_inference.py
@@ -924,10 +924,19 @@
39          self.failUnless(astng.absolute_import_activated(), True)
40          infered = get_name_node(astng, 'import_package_subpackage_module').infer().next()
41          # failed to import since absolute_import is activated
42          self.failUnless(infered is YES)
43 
44 +    def test_nonregr_absolute_import(self):
45 +        fname = join(abspath(dirname(__file__)), 'regrtest_data', 'absimp', 'string.py')
46 +        astng = builder.file_build(fname, 'absimp.string')
47 +        self.failUnless(astng.absolute_import_activated(), True)
48 +        infered = get_name_node(astng, 'string').infer().next()
49 +        self.assertIsInstance(infered, nodes.Module)
50 +        self.assertEqual(infered.name, 'string')
51 +        self.failUnless('lower' in infered.locals)
52 +
53      def test_mechanize_open(self):
54          try:
55              import mechanize
56          except ImportError:
57              self.skipTest('require mechanize installed')