MCPcopy Index your code
hub / github.com/python/cpython / test_with_extension

Method test_with_extension

Lib/test/test_import/__init__.py:454–483  ·  view source on GitHub ↗
(ext)

Source from the content-addressed store, hash-verified

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:

Callers

nothing calls this directly

Calls 8

forgetFunction · 0.90
__import__Function · 0.85
unlinkFunction · 0.85
randrangeMethod · 0.80
openFunction · 0.50
invalidate_cachesMethod · 0.45
failMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected