(ext)
| 448 | |
| 449 | def test_import(self): |
| 450 | def test_with_extension(ext): |
| 451 | # The extension is normally ".py", perhaps ".pyw". |
| 452 | source = TESTFN + ext |
| 453 | pyc = TESTFN + ".pyc" |
| 454 | |
| 455 | with open(source, "w", encoding='utf-8') as f: |
| 456 | print("# This tests Python's ability to import a", |
| 457 | ext, "file.", file=f) |
| 458 | a = random.randrange(1000) |
| 459 | b = random.randrange(1000) |
| 460 | print("a =", a, file=f) |
| 461 | print("b =", b, file=f) |
| 462 | |
| 463 | if TESTFN in sys.modules: |
| 464 | del sys.modules[TESTFN] |
| 465 | importlib.invalidate_caches() |
| 466 | try: |
| 467 | try: |
| 468 | mod = __import__(TESTFN) |
| 469 | except ImportError as err: |
| 470 | self.fail("import from %s failed: %s" % (ext, err)) |
| 471 | |
| 472 | self.assertEqual(mod.a, a, |
| 473 | "module loaded (%s) but contents invalid" % mod) |
| 474 | self.assertEqual(mod.b, b, |
| 475 | "module loaded (%s) but contents invalid" % mod) |
| 476 | finally: |
| 477 | forget(TESTFN) |
| 478 | unlink(source) |
| 479 | unlink(pyc) |
| 480 | |
| 481 | sys.path.insert(0, os.curdir) |
| 482 | try: |
nothing calls this directly
no test coverage detected