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