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

Method test_foreign_code

Lib/test/test_import/__init__.py:1463–1480  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1461 self.assertEqual(mod.func_filename, target)
1462
1463 def test_foreign_code(self):
1464 compiled_name = self.compiled_name or (self.file_name + 'c')
1465 py_compile.compile(self.file_name, compiled_name)
1466 with open(compiled_name, "rb") as f:
1467 header = f.read(16)
1468 code = marshal.load(f)
1469 constants = list(code.co_consts)
1470 foreign_code = importlib.import_module.__code__
1471 pos = constants.index(1000)
1472 constants[pos] = foreign_code
1473 code = code.replace(co_consts=tuple(constants))
1474 with open(compiled_name, "wb") as f:
1475 f.write(header)
1476 marshal.dump(code, f)
1477 if not self.compiled_name:
1478 os.remove(self.file_name)
1479 mod = self.import_module()
1480 self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
1481
1482
1483class PathsTests(unittest.TestCase):

Callers

nothing calls this directly

Calls 12

import_moduleMethod · 0.95
listClass · 0.85
openFunction · 0.50
compileMethod · 0.45
readMethod · 0.45
loadMethod · 0.45
indexMethod · 0.45
replaceMethod · 0.45
writeMethod · 0.45
dumpMethod · 0.45
removeMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected