text
stringlengths 0
267k
|
---|
"__name__": mod_name, |
"__file__": mod_fname, |
"__package__": mod_name.rpartition(".")[0], |
}) |
if alter_sys: |
expected_ns.update({ |
"run_argv0": mod_fname, |
"run_name_in_sys_modules": True, |
"module_in_sys_modules": True, |
}) |
def create_ns(init_globals): |
return run_module(mod_name, init_globals, alter_sys=alter_sys) |
try: |
if verbose > 1: print("Running from source:", mod_name) |
self.check_code_execution(create_ns, expected_ns) |
__import__(mod_name) |
os.remove(mod_fname) |
if not sys.dont_write_bytecode: |
make_legacy_pyc(mod_fname) |
unload(mod_name) # In case loader caches paths |
if verbose > 1: print("Running from compiled:", mod_name) |
self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) |
self.check_code_execution(create_ns, expected_ns) |
finally: |
self._del_pkg(pkg_dir, depth, mod_name) |
if verbose > 1: print("Module executed successfully") |
def _check_package(self, depth, alter_sys=False): |
pkg_dir, mod_fname, mod_name = ( |
self._make_pkg(example_source, depth, "__main__")) |
pkg_name = mod_name.rpartition(".")[0] |
forget(mod_name) |
expected_ns = example_namespace.copy() |
expected_ns.update({ |
"__name__": mod_name, |
"__file__": mod_fname, |
"__package__": pkg_name, |
}) |
if alter_sys: |
expected_ns.update({ |
"run_argv0": mod_fname, |
"run_name_in_sys_modules": True, |
"module_in_sys_modules": True, |
}) |
def create_ns(init_globals): |
return run_module(pkg_name, init_globals, alter_sys=alter_sys) |
try: |
if verbose > 1: print("Running from source:", pkg_name) |
self.check_code_execution(create_ns, expected_ns) |
__import__(mod_name) |
os.remove(mod_fname) |
if not sys.dont_write_bytecode: |
make_legacy_pyc(mod_fname) |
unload(mod_name) # In case loader caches paths |
if verbose > 1: print("Running from compiled:", pkg_name) |
self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) |
self.check_code_execution(create_ns, expected_ns) |
finally: |
self._del_pkg(pkg_dir, depth, pkg_name) |
if verbose > 1: print("Package executed successfully") |
def _add_relative_modules(self, base_dir, source, depth): |
if depth <= 1: |
raise ValueError("Relative module test needs depth > 1") |
pkg_name = "__runpy_pkg__" |
module_dir = base_dir |
for i in range(depth): |
parent_dir = module_dir |
module_dir = os.path.join(module_dir, pkg_name) |
# Add sibling module |
sibling_fname = os.path.join(module_dir, "sibling.py") |
sibling_file = open(sibling_fname, "w") |
sibling_file.close() |
if verbose > 1: print(" Added sibling module:", sibling_fname) |
# Add nephew module |
uncle_dir = os.path.join(parent_dir, "uncle") |
self._add_pkg_dir(uncle_dir) |
if verbose > 1: print(" Added uncle package:", uncle_dir) |
cousin_dir = os.path.join(uncle_dir, "cousin") |
self._add_pkg_dir(cousin_dir) |
if verbose > 1: print(" Added cousin package:", cousin_dir) |
nephew_fname = os.path.join(cousin_dir, "nephew.py") |
nephew_file = open(nephew_fname, "w") |
nephew_file.close() |
if verbose > 1: print(" Added nephew module:", nephew_fname) |
def _check_relative_imports(self, depth, run_name=None): |
contents = r"""\ |
from __future__ import absolute_import |
from . import sibling |
from ..uncle.cousin import nephew |
""" |
pkg_dir, mod_fname, mod_name = ( |
self._make_pkg(contents, depth)) |
if run_name is None: |
expected_name = mod_name |
else: |
expected_name = run_name |
try: |
self._add_relative_modules(pkg_dir, contents, depth) |