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

Method _run_remote_exec_test

Lib/test/test_sys.py:1995–2089  ·  view source on GitHub ↗
(self, script_code, python_args=None, env=None,
                              prologue='',
                              script_path=os_helper.TESTFN + '_remote.py')

Source from the content-addressed store, hash-verified

1993 test.support.reap_children()
1994
1995 def _run_remote_exec_test(self, script_code, python_args=None, env=None,
1996 prologue='',
1997 script_path=os_helper.TESTFN + '_remote.py'):
1998 # Create the script that will be remotely executed
1999 self.addCleanup(os_helper.unlink, script_path)
2000
2001 with open(script_path, 'w') as f:
2002 f.write(script_code)
2003
2004 # Create and run the target process
2005 target = os_helper.TESTFN + '_target.py'
2006 self.addCleanup(os_helper.unlink, target)
2007
2008 port = find_unused_port()
2009
2010 with open(target, 'w') as f:
2011 f.write(f'''
2012import sys
2013import time
2014import socket
2015
2016# Connect to the test process
2017sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2018sock.connect(('localhost', {port}))
2019
2020{prologue}
2021
2022# Signal that the process is ready
2023sock.sendall(b"ready")
2024
2025print("Target process running...")
2026
2027# Wait for remote script to be executed
2028# (the execution will happen as the following
2029# code is processed as soon as the recv call
2030# unblocks)
2031sock.recv(1024)
2032
2033# Do a bunch of work to give the remote script time to run
2034x = 0
2035for i in range(100):
2036 x += i
2037
2038# Write confirmation back
2039sock.sendall(b"executed")
2040sock.close()
2041''')
2042
2043 # Start the target process and capture its output
2044 cmd = [sys.executable]
2045 if python_args:
2046 cmd.extend(python_args)
2047 cmd.append(target)
2048
2049 # Create a socket server to communicate with the target process
2050 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2051 server_socket.bind(('localhost', port))
2052 server_socket.settimeout(SHORT_TIMEOUT)

Calls 15

find_unused_portFunction · 0.90
addCleanupMethod · 0.80
socketMethod · 0.80
skipTestMethod · 0.80
openFunction · 0.50
writeMethod · 0.45
extendMethod · 0.45
appendMethod · 0.45
bindMethod · 0.45
settimeoutMethod · 0.45
listenMethod · 0.45
acceptMethod · 0.45

Tested by

no test coverage detected