Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: fix_encoding.py

Issue 1710553003: Convert old-style exception handling to current format in fix_encoding.py. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 def flush(self): 164 def flush(self):
165 raise NotImplementedError() 165 raise NotImplementedError()
166 166
167 def write(self, text): 167 def write(self, text):
168 raise NotImplementedError() 168 raise NotImplementedError()
169 169
170 def writelines(self, lines): 170 def writelines(self, lines):
171 try: 171 try:
172 for line in lines: 172 for line in lines:
173 self.write(line) 173 self.write(line)
174 except Exception, e: 174 except Exception as e:
175 complain('%s.writelines: %r' % (self.name, e)) 175 complain('%s.writelines: %r' % (self.name, e))
176 raise 176 raise
177 177
178 178
179 class WinUnicodeConsoleOutput(WinUnicodeOutputBase): 179 class WinUnicodeConsoleOutput(WinUnicodeOutputBase):
180 """Output adapter to a Windows Console. 180 """Output adapter to a Windows Console.
181 181
182 Understands how to use the win32 console API. 182 Understands how to use the win32 console API.
183 """ 183 """
184 def __init__(self, console_handle, fileno, stream_name, encoding): 184 def __init__(self, console_handle, fileno, stream_name, encoding):
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 min(remaining, 10000), 223 min(remaining, 10000),
224 self._byref(n), None) 224 self._byref(n), None)
225 if retval == 0 or n.value == 0: 225 if retval == 0 or n.value == 0:
226 raise IOError( 226 raise IOError(
227 'WriteConsoleW returned %r, n.value = %r, last error = %r' % ( 227 'WriteConsoleW returned %r, n.value = %r, last error = %r' % (
228 retval, n.value, self._GetLastError())) 228 retval, n.value, self._GetLastError()))
229 remaining -= n.value 229 remaining -= n.value
230 if not remaining: 230 if not remaining:
231 break 231 break
232 text = text[int(n.value):] 232 text = text[int(n.value):]
233 except Exception, e: 233 except Exception as e:
234 complain('%s.write: %r' % (self.name, e)) 234 complain('%s.write: %r' % (self.name, e))
235 raise 235 raise
236 236
237 237
238 class WinUnicodeOutput(WinUnicodeOutputBase): 238 class WinUnicodeOutput(WinUnicodeOutputBase):
239 """Output adaptor to a file output on Windows. 239 """Output adaptor to a file output on Windows.
240 240
241 If the standard FileWrite function is used, it will be encoded in the current 241 If the standard FileWrite function is used, it will be encoded in the current
242 code page. WriteConsoleW() permits writting any character. 242 code page. WriteConsoleW() permits writting any character.
243 """ 243 """
244 def __init__(self, stream, fileno, encoding): 244 def __init__(self, stream, fileno, encoding):
245 super(WinUnicodeOutput, self).__init__( 245 super(WinUnicodeOutput, self).__init__(
246 fileno, '<Unicode redirected %s>' % stream.name, encoding) 246 fileno, '<Unicode redirected %s>' % stream.name, encoding)
247 # Output stream 247 # Output stream
248 self._stream = stream 248 self._stream = stream
249 249
250 # Flush right now. 250 # Flush right now.
251 self.flush() 251 self.flush()
252 252
253 def flush(self): 253 def flush(self):
254 try: 254 try:
255 self._stream.flush() 255 self._stream.flush()
256 except Exception, e: 256 except Exception as e:
257 complain('%s.flush: %r from %r' % (self.name, e, self._stream)) 257 complain('%s.flush: %r from %r' % (self.name, e, self._stream))
258 raise 258 raise
259 259
260 def write(self, text): 260 def write(self, text):
261 try: 261 try:
262 if isinstance(text, unicode): 262 if isinstance(text, unicode):
263 # Replace characters that cannot be printed instead of failing. 263 # Replace characters that cannot be printed instead of failing.
264 text = text.encode(self.encoding, 'replace') 264 text = text.encode(self.encoding, 'replace')
265 self._stream.write(text) 265 self._stream.write(text)
266 except Exception, e: 266 except Exception as e:
267 complain('%s.write: %r' % (self.name, e)) 267 complain('%s.write: %r' % (self.name, e))
268 raise 268 raise
269 269
270 270
271 def win_handle_is_a_console(handle): 271 def win_handle_is_a_console(handle):
272 """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. 273 # These types are available on linux but not Mac.
274 # pylint: disable=E0611,F0401 274 # pylint: disable=E0611,F0401
275 from ctypes import byref, POINTER, windll, WINFUNCTYPE 275 from ctypes import byref, POINTER, windll, WINFUNCTYPE
276 from ctypes.wintypes import BOOL, DWORD, HANDLE 276 from ctypes.wintypes import BOOL, DWORD, HANDLE
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 # reverted manually. 341 # reverted manually.
342 # In practice one needs to set the console font to a TTF font to be able to 342 # In practice one needs to set the console font to a TTF font to be able to
343 # see all the characters but it failed for me in practice. In any case, it 343 # see all the characters but it failed for me in practice. In any case, it
344 # won't throw any exception when printing, which is the important part. 344 # won't throw any exception when printing, which is the important part.
345 # -11 and -12 are defined in stdio.h 345 # -11 and -12 are defined in stdio.h
346 sys.stdout = win_get_unicode_stream(sys.stdout, 1, -11, encoding) 346 sys.stdout = win_get_unicode_stream(sys.stdout, 1, -11, encoding)
347 sys.stderr = win_get_unicode_stream(sys.stderr, 2, -12, encoding) 347 sys.stderr = win_get_unicode_stream(sys.stderr, 2, -12, encoding)
348 # TODO(maruel): Do sys.stdin with ReadConsoleW(). Albeit the limitation is 348 # TODO(maruel): Do sys.stdin with ReadConsoleW(). Albeit the limitation is
349 # "It doesn't appear to be possible to read Unicode characters in UTF-8 349 # "It doesn't appear to be possible to read Unicode characters in UTF-8
350 # mode" and this appears to be a limitation of cmd.exe. 350 # mode" and this appears to be a limitation of cmd.exe.
351 except Exception, e: 351 except Exception as e:
352 complain('exception %r while fixing up sys.stdout and sys.stderr' % e) 352 complain('exception %r while fixing up sys.stdout and sys.stderr' % e)
353 return True 353 return True
354 354
355 355
356 def fix_encoding(): 356 def fix_encoding():
357 """Fixes various encoding problems on all platforms. 357 """Fixes various encoding problems on all platforms.
358 358
359 Should be called at the very begining of the process. 359 Should be called at the very begining of the process.
360 """ 360 """
361 ret = True 361 ret = True
362 if sys.platform == 'win32': 362 if sys.platform == 'win32':
363 ret &= fix_win_codec() 363 ret &= fix_win_codec()
364 364
365 ret &= fix_default_encoding() 365 ret &= fix_default_encoding()
366 366
367 if sys.platform == 'win32': 367 if sys.platform == 'win32':
368 encoding = sys.getdefaultencoding() 368 encoding = sys.getdefaultencoding()
369 ret &= fix_win_sys_argv(encoding) 369 ret &= fix_win_sys_argv(encoding)
370 ret &= fix_win_console(encoding) 370 ret &= fix_win_console(encoding)
371 return ret 371 return ret
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698