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

Function run_pty

Lib/test/support/pty_helper.py:13–68  ·  view source on GitHub ↗
(script, input=b"dummy input\r", env=None)

Source from the content-addressed store, hash-verified

11from test.support.import_helper import import_module
12
13def run_pty(script, input=b"dummy input\r", env=None):
14 pty = import_module('pty')
15 output = bytearray()
16 [master, slave] = pty.openpty()
17 args = (sys.executable, '-c', script)
18
19 # Isolate readline from personal init files by setting INPUTRC
20 # to an empty file. See also GH-142353.
21 if env is None:
22 env = {**os.environ.copy(), "INPUTRC": os.devnull}
23 else:
24 env.setdefault("INPUTRC", os.devnull)
25
26 proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave, env=env)
27 os.close(slave)
28 with ExitStack() as cleanup:
29 cleanup.enter_context(proc)
30 def terminate(proc):
31 try:
32 proc.terminate()
33 except ProcessLookupError:
34 # Workaround for Open/Net BSD bug (Issue 16762)
35 pass
36 cleanup.callback(terminate, proc)
37 cleanup.callback(os.close, master)
38 # Avoid using DefaultSelector and PollSelector. Kqueue() does not
39 # work with pseudo-terminals on OS X < 10.9 (Issue 20365) and Open
40 # BSD (Issue 20667). Poll() does not work with OS X 10.6 or 10.4
41 # either (Issue 20472). Hopefully the file descriptor is low enough
42 # to use with select().
43 sel = cleanup.enter_context(selectors.SelectSelector())
44 sel.register(master, selectors.EVENT_READ | selectors.EVENT_WRITE)
45 os.set_blocking(master, False)
46 while True:
47 for [_, events] in sel.select():
48 if events & selectors.EVENT_READ:
49 try:
50 chunk = os.read(master, 0x10000)
51 except OSError as err:
52 # Linux raises EIO when slave is closed (Issue 5380)
53 if err.errno != EIO:
54 raise
55 chunk = b""
56 if not chunk:
57 return output
58 output.extend(chunk)
59 if events & selectors.EVENT_WRITE:
60 try:
61 input = input[os.write(master, input):]
62 except OSError as err:
63 # Apparently EIO means the slave was closed
64 if err.errno != EIO:
65 raise
66 input = b"" # Stop writing
67 if not input:
68 sel.modify(master, selectors.EVENT_READ)
69
70

Calls 13

import_moduleFunction · 0.90
ExitStackClass · 0.90
enter_contextMethod · 0.80
copyMethod · 0.45
setdefaultMethod · 0.45
closeMethod · 0.45
callbackMethod · 0.45
registerMethod · 0.45
selectMethod · 0.45
readMethod · 0.45
extendMethod · 0.45
writeMethod · 0.45

Tested by 15

test_nonasciiMethod · 0.72
test_history_sizeMethod · 0.72
test_basic_completionMethod · 0.72
test_local_namespaceMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…