7 if sys.platform != "win32" or done:
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
15 from allmydata.util import log
16 from allmydata.util.encodingutil import canonical_encoding
18 # <https://msdn.microsoft.com/en-us/library/ms680621%28VS.85%29.aspx>
19 SetErrorMode = WINFUNCTYPE(
22 )(("SetErrorMode", windll.kernel32))
24 SEM_FAILCRITICALERRORS = 0x0001
25 SEM_NOOPENFILEERRORBOX = 0x8000
27 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX)
29 original_stderr = sys.stderr
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)
39 # Work around <http://bugs.python.org/issue6058>.
40 codecs.register(lambda name: name == 'cp65001' and codecs.lookup('utf-8') or None)
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>
46 # <http://stackoverflow.com/questions/878972/windows-cmd-encoding-change-causes-python-crash/1432462#1432462>.
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
52 # <https://msdn.microsoft.com/en-us/library/aa364960(VS.85).aspx>
53 # DWORD WINAPI GetFileType(DWORD hFile);
55 # <https://msdn.microsoft.com/en-us/library/ms683167(VS.85).aspx>
56 # BOOL WINAPI GetConsoleMode(HANDLE hConsole, LPDWORD lpMode);
58 GetStdHandle = WINFUNCTYPE(
61 )(("GetStdHandle", windll.kernel32))
63 STD_OUTPUT_HANDLE = DWORD(-11)
64 STD_ERROR_HANDLE = DWORD(-12)
66 GetFileType = WINFUNCTYPE(
69 )(("GetFileType", windll.kernel32))
71 FILE_TYPE_CHAR = 0x0002
72 FILE_TYPE_REMOTE = 0x8000
74 GetConsoleMode = WINFUNCTYPE(
75 BOOL, HANDLE, POINTER(DWORD),
77 )(("GetConsoleMode", windll.kernel32))
79 INVALID_HANDLE_VALUE = DWORD(-1).value
81 def not_a_console(handle):
82 if handle == INVALID_HANDLE_VALUE or handle is None:
84 return ((GetFileType(handle) & ~FILE_TYPE_REMOTE) != FILE_TYPE_CHAR
85 or GetConsoleMode(handle, byref(DWORD())) == 0)
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()
96 real_stdout = (old_stdout_fileno == STDOUT_FILENO)
97 real_stderr = (old_stderr_fileno == STDERR_FILENO)
100 hStdout = GetStdHandle(STD_OUTPUT_HANDLE)
101 if not_a_console(hStdout):
105 hStderr = GetStdHandle(STD_ERROR_HANDLE)
106 if not_a_console(hStderr):
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);
114 WriteConsoleW = WINFUNCTYPE(
115 BOOL, HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID,
117 )(("WriteConsoleW", windll.kernel32))
120 def __init__(self, hConsole, stream, fileno, name):
121 self._hConsole = hConsole
122 self._stream = stream
123 self._fileno = fileno
125 self.softspace = False
127 self.encoding = 'utf-8'
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)
137 # don't really close the handle, that would only cause problems
142 if self._hConsole is None:
146 _complain("%s.flush: %r from %r" % (self.name, e, self._stream))
149 def write(self, text):
151 if self._hConsole is None:
152 if isinstance(text, unicode):
153 text = text.encode('utf-8')
154 self._stream.write(text)
156 if not isinstance(text, unicode):
157 text = str(text).decode('utf-8')
158 remaining = len(text)
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)
165 raise IOError("WriteConsoleW failed with WinError: %s" % (WinError(get_last_error()),))
167 raise IOError("WriteConsoleW returned %r, n.value = 0" % (retval,))
169 if remaining == 0: break
170 text = text[n.value:]
172 _complain("%s.write: %r" % (self.name, e))
175 def writelines(self, lines):
180 _complain("%s.writelines: %r" % (self.name, e))
184 sys.stdout = UnicodeOutput(hStdout, None, STDOUT_FILENO, '<Unicode console stdout>')
186 sys.stdout = UnicodeOutput(None, sys.stdout, old_stdout_fileno, '<Unicode redirected stdout>')
189 sys.stderr = UnicodeOutput(hStderr, None, STDERR_FILENO, '<Unicode console stderr>')
191 sys.stderr = UnicodeOutput(None, sys.stderr, old_stderr_fileno, '<Unicode redirected stderr>')
193 _complain("exception %r while fixing up sys.stdout and sys.stderr" % (e,))
195 # This works around <http://bugs.python.org/issue2128>.
197 # <https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156%28v=vs.85%29.aspx>
198 GetCommandLineW = WINFUNCTYPE(
201 )(("GetCommandLineW", windll.kernel32))
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),
207 )(("CommandLineToArgvW", windll.shell32))
210 argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
211 if argv_unicode is None:
212 raise WinError(get_last_error())
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
219 return re.sub(ur'\x7F[0-9a-fA-F]*\;', lambda m: unichr(int(m.group(0)[1:-1], 16)), s)
222 argv = [unmangle(argv_unicode[i]).encode('utf-8') for i in xrange(0, argc.value)]
224 _complain("%s: could not unmangle Unicode arguments.\n%r"
225 % (sys.argv[0], [argv_unicode[i] for i in xrange(0, argc.value)]))
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.
234 sys.argv = argv[-len(sys.argv):]
235 if sys.argv[0].endswith('.pyscript'):
236 sys.argv[0] = sys.argv[0][:-9]