##// END OF EJS Templates
Merge pull request #13213 from Kojoley/fix-bunch-of-doctests...
Merge pull request #13213 from Kojoley/fix-bunch-of-doctests Fix bunch of doctests

File last commit:

r26419:7663c521
r26940:32497c8d merge
Show More
_process_win32.py
205 lines | 6.6 KiB | text/x-python | PythonLexer
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 """Windows-specific implementation of process utilities.
This file is only meant to be imported by process.py, not by end-users.
"""
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2010-2011 The IPython Development Team
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# stdlib
import os
import sys
Jörgen Stenarson
Moved arg_split to _process_win32.py and _process_posix.py.
r5517 import ctypes
Itamar Turner-Trauring
A different approach, avoiding wait() altogether.
r25504 import time
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
Jörgen Stenarson
Moved arg_split to _process_win32.py and _process_posix.py.
r5517 from ctypes import c_int, POINTER
from ctypes.wintypes import LPCWSTR, HLOCAL
Itamar Turner-Trauring
Attempt to make Popen.wait() interruptible.
r25503 from subprocess import STDOUT, TimeoutExpired
Itamar Turner-Trauring
Try to avoid blocking in read().
r25513 from threading import Thread
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
# our own imports
MinRK
add strict kwarg to win32 arg_split...
r5713 from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
Jörgen Stenarson
Moved arg_split to _process_win32.py and _process_posix.py.
r5517 from . import py3compat
Brandon Parsons
saner default encoding mechanism
r6716 from .encoding import DEFAULT_ENCODING
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
#-----------------------------------------------------------------------------
# Function definitions
#-----------------------------------------------------------------------------
class AvoidUNCPath(object):
"""A context manager to protect command execution from UNC paths.
In the Win32 API, commands can't be invoked with the cwd being a UNC path.
This context manager temporarily changes directory to the 'C:' drive on
entering, and restores the original working directory on exit.
The context manager returns the starting working directory *if* it made a
change and None otherwise, so that users can apply the necessary adjustment
to their system calls in the event of a change.
Thomas Kluyver
Improvements to docs formatting.
r12553 Examples
--------
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 ::
cmd = 'dir'
with AvoidUNCPath() as path:
if path is not None:
cmd = '"pushd %s &&"%s' % (path, cmd)
os.system(cmd)
"""
def __enter__(self):
Srinivas Reddy Thatiparthy
rename py3compat.getcwd() -> os.getcwd()
r23045 self.path = os.getcwd()
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 self.is_unc_path = self.path.startswith(r"\\")
if self.is_unc_path:
# change to c drive (as cmd.exe cannot handle UNC addresses)
os.chdir("C:")
Fernando Perez
Fix small bug in UNC path context manager.
r2909 return self.path
else:
# We return None to signal that there was no change in the working
# directory
return None
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
def __exit__(self, exc_type, exc_value, traceback):
if self.is_unc_path:
os.chdir(self.path)
def _find_cmd(cmd):
"""Find the full path to a .bat or .exe using the win32api module."""
try:
from win32api import SearchPath
Ram Rachum
Fix exception causes all over the codebase
r25833 except ImportError as e:
raise ImportError('you need to have pywin32 installed for this to work') from e
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 else:
PATH = os.environ['PATH']
extensions = ['.exe', '.com', '.bat', '.py']
path = None
for ext in extensions:
try:
Thomas Kluyver
Allow filename with extension in find_cmd in Windows....
r10678 path = SearchPath(PATH, cmd, ext)[0]
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 except:
pass
if path is None:
raise OSError("command %r not found" % cmd)
else:
return path
def _system_body(p):
"""Callback for _system."""
Brandon Parsons
saner default encoding mechanism
r6716 enc = DEFAULT_ENCODING
Itamar Turner-Trauring
Try to avoid blocking in read().
r25513
def stdout_read():
for line in read_no_interrupt(p.stdout).splitlines():
line = line.decode(enc, 'replace')
print(line, file=sys.stdout)
def stderr_read():
for line in read_no_interrupt(p.stderr).splitlines():
line = line.decode(enc, 'replace')
print(line, file=sys.stderr)
Thread(target=stdout_read).start()
Thread(target=stderr_read).start()
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
Itamar Turner-Trauring
Attempt to make Popen.wait() interruptible.
r25503 # Wait to finish for returncode. Unfortunately, Python has a bug where
# wait() isn't interruptible (https://bugs.python.org/issue28168) so poll in
# a loop instead of just doing `return p.wait()`.
while True:
Itamar Turner-Trauring
A different approach, avoiding wait() altogether.
r25504 result = p.poll()
if result is None:
time.sleep(0.01)
else:
return result
MinRK
store exit code in user_ns['_exit_code']...
r3910
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
def system(cmd):
"""Win32 version of os.system() that works with network shares.
Note that this implementation returns None, as meant for use in IPython.
Parameters
----------
Thomas Kluyver
Allow process_handler to accept a list of arguments
r13738 cmd : str or list
Matthias Bussonnier
reformat docstring in IPython utils
r26419 A command to be executed in the system shell.
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
Returns
-------
Itamar Turner-Trauring
Match pre-existing reality.
r25510 int : child process' exit code.
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 """
Mark Wiebe
Tweak out the process controller class so it works interactively better...
r6130 # The controller provides interactivity with both
# stdin and stdout
Jonathan March
Fixes gh-1632; minimal revert of gh-1424...
r7424 #import _process_win32_controller
#_process_win32_controller.system(cmd)
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
Jonathan March
Fixes gh-1632; minimal revert of gh-1424...
r7424 with AvoidUNCPath() as path:
if path is not None:
cmd = '"pushd %s &&"%s' % (path, cmd)
return process_handler(cmd, _system_body)
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
def getoutput(cmd):
"""Return standard output of executing cmd in a shell.
Accepts the same arguments as os.system().
Parameters
----------
Thomas Kluyver
Allow process_handler to accept a list of arguments
r13738 cmd : str or list
Matthias Bussonnier
reformat docstring in IPython utils
r26419 A command to be executed in the system shell.
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
Returns
-------
stdout : str
"""
with AvoidUNCPath() as path:
if path is not None:
cmd = '"pushd %s &&"%s' % (path, cmd)
out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT)
if out is None:
Jörgen Stenarson
Declare byte-type
r7518 out = b''
Hugo
Remove redundant Python 2 code
r24010 return py3compat.decode(out)
Jörgen Stenarson
Moved arg_split to _process_win32.py and _process_posix.py.
r5517
Jörgen Stenarson
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
r5533 try:
CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
Jörgen Stenarson
fixes #1708, failing test in arg_split on windows
r6728 CommandLineToArgvW.restype = POINTER(LPCWSTR)
Jörgen Stenarson
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
r5533 LocalFree = ctypes.windll.kernel32.LocalFree
LocalFree.res_type = HLOCAL
LocalFree.arg_types = [HLOCAL]
MinRK
add strict kwarg to win32 arg_split...
r5713 def arg_split(commandline, posix=False, strict=True):
Jörgen Stenarson
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
r5533 """Split a command line's arguments in a shell-like manner.
This is a special version for windows that use a ctypes call to CommandLineToArgvW
luzpaz
Misc. typo fixes...
r24084 to do the argv splitting. The posix parameter is ignored.
Matthias Bussonnier
reformat docstring in IPython utils
r26419
MinRK
add strict kwarg to win32 arg_split...
r5713 If strict=False, process_common.arg_split(...strict=False) is used instead.
Jörgen Stenarson
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
r5533 """
#CommandLineToArgvW returns path to executable if called with empty string.
if commandline.strip() == "":
return []
MinRK
add strict kwarg to win32 arg_split...
r5713 if not strict:
# not really a cl-arg, fallback on _process_common
return py_arg_split(commandline, posix=posix, strict=strict)
Jörgen Stenarson
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
r5533 argvn = c_int()
result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
result_array_type = LPCWSTR * argvn.value
Jörgen Stenarson
fixes #1708, failing test in arg_split on windows
r6728 result = [arg for arg in result_array_type.from_address(ctypes.addressof(result_pointer.contents))]
Jörgen Stenarson
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
r5533 retval = LocalFree(result_pointer)
return result
except AttributeError:
MinRK
add strict kwarg to win32 arg_split...
r5713 arg_split = py_arg_split
Thomas Kluyver
Check for pids when listing nbserver processes
r17181
def check_pid(pid):
# OpenProcess returns 0 if no such process (of ours) exists
# positive int otherwise
return bool(ctypes.windll.kernel32.OpenProcess(1,0,pid))