]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/windows/fixups.py
Improved error handling and cosmetics for ctypes calls on Windows.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / windows / fixups.py
1
2 done = False
3
4 def initialize():
5     global done
6     import sys
7     if sys.platform != "win32" or done:
8         return True
9     done = True
10
11     import codecs, re
12     from ctypes import WINFUNCTYPE, WinError, windll, POINTER, byref, c_int, get_last_error
13     from ctypes.wintypes import BOOL, HANDLE, DWORD, UINT, LPWSTR, LPCWSTR, LPVOID
14
15     from allmydata.util import log
16     from allmydata.util.encodingutil import canonical_encoding
17
18     # <https://msdn.microsoft.com/en-us/library/ms680621%28VS.85%29.aspx>
19     SetErrorMode = WINFUNCTYPE(
20         UINT,  UINT,
21         use_last_error=True
22     )(("SetErrorMode", windll.kernel32))
23
24     SEM_FAILCRITICALERRORS = 0x0001
25     SEM_NOOPENFILEERRORBOX = 0x8000
26
27     SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX)
28
29     original_stderr = sys.stderr
30
31     # If any exception occurs in this code, we'll probably try to print it on stderr,
32     # which makes for frustrating debugging if stderr is directed to our wrapper.
33     # So be paranoid about catching errors and reporting them to original_stderr,
34     # so that we can at least see them.
35     def _complain(message):
36         print >>original_stderr, isinstance(message, str) and message or repr(message)
37         log.msg(message, level=log.WEIRD)
38
39     # Work around <http://bugs.python.org/issue6058>.
40     codecs.register(lambda name: name == 'cp65001' and codecs.lookup('utf-8') or None)
41
42     # Make Unicode console output work independently of the current code page.
43     # This also fixes <http://bugs.python.org/issue1602>.
44     # Credit to Michael Kaplan <https://blogs.msdn.com/b/michkap/archive/2010/04/07/9989346.aspx>
45     # and TZOmegaTZIOY
46     # <http://stackoverflow.com/questions/878972/windows-cmd-encoding-change-causes-python-crash/1432462#1432462>.
47     try:
48         # <https://msdn.microsoft.com/en-us/library/ms683231(VS.85).aspx>
49         # HANDLE WINAPI GetStdHandle(DWORD nStdHandle);
50         # returns INVALID_HANDLE_VALUE, NULL, or a valid handle
51         #
52         # <https://msdn.microsoft.com/en-us/library/aa364960(VS.85).aspx>
53         # DWORD WINAPI GetFileType(DWORD hFile);
54         #
55         # <https://msdn.microsoft.com/en-us/library/ms683167(VS.85).aspx>
56         # BOOL WINAPI GetConsoleMode(HANDLE hConsole, LPDWORD lpMode);
57
58         GetStdHandle = WINFUNCTYPE(
59             HANDLE,  DWORD,
60             use_last_error=True
61         )(("GetStdHandle", windll.kernel32))
62
63         STD_OUTPUT_HANDLE = DWORD(-11)
64         STD_ERROR_HANDLE  = DWORD(-12)
65
66         GetFileType = WINFUNCTYPE(
67             DWORD,  DWORD,
68             use_last_error=True
69         )(("GetFileType", windll.kernel32))
70
71         FILE_TYPE_CHAR   = 0x0002
72         FILE_TYPE_REMOTE = 0x8000
73
74         GetConsoleMode = WINFUNCTYPE(
75             BOOL,  HANDLE, POINTER(DWORD),
76             use_last_error=True
77         )(("GetConsoleMode", windll.kernel32))
78
79         INVALID_HANDLE_VALUE = DWORD(-1).value
80
81         def not_a_console(handle):
82             if handle == INVALID_HANDLE_VALUE or handle is None:
83                 return True
84             return ((GetFileType(handle) & ~FILE_TYPE_REMOTE) != FILE_TYPE_CHAR
85                     or GetConsoleMode(handle, byref(DWORD())) == 0)
86
87         old_stdout_fileno = None
88         old_stderr_fileno = None
89         if hasattr(sys.stdout, 'fileno'):
90             old_stdout_fileno = sys.stdout.fileno()
91         if hasattr(sys.stderr, 'fileno'):
92             old_stderr_fileno = sys.stderr.fileno()
93
94         STDOUT_FILENO = 1
95         STDERR_FILENO = 2
96         real_stdout = (old_stdout_fileno == STDOUT_FILENO)
97         real_stderr = (old_stderr_fileno == STDERR_FILENO)
98
99         if real_stdout:
100             hStdout = GetStdHandle(STD_OUTPUT_HANDLE)
101             if not_a_console(hStdout):
102                 real_stdout = False
103
104         if real_stderr:
105             hStderr = GetStdHandle(STD_ERROR_HANDLE)
106             if not_a_console(hStderr):
107                 real_stderr = False
108
109         if real_stdout or real_stderr:
110             # <https://msdn.microsoft.com/en-us/library/windows/desktop/ms687401%28v=vs.85%29.aspx>
111             # BOOL WINAPI WriteConsoleW(HANDLE hOutput, LPWSTR lpBuffer, DWORD nChars,
112             #                           LPDWORD lpCharsWritten, LPVOID lpReserved);
113
114             WriteConsoleW = WINFUNCTYPE(
115                 BOOL,  HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID,
116                 use_last_error=True
117             )(("WriteConsoleW", windll.kernel32))
118
119             class UnicodeOutput:
120                 def __init__(self, hConsole, stream, fileno, name):
121                     self._hConsole = hConsole
122                     self._stream = stream
123                     self._fileno = fileno
124                     self.closed = False
125                     self.softspace = False
126                     self.mode = 'w'
127                     self.encoding = 'utf-8'
128                     self.name = name
129                     if hasattr(stream, 'encoding') and canonical_encoding(stream.encoding) != 'utf-8':
130                         log.msg("%s: %r had encoding %r, but we're going to write UTF-8 to it" %
131                                 (name, stream, stream.encoding), level=log.CURIOUS)
132                     self.flush()
133
134                 def isatty(self):
135                     return False
136                 def close(self):
137                     # don't really close the handle, that would only cause problems
138                     self.closed = True
139                 def fileno(self):
140                     return self._fileno
141                 def flush(self):
142                     if self._hConsole is None:
143                         try:
144                             self._stream.flush()
145                         except Exception, e:
146                             _complain("%s.flush: %r from %r" % (self.name, e, self._stream))
147                             raise
148
149                 def write(self, text):
150                     try:
151                         if self._hConsole is None:
152                             if isinstance(text, unicode):
153                                 text = text.encode('utf-8')
154                             self._stream.write(text)
155                         else:
156                             if not isinstance(text, unicode):
157                                 text = str(text).decode('utf-8')
158                             remaining = len(text)
159                             while remaining > 0:
160                                 n = DWORD(0)
161                                 # There is a shorter-than-documented limitation on the length of the string
162                                 # passed to WriteConsoleW (see #1232).
163                                 retval = WriteConsoleW(self._hConsole, text, min(remaining, 10000), byref(n), None)
164                                 if retval == 0:
165                                     raise IOError("WriteConsoleW failed with WinError: %s" % (WinError(get_last_error()),))
166                                 if n.value == 0:
167                                     raise IOError("WriteConsoleW returned %r, n.value = 0" % (retval,))
168                                 remaining -= n.value
169                                 if remaining == 0: break
170                                 text = text[n.value:]
171                     except Exception, e:
172                         _complain("%s.write: %r" % (self.name, e))
173                         raise
174
175                 def writelines(self, lines):
176                     try:
177                         for line in lines:
178                             self.write(line)
179                     except Exception, e:
180                         _complain("%s.writelines: %r" % (self.name, e))
181                         raise
182
183             if real_stdout:
184                 sys.stdout = UnicodeOutput(hStdout, None, STDOUT_FILENO, '<Unicode console stdout>')
185             else:
186                 sys.stdout = UnicodeOutput(None, sys.stdout, old_stdout_fileno, '<Unicode redirected stdout>')
187
188             if real_stderr:
189                 sys.stderr = UnicodeOutput(hStderr, None, STDERR_FILENO, '<Unicode console stderr>')
190             else:
191                 sys.stderr = UnicodeOutput(None, sys.stderr, old_stderr_fileno, '<Unicode redirected stderr>')
192     except Exception, e:
193         _complain("exception %r while fixing up sys.stdout and sys.stderr" % (e,))
194
195     # This works around <http://bugs.python.org/issue2128>.
196
197     # <https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156%28v=vs.85%29.aspx>
198     GetCommandLineW = WINFUNCTYPE(
199         LPWSTR,
200         use_last_error=True
201     )(("GetCommandLineW", windll.kernel32))
202
203     # <https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391%28v=vs.85%29.aspx>
204     CommandLineToArgvW = WINFUNCTYPE(
205         POINTER(LPWSTR),  LPCWSTR, POINTER(c_int),
206         use_last_error=True
207     )(("CommandLineToArgvW", windll.shell32))
208
209     argc = c_int(0)
210     argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
211     if argv_unicode is None:
212         raise WinError(get_last_error())
213
214     # Because of <http://bugs.python.org/issue8775> (and similar limitations in
215     # twisted), the 'bin/tahoe' script cannot invoke us with the actual Unicode arguments.
216     # Instead it "mangles" or escapes them using \x7F as an escape character, which we
217     # unescape here.
218     def unmangle(s):
219         return re.sub(ur'\x7F[0-9a-fA-F]*\;', lambda m: unichr(int(m.group(0)[1:-1], 16)), s)
220
221     try:
222         argv = [unmangle(argv_unicode[i]).encode('utf-8') for i in xrange(0, argc.value)]
223     except Exception, e:
224         _complain("%s:  could not unmangle Unicode arguments.\n%r"
225                   % (sys.argv[0], [argv_unicode[i] for i in xrange(0, argc.value)]))
226         raise
227
228     # Take only the suffix with the same number of arguments as sys.argv.
229     # This accounts for anything that can cause initial arguments to be stripped,
230     # for example, the Python interpreter or any options passed to it, or runner
231     # scripts such as 'coverage run'. It works even if there are no such arguments,
232     # as in the case of a frozen executable created by bb-freeze or similar.
233
234     sys.argv = argv[-len(sys.argv):]
235     if sys.argv[0].endswith('.pyscript'):
236         sys.argv[0] = sys.argv[0][:-9]