Collin Winter | 6498cff | 2010-03-17 03:14:31 +0000 | [diff] [blame] | 1 | import builtins |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 2 | import imp |
Brett Cannon | 1f27479 | 2010-07-23 14:03:16 +0000 | [diff] [blame] | 3 | from importlib.test.import_ import test_relative_imports |
| 4 | from importlib.test.import_ import util as importlib_util |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 5 | import marshal |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 6 | import os |
| 7 | import py_compile |
| 8 | import random |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 9 | import stat |
| 10 | import sys |
| 11 | import unittest |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 12 | |
| 13 | from test.support import ( |
| 14 | EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, |
| 15 | make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, |
| 16 | unlink, unload) |
Guido van Rossum | bd6f4fb | 2000-10-24 17:16:32 +0000 | [diff] [blame] | 17 | |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 18 | |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 19 | def remove_files(name): |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 20 | for f in (name + ".py", |
| 21 | name + ".pyc", |
| 22 | name + ".pyo", |
| 23 | name + ".pyw", |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 24 | name + "$py.class"): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 25 | unlink(f) |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 26 | rmtree('__pycache__') |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 27 | |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 28 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 29 | class ImportTests(unittest.TestCase): |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 30 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 31 | def tearDown(self): |
| 32 | unload(TESTFN) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 33 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 34 | setUp = tearDown |
| 35 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 36 | def test_case_sensitivity(self): |
| 37 | # Brief digression to test that import is case-sensitive: if we got |
| 38 | # this far, we know for sure that "random" exists. |
Benjamin Peterson | 1c87e29 | 2010-10-30 23:04:49 +0000 | [diff] [blame] | 39 | with self.assertRaises(ImportError): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 40 | import RAnDoM |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 41 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 42 | def test_double_const(self): |
| 43 | # Another brief digression to test the accuracy of manifest float |
| 44 | # constants. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 45 | from test import double_const # don't blink -- that *was* the test |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 46 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 47 | def test_import(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 48 | def test_with_extension(ext): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 49 | # The extension is normally ".py", perhaps ".pyw". |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 50 | source = TESTFN + ext |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 51 | pyo = TESTFN + ".pyo" |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 52 | if is_jython: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 53 | pyc = TESTFN + "$py.class" |
| 54 | else: |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 55 | pyc = TESTFN + ".pyc" |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 56 | |
Benjamin Peterson | 16a1f63 | 2009-06-13 13:01:19 +0000 | [diff] [blame] | 57 | with open(source, "w") as f: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 58 | print("# This tests Python's ability to import a", |
| 59 | ext, "file.", file=f) |
Benjamin Peterson | 16a1f63 | 2009-06-13 13:01:19 +0000 | [diff] [blame] | 60 | a = random.randrange(1000) |
| 61 | b = random.randrange(1000) |
| 62 | print("a =", a, file=f) |
| 63 | print("b =", b, file=f) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 64 | |
Amaury Forgeot d'Arc | dd9e3b8 | 2007-11-16 00:56:23 +0000 | [diff] [blame] | 65 | if TESTFN in sys.modules: |
| 66 | del sys.modules[TESTFN] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 67 | try: |
| 68 | try: |
| 69 | mod = __import__(TESTFN) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 70 | except ImportError as err: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 71 | self.fail("import from %s failed: %s" % (ext, err)) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 72 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 73 | self.assertEqual(mod.a, a, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 74 | "module loaded (%s) but contents invalid" % mod) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 75 | self.assertEqual(mod.b, b, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 76 | "module loaded (%s) but contents invalid" % mod) |
| 77 | finally: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 78 | forget(TESTFN) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 79 | unlink(source) |
| 80 | unlink(pyc) |
| 81 | unlink(pyo) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 82 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 83 | sys.path.insert(0, os.curdir) |
| 84 | try: |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 85 | test_with_extension(".py") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 86 | if sys.platform.startswith("win"): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 87 | for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 88 | test_with_extension(ext) |
| 89 | finally: |
| 90 | del sys.path[0] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 91 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 92 | @unittest.skipUnless(os.name == 'posix', |
| 93 | "test meaningful only on posix systems") |
Alexandre Vassalotti | 9d58e3e | 2009-07-17 10:55:50 +0000 | [diff] [blame] | 94 | def test_execute_bit_not_copied(self): |
| 95 | # Issue 6070: under posix .pyc files got their execute bit set if |
| 96 | # the .py file had the execute bit set, but they aren't executable. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 97 | with temp_umask(0o022): |
| 98 | sys.path.insert(0, os.curdir) |
| 99 | try: |
| 100 | fname = TESTFN + os.extsep + "py" |
Benjamin Peterson | 1a7127f | 2010-10-30 23:00:54 +0000 | [diff] [blame] | 101 | open(fname, 'w').close() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 102 | os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | |
| 103 | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)) |
| 104 | __import__(TESTFN) |
| 105 | fn = imp.cache_from_source(fname) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 106 | if not os.path.exists(fn): |
| 107 | self.fail("__import__ did not result in creation of " |
| 108 | "either a .pyc or .pyo file") |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 109 | s = os.stat(fn) |
| 110 | self.assertEqual( |
| 111 | stat.S_IMODE(s.st_mode), |
| 112 | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 113 | finally: |
| 114 | del sys.path[0] |
| 115 | remove_files(TESTFN) |
| 116 | unload(TESTFN) |
Alexandre Vassalotti | 9d58e3e | 2009-07-17 10:55:50 +0000 | [diff] [blame] | 117 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 118 | def test_imp_module(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | # Verify that the imp module can correctly load and find .py files |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 120 | # XXX (ncoghlan): It would be nice to use support.CleanImport |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 121 | # here, but that breaks because the os module registers some |
| 122 | # handlers in copy_reg on import. Since CleanImport doesn't |
| 123 | # revert that registration, the module is left in a broken |
| 124 | # state after reversion. Reinitialising the module contents |
| 125 | # and just reverting os.environ to its previous state is an OK |
| 126 | # workaround |
| 127 | orig_path = os.path |
| 128 | orig_getenv = os.getenv |
| 129 | with EnvironmentVarGuard(): |
| 130 | x = imp.find_module("os") |
Benjamin Peterson | e048797 | 2010-10-30 23:06:57 +0000 | [diff] [blame] | 131 | self.addCleanup(x[0].close) |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 132 | new_os = imp.load_module("os", *x) |
| 133 | self.assertIs(os, new_os) |
| 134 | self.assertIs(orig_path, new_os.path) |
| 135 | self.assertIsNot(orig_getenv, new_os.getenv) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 136 | |
| 137 | def test_module_with_large_stack(self, module='longlist'): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 138 | # Regression test for http://bugs.python.org/issue561858. |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 139 | filename = module + '.py' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 140 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 141 | # Create a file with a list of 65000 elements. |
Brett Cannon | 2cab50b | 2010-07-03 01:32:48 +0000 | [diff] [blame] | 142 | with open(filename, 'w') as f: |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 143 | f.write('d = [\n') |
| 144 | for i in range(65000): |
| 145 | f.write('"",\n') |
| 146 | f.write(']') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 147 | |
Brett Cannon | 2cab50b | 2010-07-03 01:32:48 +0000 | [diff] [blame] | 148 | try: |
| 149 | # Compile & remove .py file; we only need .pyc (or .pyo). |
| 150 | # Bytecode must be relocated from the PEP 3147 bytecode-only location. |
| 151 | py_compile.compile(filename) |
| 152 | finally: |
| 153 | unlink(filename) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 154 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 155 | # Need to be able to load from current dir. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 156 | sys.path.append('') |
| 157 | |
Brett Cannon | 93220d0 | 2010-05-15 22:20:16 +0000 | [diff] [blame] | 158 | try: |
Brett Cannon | 2cab50b | 2010-07-03 01:32:48 +0000 | [diff] [blame] | 159 | make_legacy_pyc(filename) |
Brett Cannon | 93220d0 | 2010-05-15 22:20:16 +0000 | [diff] [blame] | 160 | # This used to crash. |
| 161 | exec('import ' + module) |
| 162 | finally: |
| 163 | # Cleanup. |
| 164 | del sys.path[-1] |
| 165 | unlink(filename + 'c') |
| 166 | unlink(filename + 'o') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 167 | |
| 168 | def test_failing_import_sticks(self): |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 169 | source = TESTFN + ".py" |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 170 | with open(source, "w") as f: |
| 171 | print("a = 1/0", file=f) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 172 | |
| 173 | # New in 2.4, we shouldn't be able to import that no matter how often |
| 174 | # we try. |
| 175 | sys.path.insert(0, os.curdir) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 176 | if TESTFN in sys.modules: |
| 177 | del sys.modules[TESTFN] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 178 | try: |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 179 | for i in [1, 2, 3]: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 180 | self.assertRaises(ZeroDivisionError, __import__, TESTFN) |
| 181 | self.assertNotIn(TESTFN, sys.modules, |
| 182 | "damaged module in sys.modules on %i try" % i) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 183 | finally: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 184 | del sys.path[0] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 185 | remove_files(TESTFN) |
| 186 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 187 | def test_import_name_binding(self): |
| 188 | # import x.y.z binds x in the current namespace |
| 189 | import test as x |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 190 | import test.support |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 191 | self.assertTrue(x is test, x.__name__) |
| 192 | self.assertTrue(hasattr(test.support, "__file__")) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 193 | |
| 194 | # import x.y.z as w binds z as w |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 195 | import test.support as y |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 196 | self.assertTrue(y is test.support, y.__name__) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 197 | |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 198 | def test_failing_reload(self): |
| 199 | # A failing reload should leave the module object in sys.modules. |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 200 | source = TESTFN + os.extsep + "py" |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 201 | with open(source, "w") as f: |
| 202 | f.write("a = 1\nb=2\n") |
| 203 | |
| 204 | sys.path.insert(0, os.curdir) |
| 205 | try: |
| 206 | mod = __import__(TESTFN) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 207 | self.assertIn(TESTFN, sys.modules) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 208 | self.assertEqual(mod.a, 1, "module has wrong attribute values") |
| 209 | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 210 | |
| 211 | # On WinXP, just replacing the .py file wasn't enough to |
| 212 | # convince reload() to reparse it. Maybe the timestamp didn't |
| 213 | # move enough. We force it to get reparsed by removing the |
| 214 | # compiled file too. |
| 215 | remove_files(TESTFN) |
| 216 | |
| 217 | # Now damage the module. |
| 218 | with open(source, "w") as f: |
| 219 | f.write("a = 10\nb=20//0\n") |
| 220 | |
| 221 | self.assertRaises(ZeroDivisionError, imp.reload, mod) |
| 222 | # But we still expect the module to be in sys.modules. |
| 223 | mod = sys.modules.get(TESTFN) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 224 | self.assertIsNot(mod, None, "expected module to be in sys.modules") |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 225 | |
| 226 | # We should have replaced a w/ 10, but the old b value should |
| 227 | # stick. |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 228 | self.assertEqual(mod.a, 10, "module has wrong attribute values") |
| 229 | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 230 | |
| 231 | finally: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 232 | del sys.path[0] |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 233 | remove_files(TESTFN) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 234 | unload(TESTFN) |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 235 | |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 236 | def test_file_to_source(self): |
| 237 | # check if __file__ points to the source file where available |
| 238 | source = TESTFN + ".py" |
| 239 | with open(source, "w") as f: |
| 240 | f.write("test = None\n") |
| 241 | |
| 242 | sys.path.insert(0, os.curdir) |
| 243 | try: |
| 244 | mod = __import__(TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 245 | self.assertTrue(mod.__file__.endswith('.py')) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 246 | os.remove(source) |
| 247 | del sys.modules[TESTFN] |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 248 | make_legacy_pyc(source) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 249 | mod = __import__(TESTFN) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 250 | base, ext = os.path.splitext(mod.__file__) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 251 | self.assertIn(ext, ('.pyc', '.pyo')) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 252 | finally: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 253 | del sys.path[0] |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 254 | remove_files(TESTFN) |
| 255 | if TESTFN in sys.modules: |
| 256 | del sys.modules[TESTFN] |
| 257 | |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 258 | def test_import_name_binding(self): |
| 259 | # import x.y.z binds x in the current namespace. |
| 260 | import test as x |
| 261 | import test.support |
| 262 | self.assertIs(x, test, x.__name__) |
| 263 | self.assertTrue(hasattr(test.support, "__file__")) |
| 264 | |
| 265 | # import x.y.z as w binds z as w. |
| 266 | import test.support as y |
| 267 | self.assertIs(y, test.support, y.__name__) |
| 268 | |
| 269 | def test_import_initless_directory_warning(self): |
| 270 | with check_warnings(('', ImportWarning)): |
| 271 | # Just a random non-package directory we always expect to be |
| 272 | # somewhere in sys.path... |
| 273 | self.assertRaises(ImportError, __import__, "site-packages") |
| 274 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 275 | def test_import_by_filename(self): |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 276 | path = os.path.abspath(TESTFN) |
Victor Stinner | 6c6f851 | 2010-08-07 10:09:35 +0000 | [diff] [blame] | 277 | encoding = sys.getfilesystemencoding() |
| 278 | try: |
| 279 | path.encode(encoding) |
| 280 | except UnicodeEncodeError: |
| 281 | self.skipTest('path is not encodable to {}'.format(encoding)) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 282 | with self.assertRaises(ImportError) as c: |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 283 | __import__(path) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 284 | self.assertEqual("Import by filename is not supported.", |
| 285 | c.exception.args[0]) |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 286 | |
Alexandre Vassalotti | 9d58e3e | 2009-07-17 10:55:50 +0000 | [diff] [blame] | 287 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 288 | class PycRewritingTests(unittest.TestCase): |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 289 | # Test that the `co_filename` attribute on code objects always points |
| 290 | # to the right file, even when various things happen (e.g. both the .py |
| 291 | # and the .pyc file are renamed). |
| 292 | |
| 293 | module_name = "unlikely_module_name" |
| 294 | module_source = """ |
| 295 | import sys |
| 296 | code_filename = sys._getframe().f_code.co_filename |
| 297 | module_filename = __file__ |
| 298 | constant = 1 |
| 299 | def func(): |
| 300 | pass |
| 301 | func_filename = func.__code__.co_filename |
| 302 | """ |
| 303 | dir_name = os.path.abspath(TESTFN) |
| 304 | file_name = os.path.join(dir_name, module_name) + os.extsep + "py" |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 305 | compiled_name = imp.cache_from_source(file_name) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 306 | |
| 307 | def setUp(self): |
| 308 | self.sys_path = sys.path[:] |
| 309 | self.orig_module = sys.modules.pop(self.module_name, None) |
| 310 | os.mkdir(self.dir_name) |
| 311 | with open(self.file_name, "w") as f: |
| 312 | f.write(self.module_source) |
| 313 | sys.path.insert(0, self.dir_name) |
| 314 | |
| 315 | def tearDown(self): |
| 316 | sys.path[:] = self.sys_path |
| 317 | if self.orig_module is not None: |
| 318 | sys.modules[self.module_name] = self.orig_module |
| 319 | else: |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 320 | unload(self.module_name) |
| 321 | unlink(self.file_name) |
| 322 | unlink(self.compiled_name) |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 323 | rmtree(self.dir_name) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 324 | |
| 325 | def import_module(self): |
| 326 | ns = globals() |
| 327 | __import__(self.module_name, ns, ns) |
| 328 | return sys.modules[self.module_name] |
| 329 | |
| 330 | def test_basics(self): |
| 331 | mod = self.import_module() |
| 332 | self.assertEqual(mod.module_filename, self.file_name) |
| 333 | self.assertEqual(mod.code_filename, self.file_name) |
| 334 | self.assertEqual(mod.func_filename, self.file_name) |
| 335 | del sys.modules[self.module_name] |
| 336 | mod = self.import_module() |
| 337 | self.assertEqual(mod.module_filename, self.file_name) |
| 338 | self.assertEqual(mod.code_filename, self.file_name) |
| 339 | self.assertEqual(mod.func_filename, self.file_name) |
| 340 | |
| 341 | def test_incorrect_code_name(self): |
| 342 | py_compile.compile(self.file_name, dfile="another_module.py") |
| 343 | mod = self.import_module() |
| 344 | self.assertEqual(mod.module_filename, self.file_name) |
| 345 | self.assertEqual(mod.code_filename, self.file_name) |
| 346 | self.assertEqual(mod.func_filename, self.file_name) |
| 347 | |
| 348 | def test_module_without_source(self): |
| 349 | target = "another_module.py" |
| 350 | py_compile.compile(self.file_name, dfile=target) |
| 351 | os.remove(self.file_name) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 352 | pyc_file = make_legacy_pyc(self.file_name) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 353 | mod = self.import_module() |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 354 | self.assertEqual(mod.module_filename, pyc_file) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 355 | self.assertEqual(mod.code_filename, target) |
| 356 | self.assertEqual(mod.func_filename, target) |
| 357 | |
| 358 | def test_foreign_code(self): |
| 359 | py_compile.compile(self.file_name) |
| 360 | with open(self.compiled_name, "rb") as f: |
| 361 | header = f.read(8) |
| 362 | code = marshal.load(f) |
| 363 | constants = list(code.co_consts) |
| 364 | foreign_code = test_main.__code__ |
| 365 | pos = constants.index(1) |
| 366 | constants[pos] = foreign_code |
| 367 | code = type(code)(code.co_argcount, code.co_kwonlyargcount, |
| 368 | code.co_nlocals, code.co_stacksize, |
| 369 | code.co_flags, code.co_code, tuple(constants), |
| 370 | code.co_names, code.co_varnames, code.co_filename, |
| 371 | code.co_name, code.co_firstlineno, code.co_lnotab, |
| 372 | code.co_freevars, code.co_cellvars) |
| 373 | with open(self.compiled_name, "wb") as f: |
| 374 | f.write(header) |
| 375 | marshal.dump(code, f) |
| 376 | mod = self.import_module() |
| 377 | self.assertEqual(mod.constant.co_filename, foreign_code.co_filename) |
| 378 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 379 | |
Christian Heimes | 204093a | 2007-11-01 22:37:07 +0000 | [diff] [blame] | 380 | class PathsTests(unittest.TestCase): |
| 381 | SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8', |
| 382 | 'test\u00b0\u00b3\u00b2') |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 383 | path = TESTFN |
| 384 | |
| 385 | def setUp(self): |
| 386 | os.mkdir(self.path) |
| 387 | self.syspath = sys.path[:] |
| 388 | |
| 389 | def tearDown(self): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 390 | rmtree(self.path) |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 391 | sys.path[:] = self.syspath |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 392 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 393 | # Regression test for http://bugs.python.org/issue1293. |
Christian Heimes | 1d1a400 | 2007-11-01 19:41:07 +0000 | [diff] [blame] | 394 | def test_trailing_slash(self): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 395 | with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
| 396 | f.write("testdata = 'test_trailing_slash'") |
Christian Heimes | 1d1a400 | 2007-11-01 19:41:07 +0000 | [diff] [blame] | 397 | sys.path.append(self.path+'/') |
| 398 | mod = __import__("test_trailing_slash") |
| 399 | self.assertEqual(mod.testdata, 'test_trailing_slash') |
| 400 | unload("test_trailing_slash") |
| 401 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 402 | # Regression test for http://bugs.python.org/issue3677. |
Kristján Valur Jónsson | d9aab51 | 2009-01-24 10:50:45 +0000 | [diff] [blame] | 403 | def _test_UNC_path(self): |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 404 | with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
| 405 | f.write("testdata = 'test_trailing_slash'") |
| 406 | # Create the UNC path, like \\myhost\c$\foo\bar. |
Kristján Valur Jónsson | d9aab51 | 2009-01-24 10:50:45 +0000 | [diff] [blame] | 407 | path = os.path.abspath(self.path) |
| 408 | import socket |
| 409 | hn = socket.gethostname() |
| 410 | drive = path[0] |
| 411 | unc = "\\\\%s\\%s$"%(hn, drive) |
| 412 | unc += path[2:] |
| 413 | sys.path.append(path) |
| 414 | mod = __import__("test_trailing_slash") |
| 415 | self.assertEqual(mod.testdata, 'test_trailing_slash') |
| 416 | unload("test_trailing_slash") |
| 417 | |
| 418 | if sys.platform == "win32": |
| 419 | test_UNC_path = _test_UNC_path |
| 420 | |
| 421 | |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 422 | class RelativeImportTests(unittest.TestCase): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 423 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 424 | def tearDown(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 425 | unload("test.relimport") |
| 426 | setUp = tearDown |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 427 | |
| 428 | def test_relimport_star(self): |
| 429 | # This will import * from .test_import. |
| 430 | from . import relimport |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 431 | self.assertTrue(hasattr(relimport, "RelativeImportTests")) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 432 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 433 | def test_issue3221(self): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 434 | # Note for mergers: the 'absolute' tests from the 2.x branch |
| 435 | # are missing in Py3k because implicit relative imports are |
| 436 | # a thing of the past |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 437 | # |
| 438 | # Regression test for http://bugs.python.org/issue3221. |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 439 | def check_relative(): |
| 440 | exec("from . import relimport", ns) |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 441 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 442 | # Check relative import OK with __package__ and __name__ correct |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 443 | ns = dict(__package__='test', __name__='test.notarealmodule') |
| 444 | check_relative() |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 445 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 446 | # Check relative import OK with only __name__ wrong |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 447 | ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') |
| 448 | check_relative() |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 449 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 450 | # Check relative import fails with only __package__ wrong |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 451 | ns = dict(__package__='foo', __name__='test.notarealmodule') |
| 452 | self.assertRaises(SystemError, check_relative) |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 453 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 454 | # Check relative import fails with __package__ and __name__ wrong |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 455 | ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') |
| 456 | self.assertRaises(SystemError, check_relative) |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 457 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 458 | # Check relative import fails with package set to a non-string |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 459 | ns = dict(__package__=object()) |
| 460 | self.assertRaises(ValueError, check_relative) |
| 461 | |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 462 | def test_absolute_import_without_future(self): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 463 | # If explicit relative import syntax is used, then do not try |
Florent Xicluna | c9c29e2 | 2010-08-16 19:03:05 +0000 | [diff] [blame] | 464 | # to perform an absolute import in the face of failure. |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 465 | # Issue #7902. |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 466 | with self.assertRaises(ImportError): |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 467 | from .os import sep |
Benjamin Peterson | 556d800 | 2010-06-27 22:37:28 +0000 | [diff] [blame] | 468 | self.fail("explicit relative import triggered an " |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 469 | "implicit absolute import") |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 470 | |
Florent Xicluna | c9c29e2 | 2010-08-16 19:03:05 +0000 | [diff] [blame] | 471 | |
Collin Winter | 6498cff | 2010-03-17 03:14:31 +0000 | [diff] [blame] | 472 | class OverridingImportBuiltinTests(unittest.TestCase): |
| 473 | def test_override_builtin(self): |
| 474 | # Test that overriding builtins.__import__ can bypass sys.modules. |
| 475 | import os |
| 476 | |
| 477 | def foo(): |
| 478 | import os |
| 479 | return os |
| 480 | self.assertEqual(foo(), os) # Quick sanity check. |
| 481 | |
| 482 | with swap_attr(builtins, "__import__", lambda *x: 5): |
| 483 | self.assertEqual(foo(), 5) |
| 484 | |
| 485 | # Test what happens when we shadow __import__ in globals(); this |
| 486 | # currently does not impact the import process, but if this changes, |
| 487 | # other code will need to change, so keep this test as a tripwire. |
| 488 | with swap_item(globals(), "__import__", lambda *x: 5): |
| 489 | self.assertEqual(foo(), os) |
| 490 | |
| 491 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 492 | class PycacheTests(unittest.TestCase): |
| 493 | # Test the various PEP 3147 related behaviors. |
| 494 | |
| 495 | tag = imp.get_tag() |
| 496 | |
| 497 | def _clean(self): |
| 498 | forget(TESTFN) |
| 499 | rmtree('__pycache__') |
| 500 | unlink(self.source) |
| 501 | |
| 502 | def setUp(self): |
| 503 | self.source = TESTFN + '.py' |
| 504 | self._clean() |
| 505 | with open(self.source, 'w') as fp: |
| 506 | print('# This is a test file written by test_import.py', file=fp) |
| 507 | sys.path.insert(0, os.curdir) |
| 508 | |
| 509 | def tearDown(self): |
| 510 | assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]' |
| 511 | del sys.path[0] |
| 512 | self._clean() |
| 513 | |
| 514 | def test_import_pyc_path(self): |
| 515 | self.assertFalse(os.path.exists('__pycache__')) |
| 516 | __import__(TESTFN) |
| 517 | self.assertTrue(os.path.exists('__pycache__')) |
| 518 | self.assertTrue(os.path.exists(os.path.join( |
Georg Brandl | fb3c84a | 2010-10-14 07:24:28 +0000 | [diff] [blame] | 519 | '__pycache__', '{}.{}.py{}'.format( |
Georg Brandl | 1c2a7b7 | 2010-10-14 07:34:56 +0000 | [diff] [blame] | 520 | TESTFN, self.tag, __debug__ and 'c' or 'o')))) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 521 | |
| 522 | @unittest.skipUnless(os.name == 'posix', |
| 523 | "test meaningful only on posix systems") |
| 524 | def test_unwritable_directory(self): |
| 525 | # When the umask causes the new __pycache__ directory to be |
| 526 | # unwritable, the import still succeeds but no .pyc file is written. |
| 527 | with temp_umask(0o222): |
| 528 | __import__(TESTFN) |
| 529 | self.assertTrue(os.path.exists('__pycache__')) |
| 530 | self.assertFalse(os.path.exists(os.path.join( |
| 531 | '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag)))) |
| 532 | |
| 533 | def test_missing_source(self): |
| 534 | # With PEP 3147 cache layout, removing the source but leaving the pyc |
| 535 | # file does not satisfy the import. |
| 536 | __import__(TESTFN) |
| 537 | pyc_file = imp.cache_from_source(self.source) |
| 538 | self.assertTrue(os.path.exists(pyc_file)) |
| 539 | os.remove(self.source) |
| 540 | forget(TESTFN) |
| 541 | self.assertRaises(ImportError, __import__, TESTFN) |
| 542 | |
| 543 | def test_missing_source_legacy(self): |
| 544 | # Like test_missing_source() except that for backward compatibility, |
| 545 | # when the pyc file lives where the py file would have been (and named |
| 546 | # without the tag), it is importable. The __file__ of the imported |
| 547 | # module is the pyc location. |
| 548 | __import__(TESTFN) |
| 549 | # pyc_file gets removed in _clean() via tearDown(). |
| 550 | pyc_file = make_legacy_pyc(self.source) |
| 551 | os.remove(self.source) |
| 552 | unload(TESTFN) |
| 553 | m = __import__(TESTFN) |
| 554 | self.assertEqual(m.__file__, |
| 555 | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
| 556 | |
| 557 | def test___cached__(self): |
| 558 | # Modules now also have an __cached__ that points to the pyc file. |
| 559 | m = __import__(TESTFN) |
| 560 | pyc_file = imp.cache_from_source(TESTFN + '.py') |
| 561 | self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file)) |
| 562 | |
| 563 | def test___cached___legacy_pyc(self): |
| 564 | # Like test___cached__() except that for backward compatibility, |
| 565 | # when the pyc file lives where the py file would have been (and named |
| 566 | # without the tag), it is importable. The __cached__ of the imported |
| 567 | # module is the pyc location. |
| 568 | __import__(TESTFN) |
| 569 | # pyc_file gets removed in _clean() via tearDown(). |
| 570 | pyc_file = make_legacy_pyc(self.source) |
| 571 | os.remove(self.source) |
| 572 | unload(TESTFN) |
| 573 | m = __import__(TESTFN) |
| 574 | self.assertEqual(m.__cached__, |
| 575 | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
| 576 | |
| 577 | def test_package___cached__(self): |
| 578 | # Like test___cached__ but for packages. |
| 579 | def cleanup(): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 580 | rmtree('pep3147') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 581 | os.mkdir('pep3147') |
| 582 | self.addCleanup(cleanup) |
| 583 | # Touch the __init__.py |
| 584 | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
| 585 | pass |
| 586 | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
| 587 | pass |
| 588 | unload('pep3147.foo') |
| 589 | unload('pep3147') |
| 590 | m = __import__('pep3147.foo') |
| 591 | init_pyc = imp.cache_from_source( |
| 592 | os.path.join('pep3147', '__init__.py')) |
| 593 | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
| 594 | foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py')) |
| 595 | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
| 596 | os.path.join(os.curdir, foo_pyc)) |
| 597 | |
| 598 | def test_package___cached___from_pyc(self): |
| 599 | # Like test___cached__ but ensuring __cached__ when imported from a |
| 600 | # PEP 3147 pyc file. |
| 601 | def cleanup(): |
Florent Xicluna | 27354cc | 2010-08-16 18:41:19 +0000 | [diff] [blame] | 602 | rmtree('pep3147') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 603 | os.mkdir('pep3147') |
| 604 | self.addCleanup(cleanup) |
| 605 | unload('pep3147.foo') |
| 606 | unload('pep3147') |
| 607 | # Touch the __init__.py |
| 608 | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
| 609 | pass |
| 610 | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
| 611 | pass |
| 612 | m = __import__('pep3147.foo') |
| 613 | unload('pep3147.foo') |
| 614 | unload('pep3147') |
| 615 | m = __import__('pep3147.foo') |
| 616 | init_pyc = imp.cache_from_source( |
| 617 | os.path.join('pep3147', '__init__.py')) |
| 618 | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
| 619 | foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py')) |
| 620 | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
| 621 | os.path.join(os.curdir, foo_pyc)) |
| 622 | |
| 623 | |
Brett Cannon | 42e54b2 | 2010-07-23 14:45:19 +0000 | [diff] [blame] | 624 | class RelativeImportFromImportlibTests(test_relative_imports.RelativeImports): |
Brett Cannon | 1f27479 | 2010-07-23 14:03:16 +0000 | [diff] [blame] | 625 | |
| 626 | def setUp(self): |
| 627 | self._importlib_util_flag = importlib_util.using___import__ |
| 628 | importlib_util.using___import__ = True |
| 629 | |
| 630 | def tearDown(self): |
| 631 | importlib_util.using___import__ = self._importlib_util_flag |
| 632 | |
| 633 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 634 | def test_main(verbose=None): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 635 | run_unittest(ImportTests, PycacheTests, |
| 636 | PycRewritingTests, PathsTests, RelativeImportTests, |
Brett Cannon | 1f27479 | 2010-07-23 14:03:16 +0000 | [diff] [blame] | 637 | OverridingImportBuiltinTests, |
Brett Cannon | 42e54b2 | 2010-07-23 14:45:19 +0000 | [diff] [blame] | 638 | RelativeImportFromImportlibTests) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 639 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 640 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 641 | if __name__ == '__main__': |
Collin Winter | 88e333d | 2010-03-17 03:09:21 +0000 | [diff] [blame] | 642 | # Test needs to be a package, so we can do relative imports. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 643 | from test.test_import import test_main |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 644 | test_main() |