Python runpy 模块,_run_module_code() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用runpy._run_module_code()

项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_run_module_code(self):
        mod_name = "<Nonsense>"
        mod_fname = "Some other nonsense"
        mod_loader = "Now you're just being silly"
        mod_package = '' # Treat as a top level module
        expected_ns = example_namespace.copy()
        expected_ns.update({
            "__name__": mod_name,
            "__file__": mod_fname,
            "__loader__": mod_loader,
            "__package__": mod_package,
            "run_argv0": mod_fname,
            "run_name_in_sys_modules": True,
            "module_in_sys_modules": True,
        })
        def create_ns(init_globals):
            return _run_module_code(example_source,
                                    init_globals,
                                    mod_name,
                                    mod_fname,
                                    mod_loader,
                                    mod_package)
        self.check_code_execution(create_ns, expected_ns)

# TODO: Use self.addCleanup to get rid of a lot of try-finally blocks
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_run_module_code(self):
        initial = object()
        name = "<Nonsense>"
        file = "Some other nonsense"
        loader = "Now you're just being silly"
        package = '' # Treat as a top level module
        d1 = dict(initial=initial)
        saved_argv0 = sys.argv[0]
        d2 = _run_module_code(self.test_source,
                              d1,
                              name,
                              file,
                              loader,
                              package)
        self.assertNotIn("result", d1)
        self.assertIs(d2["initial"], initial)
        self.assertEqual(d2["result"], self.expected_result)
        self.assertEqual(d2["nested"]["x"], 1)
        self.assertIs(d2["__name__"], name)
        self.assertTrue(d2["run_name_in_sys_modules"])
        self.assertTrue(d2["module_in_sys_modules"])
        self.assertIs(d2["__file__"], file)
        self.assertIs(d2["__cached__"], None)
        self.assertIs(d2["run_argv0"], file)
        self.assertIs(d2["__loader__"], loader)
        self.assertIs(d2["__package__"], package)
        self.assertIs(sys.argv[0], saved_argv0)
        self.assertNotIn(name, sys.modules)
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_run_module_code(self):
        initial = object()
        name = "<Nonsense>"
        file = "Some other nonsense"
        loader = "Now you're just being silly"
        package = '' # Treat as a top level module
        d1 = dict(initial=initial)
        saved_argv0 = sys.argv[0]
        d2 = _run_module_code(self.test_source,
                              d1,
                              name,
                              file,
                              loader,
                              package)
        self.assertNotIn("result", d1)
        self.assertIs(d2["initial"], initial)
        self.assertEqual(d2["result"], self.expected_result)
        self.assertEqual(d2["nested"]["x"], 1)
        self.assertIs(d2["__name__"], name)
        self.assertTrue(d2["run_name_in_sys_modules"])
        self.assertTrue(d2["module_in_sys_modules"])
        self.assertIs(d2["__file__"], file)
        self.assertIs(d2["run_argv0"], file)
        self.assertIs(d2["__loader__"], loader)
        self.assertIs(d2["__package__"], package)
        self.assertIs(sys.argv[0], saved_argv0)
        self.assertNotIn(name, sys.modules)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_run_module_code(self):
        initial = object()
        name = "<Nonsense>"
        file = "Some other nonsense"
        loader = "Now you're just being silly"
        package = '' # Treat as a top level module
        d1 = dict(initial=initial)
        saved_argv0 = sys.argv[0]
        d2 = _run_module_code(self.test_source,
                              d1,
                              name,
                              file,
                              loader,
                              package)
        self.assertNotIn("result", d1)
        self.assertIs(d2["initial"], initial)
        self.assertEqual(d2["result"], self.expected_result)
        self.assertEqual(d2["nested"]["x"], 1)
        self.assertIs(d2["__name__"], name)
        self.assertTrue(d2["run_name_in_sys_modules"])
        self.assertTrue(d2["module_in_sys_modules"])
        self.assertIs(d2["__file__"], file)
        self.assertIs(d2["run_argv0"], file)
        self.assertIs(d2["__loader__"], loader)
        self.assertIs(d2["__package__"], package)
        self.assertIs(sys.argv[0], saved_argv0)
        self.assertNotIn(name, sys.modules)
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def test_run_module_code(self):
        initial = object()
        name = "<Nonsense>"
        file = "Some other nonsense"
        loader = "Now you're just being silly"
        package = '' # Treat as a top level module
        d1 = dict(initial=initial)
        saved_argv0 = sys.argv[0]
        d2 = _run_module_code(self.test_source,
                              d1,
                              name,
                              file,
                              loader,
                              package)
        self.assertNotIn("result", d1)
        self.assertIs(d2["initial"], initial)
        self.assertEqual(d2["result"], self.expected_result)
        self.assertEqual(d2["nested"]["x"], 1)
        self.assertIs(d2["__name__"], name)
        self.assertTrue(d2["run_name_in_sys_modules"])
        self.assertTrue(d2["module_in_sys_modules"])
        self.assertIs(d2["__file__"], file)
        self.assertIs(d2["run_argv0"], file)
        self.assertIs(d2["__loader__"], loader)
        self.assertIs(d2["__package__"], package)
        self.assertIs(sys.argv[0], saved_argv0)
        self.assertNotIn(name, sys.modules)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_run_module_code(self):
        mod_name = "<Nonsense>"
        mod_fname = "Some other nonsense"
        mod_loader = "Now you're just being silly"
        mod_package = '' # Treat as a top level module
        mod_spec = importlib.machinery.ModuleSpec(mod_name,
                                                  origin=mod_fname,
                                                  loader=mod_loader)
        expected_ns = example_namespace.copy()
        expected_ns.update({
            "__name__": mod_name,
            "__file__": mod_fname,
            "__loader__": mod_loader,
            "__package__": mod_package,
            "__spec__": mod_spec,
            "run_argv0": mod_fname,
            "run_name_in_sys_modules": True,
            "module_in_sys_modules": True,
        })
        def create_ns(init_globals):
            return _run_module_code(example_source,
                                    init_globals,
                                    mod_name,
                                    mod_spec)
        self.check_code_execution(create_ns, expected_ns)

# TODO: Use self.addCleanup to get rid of a lot of try-finally blocks
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_run_module_code(self):
        initial = object()
        name = "<Nonsense>"
        file = "Some other nonsense"
        loader = "Now you're just being silly"
        package = '' # Treat as a top level module
        d1 = dict(initial=initial)
        saved_argv0 = sys.argv[0]
        d2 = _run_module_code(self.test_source,
                              d1,
                              name,
                              file,
                              loader,
                              package)
        self.assertNotIn("result", d1)
        self.assertIs(d2["initial"], initial)
        self.assertEqual(d2["result"], self.expected_result)
        self.assertEqual(d2["nested"]["x"], 1)
        self.assertIs(d2["__name__"], name)
        self.assertTrue(d2["run_name_in_sys_modules"])
        self.assertTrue(d2["module_in_sys_modules"])
        self.assertIs(d2["__file__"], file)
        self.assertIs(d2["run_argv0"], file)
        self.assertIs(d2["__loader__"], loader)
        self.assertIs(d2["__package__"], package)
        self.assertIs(sys.argv[0], saved_argv0)
        self.assertNotIn(name, sys.modules)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_run_module_code(self):
        mod_name = "<Nonsense>"
        mod_fname = "Some other nonsense"
        mod_loader = "Now you're just being silly"
        mod_package = '' # Treat as a top level module
        mod_spec = importlib.machinery.ModuleSpec(mod_name,
                                                  origin=mod_fname,
                                                  loader=mod_loader)
        expected_ns = example_namespace.copy()
        expected_ns.update({
            "__name__": mod_name,
            "__file__": mod_fname,
            "__loader__": mod_loader,
            "__package__": mod_package,
            "__spec__": mod_spec,
            "run_argv0": mod_fname,
            "run_name_in_sys_modules": True,
            "module_in_sys_modules": True,
        })
        def create_ns(init_globals):
            return _run_module_code(example_source,
                                    init_globals,
                                    mod_name,
                                    mod_spec)
        self.check_code_execution(create_ns, expected_ns)

# TODO: Use self.addCleanup to get rid of a lot of try-finally blocks