Nick Coghlan | be7e49f | 2012-07-20 23:40:09 +1000 | [diff] [blame] | 1 | # We import importlib *ASAP* in order to test #15386 |
| 2 | import importlib |
Collin Winter | 6498cff | 2010-03-17 03:14:31 +0000 | [diff] [blame] | 3 | import builtins |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 4 | import imp |
Brett Cannon | 1f27479 | 2010-07-23 14:03:16 +0000 | [diff] [blame] | 5 | from importlib.test.import_ import util as importlib_util |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 6 | import marshal |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 7 | import os |
Charles-François Natali | a13b1fa | 2011-10-04 19:17:26 +0200 | [diff] [blame] | 8 | import platform |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 9 | import py_compile |
| 10 | import random |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 11 | import stat |
| 12 | import sys |
| 13 | import unittest |
R. David Murray | ce4b170 | 2010-12-14 23:06:25 +0000 | [diff] [blame] | 14 | import textwrap |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 15 | import errno |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 16 | import shutil |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 17 | |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 18 | import test.support |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 19 | from test.support import ( |
| 20 | EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, |
| 21 | make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 22 | unlink, unload, create_empty_file, cpython_only) |
R. David Murray | ce4b170 | 2010-12-14 23:06:25 +0000 | [diff] [blame] | 23 | from test import script_helper |
Guido van Rossum | bd6f4fb | 2000-10-24 17:16:32 +0000 | [diff] [blame] | 24 | |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 25 | |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 26 | def remove_files(name): |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 27 | for f in (name + ".py", |
| 28 | name + ".pyc", |
| 29 | name + ".pyo", |
| 30 | name + ".pyw", |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 31 | name + "$py.class"): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 32 | unlink(f) |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 33 | rmtree('__pycache__') |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 34 | |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 35 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 36 | class ImportTests(unittest.TestCase): |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 37 | |
Antoine Pitrou | 46719af | 2011-03-21 19:05:02 +0100 | [diff] [blame] | 38 | def setUp(self): |
| 39 | remove_files(TESTFN) |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 40 | importlib.invalidate_caches() |
Antoine Pitrou | 46719af | 2011-03-21 19:05:02 +0100 | [diff] [blame] | 41 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 42 | def tearDown(self): |
| 43 | unload(TESTFN) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 44 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 45 | setUp = tearDown |
| 46 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 47 | def test_case_sensitivity(self): |
| 48 | # Brief digression to test that import is case-sensitive: if we got |
| 49 | # this far, we know for sure that "random" exists. |
Benjamin Peterson | 1c87e29 | 2010-10-30 23:04:49 +0000 | [diff] [blame] | 50 | with self.assertRaises(ImportError): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 51 | import RAnDoM |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 52 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 53 | def test_double_const(self): |
| 54 | # Another brief digression to test the accuracy of manifest float |
| 55 | # constants. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | from test import double_const # don't blink -- that *was* the test |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 57 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 58 | def test_import(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 59 | def test_with_extension(ext): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 60 | # The extension is normally ".py", perhaps ".pyw". |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | source = TESTFN + ext |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 62 | pyo = TESTFN + ".pyo" |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 63 | if is_jython: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 64 | pyc = TESTFN + "$py.class" |
| 65 | else: |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 66 | pyc = TESTFN + ".pyc" |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 67 | |
Benjamin Peterson | 16a1f63 | 2009-06-13 13:01:19 +0000 | [diff] [blame] | 68 | with open(source, "w") as f: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 69 | print("# This tests Python's ability to import a", |
| 70 | ext, "file.", file=f) |
Benjamin Peterson | 16a1f63 | 2009-06-13 13:01:19 +0000 | [diff] [blame] | 71 | a = random.randrange(1000) |
| 72 | b = random.randrange(1000) |
| 73 | print("a =", a, file=f) |
| 74 | print("b =", b, file=f) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 75 | |
Amaury Forgeot d'Arc | dd9e3b8 | 2007-11-16 00:56:23 +0000 | [diff] [blame] | 76 | if TESTFN in sys.modules: |
| 77 | del sys.modules[TESTFN] |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 78 | importlib.invalidate_caches() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 79 | try: |
| 80 | try: |
| 81 | mod = __import__(TESTFN) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 82 | except ImportError as err: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 83 | self.fail("import from %s failed: %s" % (ext, err)) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 84 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 85 | self.assertEqual(mod.a, a, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 86 | "module loaded (%s) but contents invalid" % mod) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 87 | self.assertEqual(mod.b, b, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 88 | "module loaded (%s) but contents invalid" % mod) |
| 89 | finally: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 90 | forget(TESTFN) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 91 | unlink(source) |
| 92 | unlink(pyc) |
| 93 | unlink(pyo) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 94 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 95 | sys.path.insert(0, os.curdir) |
| 96 | try: |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 97 | test_with_extension(".py") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 98 | if sys.platform.startswith("win"): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 99 | for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 100 | test_with_extension(ext) |
| 101 | finally: |
| 102 | del sys.path[0] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 103 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 104 | @unittest.skipUnless(os.name == 'posix', |
| 105 | "test meaningful only on posix systems") |
Charles-François Natali | 0c929d9 | 2011-11-10 19:12:29 +0100 | [diff] [blame] | 106 | def test_creation_mode(self): |
| 107 | mask = 0o022 |
| 108 | with temp_umask(mask): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 109 | sys.path.insert(0, os.curdir) |
| 110 | try: |
| 111 | fname = TESTFN + os.extsep + "py" |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 112 | create_empty_file(fname) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 113 | fn = imp.cache_from_source(fname) |
Antoine Pitrou | 28f8bee | 2011-12-21 15:50:42 +0100 | [diff] [blame] | 114 | unlink(fn) |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 115 | importlib.invalidate_caches() |
Antoine Pitrou | 28f8bee | 2011-12-21 15:50:42 +0100 | [diff] [blame] | 116 | __import__(TESTFN) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 117 | if not os.path.exists(fn): |
| 118 | self.fail("__import__ did not result in creation of " |
| 119 | "either a .pyc or .pyo file") |
Charles-François Natali | 0c929d9 | 2011-11-10 19:12:29 +0100 | [diff] [blame] | 120 | s = os.stat(fn) |
| 121 | # Check that the umask is respected, and the executable bits |
| 122 | # aren't set. |
| 123 | self.assertEqual(stat.S_IMODE(s.st_mode), 0o666 & ~mask) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 124 | finally: |
| 125 | del sys.path[0] |
| 126 | remove_files(TESTFN) |
| 127 | unload(TESTFN) |
Alexandre Vassalotti | 9d58e3e | 2009-07-17 10:55:50 +0000 | [diff] [blame] | 128 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 129 | def test_imp_module(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | # Verify that the imp module can correctly load and find .py files |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 131 | # XXX (ncoghlan): It would be nice to use support.CleanImport |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 132 | # here, but that breaks because the os module registers some |
| 133 | # handlers in copy_reg on import. Since CleanImport doesn't |
| 134 | # revert that registration, the module is left in a broken |
| 135 | # state after reversion. Reinitialising the module contents |
| 136 | # and just reverting os.environ to its previous state is an OK |
| 137 | # workaround |
| 138 | orig_path = os.path |
| 139 | orig_getenv = os.getenv |
| 140 | with EnvironmentVarGuard(): |
| 141 | x = imp.find_module("os") |
Benjamin Peterson | e048797 | 2010-10-30 23:06:57 +0000 | [diff] [blame] | 142 | self.addCleanup(x[0].close) |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 143 | new_os = imp.load_module("os", *x) |
| 144 | self.assertIs(os, new_os) |
| 145 | self.assertIs(orig_path, new_os.path) |
| 146 | self.assertIsNot(orig_getenv, new_os.getenv) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 147 | |
Victor Stinner | 53ffdc5 | 2011-09-23 18:54:40 +0200 | [diff] [blame] | 148 | def test_bug7732(self): |
| 149 | source = TESTFN + '.py' |
| 150 | os.mkdir(source) |
| 151 | try: |
| 152 | self.assertRaisesRegex(ImportError, '^No module', |
| 153 | imp.find_module, TESTFN, ["."]) |
| 154 | finally: |
| 155 | os.rmdir(source) |
| 156 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 157 | def test_module_with_large_stack(self, module='longlist'): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 158 | # Regression test for http://bugs.python.org/issue561858. |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 159 | filename = module + '.py' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 160 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 161 | # Create a file with a list of 65000 elements. |
Brett Cannon | 2cab50b | 2010-07-03 01:32:48 +0000 | [diff] [blame] | 162 | with open(filename, 'w') as f: |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 163 | f.write('d = [\n') |
| 164 | for i in range(65000): |
| 165 | f.write('"",\n') |
| 166 | f.write(']') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 167 | |
Brett Cannon | 2cab50b | 2010-07-03 01:32:48 +0000 | [diff] [blame] | 168 | try: |
| 169 | # Compile & remove .py file; we only need .pyc (or .pyo). |
| 170 | # Bytecode must be relocated from the PEP 3147 bytecode-only location. |
| 171 | py_compile.compile(filename) |
| 172 | finally: |
| 173 | unlink(filename) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 174 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 175 | # Need to be able to load from current dir. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 176 | sys.path.append('') |
Brett Cannon | 73def61 | 2012-04-14 14:38:19 -0400 | [diff] [blame] | 177 | importlib.invalidate_caches() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 178 | |
Brett Cannon | 93220d0 | 2010-05-15 22:20:16 +0000 | [diff] [blame] | 179 | try: |
Brett Cannon | 2cab50b | 2010-07-03 01:32:48 +0000 | [diff] [blame] | 180 | make_legacy_pyc(filename) |
Brett Cannon | 93220d0 | 2010-05-15 22:20:16 +0000 | [diff] [blame] | 181 | # This used to crash. |
| 182 | exec('import ' + module) |
| 183 | finally: |
| 184 | # Cleanup. |
| 185 | del sys.path[-1] |
| 186 | unlink(filename + 'c') |
| 187 | unlink(filename + 'o') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 188 | |
| 189 | def test_failing_import_sticks(self): |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 190 | source = TESTFN + ".py" |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 191 | with open(source, "w") as f: |
| 192 | print("a = 1/0", file=f) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 193 | |
| 194 | # New in 2.4, we shouldn't be able to import that no matter how often |
| 195 | # we try. |
| 196 | sys.path.insert(0, os.curdir) |
Antoine Pitrou | 021548c | 2012-07-12 19:21:43 +0200 | [diff] [blame] | 197 | importlib.invalidate_caches() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 198 | if TESTFN in sys.modules: |
| 199 | del sys.modules[TESTFN] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 200 | try: |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 201 | for i in [1, 2, 3]: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 202 | self.assertRaises(ZeroDivisionError, __import__, TESTFN) |
| 203 | self.assertNotIn(TESTFN, sys.modules, |
| 204 | "damaged module in sys.modules on %i try" % i) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 205 | finally: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 206 | del sys.path[0] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 207 | remove_files(TESTFN) |
| 208 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 209 | def test_import_name_binding(self): |
| 210 | # import x.y.z binds x in the current namespace |
| 211 | import test as x |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 212 | import test.support |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 213 | self.assertTrue(x is test, x.__name__) |
| 214 | self.assertTrue(hasattr(test.support, "__file__")) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 215 | |
| 216 | # import x.y.z as w binds z as w |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 217 | import test.support as y |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 218 | self.assertTrue(y is test.support, y.__name__) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 219 | |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 220 | def test_failing_reload(self): |
| 221 | # A failing reload should leave the module object in sys.modules. |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 222 | source = TESTFN + os.extsep + "py" |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 223 | with open(source, "w") as f: |
| 224 | f.write("a = 1\nb=2\n") |
| 225 | |
| 226 | sys.path.insert(0, os.curdir) |
| 227 | try: |
| 228 | mod = __import__(TESTFN) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 229 | self.assertIn(TESTFN, sys.modules) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 230 | self.assertEqual(mod.a, 1, "module has wrong attribute values") |
| 231 | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 232 | |
| 233 | # On WinXP, just replacing the .py file wasn't enough to |
| 234 | # convince reload() to reparse it. Maybe the timestamp didn't |
| 235 | # move enough. We force it to get reparsed by removing the |
| 236 | # compiled file too. |
| 237 | remove_files(TESTFN) |
| 238 | |
| 239 | # Now damage the module. |
| 240 | with open(source, "w") as f: |
| 241 | f.write("a = 10\nb=20//0\n") |
| 242 | |
| 243 | self.assertRaises(ZeroDivisionError, imp.reload, mod) |
| 244 | # But we still expect the module to be in sys.modules. |
| 245 | mod = sys.modules.get(TESTFN) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 246 | self.assertIsNot(mod, None, "expected module to be in sys.modules") |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 247 | |
| 248 | # We should have replaced a w/ 10, but the old b value should |
| 249 | # stick. |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 250 | self.assertEqual(mod.a, 10, "module has wrong attribute values") |
| 251 | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 252 | |
| 253 | finally: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 254 | del sys.path[0] |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 255 | remove_files(TESTFN) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 256 | unload(TESTFN) |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 257 | |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 258 | def test_file_to_source(self): |
| 259 | # check if __file__ points to the source file where available |
| 260 | source = TESTFN + ".py" |
| 261 | with open(source, "w") as f: |
| 262 | f.write("test = None\n") |
| 263 | |
| 264 | sys.path.insert(0, os.curdir) |
| 265 | try: |
| 266 | mod = __import__(TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 267 | self.assertTrue(mod.__file__.endswith('.py')) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 268 | os.remove(source) |
| 269 | del sys.modules[TESTFN] |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 270 | make_legacy_pyc(source) |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 271 | importlib.invalidate_caches() |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 272 | mod = __import__(TESTFN) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 273 | base, ext = os.path.splitext(mod.__file__) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 274 | self.assertIn(ext, ('.pyc', '.pyo')) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 275 | finally: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 276 | del sys.path[0] |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 277 | remove_files(TESTFN) |
| 278 | if TESTFN in sys.modules: |
| 279 | del sys.modules[TESTFN] |
| 280 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 281 | def test_import_name_binding(self): |
| 282 | # import x.y.z binds x in the current namespace. |
| 283 | import test as x |
| 284 | import test.support |
| 285 | self.assertIs(x, test, x.__name__) |
| 286 | self.assertTrue(hasattr(test.support, "__file__")) |
| 287 | |
| 288 | # import x.y.z as w binds z as w. |
| 289 | import test.support as y |
| 290 | self.assertIs(y, test.support, y.__name__) |
| 291 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 292 | def test_import_by_filename(self): |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 293 | path = os.path.abspath(TESTFN) |
Victor Stinner | 6c6f851 | 2010-08-07 10:09:35 +0000 | [diff] [blame] | 294 | encoding = sys.getfilesystemencoding() |
| 295 | try: |
| 296 | path.encode(encoding) |
| 297 | except UnicodeEncodeError: |
| 298 | self.skipTest('path is not encodable to {}'.format(encoding)) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 299 | with self.assertRaises(ImportError) as c: |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 300 | __import__(path) |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 301 | |
R. David Murray | ce4b170 | 2010-12-14 23:06:25 +0000 | [diff] [blame] | 302 | def test_import_in_del_does_not_crash(self): |
| 303 | # Issue 4236 |
| 304 | testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\ |
| 305 | import sys |
| 306 | class C: |
| 307 | def __del__(self): |
| 308 | import imp |
| 309 | sys.argv.insert(0, C()) |
| 310 | """)) |
| 311 | script_helper.assert_python_ok(testfn) |
| 312 | |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 313 | def test_timestamp_overflow(self): |
| 314 | # A modification timestamp larger than 2**32 should not be a problem |
| 315 | # when importing a module (issue #11235). |
Antoine Pitrou | 05f29b7 | 2012-01-25 01:35:26 +0100 | [diff] [blame] | 316 | sys.path.insert(0, os.curdir) |
| 317 | try: |
| 318 | source = TESTFN + ".py" |
| 319 | compiled = imp.cache_from_source(source) |
| 320 | with open(source, 'w') as f: |
| 321 | pass |
| 322 | try: |
Antoine Pitrou | 33d15f7 | 2012-01-25 18:01:45 +0100 | [diff] [blame] | 323 | os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
Antoine Pitrou | 05f29b7 | 2012-01-25 01:35:26 +0100 | [diff] [blame] | 324 | except OverflowError: |
| 325 | self.skipTest("cannot set modification time to large integer") |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 326 | except OSError as e: |
| 327 | if e.errno != getattr(errno, 'EOVERFLOW', None): |
| 328 | raise |
| 329 | self.skipTest("cannot set modification time to large integer ({})".format(e)) |
Antoine Pitrou | 05f29b7 | 2012-01-25 01:35:26 +0100 | [diff] [blame] | 330 | __import__(TESTFN) |
| 331 | # The pyc file was created. |
| 332 | os.stat(compiled) |
| 333 | finally: |
| 334 | del sys.path[0] |
| 335 | remove_files(TESTFN) |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 336 | |
Alexandre Vassalotti | 9d58e3e | 2009-07-17 10:55:50 +0000 | [diff] [blame] | 337 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 338 | class PycRewritingTests(unittest.TestCase): |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 339 | # Test that the `co_filename` attribute on code objects always points |
| 340 | # to the right file, even when various things happen (e.g. both the .py |
| 341 | # and the .pyc file are renamed). |
| 342 | |
| 343 | module_name = "unlikely_module_name" |
| 344 | module_source = """ |
| 345 | import sys |
| 346 | code_filename = sys._getframe().f_code.co_filename |
| 347 | module_filename = __file__ |
| 348 | constant = 1 |
| 349 | def func(): |
| 350 | pass |
| 351 | func_filename = func.__code__.co_filename |
| 352 | """ |
| 353 | dir_name = os.path.abspath(TESTFN) |
| 354 | file_name = os.path.join(dir_name, module_name) + os.extsep + "py" |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 355 | compiled_name = imp.cache_from_source(file_name) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 356 | |
| 357 | def setUp(self): |
| 358 | self.sys_path = sys.path[:] |
| 359 | self.orig_module = sys.modules.pop(self.module_name, None) |
| 360 | os.mkdir(self.dir_name) |
| 361 | with open(self.file_name, "w") as f: |
| 362 | f.write(self.module_source) |
| 363 | sys.path.insert(0, self.dir_name) |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 364 | importlib.invalidate_caches() |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 365 | |
| 366 | def tearDown(self): |
| 367 | sys.path[:] = self.sys_path |
| 368 | if self.orig_module is not None: |
| 369 | sys.modules[self.module_name] = self.orig_module |
| 370 | else: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 371 | unload(self.module_name) |
| 372 | unlink(self.file_name) |
| 373 | unlink(self.compiled_name) |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 374 | rmtree(self.dir_name) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 375 | |
| 376 | def import_module(self): |
| 377 | ns = globals() |
| 378 | __import__(self.module_name, ns, ns) |
| 379 | return sys.modules[self.module_name] |
| 380 | |
| 381 | def test_basics(self): |
| 382 | mod = self.import_module() |
| 383 | self.assertEqual(mod.module_filename, self.file_name) |
| 384 | self.assertEqual(mod.code_filename, self.file_name) |
| 385 | self.assertEqual(mod.func_filename, self.file_name) |
| 386 | del sys.modules[self.module_name] |
| 387 | mod = self.import_module() |
| 388 | self.assertEqual(mod.module_filename, self.file_name) |
| 389 | self.assertEqual(mod.code_filename, self.file_name) |
| 390 | self.assertEqual(mod.func_filename, self.file_name) |
| 391 | |
| 392 | def test_incorrect_code_name(self): |
| 393 | py_compile.compile(self.file_name, dfile="another_module.py") |
| 394 | mod = self.import_module() |
| 395 | self.assertEqual(mod.module_filename, self.file_name) |
| 396 | self.assertEqual(mod.code_filename, self.file_name) |
| 397 | self.assertEqual(mod.func_filename, self.file_name) |
| 398 | |
| 399 | def test_module_without_source(self): |
| 400 | target = "another_module.py" |
| 401 | py_compile.compile(self.file_name, dfile=target) |
| 402 | os.remove(self.file_name) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 403 | pyc_file = make_legacy_pyc(self.file_name) |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 404 | importlib.invalidate_caches() |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 405 | mod = self.import_module() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 406 | self.assertEqual(mod.module_filename, pyc_file) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 407 | self.assertEqual(mod.code_filename, target) |
| 408 | self.assertEqual(mod.func_filename, target) |
| 409 | |
| 410 | def test_foreign_code(self): |
| 411 | py_compile.compile(self.file_name) |
| 412 | with open(self.compiled_name, "rb") as f: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 413 | header = f.read(12) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 414 | code = marshal.load(f) |
| 415 | constants = list(code.co_consts) |
| 416 | foreign_code = test_main.__code__ |
| 417 | pos = constants.index(1) |
| 418 | constants[pos] = foreign_code |
| 419 | code = type(code)(code.co_argcount, code.co_kwonlyargcount, |
| 420 | code.co_nlocals, code.co_stacksize, |
| 421 | code.co_flags, code.co_code, tuple(constants), |
| 422 | code.co_names, code.co_varnames, code.co_filename, |
| 423 | code.co_name, code.co_firstlineno, code.co_lnotab, |
| 424 | code.co_freevars, code.co_cellvars) |
| 425 | with open(self.compiled_name, "wb") as f: |
| 426 | f.write(header) |
| 427 | marshal.dump(code, f) |
| 428 | mod = self.import_module() |
| 429 | self.assertEqual(mod.constant.co_filename, foreign_code.co_filename) |
| 430 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 431 | |
Christian Heimes | 204093a | 2007-11-01 22:37:07 +0000 | [diff] [blame] | 432 | class PathsTests(unittest.TestCase): |
| 433 | SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8', |
| 434 | 'test\u00b0\u00b3\u00b2') |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 435 | path = TESTFN |
| 436 | |
| 437 | def setUp(self): |
| 438 | os.mkdir(self.path) |
| 439 | self.syspath = sys.path[:] |
| 440 | |
| 441 | def tearDown(self): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 442 | rmtree(self.path) |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 443 | sys.path[:] = self.syspath |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 444 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 445 | # Regression test for http://bugs.python.org/issue1293. |
Christian Heimes | 1d1a400 | 2007-11-01 19:41:07 +0000 | [diff] [blame] | 446 | def test_trailing_slash(self): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 447 | with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
| 448 | f.write("testdata = 'test_trailing_slash'") |
Christian Heimes | 1d1a400 | 2007-11-01 19:41:07 +0000 | [diff] [blame] | 449 | sys.path.append(self.path+'/') |
| 450 | mod = __import__("test_trailing_slash") |
| 451 | self.assertEqual(mod.testdata, 'test_trailing_slash') |
| 452 | unload("test_trailing_slash") |
| 453 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 454 | # Regression test for http://bugs.python.org/issue3677. |
Brett Cannon | bbdc9cd | 2012-04-20 16:29:39 -0400 | [diff] [blame] | 455 | @unittest.skipUnless(sys.platform == 'win32', 'Windows-specific') |
Brett Cannon | 9e924ed | 2012-04-20 17:34:59 -0400 | [diff] [blame] | 456 | def test_UNC_path(self): |
Antoine Pitrou | f189e80 | 2012-07-12 19:48:49 +0200 | [diff] [blame] | 457 | with open(os.path.join(self.path, 'test_unc_path.py'), 'w') as f: |
| 458 | f.write("testdata = 'test_unc_path'") |
Brett Cannon | b8c0206 | 2012-04-21 19:11:58 -0400 | [diff] [blame] | 459 | importlib.invalidate_caches() |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 460 | # Create the UNC path, like \\myhost\c$\foo\bar. |
Kristján Valur Jónsson | d9aab51 | 2009-01-24 10:50:45 +0000 | [diff] [blame] | 461 | path = os.path.abspath(self.path) |
| 462 | import socket |
| 463 | hn = socket.gethostname() |
| 464 | drive = path[0] |
| 465 | unc = "\\\\%s\\%s$"%(hn, drive) |
| 466 | unc += path[2:] |
Brett Cannon | b8c0206 | 2012-04-21 19:11:58 -0400 | [diff] [blame] | 467 | try: |
Antoine Pitrou | 5df0204 | 2012-07-12 19:50:03 +0200 | [diff] [blame] | 468 | os.listdir(unc) |
Antoine Pitrou | 68f4247 | 2012-07-13 20:54:42 +0200 | [diff] [blame] | 469 | except OSError as e: |
| 470 | if e.errno in (errno.EPERM, errno.EACCES): |
| 471 | # See issue #15338 |
| 472 | self.skipTest("cannot access administrative share %r" % (unc,)) |
| 473 | raise |
Antoine Pitrou | c27ace6 | 2012-07-13 20:59:19 +0200 | [diff] [blame] | 474 | sys.path.insert(0, unc) |
| 475 | try: |
| 476 | mod = __import__("test_unc_path") |
| 477 | except ImportError as e: |
| 478 | self.fail("could not import 'test_unc_path' from %r: %r" |
| 479 | % (unc, e)) |
| 480 | self.assertEqual(mod.testdata, 'test_unc_path') |
| 481 | self.assertTrue(mod.__file__.startswith(unc), mod.__file__) |
| 482 | unload("test_unc_path") |
Kristján Valur Jónsson | d9aab51 | 2009-01-24 10:50:45 +0000 | [diff] [blame] | 483 | |
Kristján Valur Jónsson | d9aab51 | 2009-01-24 10:50:45 +0000 | [diff] [blame] | 484 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 485 | class RelativeImportTests(unittest.TestCase): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 486 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 487 | def tearDown(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 488 | unload("test.relimport") |
| 489 | setUp = tearDown |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 490 | |
| 491 | def test_relimport_star(self): |
| 492 | # This will import * from .test_import. |
| 493 | from . import relimport |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 494 | self.assertTrue(hasattr(relimport, "RelativeImportTests")) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 495 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 496 | def test_issue3221(self): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 497 | # Note for mergers: the 'absolute' tests from the 2.x branch |
| 498 | # are missing in Py3k because implicit relative imports are |
| 499 | # a thing of the past |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 500 | # |
| 501 | # Regression test for http://bugs.python.org/issue3221. |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 502 | def check_relative(): |
| 503 | exec("from . import relimport", ns) |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 504 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 505 | # Check relative import OK with __package__ and __name__ correct |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 506 | ns = dict(__package__='test', __name__='test.notarealmodule') |
| 507 | check_relative() |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 508 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 509 | # Check relative import OK with only __name__ wrong |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 510 | ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') |
| 511 | check_relative() |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 512 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 513 | # Check relative import fails with only __package__ wrong |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 514 | ns = dict(__package__='foo', __name__='test.notarealmodule') |
| 515 | self.assertRaises(SystemError, check_relative) |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 516 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 517 | # Check relative import fails with __package__ and __name__ wrong |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 518 | ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') |
| 519 | self.assertRaises(SystemError, check_relative) |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 520 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 521 | # Check relative import fails with package set to a non-string |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 522 | ns = dict(__package__=object()) |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 523 | self.assertRaises(TypeError, check_relative) |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 524 | |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 525 | def test_absolute_import_without_future(self): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 526 | # If explicit relative import syntax is used, then do not try |
Florent Xicluna | c9c29e2 | 2010-08-16 19:03:05 +0000 | [diff] [blame] | 527 | # to perform an absolute import in the face of failure. |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 528 | # Issue #7902. |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 529 | with self.assertRaises(ImportError): |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 530 | from .os import sep |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 531 | self.fail("explicit relative import triggered an " |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 532 | "implicit absolute import") |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 533 | |
Florent Xicluna | c9c29e2 | 2010-08-16 19:03:05 +0000 | [diff] [blame] | 534 | |
Collin Winter | 6498cff | 2010-03-17 03:14:31 +0000 | [diff] [blame] | 535 | class OverridingImportBuiltinTests(unittest.TestCase): |
| 536 | def test_override_builtin(self): |
| 537 | # Test that overriding builtins.__import__ can bypass sys.modules. |
| 538 | import os |
| 539 | |
| 540 | def foo(): |
| 541 | import os |
| 542 | return os |
| 543 | self.assertEqual(foo(), os) # Quick sanity check. |
| 544 | |
| 545 | with swap_attr(builtins, "__import__", lambda *x: 5): |
| 546 | self.assertEqual(foo(), 5) |
| 547 | |
| 548 | # Test what happens when we shadow __import__ in globals(); this |
| 549 | # currently does not impact the import process, but if this changes, |
| 550 | # other code will need to change, so keep this test as a tripwire. |
| 551 | with swap_item(globals(), "__import__", lambda *x: 5): |
| 552 | self.assertEqual(foo(), os) |
| 553 | |
| 554 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 555 | class PycacheTests(unittest.TestCase): |
| 556 | # Test the various PEP 3147 related behaviors. |
| 557 | |
| 558 | tag = imp.get_tag() |
| 559 | |
| 560 | def _clean(self): |
| 561 | forget(TESTFN) |
| 562 | rmtree('__pycache__') |
| 563 | unlink(self.source) |
| 564 | |
| 565 | def setUp(self): |
| 566 | self.source = TESTFN + '.py' |
| 567 | self._clean() |
| 568 | with open(self.source, 'w') as fp: |
| 569 | print('# This is a test file written by test_import.py', file=fp) |
| 570 | sys.path.insert(0, os.curdir) |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 571 | importlib.invalidate_caches() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 572 | |
| 573 | def tearDown(self): |
| 574 | assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]' |
| 575 | del sys.path[0] |
| 576 | self._clean() |
| 577 | |
| 578 | def test_import_pyc_path(self): |
| 579 | self.assertFalse(os.path.exists('__pycache__')) |
| 580 | __import__(TESTFN) |
| 581 | self.assertTrue(os.path.exists('__pycache__')) |
| 582 | self.assertTrue(os.path.exists(os.path.join( |
Georg Brandl | fb3c84a | 2010-10-14 07:24:28 +0000 | [diff] [blame] | 583 | '__pycache__', '{}.{}.py{}'.format( |
Georg Brandl | 1c2a7b7 | 2010-10-14 07:34:56 +0000 | [diff] [blame] | 584 | TESTFN, self.tag, __debug__ and 'c' or 'o')))) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 585 | |
| 586 | @unittest.skipUnless(os.name == 'posix', |
| 587 | "test meaningful only on posix systems") |
Charles-François Natali | 035018d | 2011-10-04 23:35:47 +0200 | [diff] [blame] | 588 | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
Charles-François Natali | 79164c8 | 2011-10-04 20:40:58 +0200 | [diff] [blame] | 589 | "due to varying filesystem permission semantics (issue #11956)") |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 590 | def test_unwritable_directory(self): |
| 591 | # When the umask causes the new __pycache__ directory to be |
| 592 | # unwritable, the import still succeeds but no .pyc file is written. |
| 593 | with temp_umask(0o222): |
| 594 | __import__(TESTFN) |
| 595 | self.assertTrue(os.path.exists('__pycache__')) |
| 596 | self.assertFalse(os.path.exists(os.path.join( |
| 597 | '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag)))) |
| 598 | |
| 599 | def test_missing_source(self): |
| 600 | # With PEP 3147 cache layout, removing the source but leaving the pyc |
| 601 | # file does not satisfy the import. |
| 602 | __import__(TESTFN) |
| 603 | pyc_file = imp.cache_from_source(self.source) |
| 604 | self.assertTrue(os.path.exists(pyc_file)) |
| 605 | os.remove(self.source) |
| 606 | forget(TESTFN) |
| 607 | self.assertRaises(ImportError, __import__, TESTFN) |
| 608 | |
| 609 | def test_missing_source_legacy(self): |
| 610 | # Like test_missing_source() except that for backward compatibility, |
| 611 | # when the pyc file lives where the py file would have been (and named |
| 612 | # without the tag), it is importable. The __file__ of the imported |
| 613 | # module is the pyc location. |
| 614 | __import__(TESTFN) |
| 615 | # pyc_file gets removed in _clean() via tearDown(). |
| 616 | pyc_file = make_legacy_pyc(self.source) |
| 617 | os.remove(self.source) |
| 618 | unload(TESTFN) |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 619 | importlib.invalidate_caches() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 620 | m = __import__(TESTFN) |
| 621 | self.assertEqual(m.__file__, |
| 622 | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
| 623 | |
| 624 | def test___cached__(self): |
| 625 | # Modules now also have an __cached__ that points to the pyc file. |
| 626 | m = __import__(TESTFN) |
| 627 | pyc_file = imp.cache_from_source(TESTFN + '.py') |
| 628 | self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file)) |
| 629 | |
| 630 | def test___cached___legacy_pyc(self): |
| 631 | # Like test___cached__() except that for backward compatibility, |
| 632 | # when the pyc file lives where the py file would have been (and named |
| 633 | # without the tag), it is importable. The __cached__ of the imported |
| 634 | # module is the pyc location. |
| 635 | __import__(TESTFN) |
| 636 | # pyc_file gets removed in _clean() via tearDown(). |
| 637 | pyc_file = make_legacy_pyc(self.source) |
| 638 | os.remove(self.source) |
| 639 | unload(TESTFN) |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 640 | importlib.invalidate_caches() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 641 | m = __import__(TESTFN) |
| 642 | self.assertEqual(m.__cached__, |
| 643 | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
| 644 | |
| 645 | def test_package___cached__(self): |
| 646 | # Like test___cached__ but for packages. |
| 647 | def cleanup(): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 648 | rmtree('pep3147') |
Antoine Pitrou | d4daa87 | 2012-06-23 18:09:55 +0200 | [diff] [blame] | 649 | unload('pep3147.foo') |
| 650 | unload('pep3147') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 651 | os.mkdir('pep3147') |
| 652 | self.addCleanup(cleanup) |
| 653 | # Touch the __init__.py |
| 654 | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
| 655 | pass |
| 656 | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
| 657 | pass |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 658 | importlib.invalidate_caches() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 659 | m = __import__('pep3147.foo') |
| 660 | init_pyc = imp.cache_from_source( |
| 661 | os.path.join('pep3147', '__init__.py')) |
| 662 | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
| 663 | foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py')) |
| 664 | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
| 665 | os.path.join(os.curdir, foo_pyc)) |
| 666 | |
| 667 | def test_package___cached___from_pyc(self): |
| 668 | # Like test___cached__ but ensuring __cached__ when imported from a |
| 669 | # PEP 3147 pyc file. |
| 670 | def cleanup(): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 671 | rmtree('pep3147') |
Antoine Pitrou | d4daa87 | 2012-06-23 18:09:55 +0200 | [diff] [blame] | 672 | unload('pep3147.foo') |
| 673 | unload('pep3147') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 674 | os.mkdir('pep3147') |
| 675 | self.addCleanup(cleanup) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 676 | # Touch the __init__.py |
| 677 | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
| 678 | pass |
| 679 | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
| 680 | pass |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 681 | importlib.invalidate_caches() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 682 | m = __import__('pep3147.foo') |
| 683 | unload('pep3147.foo') |
| 684 | unload('pep3147') |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 685 | importlib.invalidate_caches() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 686 | m = __import__('pep3147.foo') |
| 687 | init_pyc = imp.cache_from_source( |
| 688 | os.path.join('pep3147', '__init__.py')) |
| 689 | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
| 690 | foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py')) |
| 691 | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
| 692 | os.path.join(os.curdir, foo_pyc)) |
| 693 | |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 694 | def test_recompute_pyc_same_second(self): |
| 695 | # Even when the source file doesn't change timestamp, a change in |
| 696 | # source size is enough to trigger recomputation of the pyc file. |
| 697 | __import__(TESTFN) |
| 698 | unload(TESTFN) |
| 699 | with open(self.source, 'a') as fp: |
| 700 | print("x = 5", file=fp) |
| 701 | m = __import__(TESTFN) |
| 702 | self.assertEqual(m.x, 5) |
| 703 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 704 | |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 705 | class TestSymbolicallyLinkedPackage(unittest.TestCase): |
| 706 | package_name = 'sample' |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 707 | tagged = package_name + '-tagged' |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 708 | |
| 709 | def setUp(self): |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 710 | test.support.rmtree(self.tagged) |
| 711 | test.support.rmtree(self.package_name) |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 712 | self.orig_sys_path = sys.path[:] |
| 713 | |
| 714 | # create a sample package; imagine you have a package with a tag and |
| 715 | # you want to symbolically link it from its untagged name. |
| 716 | os.mkdir(self.tagged) |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 717 | self.addCleanup(test.support.rmtree, self.tagged) |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 718 | init_file = os.path.join(self.tagged, '__init__.py') |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 719 | test.support.create_empty_file(init_file) |
| 720 | assert os.path.exists(init_file) |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 721 | |
| 722 | # now create a symlink to the tagged package |
| 723 | # sample -> sample-tagged |
| 724 | os.symlink(self.tagged, self.package_name) |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 725 | self.addCleanup(test.support.unlink, self.package_name) |
| 726 | importlib.invalidate_caches() |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 727 | |
Jason R. Coombs | 42c9b04 | 2012-06-20 10:24:24 -0400 | [diff] [blame] | 728 | # disabled because os.isdir currently fails (see issue 15093) |
| 729 | # self.assertEqual(os.path.isdir(self.package_name), True) |
| 730 | |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 731 | assert os.path.isfile(os.path.join(self.package_name, '__init__.py')) |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 732 | |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 733 | def tearDown(self): |
| 734 | sys.path[:] = self.orig_sys_path |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 735 | |
| 736 | # regression test for issue6727 |
| 737 | @unittest.skipUnless( |
| 738 | not hasattr(sys, 'getwindowsversion') |
| 739 | or sys.getwindowsversion() >= (6, 0), |
| 740 | "Windows Vista or later required") |
| 741 | @test.support.skip_unless_symlink |
| 742 | def test_symlinked_dir_importable(self): |
| 743 | # make sure sample can only be imported from the current directory. |
| 744 | sys.path[:] = ['.'] |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 745 | assert os.path.exists(self.package_name) |
| 746 | assert os.path.exists(os.path.join(self.package_name, '__init__.py')) |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 747 | |
Brett Cannon | 6ee9695 | 2012-07-20 14:22:04 -0400 | [diff] [blame^] | 748 | # Try to import the package |
| 749 | importlib.import_module(self.package_name) |
Jason R. Coombs | 71fde31 | 2012-06-17 03:53:47 -0400 | [diff] [blame] | 750 | |
| 751 | |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 752 | @cpython_only |
| 753 | class ImportlibBootstrapTests(unittest.TestCase): |
| 754 | # These tests check that importlib is bootstrapped. |
| 755 | |
| 756 | def test_frozen_importlib(self): |
| 757 | mod = sys.modules['_frozen_importlib'] |
| 758 | self.assertTrue(mod) |
| 759 | |
| 760 | def test_frozen_importlib_is_bootstrap(self): |
| 761 | from importlib import _bootstrap |
| 762 | mod = sys.modules['_frozen_importlib'] |
| 763 | self.assertIs(mod, _bootstrap) |
| 764 | self.assertEqual(mod.__name__, 'importlib._bootstrap') |
| 765 | self.assertEqual(mod.__package__, 'importlib') |
| 766 | self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__) |
| 767 | |
Nick Coghlan | be7e49f | 2012-07-20 23:40:09 +1000 | [diff] [blame] | 768 | def test_there_can_be_only_one(self): |
| 769 | # Issue #15386 revealed a tricky loophole in the bootstrapping |
| 770 | # This test is technically redundant, since the bug caused importing |
| 771 | # this test module to crash completely, but it helps prove the point |
| 772 | from importlib import machinery |
| 773 | mod = sys.modules['_frozen_importlib'] |
| 774 | self.assertIs(machinery.FileFinder, mod.FileFinder) |
| 775 | self.assertIs(imp.new_module, mod.new_module) |
| 776 | |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 777 | |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 778 | class ImportTracebackTests(unittest.TestCase): |
| 779 | |
| 780 | def setUp(self): |
| 781 | os.mkdir(TESTFN) |
| 782 | self.old_path = sys.path[:] |
| 783 | sys.path.insert(0, TESTFN) |
| 784 | |
| 785 | def tearDown(self): |
| 786 | sys.path[:] = self.old_path |
| 787 | rmtree(TESTFN) |
| 788 | |
| 789 | def create_module(self, mod, contents): |
| 790 | with open(os.path.join(TESTFN, mod + ".py"), "w") as f: |
| 791 | f.write(contents) |
| 792 | self.addCleanup(unload, mod) |
| 793 | importlib.invalidate_caches() |
| 794 | |
| 795 | def assert_traceback(self, tb, files): |
| 796 | deduped_files = [] |
| 797 | while tb: |
| 798 | code = tb.tb_frame.f_code |
| 799 | fn = code.co_filename |
| 800 | if not deduped_files or fn != deduped_files[-1]: |
| 801 | deduped_files.append(fn) |
| 802 | tb = tb.tb_next |
| 803 | self.assertEqual(len(deduped_files), len(files), deduped_files) |
| 804 | for fn, pat in zip(deduped_files, files): |
Antoine Pitrou | 6803855 | 2012-07-08 13:16:15 +0200 | [diff] [blame] | 805 | self.assertIn(pat, fn) |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 806 | |
| 807 | def test_nonexistent_module(self): |
| 808 | try: |
| 809 | # assertRaises() clears __traceback__ |
| 810 | import nonexistent_xyzzy |
| 811 | except ImportError as e: |
| 812 | tb = e.__traceback__ |
| 813 | else: |
| 814 | self.fail("ImportError should have been raised") |
| 815 | self.assert_traceback(tb, [__file__]) |
| 816 | |
| 817 | def test_nonexistent_module_nested(self): |
| 818 | self.create_module("foo", "import nonexistent_xyzzy") |
| 819 | try: |
| 820 | import foo |
| 821 | except ImportError as e: |
| 822 | tb = e.__traceback__ |
| 823 | else: |
| 824 | self.fail("ImportError should have been raised") |
| 825 | self.assert_traceback(tb, [__file__, 'foo.py']) |
| 826 | |
| 827 | def test_exec_failure(self): |
| 828 | self.create_module("foo", "1/0") |
| 829 | try: |
| 830 | import foo |
| 831 | except ZeroDivisionError as e: |
| 832 | tb = e.__traceback__ |
| 833 | else: |
| 834 | self.fail("ZeroDivisionError should have been raised") |
| 835 | self.assert_traceback(tb, [__file__, 'foo.py']) |
| 836 | |
| 837 | def test_exec_failure_nested(self): |
| 838 | self.create_module("foo", "import bar") |
| 839 | self.create_module("bar", "1/0") |
| 840 | try: |
| 841 | import foo |
| 842 | except ZeroDivisionError as e: |
| 843 | tb = e.__traceback__ |
| 844 | else: |
| 845 | self.fail("ZeroDivisionError should have been raised") |
| 846 | self.assert_traceback(tb, [__file__, 'foo.py', 'bar.py']) |
| 847 | |
| 848 | @cpython_only |
| 849 | def test_import_bug(self): |
| 850 | # We simulate a bug in importlib and check that it's not stripped |
| 851 | # away from the traceback. |
| 852 | self.create_module("foo", "") |
| 853 | importlib = sys.modules['_frozen_importlib'] |
| 854 | old_load_module = importlib.SourceLoader.load_module |
| 855 | try: |
| 856 | def load_module(*args): |
| 857 | 1/0 |
| 858 | importlib.SourceLoader.load_module = load_module |
| 859 | try: |
| 860 | import foo |
| 861 | except ZeroDivisionError as e: |
| 862 | tb = e.__traceback__ |
| 863 | else: |
| 864 | self.fail("ZeroDivisionError should have been raised") |
| 865 | self.assert_traceback(tb, [__file__, '<frozen importlib', __file__]) |
| 866 | finally: |
| 867 | importlib.SourceLoader.load_module = old_load_module |
| 868 | |
| 869 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 870 | def test_main(verbose=None): |
Brett Cannon | ba52586 | 2012-07-20 14:01:34 -0400 | [diff] [blame] | 871 | run_unittest(ImportTests, PycacheTests, |
| 872 | PycRewritingTests, PathsTests, RelativeImportTests, |
| 873 | OverridingImportBuiltinTests, |
| 874 | ImportlibBootstrapTests, |
| 875 | TestSymbolicallyLinkedPackage, |
| 876 | ImportTracebackTests) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 877 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 878 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 879 | if __name__ == '__main__': |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 880 | # Test needs to be a package, so we can do relative imports. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 881 | from test.test_import import test_main |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 882 | test_main() |