MCPcopy Create free account
hub / github.com/ipython/ipython / Win32ShellCommandController

Class Win32ShellCommandController

IPython/utils/_process_win32_controller.py:189–543  ·  view source on GitHub ↗

Runs a shell command in a 'with' context. This implementation is Win32-specific. Example: # Runs the command interactively with default console stdin/stdout with ShellCommandController('python -i') as scc: scc.run() # Runs the command using the provided

Source from the content-addressed store, hash-verified

187
188
189class Win32ShellCommandController(object):
190 """Runs a shell command in a 'with' context.
191
192 This implementation is Win32-specific.
193
194 Example:
195 # Runs the command interactively with default console stdin/stdout
196 with ShellCommandController('python -i') as scc:
197 scc.run()
198
199 # Runs the command using the provided functions for stdin/stdout
200 def my_stdout_func(s):
201 # print or save the string 's'
202 write_to_stdout(s)
203 def my_stdin_func():
204 # If input is available, return it as a string.
205 if input_available():
206 return get_input()
207 # If no input available, return None after a short delay to
208 # keep from blocking.
209 else:
210 time.sleep(0.01)
211 return None
212
213 with ShellCommandController('python -i') as scc:
214 scc.run(my_stdout_func, my_stdin_func)
215 """
216
217 def __init__(self, cmd, mergeout = True):
218 """Initializes the shell command controller.
219
220 The cmd is the program to execute, and mergeout is
221 whether to blend stdout and stderr into one output
222 in stdout. Merging them together in this fashion more
223 reliably keeps stdout and stderr in the correct order
224 especially for interactive shell usage.
225 """
226 self.cmd = cmd
227 self.mergeout = mergeout
228
229 def __enter__(self):
230 cmd = self.cmd
231 mergeout = self.mergeout
232
233 self.hstdout, self.hstdin, self.hstderr = None, None, None
234 self.piProcInfo = None
235 try:
236 p_hstdout, c_hstdout, p_hstderr, \
237 c_hstderr, p_hstdin, c_hstdin = [None]*6
238
239 # SECURITY_ATTRIBUTES with inherit handle set to True
240 saAttr = SECURITY_ATTRIBUTES()
241 saAttr.nLength = ctypes.sizeof(saAttr)
242 saAttr.bInheritHandle = True
243 saAttr.lpSecurityDescriptor = None
244
245 def create_pipe(uninherit):
246 """Creates a Windows pipe, which consists of two handles.

Callers 1

systemFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected