Mercurial > ~astiob > upreckon > hgweb
comparison win32.py @ 95:6c2997616bdf
Added a search for the location of the executable for Popen on Win32
This _really_ should be done by the Win32 API.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Mon, 28 Feb 2011 21:36:57 +0000 |
parents | c62c9bfd614a |
children | 6bb59a011bcb |
comparison
equal
deleted
inserted
replaced
94:b7fb64ce03d9 | 95:6c2997616bdf |
---|---|
6 import testcases # mutual import | 6 import testcases # mutual import |
7 | 7 |
8 from ctypes import * | 8 from ctypes import * |
9 from ctypes.wintypes import * | 9 from ctypes.wintypes import * |
10 from msvcrt import getch as pause | 10 from msvcrt import getch as pause |
11 from subprocess import Popen | 11 import os, subprocess, sys |
12 import sys | 12 |
13 try: | |
14 from _winreg import * | |
15 except ImportError: | |
16 from winreg import * | |
13 | 17 |
14 try: | 18 try: |
15 from testcases import clock | 19 from testcases import clock |
16 except ImportError: | 20 except ImportError: |
17 from time import clock | 21 from time import clock |
42 kernel, user = (property(itemgetter(i)) for i in (0, 1)) | 46 kernel, user = (property(itemgetter(i)) for i in (0, 1)) |
43 else: | 47 else: |
44 ProcessTimes = namedtuple('ProcessTimes', 'kernel user') | 48 ProcessTimes = namedtuple('ProcessTimes', 'kernel user') |
45 | 49 |
46 __all__ = 'call', 'kill', 'terminate', 'pause', 'clock' | 50 __all__ = 'call', 'kill', 'terminate', 'pause', 'clock' |
51 | |
52 | |
53 from functools import wraps | |
54 pathext = [''] + os.environ['PATHEXT'].split(';') | |
55 @wraps(subprocess.Popen) | |
56 def Popen(cmdline, *args, **kwargs): | |
57 try: | |
58 return subprocess.Popen(cmdline, *args, **kwargs) | |
59 except WindowsError: | |
60 for ext in pathext: | |
61 path = cmdline[0] + ext | |
62 newcmdline = type(cmdline)((path,)) + cmdline[1:] | |
63 try: | |
64 return subprocess.Popen(newcmdline, *args, **kwargs) | |
65 except WindowsError: | |
66 pass | |
67 for branch in HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE: | |
68 try: | |
69 path = (R'SOFTWARE\Microsoft\Windows\CurrentVersion' | |
70 R'\App Paths\%s%s' % (cmdline[0], ext)) | |
71 path = QueryValue(branch, path) | |
72 break | |
73 except WindowsError: | |
74 pass | |
75 else: | |
76 continue | |
77 if path[0] == '"' == path[-1]: | |
78 path = path[1:-1] | |
79 newcmdline = type(cmdline)((path,)) + cmdline[1:] | |
80 try: | |
81 return subprocess.Popen(newcmdline, *args, **kwargs) | |
82 except WindowsError: | |
83 pass | |
84 # I'd like to transparently re-raise the exception generated | |
85 # on the very first try, but syntax differences preclude me from | |
86 # doing so in Python 2 and it can't be done at all in Python 3 | |
87 raise | |
47 | 88 |
48 | 89 |
49 # Automatically convert _subprocess handle objects into low-level HANDLEs | 90 # Automatically convert _subprocess handle objects into low-level HANDLEs |
50 # and replicate their functionality for our own use | 91 # and replicate their functionality for our own use |
51 try: | 92 try: |