• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python script_helper.spawn_python函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中test.script_helper.spawn_python函数的典型用法代码示例。如果您正苦于以下问题:Python spawn_python函数的具体用法?Python spawn_python怎么用?Python spawn_python使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了spawn_python函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: readpipe_interrupted

    def readpipe_interrupted(self, interrupt):
        """Perform a read during which a signal will arrive.  Return True if the
        read is interrupted by the signal and raises an exception.  Return False
        if it returns normally.
        """
        # use a subprocess to have only one thread, to have a timeout on the
        # blocking read and to not touch signal handling in this process
        code = """if 1:
            import errno
            import os
            import signal
            import sys

            interrupt = %r
            r, w = os.pipe()

            def handler(signum, frame):
                pass

            signal.signal(signal.SIGALRM, handler)
            if interrupt is not None:
                signal.siginterrupt(signal.SIGALRM, interrupt)

            print("ready")
            sys.stdout.flush()

            # run the test twice
            try:
                for loop in range(2):
                    # send a SIGALRM in a second (during the read)
                    signal.alarm(1)
                    try:
                        # blocking call: read from a pipe without data
                        os.read(r, 1)
                    except OSError as err:
                        if err.errno != errno.EINTR:
                            raise
                    else:
                        sys.exit(2)
                sys.exit(3)
            finally:
                os.close(r)
                os.close(w)
        """ % (interrupt,)
        with spawn_python('-c', code) as process:
            try:
                # wait until the child process is loaded and has started
                first_line = process.stdout.readline()

                stdout, stderr = process.communicate(timeout=5.0)
            except subprocess.TimeoutExpired:
                process.kill()
                return False
            else:
                stdout = first_line + stdout
                exitcode = process.wait()
                if exitcode not in (2, 3):
                    raise Exception("Child error (exit code %s): %r"
                                    % (exitcode, stdout))
                return (exitcode == 3)
开发者ID:kwatch,项目名称:cpython,代码行数:60,代码来源:test_signal.py


示例2: get_output

    def get_output(self, code, filename=None):
        """
        Run the specified code in Python (in a new child process) and read the
        output from the standard error or from a file (if filename is set).
        Return the output lines as a list.

        Strip the reference count from the standard error for Python debug
        build, and replace "Current thread 0x00007f8d8fbd9700" by "Current
        thread XXX".
        """
        options = {}
        if prepare_subprocess:
            options['preexec_fn'] = prepare_subprocess
        process = script_helper.spawn_python('-c', code, **options)
        stdout, stderr = process.communicate()
        exitcode = process.wait()
        output = support.strip_python_stderr(stdout)
        output = output.decode('ascii', 'backslashreplace')
        if filename:
            self.assertEqual(output, '')
            with open(filename, "rb") as fp:
                output = fp.read()
            output = output.decode('ascii', 'backslashreplace')
        output = re.sub('Current thread 0x[0-9a-f]+',
                        'Current thread XXX',
                        output)
        return output.splitlines(), exitcode
开发者ID:mikegraham,项目名称:cpython,代码行数:27,代码来源:test_faulthandler.py


示例3: get_output

    def get_output(self, code, filename=None, fd=None):
        """
        Run the specified code in Python (in a new child process) and read the
        output from the standard error or from a file (if filename is set).
        Return the output lines as a list.

        Strip the reference count from the standard error for Python debug
        build, and replace "Current thread 0x00007f8d8fbd9700" by "Current
        thread XXX".
        """
        code = dedent(code).strip()
        pass_fds = []
        if fd is not None:
            pass_fds.append(fd)
        with support.SuppressCrashReport():
            process = script_helper.spawn_python('-c', code, pass_fds=pass_fds)
        stdout, stderr = process.communicate()
        exitcode = process.wait()
        output = support.strip_python_stderr(stdout)
        output = output.decode('ascii', 'backslashreplace')
        if filename:
            self.assertEqual(output, '')
            with open(filename, "rb") as fp:
                output = fp.read()
            output = output.decode('ascii', 'backslashreplace')
        elif fd is not None:
            self.assertEqual(output, '')
            os.lseek(fd, os.SEEK_SET, 0)
            with open(fd, "rb", closefd=False) as fp:
                output = fp.read()
            output = output.decode('ascii', 'backslashreplace')
        output = re.sub('Current thread 0x[0-9a-f]+',
                        'Current thread XXX',
                        output)
        return output.splitlines(), exitcode
开发者ID:Cartmanfku,项目名称:cpython,代码行数:35,代码来源:test_faulthandler.py


示例4: test_unbuffered_input

 def test_unbuffered_input(self):
     # sys.stdin still works with '-u'
     code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
     p = spawn_python('-u', '-c', code)
     p.stdin.write(b'x')
     p.stdin.flush()
     data, rc = _kill_python_and_exit_code(p)
     self.assertEqual(rc, 0)
     self.assertTrue(data.startswith(b'x'), data)
开发者ID:7modelsan,项目名称:kbengine,代码行数:9,代码来源:test_cmd_line.py


示例5: interactive_python

 def interactive_python(self, separate_stderr=False):
     if separate_stderr:
         p = spawn_python('-i', bufsize=1, stderr=subprocess.PIPE)
         stderr = p.stderr
     else:
         p = spawn_python('-i', bufsize=1, stderr=subprocess.STDOUT)
         stderr = p.stdout
     try:
         # Drain stderr until prompt
         while True:
             data = stderr.read(4)
             if data == b">>> ":
                 break
             stderr.readline()
         yield p
     finally:
         kill_python(p)
         stderr.close()
开发者ID:5outh,项目名称:Databases-Fall2014,代码行数:18,代码来源:test_cmd_line_script.py


示例6: test_run_module_bug1764407

 def test_run_module_bug1764407(self):
     # -m and -i need to play well together
     # Runs the timeit module and checks the __main__
     # namespace has been populated appropriately
     p = spawn_python('-i', '-m', 'timeit', '-n', '1')
     p.stdin.write(b'Timer\n')
     p.stdin.write(b'exit()\n')
     data = kill_python(p)
     self.assertTrue(data.find(b'1 loop') != -1)
     self.assertTrue(data.find(b'__main__.Timer') != -1)
开发者ID:7modelsan,项目名称:kbengine,代码行数:10,代码来源:test_cmd_line.py


示例7: test_run_module_bug1764407

 def test_run_module_bug1764407(self):
     # -m and -i need to play well together
     # Runs the timeit module and checks the __main__
     # namespace has been populated appropriately
     p = spawn_python("-i", "-m", "timeit", "-n", "1")
     p.stdin.write("Timer\n")
     p.stdin.write("exit()\n")
     data = kill_python(p)
     self.assertTrue(data.startswith("1 loop"))
     self.assertIn("__main__.Timer", data)
开发者ID:Zekom,项目名称:pypyjs,代码行数:10,代码来源:test_cmd_line.py


示例8: test_include_on_stdin

 def test_include_on_stdin(self):
     f1 = script_helper.make_script(self.pkgdir, 'f1', '')
     f2 = script_helper.make_script(self.pkgdir, 'f2', '')
     f3 = script_helper.make_script(self.pkgdir, 'f3', '')
     f4 = script_helper.make_script(self.pkgdir, 'f4', '')
     p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-']))
     p.stdin.write((f3+os.linesep).encode('ascii'))
     script_helper.kill_python(p)
     self.assertNotCompiled(f1)
     self.assertNotCompiled(f2)
     self.assertCompiled(f3)
     self.assertNotCompiled(f4)
开发者ID:chidea,项目名称:GoPythonDLLWrapper,代码行数:12,代码来源:test_compileall.py


示例9: test_stdin_loader

 def test_stdin_loader(self):
     # Unfortunately, there's no way to automatically test the fully
     # interactive REPL, since that code path only gets executed when
     # stdin is an interactive tty.
     p = spawn_python()
     try:
         p.stdin.write(b"print(__loader__)\n")
         p.stdin.flush()
     finally:
         out = kill_python(p)
     expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
     self.assertIn(expected, out)
开发者ID:BeboPremo,项目名称:cpython,代码行数:12,代码来源:test_cmd_line_script.py


示例10: test_include_on_stdin

 def test_include_on_stdin(self):
     f1 = script_helper.make_script(self.pkgdir, "f1", "")
     f2 = script_helper.make_script(self.pkgdir, "f2", "")
     f3 = script_helper.make_script(self.pkgdir, "f3", "")
     f4 = script_helper.make_script(self.pkgdir, "f4", "")
     p = script_helper.spawn_python(*(self._get_run_args(()) + ["-i", "-"]))
     p.stdin.write((f3 + os.linesep).encode("ascii"))
     script_helper.kill_python(p)
     self.assertNotCompiled(f1)
     self.assertNotCompiled(f2)
     self.assertCompiled(f3)
     self.assertNotCompiled(f4)
开发者ID:Orav,项目名称:kbengine,代码行数:12,代码来源:test_compileall.py


示例11: test_pdb_issue4201

    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""\
                    def f():
                        pass

                    import pdb
                    pdb.Pdb(nosigint=True).runcall(f)
                    """)
        with temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            p = spawn_python(script_name)
            p.stdin.write(b'l\n')
            data = kill_python(p)
            # bdb/pdb applies normcase to its filename before displaying
            self.assertIn(os.path.normcase(script_name.encode('utf-8')), data)
            zip_name, run_name = make_zip_script(d, "test_zip",
                                                script_name, '__main__.py')
            p = spawn_python(zip_name)
            p.stdin.write(b'l\n')
            data = kill_python(p)
            # bdb/pdb applies normcase to its filename before displaying
            self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
开发者ID:0jpq0,项目名称:kbengine,代码行数:22,代码来源:test_zipimport_support.py


示例12: test_pdb_issue4201

    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""\
                    def f():
                        pass

                    import pdb
                    pdb.runcall(f)
                    """)

        with temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            p = spawn_python(script_name)
            p.stdin.write('l\n')
            data = kill_python(p)
            # Back-port from CPython 3 (see CPython Issue 14255).
            self.assertNormalisedIn(script_name, data)

            zip_name, run_name = make_zip_script(d, "test_zip",
                                                script_name, '__main__.py')
            p = spawn_python(zip_name)
            p.stdin.write('l\n')
            data = kill_python(p)
            # Back-port from CPython 3 (see CPython Issue 14255).
            self.assertNormalisedIn(run_name, data)
开发者ID:Stewori,项目名称:jython,代码行数:24,代码来源:test_zipimport_support.py


示例13: test_pthread_kill_main_thread

    def test_pthread_kill_main_thread(self):
        # Test that a signal can be sent to the main thread with pthread_kill()
        # before any other thread has been created (see issue #12392).
        code = """if True:
            import threading
            import signal
            import sys

            def handler(signum, frame):
                sys.exit(3)

            signal.signal(signal.SIGUSR1, handler)
            signal.pthread_kill(threading.get_ident(), signal.SIGUSR1)
            sys.exit(2)
        """

        with spawn_python("-c", code) as process:
            stdout, stderr = process.communicate()
            exitcode = process.wait()
            if exitcode != 3:
                raise Exception("Child error (exit code %s): %s" % (exitcode, stdout))
开发者ID:kimjeong,项目名称:FDA_Social_Media_Hbase_Python,代码行数:21,代码来源:test_signal.py


示例14: start_python

 def start_python(self, *args):
     p = spawn_python(*args)
     return kill_python(p)
开发者ID:dr4ke616,项目名称:custom_python,代码行数:3,代码来源:test_cmd_line.py


示例15: test_delete_builtins_import

 def test_delete_builtins_import(self):
     args = ["-c", "del __builtins__.__import__; import os"]
     popen = script_helper.spawn_python(*args)
     stdout, stderr = popen.communicate()
     self.assertIn(b"ImportError", stdout)
开发者ID:Stewori,项目名称:JyNIgate,代码行数:5,代码来源:__init__.py



注:本文中的test.script_helper.spawn_python函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python script_helper.temp_dir函数代码示例发布时间:2022-05-27
下一篇:
Python script_helper.make_zip_script函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap