Mercurial > public > mercurial-scm > hg
comparison mercurial/windows.py @ 10237:2f7a38f336f4
serve: add and use portable spawnvp replacement
There is no standard python command to really detach a process under Windows.
Instead we use the low level API wrapped by subprocess module with all
necessary options to avoid any kind of context inheritance. Unfortunately, this
version still opens a new window for the child process.
The following have been tried:
- os.spawnv(os.P_NOWAIT): works but the child process is killed when parent
console terminates.
- os.spawnv(os.P_DETACH): works on python25, hang on python26 when writing to
the hgweb output socket.
- subprocess.CreateProcess() hack without shell mode: similar to
os.spawnv(os.P_DETACH).
Fix 1/3 for issue421
author | Patrick Mezard <pmezard@gmail.com> |
---|---|
date | Fri, 10 Apr 2009 21:20:25 +0200 |
parents | c31ac3f7fd8f |
children | 8e4be44a676f |
comparison
equal
deleted
inserted
replaced
10236:49a8625b8cac | 10237:2f7a38f336f4 |
---|---|
5 # This software may be used and distributed according to the terms of the | 5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2, incorporated herein by reference. | 6 # GNU General Public License version 2, incorporated herein by reference. |
7 | 7 |
8 from i18n import _ | 8 from i18n import _ |
9 import osutil, error | 9 import osutil, error |
10 import errno, msvcrt, os, re, sys, random | 10 import errno, msvcrt, os, re, sys, random, subprocess |
11 | 11 |
12 nulldev = 'NUL:' | 12 nulldev = 'NUL:' |
13 umask = 002 | 13 umask = 002 |
14 | 14 |
15 # wrap osutil.posixfile to provide friendlier exceptions | 15 # wrap osutil.posixfile to provide friendlier exceptions |
319 # aborting at this point may leave serious inconsistencies. | 319 # aborting at this point may leave serious inconsistencies. |
320 # Ideally, we would notify the user here. | 320 # Ideally, we would notify the user here. |
321 pass | 321 pass |
322 os.rename(src, dst) | 322 os.rename(src, dst) |
323 | 323 |
324 def spawndetached(args): | |
325 # No standard library function really spawns a fully detached | |
326 # process under win32 because they allocate pipes or other objects | |
327 # to handle standard streams communications. Passing these objects | |
328 # to the child process requires handle inheritance to be enabled | |
329 # which makes really detached processes impossible. | |
330 class STARTUPINFO: | |
331 dwFlags = subprocess.STARTF_USESHOWWINDOW | |
332 hStdInput = None | |
333 hStdOutput = None | |
334 hStdError = None | |
335 wShowWindow = subprocess.SW_HIDE | |
336 | |
337 args = subprocess.list2cmdline(args) | |
338 # Not running the command in shell mode makes python26 hang when | |
339 # writing to hgweb output socket. | |
340 comspec = os.environ.get("COMSPEC", "cmd.exe") | |
341 args = comspec + " /c " + args | |
342 hp, ht, pid, tid = subprocess.CreateProcess( | |
343 None, args, | |
344 # no special security | |
345 None, None, | |
346 # Do not inherit handles | |
347 0, | |
348 # DETACHED_PROCESS | |
349 0x00000008, | |
350 os.environ, | |
351 os.getcwd(), | |
352 STARTUPINFO()) | |
353 return pid | |
354 | |
324 try: | 355 try: |
325 # override functions with win32 versions if possible | 356 # override functions with win32 versions if possible |
326 from win32 import * | 357 from win32 import * |
327 except ImportError: | 358 except ImportError: |
328 pass | 359 pass |