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

Function _copy

Lib/pty.py:93–156  ·  view source on GitHub ↗

Parent copy loop. Copies pty master -> standard output (master_read) standard input -> pty master (stdin_read)

(master_fd, master_read=_read, stdin_read=_read)

Source from the content-addressed store, hash-verified

91 return os.read(fd, 1024)
92
93def _copy(master_fd, master_read=_read, stdin_read=_read):
94 """Parent copy loop.
95 Copies
96 pty master -> standard output (master_read)
97 standard input -> pty master (stdin_read)"""
98 if os.get_blocking(master_fd):
99 # If we write more than tty/ndisc is willing to buffer, we may block
100 # indefinitely. So we set master_fd to non-blocking temporarily during
101 # the copy operation.
102 os.set_blocking(master_fd, False)
103 try:
104 _copy(master_fd, master_read=master_read, stdin_read=stdin_read)
105 finally:
106 # restore blocking mode for backwards compatibility
107 os.set_blocking(master_fd, True)
108 return
109 high_waterlevel = 4096
110 stdin_avail = master_fd != STDIN_FILENO
111 stdout_avail = master_fd != STDOUT_FILENO
112 i_buf = b''
113 o_buf = b''
114 while 1:
115 rfds = []
116 wfds = []
117 if stdin_avail and len(i_buf) < high_waterlevel:
118 rfds.append(STDIN_FILENO)
119 if stdout_avail and len(o_buf) < high_waterlevel:
120 rfds.append(master_fd)
121 if stdout_avail and len(o_buf) > 0:
122 wfds.append(STDOUT_FILENO)
123 if len(i_buf) > 0:
124 wfds.append(master_fd)
125
126 rfds, wfds, _xfds = select(rfds, wfds, [])
127
128 if STDOUT_FILENO in wfds:
129 try:
130 n = os.write(STDOUT_FILENO, o_buf)
131 o_buf = o_buf[n:]
132 except OSError:
133 stdout_avail = False
134
135 if master_fd in rfds:
136 # Some OSes signal EOF by returning an empty byte string,
137 # some throw OSErrors.
138 try:
139 data = master_read(master_fd)
140 except OSError:
141 data = b""
142 if not data: # Reached EOF.
143 return # Assume the child process has exited and is
144 # unreachable, so we clean up.
145 o_buf += data
146
147 if master_fd in wfds:
148 n = os.write(master_fd, i_buf)
149 i_buf = i_buf[n:]
150

Callers 3

spawnFunction · 0.85
copyMethod · 0.85
_check_weakrefMethod · 0.85

Calls 3

selectFunction · 0.50
appendMethod · 0.45
writeMethod · 0.45

Tested by 1

_check_weakrefMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…