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

Method _check_relative_imports

Lib/test/test_runpy.py:402–440  ·  view source on GitHub ↗
(self, depth, run_name=None)

Source from the content-addressed store, hash-verified

400 if verbose > 1: print(" Added nephew module:", nephew_fname)
401
402 def _check_relative_imports(self, depth, run_name=None):
403 contents = r"""\
404from __future__ import absolute_import
405from . import sibling
406from ..uncle.cousin import nephew
407"""
408 pkg_dir, mod_fname, mod_name, mod_spec = (
409 self._make_pkg(contents, depth))
410 if run_name is None:
411 expected_name = mod_name
412 else:
413 expected_name = run_name
414 try:
415 self._add_relative_modules(pkg_dir, contents, depth)
416 pkg_name = mod_name.rpartition('.')[0]
417 if verbose > 1: print("Running from source:", mod_name)
418 d1 = run_module(mod_name, run_name=run_name) # Read from source
419 self.assertEqual(d1["__name__"], expected_name)
420 self.assertEqual(d1["__package__"], pkg_name)
421 self.assertIn("sibling", d1)
422 self.assertIn("nephew", d1)
423 del d1 # Ensure __loader__ entry doesn't keep file open
424 importlib.invalidate_caches()
425 __import__(mod_name)
426 os.remove(mod_fname)
427 if not sys.dont_write_bytecode and sys.implementation.cache_tag:
428 make_legacy_pyc(mod_fname)
429 unload(mod_name) # In case the loader caches paths
430 if verbose > 1: print("Running from compiled:", mod_name)
431 importlib.invalidate_caches()
432 d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
433 self.assertEqual(d2["__name__"], expected_name)
434 self.assertEqual(d2["__package__"], pkg_name)
435 self.assertIn("sibling", d2)
436 self.assertIn("nephew", d2)
437 del d2 # Ensure __loader__ entry doesn't keep file open
438 finally:
439 self._del_pkg(pkg_dir)
440 if verbose > 1: print("Module executed successfully")
441
442 def test_run_module(self):
443 for depth in range(4):

Callers 2

Calls 12

_make_pkgMethod · 0.95
_add_relative_modulesMethod · 0.95
_del_pkgMethod · 0.95
run_moduleFunction · 0.90
make_legacy_pycFunction · 0.90
unloadFunction · 0.90
__import__Function · 0.85
assertInMethod · 0.80
rpartitionMethod · 0.45
assertEqualMethod · 0.45
invalidate_cachesMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected