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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 # Module 'sys' has no 'setdefaultencoding' member | 42 # Module 'sys' has no 'setdefaultencoding' member |
43 # pylint: disable=E1101 | 43 # pylint: disable=E1101 |
44 sys.setdefaultencoding('utf-8') | 44 sys.setdefaultencoding('utf-8') |
45 for attr in dir(locale): | 45 for attr in dir(locale): |
46 if attr[0:3] != 'LC_': | 46 if attr[0:3] != 'LC_': |
47 continue | 47 continue |
48 aref = getattr(locale, attr) | 48 aref = getattr(locale, attr) |
49 locale.setlocale(aref, '') | 49 locale.setlocale(aref, '') |
50 try: | 50 try: |
51 lang = locale.getlocale(aref)[0] | 51 lang = locale.getlocale(aref)[0] |
52 except TypeError: | 52 except (TypeError, ValueError): |
53 lang = None | 53 lang = None |
54 if lang: | 54 if lang: |
55 try: | 55 try: |
56 locale.setlocale(aref, (lang, 'UTF-8')) | 56 locale.setlocale(aref, (lang, 'UTF-8')) |
57 except locale.Error: | 57 except locale.Error: |
58 os.environ[attr] = lang + '.UTF-8' | 58 os.environ[attr] = lang + '.UTF-8' |
59 locale.setlocale(locale.LC_ALL, '') | 59 locale.setlocale(locale.LC_ALL, '') |
60 return True | 60 return True |
61 | 61 |
62 | 62 |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 if sys.platform == 'win32': | 347 if sys.platform == 'win32': |
348 ret &= fix_win_codec() | 348 ret &= fix_win_codec() |
349 | 349 |
350 ret &= fix_default_encoding() | 350 ret &= fix_default_encoding() |
351 | 351 |
352 if sys.platform == 'win32': | 352 if sys.platform == 'win32': |
353 encoding = sys.getdefaultencoding() | 353 encoding = sys.getdefaultencoding() |
354 ret &= fix_win_sys_argv(encoding) | 354 ret &= fix_win_sys_argv(encoding) |
355 ret &= fix_win_console(encoding) | 355 ret &= fix_win_console(encoding) |
356 return ret | 356 return ret |
OLD | NEW |