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