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