|
9 | 9 | import time |
10 | 10 | import unittest |
11 | 11 | import unittest.mock |
| 12 | +import warnings |
12 | 13 |
|
13 | 14 | from test import support |
14 | 15 | from test.support import import_helper |
@@ -229,6 +230,81 @@ def testPy(self): |
229 | 230 | files = {TESTMOD + ".py": test_src} |
230 | 231 | self.doTest(".py", files, TESTMOD) |
231 | 232 |
|
| 233 | + def test_syntax_warning(self): |
| 234 | + files = {TESTMOD + ".py": "x = 1 is 1\n"} |
| 235 | + self.makeZip(files) |
| 236 | + zi = zipimport.zipimporter(TEMP_ZIP) |
| 237 | + |
| 238 | + with warnings.catch_warnings(record=True) as caught: |
| 239 | + warnings.simplefilter("always", SyntaxWarning) |
| 240 | + spec = zi.find_spec(TESTMOD) |
| 241 | + self.assertIsNotNone(spec) |
| 242 | + self.assertEqual(caught, []) |
| 243 | + |
| 244 | + mod = importlib.util.module_from_spec(spec) |
| 245 | + spec.loader.exec_module(mod) |
| 246 | + |
| 247 | + self.assertEqual(len(caught), 1) |
| 248 | + self.assertIsInstance(caught[0].message, SyntaxWarning) |
| 249 | + |
| 250 | + def test_syntax_error_defers_to_exec(self): |
| 251 | + files = {TESTMOD + ".py": "x =\n"} |
| 252 | + self.makeZip(files) |
| 253 | + zi = zipimport.zipimporter(TEMP_ZIP) |
| 254 | + expected_path = os.path.join(TEMP_ZIP, TESTMOD + ".py") |
| 255 | + |
| 256 | + spec = zi.find_spec(TESTMOD) |
| 257 | + self.assertIsNotNone(spec) |
| 258 | + self.assertEqual(spec.origin, expected_path) |
| 259 | + self.assertEqual(zi.get_filename(TESTMOD), expected_path) |
| 260 | + |
| 261 | + mod = importlib.util.module_from_spec(spec) |
| 262 | + with self.assertRaises(SyntaxError): |
| 263 | + spec.loader.exec_module(mod) |
| 264 | + |
| 265 | + def test_get_filename_no_warnings(self): |
| 266 | + files = {TESTMOD + ".py": "x = 1 is 1\n"} |
| 267 | + self.makeZip(files) |
| 268 | + zi = zipimport.zipimporter(TEMP_ZIP) |
| 269 | + |
| 270 | + with warnings.catch_warnings(record=True) as caught: |
| 271 | + warnings.simplefilter("always", SyntaxWarning) |
| 272 | + zi.get_filename(TESTMOD) |
| 273 | + |
| 274 | + self.assertEqual(caught, []) |
| 275 | + |
| 276 | + def test_syntax_error_in_source_with_valid_pyc(self): |
| 277 | + bad_src = "x =\n" |
| 278 | + files = {TESTMOD + ".py": bad_src, |
| 279 | + TESTMOD + pyc_ext: make_pyc(test_co, NOW, len(bad_src))} |
| 280 | + self.makeZip(files) |
| 281 | + zi = zipimport.zipimporter(TEMP_ZIP) |
| 282 | + expected_path = os.path.join(TEMP_ZIP, TESTMOD + pyc_ext) |
| 283 | + |
| 284 | + spec = zi.find_spec(TESTMOD) |
| 285 | + self.assertIsNotNone(spec) |
| 286 | + self.assertEqual(spec.origin, expected_path) |
| 287 | + |
| 288 | + mod = importlib.util.module_from_spec(spec) |
| 289 | + spec.loader.exec_module(mod) |
| 290 | + self.assertEqual(mod.get_file(), expected_path) |
| 291 | + |
| 292 | + def test_syntax_error_in_package_init_no_fallthrough(self): |
| 293 | + packdir = TESTPACK + os.sep |
| 294 | + files = {packdir + "__init__.py": "x =\n", |
| 295 | + TESTPACK + ".py": test_src} |
| 296 | + self.makeZip(files) |
| 297 | + zi = zipimport.zipimporter(TEMP_ZIP) |
| 298 | + expected_path = os.path.join(TEMP_ZIP, TESTPACK, "__init__.py") |
| 299 | + |
| 300 | + spec = zi.find_spec(TESTPACK) |
| 301 | + self.assertIsNotNone(spec) |
| 302 | + self.assertEqual(spec.origin, expected_path) |
| 303 | + |
| 304 | + mod = importlib.util.module_from_spec(spec) |
| 305 | + with self.assertRaises(SyntaxError): |
| 306 | + spec.loader.exec_module(mod) |
| 307 | + |
232 | 308 | def testPyc(self): |
233 | 309 | files = {TESTMOD + pyc_ext: test_pyc} |
234 | 310 | self.doTest(pyc_ext, files, TESTMOD) |
|
0 commit comments