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

Method _target_process

Lib/test/test_external_inspection.py:2708–2739  ·  view source on GitHub ↗

Context manager for running a target process with socket sync.

(self, script_body)

Source from the content-addressed store, hash-verified

2706
2707 @contextmanager
2708 def _target_process(self, script_body):
2709 """Context manager for running a target process with socket sync."""
2710 port = find_unused_port()
2711 script = f"""\
2712import socket
2713sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2714sock.connect(('localhost', {port}))
2715{textwrap.dedent(script_body)}
2716"""
2717
2718 with os_helper.temp_dir() as work_dir:
2719 script_dir = os.path.join(work_dir, "script_pkg")
2720 os.mkdir(script_dir)
2721
2722 server_socket = _create_server_socket(port)
2723 script_name = _make_test_script(script_dir, "script", script)
2724 client_socket = None
2725
2726 try:
2727 with _managed_subprocess([sys.executable, script_name]) as p:
2728 client_socket, _ = server_socket.accept()
2729 server_socket.close()
2730 server_socket = None
2731
2732 def make_unwinder(cache_frames=True):
2733 return RemoteUnwinder(
2734 p.pid, all_threads=True, cache_frames=cache_frames
2735 )
2736
2737 yield p, client_socket, make_unwinder
2738 finally:
2739 _cleanup_sockets(client_socket, server_socket)
2740
2741 def _get_frames_with_retry(self, unwinder, required_funcs):
2742 """Get frames containing required_funcs, with retry for transient errors."""

Calls 11

find_unused_portFunction · 0.90
_create_server_socketFunction · 0.85
_managed_subprocessFunction · 0.85
temp_dirMethod · 0.80
_make_test_scriptFunction · 0.70
_cleanup_socketsFunction · 0.70
dedentMethod · 0.45
joinMethod · 0.45
mkdirMethod · 0.45
acceptMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected