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