MCPcopy Create free account
hub / github.com/RustPython/RustPython / _copy

Function _copy

Lib/pty.py:122–185  ·  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

120 return os.read(fd, 1024)
121
122def _copy(master_fd, master_read=_read, stdin_read=_read):
123 """Parent copy loop.
124 Copies
125 pty master -> standard output (master_read)
126 standard input -> pty master (stdin_read)"""
127 if os.get_blocking(master_fd):
128 # If we write more than tty/ndisc is willing to buffer, we may block
129 # indefinitely. So we set master_fd to non-blocking temporarily during
130 # the copy operation.
131 os.set_blocking(master_fd, False)
132 try:
133 _copy(master_fd, master_read=master_read, stdin_read=stdin_read)
134 finally:
135 # restore blocking mode for backwards compatibility
136 os.set_blocking(master_fd, True)
137 return
138 high_waterlevel = 4096
139 stdin_avail = master_fd != STDIN_FILENO
140 stdout_avail = master_fd != STDOUT_FILENO
141 i_buf = b''
142 o_buf = b''
143 while 1:
144 rfds = []
145 wfds = []
146 if stdin_avail and len(i_buf) < high_waterlevel:
147 rfds.append(STDIN_FILENO)
148 if stdout_avail and len(o_buf) < high_waterlevel:
149 rfds.append(master_fd)
150 if stdout_avail and len(o_buf) > 0:
151 wfds.append(STDOUT_FILENO)
152 if len(i_buf) > 0:
153 wfds.append(master_fd)
154
155 rfds, wfds, _xfds = select(rfds, wfds, [])
156
157 if STDOUT_FILENO in wfds:
158 try:
159 n = os.write(STDOUT_FILENO, o_buf)
160 o_buf = o_buf[n:]
161 except OSError:
162 stdout_avail = False
163
164 if master_fd in rfds:
165 # Some OSes signal EOF by returning an empty byte string,
166 # some throw OSErrors.
167 try:
168 data = master_read(master_fd)
169 except OSError:
170 data = b""
171 if not data: # Reached EOF.
172 return # Assume the child process has exited and is
173 # unreachable, so we clean up.
174 o_buf += data
175
176 if master_fd in wfds:
177 n = os.write(master_fd, i_buf)
178 i_buf = i_buf[n:]
179

Callers 2

spawnFunction · 0.85
_check_weakrefMethod · 0.85

Calls 4

selectFunction · 0.90
lenFunction · 0.85
appendMethod · 0.45
writeMethod · 0.45

Tested by 1

_check_weakrefMethod · 0.68