MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _copy

Function _copy

tools/python-3.11.9-amd64/Lib/pty.py:128–191  ·  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

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

Callers 1

spawnFunction · 0.70

Calls 3

selectFunction · 0.85
appendMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected