MCPcopy Index your code
hub / github.com/RustPython/RustPython / _run_remote_exec_test

Method _run_remote_exec_test

Lib/test/test_sys.py:1967–2061  ·  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

1965 test.support.reap_children()
1966
1967 def _run_remote_exec_test(self, script_code, python_args=None, env=None,
1968 prologue='',
1969 script_path=os_helper.TESTFN + '_remote.py'):
1970 # Create the script that will be remotely executed
1971 self.addCleanup(os_helper.unlink, script_path)
1972
1973 with open(script_path, 'w') as f:
1974 f.write(script_code)
1975
1976 # Create and run the target process
1977 target = os_helper.TESTFN + '_target.py'
1978 self.addCleanup(os_helper.unlink, target)
1979
1980 port = find_unused_port()
1981
1982 with open(target, 'w') as f:
1983 f.write(f'''
1984import sys
1985import time
1986import socket
1987
1988# Connect to the test process
1989sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1990sock.connect(('localhost', {port}))
1991
1992{prologue}
1993
1994# Signal that the process is ready
1995sock.sendall(b"ready")
1996
1997print("Target process running...")
1998
1999# Wait for remote script to be executed
2000# (the execution will happen as the following
2001# code is processed as soon as the recv call
2002# unblocks)
2003sock.recv(1024)
2004
2005# Do a bunch of work to give the remote script time to run
2006x = 0
2007for i in range(100):
2008 x += i
2009
2010# Write confirmation back
2011sock.sendall(b"executed")
2012sock.close()
2013''')
2014
2015 # Start the target process and capture its output
2016 cmd = [sys.executable]
2017 if python_args:
2018 cmd.extend(python_args)
2019 cmd.append(target)
2020
2021 # Create a socket server to communicate with the target process
2022 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2023 server_socket.bind(('localhost', port))
2024 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