OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Collection of functions and classes to fix various encoding problems on | 5 """Collection of functions and classes to fix various encoding problems on |
6 multiple platforms with python. | 6 multiple platforms with python. |
7 """ | 7 """ |
8 | 8 |
9 import codecs | 9 import codecs |
10 import locale | 10 import locale |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 """Converts sys.argv to 'encoding' encoded string. | 74 """Converts sys.argv to 'encoding' encoded string. |
75 | 75 |
76 utf-8 is recommended. | 76 utf-8 is recommended. |
77 | 77 |
78 Works around <http://bugs.python.org/issue2128>. | 78 Works around <http://bugs.python.org/issue2128>. |
79 """ | 79 """ |
80 global _SYS_ARGV_PROCESSED | 80 global _SYS_ARGV_PROCESSED |
81 if _SYS_ARGV_PROCESSED: | 81 if _SYS_ARGV_PROCESSED: |
82 return False | 82 return False |
83 | 83 |
| 84 # These types are available on linux but not Mac. |
| 85 # pylint: disable=E0611,F0401 |
84 from ctypes import byref, c_int, POINTER, windll, WINFUNCTYPE | 86 from ctypes import byref, c_int, POINTER, windll, WINFUNCTYPE |
85 from ctypes.wintypes import LPCWSTR, LPWSTR | 87 from ctypes.wintypes import LPCWSTR, LPWSTR |
86 | 88 |
87 # <http://msdn.microsoft.com/en-us/library/ms683156.aspx> | 89 # <http://msdn.microsoft.com/en-us/library/ms683156.aspx> |
88 GetCommandLineW = WINFUNCTYPE(LPWSTR)(('GetCommandLineW', windll.kernel32)) | 90 GetCommandLineW = WINFUNCTYPE(LPWSTR)(('GetCommandLineW', windll.kernel32)) |
89 # <http://msdn.microsoft.com/en-us/library/bb776391.aspx> | 91 # <http://msdn.microsoft.com/en-us/library/bb776391.aspx> |
90 CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))( | 92 CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))( |
91 ('CommandLineToArgvW', windll.shell32)) | 93 ('CommandLineToArgvW', windll.shell32)) |
92 | 94 |
93 argc = c_int(0) | 95 argc = c_int(0) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 181 |
180 Understands how to use the win32 console API. | 182 Understands how to use the win32 console API. |
181 """ | 183 """ |
182 def __init__(self, console_handle, fileno, stream_name, encoding): | 184 def __init__(self, console_handle, fileno, stream_name, encoding): |
183 super(WinUnicodeConsoleOutput, self).__init__( | 185 super(WinUnicodeConsoleOutput, self).__init__( |
184 fileno, '<Unicode console %s>' % stream_name, encoding) | 186 fileno, '<Unicode console %s>' % stream_name, encoding) |
185 # Handle to use for WriteConsoleW | 187 # Handle to use for WriteConsoleW |
186 self._console_handle = console_handle | 188 self._console_handle = console_handle |
187 | 189 |
188 # Loads the necessary function. | 190 # Loads the necessary function. |
| 191 # These types are available on linux but not Mac. |
| 192 # pylint: disable=E0611,F0401 |
189 from ctypes import byref, GetLastError, POINTER, windll, WINFUNCTYPE | 193 from ctypes import byref, GetLastError, POINTER, windll, WINFUNCTYPE |
190 from ctypes.wintypes import BOOL, DWORD, HANDLE, LPWSTR | 194 from ctypes.wintypes import BOOL, DWORD, HANDLE, LPWSTR |
191 from ctypes.wintypes import LPVOID # pylint: disable=E0611 | 195 from ctypes.wintypes import LPVOID # pylint: disable=E0611 |
192 | 196 |
193 self._DWORD = DWORD | 197 self._DWORD = DWORD |
194 self._byref = byref | 198 self._byref = byref |
195 | 199 |
196 # <http://msdn.microsoft.com/en-us/library/ms687401.aspx> | 200 # <http://msdn.microsoft.com/en-us/library/ms687401.aspx> |
197 self._WriteConsoleW = WINFUNCTYPE( | 201 self._WriteConsoleW = WINFUNCTYPE( |
198 BOOL, HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID)( | 202 BOOL, HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID)( |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 # Replace characters that cannot be printed instead of failing. | 263 # Replace characters that cannot be printed instead of failing. |
260 text = text.encode(self.encoding, 'replace') | 264 text = text.encode(self.encoding, 'replace') |
261 self._stream.write(text) | 265 self._stream.write(text) |
262 except Exception, e: | 266 except Exception, e: |
263 complain('%s.write: %r' % (self.name, e)) | 267 complain('%s.write: %r' % (self.name, e)) |
264 raise | 268 raise |
265 | 269 |
266 | 270 |
267 def win_handle_is_a_console(handle): | 271 def win_handle_is_a_console(handle): |
268 """Returns True if a Windows file handle is a handle to a console.""" | 272 """Returns True if a Windows file handle is a handle to a console.""" |
| 273 # These types are available on linux but not Mac. |
| 274 # pylint: disable=E0611,F0401 |
269 from ctypes import byref, POINTER, windll, WINFUNCTYPE | 275 from ctypes import byref, POINTER, windll, WINFUNCTYPE |
270 from ctypes.wintypes import BOOL, DWORD, HANDLE | 276 from ctypes.wintypes import BOOL, DWORD, HANDLE |
271 | 277 |
272 FILE_TYPE_CHAR = 0x0002 | 278 FILE_TYPE_CHAR = 0x0002 |
273 FILE_TYPE_REMOTE = 0x8000 | 279 FILE_TYPE_REMOTE = 0x8000 |
274 INVALID_HANDLE_VALUE = DWORD(-1).value | 280 INVALID_HANDLE_VALUE = DWORD(-1).value |
275 | 281 |
276 # <http://msdn.microsoft.com/en-us/library/ms683167.aspx> | 282 # <http://msdn.microsoft.com/en-us/library/ms683167.aspx> |
277 GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))( | 283 GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))( |
278 ('GetConsoleMode', windll.kernel32)) | 284 ('GetConsoleMode', windll.kernel32)) |
(...skipping 11 matching lines...) Expand all Loading... |
290 def win_get_unicode_stream(stream, excepted_fileno, output_handle, encoding): | 296 def win_get_unicode_stream(stream, excepted_fileno, output_handle, encoding): |
291 """Returns a unicode-compatible stream. | 297 """Returns a unicode-compatible stream. |
292 | 298 |
293 This function will return a direct-Console writing object only if: | 299 This function will return a direct-Console writing object only if: |
294 - the file number is the expected console file number | 300 - the file number is the expected console file number |
295 - the handle the expected file handle | 301 - the handle the expected file handle |
296 - the 'real' handle is in fact a handle to a console. | 302 - the 'real' handle is in fact a handle to a console. |
297 """ | 303 """ |
298 old_fileno = getattr(stream, 'fileno', lambda: None)() | 304 old_fileno = getattr(stream, 'fileno', lambda: None)() |
299 if old_fileno == excepted_fileno: | 305 if old_fileno == excepted_fileno: |
| 306 # These types are available on linux but not Mac. |
| 307 # pylint: disable=E0611,F0401 |
300 from ctypes import windll, WINFUNCTYPE | 308 from ctypes import windll, WINFUNCTYPE |
301 from ctypes.wintypes import DWORD, HANDLE | 309 from ctypes.wintypes import DWORD, HANDLE |
302 | 310 |
303 # <http://msdn.microsoft.com/en-us/library/ms683231.aspx> | 311 # <http://msdn.microsoft.com/en-us/library/ms683231.aspx> |
304 GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(('GetStdHandle', windll.kernel32)) | 312 GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(('GetStdHandle', windll.kernel32)) |
305 | 313 |
306 real_output_handle = GetStdHandle(DWORD(output_handle)) | 314 real_output_handle = GetStdHandle(DWORD(output_handle)) |
307 if win_handle_is_a_console(real_output_handle): | 315 if win_handle_is_a_console(real_output_handle): |
308 # It's a console. | 316 # It's a console. |
309 return WinUnicodeConsoleOutput( | 317 return WinUnicodeConsoleOutput( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 if sys.platform == 'win32': | 362 if sys.platform == 'win32': |
355 ret &= fix_win_codec() | 363 ret &= fix_win_codec() |
356 | 364 |
357 ret &= fix_default_encoding() | 365 ret &= fix_default_encoding() |
358 | 366 |
359 if sys.platform == 'win32': | 367 if sys.platform == 'win32': |
360 encoding = sys.getdefaultencoding() | 368 encoding = sys.getdefaultencoding() |
361 ret &= fix_win_sys_argv(encoding) | 369 ret &= fix_win_sys_argv(encoding) |
362 ret &= fix_win_console(encoding) | 370 ret &= fix_win_console(encoding) |
363 return ret | 371 return ret |
OLD | NEW |