annotate Lib/test/test_importlib.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 58db4deb7ab1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 import contextlib
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2 import imp
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3 import importlib
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
4 import sys
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5 import unittest
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
8 @contextlib.contextmanager
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
9 def uncache(*names):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
10 """Uncache a module from sys.modules.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
11
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12 A basic sanity check is performed to prevent uncaching modules that either
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
13 cannot/shouldn't be uncached.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
14
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15 """
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16 for name in names:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
17 if name in ('sys', 'marshal', 'imp'):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
18 raise ValueError(
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
19 "cannot uncache {0} as it will break _importlib".format(name))
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
20 try:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
21 del sys.modules[name]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
22 except KeyError:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
23 pass
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
24 try:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
25 yield
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
26 finally:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
27 for name in names:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
28 try:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
29 del sys.modules[name]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
30 except KeyError:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
31 pass
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
32
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
33
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
34 @contextlib.contextmanager
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
35 def import_state(**kwargs):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
36 """Context manager to manage the various importers and stored state in the
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
37 sys module.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
38
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
39 The 'modules' attribute is not supported as the interpreter state stores a
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
40 pointer to the dict that the interpreter uses internally;
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
41 reassigning to sys.modules does not have the desired effect.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
42
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
43 """
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
44 originals = {}
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
45 try:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
46 for attr, default in (('meta_path', []), ('path', []),
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
47 ('path_hooks', []),
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
48 ('path_importer_cache', {})):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
49 originals[attr] = getattr(sys, attr)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
50 if attr in kwargs:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
51 new_value = kwargs[attr]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
52 del kwargs[attr]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
53 else:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
54 new_value = default
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
55 setattr(sys, attr, new_value)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
56 if len(kwargs):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
57 raise ValueError(
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
58 'unrecognized arguments: {0}'.format(kwargs.keys()))
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59 yield
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
60 finally:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
61 for attr, value in originals.items():
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
62 setattr(sys, attr, value)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
63
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
64
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
65 class mock_modules(object):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
66
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
67 """A mock importer/loader."""
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
68
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
69 def __init__(self, *names):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
70 self.modules = {}
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
71 for name in names:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
72 if not name.endswith('.__init__'):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
73 import_name = name
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
74 else:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
75 import_name = name[:-len('.__init__')]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
76 if '.' not in name:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
77 package = None
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
78 elif import_name == name:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
79 package = name.rsplit('.', 1)[0]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
80 else:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
81 package = import_name
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
82 module = imp.new_module(import_name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
83 module.__loader__ = self
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
84 module.__file__ = '<mock __file__>'
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
85 module.__package__ = package
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
86 module.attr = name
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
87 if import_name != name:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
88 module.__path__ = ['<mock __path__>']
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
89 self.modules[import_name] = module
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
90
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
91 def __getitem__(self, name):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
92 return self.modules[name]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
93
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
94 def find_module(self, fullname, path=None):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
95 if fullname not in self.modules:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
96 return None
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
97 else:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
98 return self
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
99
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
100 def load_module(self, fullname):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
101 if fullname not in self.modules:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
102 raise ImportError
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
103 else:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
104 sys.modules[fullname] = self.modules[fullname]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
105 return self.modules[fullname]
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
106
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
107 def __enter__(self):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
108 self._uncache = uncache(*self.modules.keys())
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
109 self._uncache.__enter__()
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
110 return self
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
111
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
112 def __exit__(self, *exc_info):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
113 self._uncache.__exit__(None, None, None)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
114
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
115
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
116
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
117 class ImportModuleTests(unittest.TestCase):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
118
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
119 """Test importlib.import_module."""
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
120
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
121 def test_module_import(self):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
122 # Test importing a top-level module.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
123 with mock_modules('top_level') as mock:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
124 with import_state(meta_path=[mock]):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
125 module = importlib.import_module('top_level')
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
126 self.assertEqual(module.__name__, 'top_level')
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
127
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
128 def test_absolute_package_import(self):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
129 # Test importing a module from a package with an absolute name.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
130 pkg_name = 'pkg'
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
131 pkg_long_name = '{0}.__init__'.format(pkg_name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
132 name = '{0}.mod'.format(pkg_name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
133 with mock_modules(pkg_long_name, name) as mock:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
134 with import_state(meta_path=[mock]):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
135 module = importlib.import_module(name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
136 self.assertEqual(module.__name__, name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
137
51992
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
138 def test_shallow_relative_package_import(self):
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
139 modules = ['a.__init__', 'a.b.__init__', 'a.b.c.__init__', 'a.b.c.d']
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
140 with mock_modules(*modules) as mock:
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
141 with import_state(meta_path=[mock]):
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
142 module = importlib.import_module('.d', 'a.b.c')
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
143 self.assertEqual(module.__name__, 'a.b.c.d')
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
144
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
145 def test_deep_relative_package_import(self):
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
146 # Test importing a module from a package through a relatve import.
51992
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
147 modules = ['a.__init__', 'a.b.__init__', 'a.c']
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
148 with mock_modules(*modules) as mock:
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
149 with import_state(meta_path=[mock]):
51992
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
150 module = importlib.import_module('..c', 'a.b')
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
151 self.assertEqual(module.__name__, 'a.c')
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
152
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
153 def test_absolute_import_with_package(self):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
154 # Test importing a module from a package with an absolute name with
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
155 # the 'package' argument given.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
156 pkg_name = 'pkg'
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
157 pkg_long_name = '{0}.__init__'.format(pkg_name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
158 name = '{0}.mod'.format(pkg_name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
159 with mock_modules(pkg_long_name, name) as mock:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
160 with import_state(meta_path=[mock]):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
161 module = importlib.import_module(name, pkg_name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
162 self.assertEqual(module.__name__, name)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
163
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
164 def test_relative_import_wo_package(self):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
165 # Relative imports cannot happen without the 'package' argument being
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
166 # set.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
167 self.assertRaises(TypeError, importlib.import_module, '.support')
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
168
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
169
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
170 def test_main():
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
171 from test.test_support import run_unittest
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
172 run_unittest(ImportModuleTests)
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
173
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
174
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
175 if __name__ == '__main__':
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
176 test_main()