blob: b0e5a95b23aa8ca69b00388d96c9d27431865110 [file] [log] [blame]
Nick Coghlanbe7e49f2012-07-20 23:40:09 +10001# We import importlib *ASAP* in order to test #15386
2import importlib
Collin Winter6498cff2010-03-17 03:14:31 +00003import builtins
Christian Heimes13a7a212008-01-07 17:13:09 +00004import imp
Brett Cannon86ae9812012-07-20 15:40:57 -04005from test.test_importlib.import_ import util as importlib_util
Antoine Pitroud35cbf62009-01-06 19:02:24 +00006import marshal
Collin Winter88e333d2010-03-17 03:09:21 +00007import os
Charles-François Natalia13b1fa2011-10-04 19:17:26 +02008import platform
Collin Winter88e333d2010-03-17 03:09:21 +00009import py_compile
10import random
Collin Winter88e333d2010-03-17 03:09:21 +000011import stat
12import sys
13import unittest
R. David Murrayce4b1702010-12-14 23:06:25 +000014import textwrap
Antoine Pitroudd21f682012-01-25 03:00:57 +010015import errno
Jason R. Coombs71fde312012-06-17 03:53:47 -040016import shutil
Nick Coghlaneb8d6272012-10-19 23:32:00 +100017import contextlib
Barry Warsaw28a691b2010-04-17 00:19:56 +000018
Jason R. Coombs71fde312012-06-17 03:53:47 -040019import test.support
Barry Warsaw28a691b2010-04-17 00:19:56 +000020from test.support import (
21 EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,
22 make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,
Antoine Pitrou48114b92012-06-17 22:33:38 +020023 unlink, unload, create_empty_file, cpython_only)
R. David Murrayce4b1702010-12-14 23:06:25 +000024from test import script_helper
Guido van Rossumbd6f4fb2000-10-24 17:16:32 +000025
Tim Peters72f98e92001-05-08 15:19:57 +000026
Tim Peters08138fd2004-08-02 03:58:27 +000027def remove_files(name):
Skip Montanaro7a98be22007-08-16 14:35:24 +000028 for f in (name + ".py",
29 name + ".pyc",
30 name + ".pyo",
31 name + ".pyw",
Tim Peters08138fd2004-08-02 03:58:27 +000032 name + "$py.class"):
Florent Xicluna8fbddf12010-03-17 20:29:51 +000033 unlink(f)
Florent Xicluna27354cc2010-08-16 18:41:19 +000034 rmtree('__pycache__')
Tim Peters08138fd2004-08-02 03:58:27 +000035
Tim Petersc1731372001-08-04 08:12:36 +000036
Nick Coghlaneb8d6272012-10-19 23:32:00 +100037@contextlib.contextmanager
38def _ready_to_import(name=None, source=""):
39 # sets up a temporary directory and removes it
40 # creates the module file
41 # temporarily clears the module from sys.modules (if any)
42 name = name or "spam"
43 with script_helper.temp_dir() as tempdir:
44 path = script_helper.make_script(tempdir, name, source)
45 old_module = sys.modules.pop(name, None)
46 try:
47 sys.path.insert(0, tempdir)
48 yield name, path
49 sys.path.remove(tempdir)
50 finally:
51 if old_module is not None:
52 sys.modules[name] = old_module
53
54
Collin Winter88e333d2010-03-17 03:09:21 +000055class ImportTests(unittest.TestCase):
Tim Petersc1731372001-08-04 08:12:36 +000056
Antoine Pitrou46719af2011-03-21 19:05:02 +010057 def setUp(self):
58 remove_files(TESTFN)
Antoine Pitrouc541f8e2012-02-20 01:48:16 +010059 importlib.invalidate_caches()
Antoine Pitrou46719af2011-03-21 19:05:02 +010060
Florent Xicluna8fbddf12010-03-17 20:29:51 +000061 def tearDown(self):
62 unload(TESTFN)
Barry Warsaw28a691b2010-04-17 00:19:56 +000063
Florent Xicluna8fbddf12010-03-17 20:29:51 +000064 setUp = tearDown
65
Collin Winter88e333d2010-03-17 03:09:21 +000066 def test_case_sensitivity(self):
67 # Brief digression to test that import is case-sensitive: if we got
68 # this far, we know for sure that "random" exists.
Benjamin Peterson1c87e292010-10-30 23:04:49 +000069 with self.assertRaises(ImportError):
Thomas Wouters89f507f2006-12-13 04:49:30 +000070 import RAnDoM
Tim Peters08138fd2004-08-02 03:58:27 +000071
Collin Winter88e333d2010-03-17 03:09:21 +000072 def test_double_const(self):
73 # Another brief digression to test the accuracy of manifest float
74 # constants.
Thomas Wouters89f507f2006-12-13 04:49:30 +000075 from test import double_const # don't blink -- that *was* the test
Tim Peters08138fd2004-08-02 03:58:27 +000076
Collin Winter88e333d2010-03-17 03:09:21 +000077 def test_import(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +000078 def test_with_extension(ext):
Collin Winter88e333d2010-03-17 03:09:21 +000079 # The extension is normally ".py", perhaps ".pyw".
Thomas Wouters89f507f2006-12-13 04:49:30 +000080 source = TESTFN + ext
Skip Montanaro7a98be22007-08-16 14:35:24 +000081 pyo = TESTFN + ".pyo"
Florent Xicluna8fbddf12010-03-17 20:29:51 +000082 if is_jython:
Thomas Wouters89f507f2006-12-13 04:49:30 +000083 pyc = TESTFN + "$py.class"
84 else:
Skip Montanaro7a98be22007-08-16 14:35:24 +000085 pyc = TESTFN + ".pyc"
Tim Peters08138fd2004-08-02 03:58:27 +000086
Benjamin Peterson16a1f632009-06-13 13:01:19 +000087 with open(source, "w") as f:
Barry Warsaw28a691b2010-04-17 00:19:56 +000088 print("# This tests Python's ability to import a",
89 ext, "file.", file=f)
Benjamin Peterson16a1f632009-06-13 13:01:19 +000090 a = random.randrange(1000)
91 b = random.randrange(1000)
92 print("a =", a, file=f)
93 print("b =", b, file=f)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000094
Amaury Forgeot d'Arcdd9e3b82007-11-16 00:56:23 +000095 if TESTFN in sys.modules:
96 del sys.modules[TESTFN]
Brett Cannonfd074152012-04-14 14:10:13 -040097 importlib.invalidate_caches()
Thomas Wouters89f507f2006-12-13 04:49:30 +000098 try:
99 try:
100 mod = __import__(TESTFN)
Guido van Rossumb940e112007-01-10 16:19:56 +0000101 except ImportError as err:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000102 self.fail("import from %s failed: %s" % (ext, err))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000104 self.assertEqual(mod.a, a,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 "module loaded (%s) but contents invalid" % mod)
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000106 self.assertEqual(mod.b, b,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000107 "module loaded (%s) but contents invalid" % mod)
108 finally:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000109 forget(TESTFN)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000110 unlink(source)
111 unlink(pyc)
112 unlink(pyo)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000113
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 sys.path.insert(0, os.curdir)
115 try:
Skip Montanaro7a98be22007-08-16 14:35:24 +0000116 test_with_extension(".py")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117 if sys.platform.startswith("win"):
Collin Winter88e333d2010-03-17 03:09:21 +0000118 for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 test_with_extension(ext)
120 finally:
121 del sys.path[0]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000122
Victor Stinner53ffdc52011-09-23 18:54:40 +0200123 def test_bug7732(self):
124 source = TESTFN + '.py'
125 os.mkdir(source)
126 try:
127 self.assertRaisesRegex(ImportError, '^No module',
128 imp.find_module, TESTFN, ["."])
129 finally:
130 os.rmdir(source)
131
Thomas Wouters89f507f2006-12-13 04:49:30 +0000132 def test_module_with_large_stack(self, module='longlist'):
Collin Winter88e333d2010-03-17 03:09:21 +0000133 # Regression test for http://bugs.python.org/issue561858.
Skip Montanaro7a98be22007-08-16 14:35:24 +0000134 filename = module + '.py'
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135
Collin Winter88e333d2010-03-17 03:09:21 +0000136 # Create a file with a list of 65000 elements.
Brett Cannon2cab50b2010-07-03 01:32:48 +0000137 with open(filename, 'w') as f:
Collin Winter88e333d2010-03-17 03:09:21 +0000138 f.write('d = [\n')
139 for i in range(65000):
140 f.write('"",\n')
141 f.write(']')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000142
Brett Cannon2cab50b2010-07-03 01:32:48 +0000143 try:
144 # Compile & remove .py file; we only need .pyc (or .pyo).
145 # Bytecode must be relocated from the PEP 3147 bytecode-only location.
146 py_compile.compile(filename)
147 finally:
148 unlink(filename)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000149
Collin Winter88e333d2010-03-17 03:09:21 +0000150 # Need to be able to load from current dir.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000151 sys.path.append('')
Brett Cannon73def612012-04-14 14:38:19 -0400152 importlib.invalidate_caches()
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153
Brett Cannon93220d02010-05-15 22:20:16 +0000154 try:
Brett Cannon2cab50b2010-07-03 01:32:48 +0000155 make_legacy_pyc(filename)
Brett Cannon93220d02010-05-15 22:20:16 +0000156 # This used to crash.
157 exec('import ' + module)
158 finally:
159 # Cleanup.
160 del sys.path[-1]
161 unlink(filename + 'c')
162 unlink(filename + 'o')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000163
164 def test_failing_import_sticks(self):
Skip Montanaro7a98be22007-08-16 14:35:24 +0000165 source = TESTFN + ".py"
Collin Winter88e333d2010-03-17 03:09:21 +0000166 with open(source, "w") as f:
167 print("a = 1/0", file=f)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000168
169 # New in 2.4, we shouldn't be able to import that no matter how often
170 # we try.
171 sys.path.insert(0, os.curdir)
Antoine Pitrou021548c2012-07-12 19:21:43 +0200172 importlib.invalidate_caches()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000173 if TESTFN in sys.modules:
174 del sys.modules[TESTFN]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175 try:
Collin Winter88e333d2010-03-17 03:09:21 +0000176 for i in [1, 2, 3]:
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000177 self.assertRaises(ZeroDivisionError, __import__, TESTFN)
178 self.assertNotIn(TESTFN, sys.modules,
179 "damaged module in sys.modules on %i try" % i)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180 finally:
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000181 del sys.path[0]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000182 remove_files(TESTFN)
183
Thomas Wouters89f507f2006-12-13 04:49:30 +0000184 def test_import_name_binding(self):
185 # import x.y.z binds x in the current namespace
186 import test as x
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000187 import test.support
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000188 self.assertTrue(x is test, x.__name__)
189 self.assertTrue(hasattr(test.support, "__file__"))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190
191 # import x.y.z as w binds z as w
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000192 import test.support as y
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000193 self.assertTrue(y is test.support, y.__name__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000194
Christian Heimes13a7a212008-01-07 17:13:09 +0000195 def test_failing_reload(self):
196 # A failing reload should leave the module object in sys.modules.
Collin Winter88e333d2010-03-17 03:09:21 +0000197 source = TESTFN + os.extsep + "py"
Christian Heimes13a7a212008-01-07 17:13:09 +0000198 with open(source, "w") as f:
199 f.write("a = 1\nb=2\n")
200
201 sys.path.insert(0, os.curdir)
202 try:
203 mod = __import__(TESTFN)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000204 self.assertIn(TESTFN, sys.modules)
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000205 self.assertEqual(mod.a, 1, "module has wrong attribute values")
206 self.assertEqual(mod.b, 2, "module has wrong attribute values")
Christian Heimes13a7a212008-01-07 17:13:09 +0000207
208 # On WinXP, just replacing the .py file wasn't enough to
209 # convince reload() to reparse it. Maybe the timestamp didn't
210 # move enough. We force it to get reparsed by removing the
211 # compiled file too.
212 remove_files(TESTFN)
213
214 # Now damage the module.
215 with open(source, "w") as f:
216 f.write("a = 10\nb=20//0\n")
217
218 self.assertRaises(ZeroDivisionError, imp.reload, mod)
219 # But we still expect the module to be in sys.modules.
220 mod = sys.modules.get(TESTFN)
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000221 self.assertIsNot(mod, None, "expected module to be in sys.modules")
Christian Heimes13a7a212008-01-07 17:13:09 +0000222
223 # We should have replaced a w/ 10, but the old b value should
224 # stick.
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000225 self.assertEqual(mod.a, 10, "module has wrong attribute values")
226 self.assertEqual(mod.b, 2, "module has wrong attribute values")
Christian Heimes13a7a212008-01-07 17:13:09 +0000227
228 finally:
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000229 del sys.path[0]
Christian Heimes13a7a212008-01-07 17:13:09 +0000230 remove_files(TESTFN)
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000231 unload(TESTFN)
Christian Heimes13a7a212008-01-07 17:13:09 +0000232
Christian Heimes3b06e532008-01-07 20:12:44 +0000233 def test_file_to_source(self):
234 # check if __file__ points to the source file where available
235 source = TESTFN + ".py"
236 with open(source, "w") as f:
237 f.write("test = None\n")
238
239 sys.path.insert(0, os.curdir)
240 try:
241 mod = __import__(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000242 self.assertTrue(mod.__file__.endswith('.py'))
Christian Heimes3b06e532008-01-07 20:12:44 +0000243 os.remove(source)
244 del sys.modules[TESTFN]
Barry Warsaw28a691b2010-04-17 00:19:56 +0000245 make_legacy_pyc(source)
Antoine Pitrouc541f8e2012-02-20 01:48:16 +0100246 importlib.invalidate_caches()
Christian Heimes3b06e532008-01-07 20:12:44 +0000247 mod = __import__(TESTFN)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000248 base, ext = os.path.splitext(mod.__file__)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000249 self.assertIn(ext, ('.pyc', '.pyo'))
Christian Heimes3b06e532008-01-07 20:12:44 +0000250 finally:
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000251 del sys.path[0]
Christian Heimes3b06e532008-01-07 20:12:44 +0000252 remove_files(TESTFN)
253 if TESTFN in sys.modules:
254 del sys.modules[TESTFN]
255
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000256 def test_import_name_binding(self):
257 # import x.y.z binds x in the current namespace.
258 import test as x
259 import test.support
260 self.assertIs(x, test, x.__name__)
261 self.assertTrue(hasattr(test.support, "__file__"))
262
263 # import x.y.z as w binds z as w.
264 import test.support as y
265 self.assertIs(y, test.support, y.__name__)
266
Collin Winter88e333d2010-03-17 03:09:21 +0000267 def test_import_by_filename(self):
Christian Heimes454f37b2008-01-10 00:10:02 +0000268 path = os.path.abspath(TESTFN)
Victor Stinner6c6f8512010-08-07 10:09:35 +0000269 encoding = sys.getfilesystemencoding()
270 try:
271 path.encode(encoding)
272 except UnicodeEncodeError:
273 self.skipTest('path is not encodable to {}'.format(encoding))
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000274 with self.assertRaises(ImportError) as c:
Christian Heimes454f37b2008-01-10 00:10:02 +0000275 __import__(path)
Christian Heimes454f37b2008-01-10 00:10:02 +0000276
R. David Murrayce4b1702010-12-14 23:06:25 +0000277 def test_import_in_del_does_not_crash(self):
278 # Issue 4236
279 testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
280 import sys
281 class C:
282 def __del__(self):
283 import imp
284 sys.argv.insert(0, C())
285 """))
286 script_helper.assert_python_ok(testfn)
287
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100288 def test_timestamp_overflow(self):
289 # A modification timestamp larger than 2**32 should not be a problem
290 # when importing a module (issue #11235).
Antoine Pitrou05f29b72012-01-25 01:35:26 +0100291 sys.path.insert(0, os.curdir)
292 try:
293 source = TESTFN + ".py"
294 compiled = imp.cache_from_source(source)
295 with open(source, 'w') as f:
296 pass
297 try:
Antoine Pitrou33d15f72012-01-25 18:01:45 +0100298 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Antoine Pitrou05f29b72012-01-25 01:35:26 +0100299 except OverflowError:
300 self.skipTest("cannot set modification time to large integer")
Antoine Pitroudd21f682012-01-25 03:00:57 +0100301 except OSError as e:
302 if e.errno != getattr(errno, 'EOVERFLOW', None):
303 raise
304 self.skipTest("cannot set modification time to large integer ({})".format(e))
Antoine Pitrou05f29b72012-01-25 01:35:26 +0100305 __import__(TESTFN)
306 # The pyc file was created.
307 os.stat(compiled)
308 finally:
309 del sys.path[0]
310 remove_files(TESTFN)
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100311
Brett Cannon7385adc2012-08-17 13:21:16 -0400312 def test_bogus_fromlist(self):
313 try:
314 __import__('http', fromlist=['blah'])
315 except ImportError:
316 self.fail("fromlist must allow bogus names")
317
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +0000318
Nick Coghlaneb8d6272012-10-19 23:32:00 +1000319class FilePermissionTests(unittest.TestCase):
320 # tests for file mode on cached .pyc/.pyo files
321
322 @unittest.skipUnless(os.name == 'posix',
323 "test meaningful only on posix systems")
324 def test_creation_mode(self):
325 mask = 0o022
326 with temp_umask(mask), _ready_to_import() as (name, path):
327 cached_path = imp.cache_from_source(path)
328 module = __import__(name)
329 if not os.path.exists(cached_path):
330 self.fail("__import__ did not result in creation of "
331 "either a .pyc or .pyo file")
332 stat_info = os.stat(cached_path)
333
334 # Check that the umask is respected, and the executable bits
335 # aren't set.
336 self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)),
337 oct(0o666 & ~mask))
338
339 @unittest.skipUnless(os.name == 'posix',
340 "test meaningful only on posix systems")
341 def test_cached_mode_issue_2051(self):
342 # permissions of .pyc should match those of .py, regardless of mask
343 mode = 0o600
344 with temp_umask(0o022), _ready_to_import() as (name, path):
345 cached_path = imp.cache_from_source(path)
346 os.chmod(path, mode)
347 __import__(name)
348 if not os.path.exists(cached_path):
349 self.fail("__import__ did not result in creation of "
350 "either a .pyc or .pyo file")
351 stat_info = os.stat(cached_path)
352
353 self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(mode))
354
355 @unittest.skipUnless(os.name == 'posix',
356 "test meaningful only on posix systems")
357 def test_cached_readonly(self):
358 mode = 0o400
359 with temp_umask(0o022), _ready_to_import() as (name, path):
360 cached_path = imp.cache_from_source(path)
361 os.chmod(path, mode)
362 __import__(name)
363 if not os.path.exists(cached_path):
364 self.fail("__import__ did not result in creation of "
365 "either a .pyc or .pyo file")
366 stat_info = os.stat(cached_path)
367
368 expected = mode | 0o200 # Account for fix for issue #6074
369 self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(expected))
370
371 def test_pyc_always_writable(self):
372 # Initially read-only .pyc files on Windows used to cause problems
373 # with later updates, see issue #6074 for details
374 with _ready_to_import() as (name, path):
375 # Write a Python file, make it read-only and import it
376 with open(path, 'w') as f:
377 f.write("x = 'original'\n")
378 # Tweak the mtime of the source to ensure pyc gets updated later
379 s = os.stat(path)
380 os.utime(path, (s.st_atime, s.st_mtime-100000000))
381 os.chmod(path, 0o400)
382 m = __import__(name)
383 self.assertEqual(m.x, 'original')
384 # Change the file and then reimport it
385 os.chmod(path, 0o600)
386 with open(path, 'w') as f:
387 f.write("x = 'rewritten'\n")
388 unload(name)
389 importlib.invalidate_caches()
390 m = __import__(name)
391 self.assertEqual(m.x, 'rewritten')
392 # Now delete the source file and check the pyc was rewritten
393 unlink(path)
394 unload(name)
395 importlib.invalidate_caches()
396 if __debug__:
397 bytecode_only = path + "c"
398 else:
399 bytecode_only = path + "o"
400 os.rename(imp.cache_from_source(path), bytecode_only)
401 m = __import__(name)
402 self.assertEqual(m.x, 'rewritten')
403
404
Collin Winter88e333d2010-03-17 03:09:21 +0000405class PycRewritingTests(unittest.TestCase):
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000406 # Test that the `co_filename` attribute on code objects always points
407 # to the right file, even when various things happen (e.g. both the .py
408 # and the .pyc file are renamed).
409
410 module_name = "unlikely_module_name"
411 module_source = """
412import sys
413code_filename = sys._getframe().f_code.co_filename
414module_filename = __file__
415constant = 1
416def func():
417 pass
418func_filename = func.__code__.co_filename
419"""
420 dir_name = os.path.abspath(TESTFN)
421 file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
Barry Warsaw28a691b2010-04-17 00:19:56 +0000422 compiled_name = imp.cache_from_source(file_name)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000423
424 def setUp(self):
425 self.sys_path = sys.path[:]
426 self.orig_module = sys.modules.pop(self.module_name, None)
427 os.mkdir(self.dir_name)
428 with open(self.file_name, "w") as f:
429 f.write(self.module_source)
430 sys.path.insert(0, self.dir_name)
Antoine Pitrouc541f8e2012-02-20 01:48:16 +0100431 importlib.invalidate_caches()
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000432
433 def tearDown(self):
434 sys.path[:] = self.sys_path
435 if self.orig_module is not None:
436 sys.modules[self.module_name] = self.orig_module
437 else:
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000438 unload(self.module_name)
439 unlink(self.file_name)
440 unlink(self.compiled_name)
Florent Xicluna27354cc2010-08-16 18:41:19 +0000441 rmtree(self.dir_name)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000442
443 def import_module(self):
444 ns = globals()
445 __import__(self.module_name, ns, ns)
446 return sys.modules[self.module_name]
447
448 def test_basics(self):
449 mod = self.import_module()
450 self.assertEqual(mod.module_filename, self.file_name)
451 self.assertEqual(mod.code_filename, self.file_name)
452 self.assertEqual(mod.func_filename, self.file_name)
453 del sys.modules[self.module_name]
454 mod = self.import_module()
455 self.assertEqual(mod.module_filename, self.file_name)
456 self.assertEqual(mod.code_filename, self.file_name)
457 self.assertEqual(mod.func_filename, self.file_name)
458
459 def test_incorrect_code_name(self):
460 py_compile.compile(self.file_name, dfile="another_module.py")
461 mod = self.import_module()
462 self.assertEqual(mod.module_filename, self.file_name)
463 self.assertEqual(mod.code_filename, self.file_name)
464 self.assertEqual(mod.func_filename, self.file_name)
465
466 def test_module_without_source(self):
467 target = "another_module.py"
468 py_compile.compile(self.file_name, dfile=target)
469 os.remove(self.file_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000470 pyc_file = make_legacy_pyc(self.file_name)
Brett Cannonfd074152012-04-14 14:10:13 -0400471 importlib.invalidate_caches()
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000472 mod = self.import_module()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000473 self.assertEqual(mod.module_filename, pyc_file)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000474 self.assertEqual(mod.code_filename, target)
475 self.assertEqual(mod.func_filename, target)
476
477 def test_foreign_code(self):
478 py_compile.compile(self.file_name)
479 with open(self.compiled_name, "rb") as f:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100480 header = f.read(12)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000481 code = marshal.load(f)
482 constants = list(code.co_consts)
483 foreign_code = test_main.__code__
484 pos = constants.index(1)
485 constants[pos] = foreign_code
486 code = type(code)(code.co_argcount, code.co_kwonlyargcount,
487 code.co_nlocals, code.co_stacksize,
488 code.co_flags, code.co_code, tuple(constants),
489 code.co_names, code.co_varnames, code.co_filename,
490 code.co_name, code.co_firstlineno, code.co_lnotab,
491 code.co_freevars, code.co_cellvars)
492 with open(self.compiled_name, "wb") as f:
493 f.write(header)
494 marshal.dump(code, f)
495 mod = self.import_module()
496 self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
497
Collin Winter88e333d2010-03-17 03:09:21 +0000498
Christian Heimes204093a2007-11-01 22:37:07 +0000499class PathsTests(unittest.TestCase):
500 SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
501 'test\u00b0\u00b3\u00b2')
Christian Heimes90333392007-11-01 19:08:42 +0000502 path = TESTFN
503
504 def setUp(self):
505 os.mkdir(self.path)
506 self.syspath = sys.path[:]
507
508 def tearDown(self):
Florent Xicluna27354cc2010-08-16 18:41:19 +0000509 rmtree(self.path)
Nick Coghlan6ead5522009-10-18 13:19:33 +0000510 sys.path[:] = self.syspath
Christian Heimes90333392007-11-01 19:08:42 +0000511
Collin Winter88e333d2010-03-17 03:09:21 +0000512 # Regression test for http://bugs.python.org/issue1293.
Christian Heimes1d1a4002007-11-01 19:41:07 +0000513 def test_trailing_slash(self):
Collin Winter88e333d2010-03-17 03:09:21 +0000514 with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f:
515 f.write("testdata = 'test_trailing_slash'")
Christian Heimes1d1a4002007-11-01 19:41:07 +0000516 sys.path.append(self.path+'/')
517 mod = __import__("test_trailing_slash")
518 self.assertEqual(mod.testdata, 'test_trailing_slash')
519 unload("test_trailing_slash")
520
Collin Winter88e333d2010-03-17 03:09:21 +0000521 # Regression test for http://bugs.python.org/issue3677.
Brett Cannonbbdc9cd2012-04-20 16:29:39 -0400522 @unittest.skipUnless(sys.platform == 'win32', 'Windows-specific')
Brett Cannon9e924ed2012-04-20 17:34:59 -0400523 def test_UNC_path(self):
Antoine Pitrouf189e802012-07-12 19:48:49 +0200524 with open(os.path.join(self.path, 'test_unc_path.py'), 'w') as f:
525 f.write("testdata = 'test_unc_path'")
Brett Cannonb8c02062012-04-21 19:11:58 -0400526 importlib.invalidate_caches()
Collin Winter88e333d2010-03-17 03:09:21 +0000527 # Create the UNC path, like \\myhost\c$\foo\bar.
Kristján Valur Jónssond9aab512009-01-24 10:50:45 +0000528 path = os.path.abspath(self.path)
529 import socket
530 hn = socket.gethostname()
531 drive = path[0]
532 unc = "\\\\%s\\%s$"%(hn, drive)
533 unc += path[2:]
Brett Cannonb8c02062012-04-21 19:11:58 -0400534 try:
Antoine Pitrou5df02042012-07-12 19:50:03 +0200535 os.listdir(unc)
Antoine Pitrou68f42472012-07-13 20:54:42 +0200536 except OSError as e:
537 if e.errno in (errno.EPERM, errno.EACCES):
538 # See issue #15338
539 self.skipTest("cannot access administrative share %r" % (unc,))
540 raise
Antoine Pitrouc27ace62012-07-13 20:59:19 +0200541 sys.path.insert(0, unc)
542 try:
543 mod = __import__("test_unc_path")
544 except ImportError as e:
545 self.fail("could not import 'test_unc_path' from %r: %r"
546 % (unc, e))
547 self.assertEqual(mod.testdata, 'test_unc_path')
548 self.assertTrue(mod.__file__.startswith(unc), mod.__file__)
549 unload("test_unc_path")
Kristján Valur Jónssond9aab512009-01-24 10:50:45 +0000550
Kristján Valur Jónssond9aab512009-01-24 10:50:45 +0000551
Collin Winter88e333d2010-03-17 03:09:21 +0000552class RelativeImportTests(unittest.TestCase):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000553
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000554 def tearDown(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000555 unload("test.relimport")
556 setUp = tearDown
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000557
558 def test_relimport_star(self):
559 # This will import * from .test_import.
560 from . import relimport
Collin Winter88e333d2010-03-17 03:09:21 +0000561 self.assertTrue(hasattr(relimport, "RelativeImportTests"))
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000562
Georg Brandl2ee470f2008-07-16 12:55:28 +0000563 def test_issue3221(self):
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000564 # Note for mergers: the 'absolute' tests from the 2.x branch
565 # are missing in Py3k because implicit relative imports are
566 # a thing of the past
Collin Winter88e333d2010-03-17 03:09:21 +0000567 #
568 # Regression test for http://bugs.python.org/issue3221.
Georg Brandl2ee470f2008-07-16 12:55:28 +0000569 def check_relative():
570 exec("from . import relimport", ns)
Collin Winter88e333d2010-03-17 03:09:21 +0000571
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000572 # Check relative import OK with __package__ and __name__ correct
Georg Brandl2ee470f2008-07-16 12:55:28 +0000573 ns = dict(__package__='test', __name__='test.notarealmodule')
574 check_relative()
Collin Winter88e333d2010-03-17 03:09:21 +0000575
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000576 # Check relative import OK with only __name__ wrong
Georg Brandl2ee470f2008-07-16 12:55:28 +0000577 ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
578 check_relative()
Collin Winter88e333d2010-03-17 03:09:21 +0000579
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000580 # Check relative import fails with only __package__ wrong
Georg Brandl2ee470f2008-07-16 12:55:28 +0000581 ns = dict(__package__='foo', __name__='test.notarealmodule')
582 self.assertRaises(SystemError, check_relative)
Collin Winter88e333d2010-03-17 03:09:21 +0000583
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000584 # Check relative import fails with __package__ and __name__ wrong
Georg Brandl2ee470f2008-07-16 12:55:28 +0000585 ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
586 self.assertRaises(SystemError, check_relative)
Collin Winter88e333d2010-03-17 03:09:21 +0000587
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000588 # Check relative import fails with package set to a non-string
Georg Brandl2ee470f2008-07-16 12:55:28 +0000589 ns = dict(__package__=object())
Brett Cannonfd074152012-04-14 14:10:13 -0400590 self.assertRaises(TypeError, check_relative)
Georg Brandl2ee470f2008-07-16 12:55:28 +0000591
Benjamin Peterson556d8002010-06-27 22:37:28 +0000592 def test_absolute_import_without_future(self):
Florent Xicluna27354cc2010-08-16 18:41:19 +0000593 # If explicit relative import syntax is used, then do not try
Florent Xiclunac9c29e22010-08-16 19:03:05 +0000594 # to perform an absolute import in the face of failure.
Benjamin Peterson556d8002010-06-27 22:37:28 +0000595 # Issue #7902.
Florent Xicluna27354cc2010-08-16 18:41:19 +0000596 with self.assertRaises(ImportError):
Benjamin Peterson556d8002010-06-27 22:37:28 +0000597 from .os import sep
Benjamin Peterson556d8002010-06-27 22:37:28 +0000598 self.fail("explicit relative import triggered an "
Florent Xicluna27354cc2010-08-16 18:41:19 +0000599 "implicit absolute import")
Collin Winter88e333d2010-03-17 03:09:21 +0000600
Florent Xiclunac9c29e22010-08-16 19:03:05 +0000601
Collin Winter6498cff2010-03-17 03:14:31 +0000602class OverridingImportBuiltinTests(unittest.TestCase):
603 def test_override_builtin(self):
604 # Test that overriding builtins.__import__ can bypass sys.modules.
605 import os
606
607 def foo():
608 import os
609 return os
610 self.assertEqual(foo(), os) # Quick sanity check.
611
612 with swap_attr(builtins, "__import__", lambda *x: 5):
613 self.assertEqual(foo(), 5)
614
615 # Test what happens when we shadow __import__ in globals(); this
616 # currently does not impact the import process, but if this changes,
617 # other code will need to change, so keep this test as a tripwire.
618 with swap_item(globals(), "__import__", lambda *x: 5):
619 self.assertEqual(foo(), os)
620
621
Barry Warsaw28a691b2010-04-17 00:19:56 +0000622class PycacheTests(unittest.TestCase):
623 # Test the various PEP 3147 related behaviors.
624
625 tag = imp.get_tag()
626
627 def _clean(self):
628 forget(TESTFN)
629 rmtree('__pycache__')
630 unlink(self.source)
631
632 def setUp(self):
633 self.source = TESTFN + '.py'
634 self._clean()
635 with open(self.source, 'w') as fp:
636 print('# This is a test file written by test_import.py', file=fp)
637 sys.path.insert(0, os.curdir)
Antoine Pitrouc541f8e2012-02-20 01:48:16 +0100638 importlib.invalidate_caches()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000639
640 def tearDown(self):
641 assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]'
642 del sys.path[0]
643 self._clean()
644
645 def test_import_pyc_path(self):
646 self.assertFalse(os.path.exists('__pycache__'))
647 __import__(TESTFN)
648 self.assertTrue(os.path.exists('__pycache__'))
649 self.assertTrue(os.path.exists(os.path.join(
Georg Brandlfb3c84a2010-10-14 07:24:28 +0000650 '__pycache__', '{}.{}.py{}'.format(
Benjamin Petersond388c4e2012-09-25 11:01:41 -0400651 TESTFN, self.tag, 'c' if __debug__ else 'o'))))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000652
653 @unittest.skipUnless(os.name == 'posix',
654 "test meaningful only on posix systems")
Charles-François Natali035018d2011-10-04 23:35:47 +0200655 @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
Charles-François Natali79164c82011-10-04 20:40:58 +0200656 "due to varying filesystem permission semantics (issue #11956)")
Barry Warsaw28a691b2010-04-17 00:19:56 +0000657 def test_unwritable_directory(self):
658 # When the umask causes the new __pycache__ directory to be
659 # unwritable, the import still succeeds but no .pyc file is written.
660 with temp_umask(0o222):
661 __import__(TESTFN)
662 self.assertTrue(os.path.exists('__pycache__'))
663 self.assertFalse(os.path.exists(os.path.join(
664 '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag))))
665
666 def test_missing_source(self):
667 # With PEP 3147 cache layout, removing the source but leaving the pyc
668 # file does not satisfy the import.
669 __import__(TESTFN)
670 pyc_file = imp.cache_from_source(self.source)
671 self.assertTrue(os.path.exists(pyc_file))
672 os.remove(self.source)
673 forget(TESTFN)
674 self.assertRaises(ImportError, __import__, TESTFN)
675
676 def test_missing_source_legacy(self):
677 # Like test_missing_source() except that for backward compatibility,
678 # when the pyc file lives where the py file would have been (and named
679 # without the tag), it is importable. The __file__ of the imported
680 # module is the pyc location.
681 __import__(TESTFN)
682 # pyc_file gets removed in _clean() via tearDown().
683 pyc_file = make_legacy_pyc(self.source)
684 os.remove(self.source)
685 unload(TESTFN)
Antoine Pitrouc541f8e2012-02-20 01:48:16 +0100686 importlib.invalidate_caches()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000687 m = __import__(TESTFN)
688 self.assertEqual(m.__file__,
689 os.path.join(os.curdir, os.path.relpath(pyc_file)))
690
691 def test___cached__(self):
692 # Modules now also have an __cached__ that points to the pyc file.
693 m = __import__(TESTFN)
694 pyc_file = imp.cache_from_source(TESTFN + '.py')
695 self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file))
696
697 def test___cached___legacy_pyc(self):
698 # Like test___cached__() except that for backward compatibility,
699 # when the pyc file lives where the py file would have been (and named
700 # without the tag), it is importable. The __cached__ of the imported
701 # module is the pyc location.
702 __import__(TESTFN)
703 # pyc_file gets removed in _clean() via tearDown().
704 pyc_file = make_legacy_pyc(self.source)
705 os.remove(self.source)
706 unload(TESTFN)
Antoine Pitrouc541f8e2012-02-20 01:48:16 +0100707 importlib.invalidate_caches()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000708 m = __import__(TESTFN)
709 self.assertEqual(m.__cached__,
710 os.path.join(os.curdir, os.path.relpath(pyc_file)))
711
712 def test_package___cached__(self):
713 # Like test___cached__ but for packages.
714 def cleanup():
Florent Xicluna27354cc2010-08-16 18:41:19 +0000715 rmtree('pep3147')
Antoine Pitroud4daa872012-06-23 18:09:55 +0200716 unload('pep3147.foo')
717 unload('pep3147')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000718 os.mkdir('pep3147')
719 self.addCleanup(cleanup)
720 # Touch the __init__.py
721 with open(os.path.join('pep3147', '__init__.py'), 'w'):
722 pass
723 with open(os.path.join('pep3147', 'foo.py'), 'w'):
724 pass
Brett Cannonfd074152012-04-14 14:10:13 -0400725 importlib.invalidate_caches()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000726 m = __import__('pep3147.foo')
727 init_pyc = imp.cache_from_source(
728 os.path.join('pep3147', '__init__.py'))
729 self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
730 foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
731 self.assertEqual(sys.modules['pep3147.foo'].__cached__,
732 os.path.join(os.curdir, foo_pyc))
733
734 def test_package___cached___from_pyc(self):
735 # Like test___cached__ but ensuring __cached__ when imported from a
736 # PEP 3147 pyc file.
737 def cleanup():
Florent Xicluna27354cc2010-08-16 18:41:19 +0000738 rmtree('pep3147')
Antoine Pitroud4daa872012-06-23 18:09:55 +0200739 unload('pep3147.foo')
740 unload('pep3147')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000741 os.mkdir('pep3147')
742 self.addCleanup(cleanup)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000743 # Touch the __init__.py
744 with open(os.path.join('pep3147', '__init__.py'), 'w'):
745 pass
746 with open(os.path.join('pep3147', 'foo.py'), 'w'):
747 pass
Brett Cannonfd074152012-04-14 14:10:13 -0400748 importlib.invalidate_caches()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000749 m = __import__('pep3147.foo')
750 unload('pep3147.foo')
751 unload('pep3147')
Brett Cannonfd074152012-04-14 14:10:13 -0400752 importlib.invalidate_caches()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000753 m = __import__('pep3147.foo')
754 init_pyc = imp.cache_from_source(
755 os.path.join('pep3147', '__init__.py'))
756 self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
757 foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
758 self.assertEqual(sys.modules['pep3147.foo'].__cached__,
759 os.path.join(os.curdir, foo_pyc))
760
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100761 def test_recompute_pyc_same_second(self):
762 # Even when the source file doesn't change timestamp, a change in
763 # source size is enough to trigger recomputation of the pyc file.
764 __import__(TESTFN)
765 unload(TESTFN)
766 with open(self.source, 'a') as fp:
767 print("x = 5", file=fp)
768 m = __import__(TESTFN)
769 self.assertEqual(m.x, 5)
770
Barry Warsaw28a691b2010-04-17 00:19:56 +0000771
Jason R. Coombs71fde312012-06-17 03:53:47 -0400772class TestSymbolicallyLinkedPackage(unittest.TestCase):
773 package_name = 'sample'
Brett Cannon6ee96952012-07-20 14:22:04 -0400774 tagged = package_name + '-tagged'
Jason R. Coombs71fde312012-06-17 03:53:47 -0400775
776 def setUp(self):
Brett Cannon6ee96952012-07-20 14:22:04 -0400777 test.support.rmtree(self.tagged)
778 test.support.rmtree(self.package_name)
Jason R. Coombs71fde312012-06-17 03:53:47 -0400779 self.orig_sys_path = sys.path[:]
780
781 # create a sample package; imagine you have a package with a tag and
782 # you want to symbolically link it from its untagged name.
783 os.mkdir(self.tagged)
Brett Cannon6ee96952012-07-20 14:22:04 -0400784 self.addCleanup(test.support.rmtree, self.tagged)
Jason R. Coombs71fde312012-06-17 03:53:47 -0400785 init_file = os.path.join(self.tagged, '__init__.py')
Brett Cannon6ee96952012-07-20 14:22:04 -0400786 test.support.create_empty_file(init_file)
787 assert os.path.exists(init_file)
Jason R. Coombs71fde312012-06-17 03:53:47 -0400788
789 # now create a symlink to the tagged package
790 # sample -> sample-tagged
Jason R. Coombsd0118e12012-07-26 15:21:17 -0400791 os.symlink(self.tagged, self.package_name, target_is_directory=True)
Brett Cannon6ee96952012-07-20 14:22:04 -0400792 self.addCleanup(test.support.unlink, self.package_name)
793 importlib.invalidate_caches()
Jason R. Coombs71fde312012-06-17 03:53:47 -0400794
Jason R. Coombsd0118e12012-07-26 15:21:17 -0400795 self.assertEqual(os.path.isdir(self.package_name), True)
Jason R. Coombs42c9b042012-06-20 10:24:24 -0400796
Brett Cannon6ee96952012-07-20 14:22:04 -0400797 assert os.path.isfile(os.path.join(self.package_name, '__init__.py'))
Jason R. Coombs71fde312012-06-17 03:53:47 -0400798
Brett Cannon6ee96952012-07-20 14:22:04 -0400799 def tearDown(self):
800 sys.path[:] = self.orig_sys_path
Jason R. Coombs71fde312012-06-17 03:53:47 -0400801
802 # regression test for issue6727
803 @unittest.skipUnless(
804 not hasattr(sys, 'getwindowsversion')
805 or sys.getwindowsversion() >= (6, 0),
806 "Windows Vista or later required")
807 @test.support.skip_unless_symlink
808 def test_symlinked_dir_importable(self):
809 # make sure sample can only be imported from the current directory.
810 sys.path[:] = ['.']
Brett Cannon6ee96952012-07-20 14:22:04 -0400811 assert os.path.exists(self.package_name)
812 assert os.path.exists(os.path.join(self.package_name, '__init__.py'))
Jason R. Coombs71fde312012-06-17 03:53:47 -0400813
Brett Cannon6ee96952012-07-20 14:22:04 -0400814 # Try to import the package
815 importlib.import_module(self.package_name)
Jason R. Coombs71fde312012-06-17 03:53:47 -0400816
817
Antoine Pitrou48114b92012-06-17 22:33:38 +0200818@cpython_only
819class ImportlibBootstrapTests(unittest.TestCase):
820 # These tests check that importlib is bootstrapped.
821
822 def test_frozen_importlib(self):
823 mod = sys.modules['_frozen_importlib']
824 self.assertTrue(mod)
825
826 def test_frozen_importlib_is_bootstrap(self):
827 from importlib import _bootstrap
828 mod = sys.modules['_frozen_importlib']
829 self.assertIs(mod, _bootstrap)
830 self.assertEqual(mod.__name__, 'importlib._bootstrap')
831 self.assertEqual(mod.__package__, 'importlib')
832 self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__)
833
Nick Coghlanbe7e49f2012-07-20 23:40:09 +1000834 def test_there_can_be_only_one(self):
835 # Issue #15386 revealed a tricky loophole in the bootstrapping
836 # This test is technically redundant, since the bug caused importing
837 # this test module to crash completely, but it helps prove the point
838 from importlib import machinery
839 mod = sys.modules['_frozen_importlib']
840 self.assertIs(machinery.FileFinder, mod.FileFinder)
841 self.assertIs(imp.new_module, mod.new_module)
842
Antoine Pitrou48114b92012-06-17 22:33:38 +0200843
Antoine Pitroubc07a5c2012-07-08 12:01:27 +0200844class ImportTracebackTests(unittest.TestCase):
845
846 def setUp(self):
847 os.mkdir(TESTFN)
848 self.old_path = sys.path[:]
849 sys.path.insert(0, TESTFN)
850
851 def tearDown(self):
852 sys.path[:] = self.old_path
853 rmtree(TESTFN)
854
Nick Coghlan42c07662012-07-31 21:14:18 +1000855 def create_module(self, mod, contents, ext=".py"):
856 fname = os.path.join(TESTFN, mod + ext)
857 with open(fname, "w") as f:
Antoine Pitroubc07a5c2012-07-08 12:01:27 +0200858 f.write(contents)
859 self.addCleanup(unload, mod)
860 importlib.invalidate_caches()
Nick Coghlan42c07662012-07-31 21:14:18 +1000861 return fname
Antoine Pitroubc07a5c2012-07-08 12:01:27 +0200862
863 def assert_traceback(self, tb, files):
864 deduped_files = []
865 while tb:
866 code = tb.tb_frame.f_code
867 fn = code.co_filename
868 if not deduped_files or fn != deduped_files[-1]:
869 deduped_files.append(fn)
870 tb = tb.tb_next
871 self.assertEqual(len(deduped_files), len(files), deduped_files)
872 for fn, pat in zip(deduped_files, files):
Antoine Pitrou68038552012-07-08 13:16:15 +0200873 self.assertIn(pat, fn)
Antoine Pitroubc07a5c2012-07-08 12:01:27 +0200874
875 def test_nonexistent_module(self):
876 try:
877 # assertRaises() clears __traceback__
878 import nonexistent_xyzzy
879 except ImportError as e:
880 tb = e.__traceback__
881 else:
882 self.fail("ImportError should have been raised")
883 self.assert_traceback(tb, [__file__])
884
885 def test_nonexistent_module_nested(self):
886 self.create_module("foo", "import nonexistent_xyzzy")
887 try:
888 import foo
889 except ImportError as e:
890 tb = e.__traceback__
891 else:
892 self.fail("ImportError should have been raised")
893 self.assert_traceback(tb, [__file__, 'foo.py'])
894
895 def test_exec_failure(self):
896 self.create_module("foo", "1/0")
897 try:
898 import foo
899 except ZeroDivisionError as e:
900 tb = e.__traceback__
901 else:
902 self.fail("ZeroDivisionError should have been raised")
903 self.assert_traceback(tb, [__file__, 'foo.py'])
904
905 def test_exec_failure_nested(self):
906 self.create_module("foo", "import bar")
907 self.create_module("bar", "1/0")
908 try:
909 import foo
910 except ZeroDivisionError as e:
911 tb = e.__traceback__
912 else:
913 self.fail("ZeroDivisionError should have been raised")
914 self.assert_traceback(tb, [__file__, 'foo.py', 'bar.py'])
915
Nick Coghlan5ee98922012-07-29 20:30:36 +1000916 # A few more examples from issue #15425
917 def test_syntax_error(self):
918 self.create_module("foo", "invalid syntax is invalid")
919 try:
920 import foo
921 except SyntaxError as e:
922 tb = e.__traceback__
923 else:
924 self.fail("SyntaxError should have been raised")
925 self.assert_traceback(tb, [__file__])
926
927 def _setup_broken_package(self, parent, child):
928 pkg_name = "_parent_foo"
Nick Coghlan336d9ac2012-07-31 21:39:42 +1000929 self.addCleanup(unload, pkg_name)
930 pkg_path = os.path.join(TESTFN, pkg_name)
931 os.mkdir(pkg_path)
Nick Coghlan5ee98922012-07-29 20:30:36 +1000932 # Touch the __init__.py
Nick Coghlan336d9ac2012-07-31 21:39:42 +1000933 init_path = os.path.join(pkg_path, '__init__.py')
Nick Coghlan5ee98922012-07-29 20:30:36 +1000934 with open(init_path, 'w') as f:
935 f.write(parent)
Nick Coghlan336d9ac2012-07-31 21:39:42 +1000936 bar_path = os.path.join(pkg_path, 'bar.py')
Nick Coghlan5ee98922012-07-29 20:30:36 +1000937 with open(bar_path, 'w') as f:
938 f.write(child)
939 importlib.invalidate_caches()
940 return init_path, bar_path
941
942 def test_broken_submodule(self):
943 init_path, bar_path = self._setup_broken_package("", "1/0")
944 try:
945 import _parent_foo.bar
946 except ZeroDivisionError as e:
947 tb = e.__traceback__
948 else:
949 self.fail("ZeroDivisionError should have been raised")
950 self.assert_traceback(tb, [__file__, bar_path])
951
952 def test_broken_from(self):
953 init_path, bar_path = self._setup_broken_package("", "1/0")
954 try:
955 from _parent_foo import bar
956 except ZeroDivisionError as e:
957 tb = e.__traceback__
958 else:
959 self.fail("ImportError should have been raised")
960 self.assert_traceback(tb, [__file__, bar_path])
961
962 def test_broken_parent(self):
963 init_path, bar_path = self._setup_broken_package("1/0", "")
964 try:
965 import _parent_foo.bar
966 except ZeroDivisionError as e:
967 tb = e.__traceback__
968 else:
969 self.fail("ZeroDivisionError should have been raised")
970 self.assert_traceback(tb, [__file__, init_path])
971
972 def test_broken_parent_from(self):
973 init_path, bar_path = self._setup_broken_package("1/0", "")
974 try:
975 from _parent_foo import bar
976 except ZeroDivisionError as e:
977 tb = e.__traceback__
978 else:
979 self.fail("ZeroDivisionError should have been raised")
980 self.assert_traceback(tb, [__file__, init_path])
981
Antoine Pitroubc07a5c2012-07-08 12:01:27 +0200982 @cpython_only
983 def test_import_bug(self):
984 # We simulate a bug in importlib and check that it's not stripped
985 # away from the traceback.
986 self.create_module("foo", "")
987 importlib = sys.modules['_frozen_importlib']
988 old_load_module = importlib.SourceLoader.load_module
989 try:
990 def load_module(*args):
991 1/0
992 importlib.SourceLoader.load_module = load_module
993 try:
994 import foo
995 except ZeroDivisionError as e:
996 tb = e.__traceback__
997 else:
998 self.fail("ZeroDivisionError should have been raised")
999 self.assert_traceback(tb, [__file__, '<frozen importlib', __file__])
1000 finally:
1001 importlib.SourceLoader.load_module = old_load_module
1002
1003
Thomas Wouters89f507f2006-12-13 04:49:30 +00001004def test_main(verbose=None):
Nick Coghlaneb8d6272012-10-19 23:32:00 +10001005 run_unittest(ImportTests, PycacheTests, FilePermissionTests,
Brett Cannonba525862012-07-20 14:01:34 -04001006 PycRewritingTests, PathsTests, RelativeImportTests,
1007 OverridingImportBuiltinTests,
1008 ImportlibBootstrapTests,
1009 TestSymbolicallyLinkedPackage,
1010 ImportTracebackTests)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001011
Barry Warsaw28a691b2010-04-17 00:19:56 +00001012
Thomas Wouters89f507f2006-12-13 04:49:30 +00001013if __name__ == '__main__':
Collin Winter88e333d2010-03-17 03:09:21 +00001014 # Test needs to be a package, so we can do relative imports.
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001015 from test.test_import import test_main
Thomas Wouters89f507f2006-12-13 04:49:30 +00001016 test_main()