popen_spawn_win32.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os
  2. import msvcrt
  3. import signal
  4. import sys
  5. import _winapi
  6. from .context import reduction, get_spawning_popen, set_spawning_popen
  7. from . import spawn
  8. from . import util
  9. __all__ = ['Popen']
  10. #
  11. #
  12. #
  13. TERMINATE = 0x10000
  14. WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
  15. WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
  16. def _path_eq(p1, p2):
  17. return p1 == p2 or os.path.normcase(p1) == os.path.normcase(p2)
  18. WINENV = (hasattr(sys, '_base_executable') and
  19. not _path_eq(sys.executable, sys._base_executable))
  20. def _close_handles(*handles):
  21. for handle in handles:
  22. _winapi.CloseHandle(handle)
  23. #
  24. # We define a Popen class similar to the one from subprocess, but
  25. # whose constructor takes a process object as its argument.
  26. #
  27. class Popen(object):
  28. '''
  29. Start a subprocess to run the code of a process object
  30. '''
  31. method = 'spawn'
  32. def __init__(self, process_obj):
  33. prep_data = spawn.get_preparation_data(process_obj._name)
  34. # read end of pipe will be "stolen" by the child process
  35. # -- see spawn_main() in spawn.py.
  36. rhandle, whandle = _winapi.CreatePipe(None, 0)
  37. wfd = msvcrt.open_osfhandle(whandle, 0)
  38. cmd = spawn.get_command_line(parent_pid=os.getpid(),
  39. pipe_handle=rhandle)
  40. cmd = ' '.join('"%s"' % x for x in cmd)
  41. python_exe = spawn.get_executable()
  42. # bpo-35797: When running in a venv, we bypass the redirect
  43. # executor and launch our base Python.
  44. if WINENV and _path_eq(python_exe, sys.executable):
  45. python_exe = sys._base_executable
  46. env = os.environ.copy()
  47. env["__PYVENV_LAUNCHER__"] = sys.executable
  48. else:
  49. env = None
  50. with open(wfd, 'wb', closefd=True) as to_child:
  51. # start process
  52. try:
  53. hp, ht, pid, tid = _winapi.CreateProcess(
  54. python_exe, cmd,
  55. None, None, False, 0, env, None, None)
  56. _winapi.CloseHandle(ht)
  57. except:
  58. _winapi.CloseHandle(rhandle)
  59. raise
  60. # set attributes of self
  61. self.pid = pid
  62. self.returncode = None
  63. self._handle = hp
  64. self.sentinel = int(hp)
  65. self.finalizer = util.Finalize(self, _winapi.CloseHandle, (self.sentinel,))
  66. # send information to child
  67. set_spawning_popen(self)
  68. try:
  69. reduction.dump(prep_data, to_child)
  70. reduction.dump(process_obj, to_child)
  71. finally:
  72. set_spawning_popen(None)
  73. def duplicate_for_child(self, handle):
  74. assert self is get_spawning_popen()
  75. return reduction.duplicate(handle, self.sentinel)
  76. def wait(self, timeout=None):
  77. if self.returncode is None:
  78. if timeout is None:
  79. msecs = _winapi.INFINITE
  80. else:
  81. msecs = max(0, int(timeout * 1000 + 0.5))
  82. res = _winapi.WaitForSingleObject(int(self._handle), msecs)
  83. if res == _winapi.WAIT_OBJECT_0:
  84. code = _winapi.GetExitCodeProcess(self._handle)
  85. if code == TERMINATE:
  86. code = -signal.SIGTERM
  87. self.returncode = code
  88. return self.returncode
  89. def poll(self):
  90. return self.wait(timeout=0)
  91. def terminate(self):
  92. if self.returncode is None:
  93. try:
  94. _winapi.TerminateProcess(int(self._handle), TERMINATE)
  95. except OSError:
  96. if self.wait(timeout=1.0) is None:
  97. raise
  98. kill = terminate
  99. def close(self):
  100. self.finalizer()