diff options
author | Chris Ball <cjb@laptop.org> | 2012-01-02 15:43:30 -0500 |
---|---|---|
committer | Chris Ball <cjb@laptop.org> | 2012-01-02 15:43:30 -0500 |
commit | f0011cf09d1385f983853b1d11ae5573148139bb (patch) | |
tree | df20d8d032fc4ecf7cd4344c6e4c6262679f90a9 /libbe/util/encoding.py | |
parent | b1527580ece00959b9e9d90e8f0f7dd1bce0342a (diff) | |
parent | 6f2d652b2697d7aec6bb48a2bf4d6fff1ef03521 (diff) | |
download | bugseverywhere-f0011cf09d1385f983853b1d11ae5573148139bb.tar.gz |
Merge http://www.physics.drexel.edu/~wking/code/git/be
Diffstat (limited to 'libbe/util/encoding.py')
-rw-r--r-- | libbe/util/encoding.py | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index 22a2e30..949b3ce 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -23,6 +23,7 @@ Support input/output/filesystem encodings (e.g. UTF-8). import codecs import locale +import os import sys import types @@ -31,7 +32,8 @@ if libbe.TESTING == True: import doctest -ENCODING = None # override get_encoding() output by setting this +ENCODING = os.environ.get('BE_ENCODING', None) +"override get_encoding() output" def get_encoding(): """ @@ -41,18 +43,17 @@ def get_encoding(): if ENCODING != None: return ENCODING encoding = locale.getpreferredencoding() or sys.getdefaultencoding() - if sys.platform != 'win32' or sys.version_info[:2] > (2, 3): - encoding = locale.getlocale(locale.LC_TIME)[1] or encoding - # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ' return encoding def get_input_encoding(): - return get_encoding() + return sys.__stdin__.encoding or get_encoding() def get_output_encoding(): return sys.__stdout__.encoding or get_encoding() -def get_filesystem_encoding(): +def get_text_file_encoding(): + """Return the encoding that should be used for file contents + """ return get_encoding() def get_argv_encoding(): @@ -74,7 +75,7 @@ def known_encoding(encoding): def get_file_contents(path, mode='r', encoding=None, decode=False): if decode == True: if encoding == None: - encoding = get_filesystem_encoding() + encoding = get_text_file_encoding() f = codecs.open(path, mode, encoding) else: f = open(path, mode) @@ -85,7 +86,7 @@ def get_file_contents(path, mode='r', encoding=None, decode=False): def set_file_contents(path, contents, mode='w', encoding=None): if type(contents) == types.UnicodeType: if encoding == None: - encoding = get_filesystem_encoding() + encoding = get_text_file_encoding() f = codecs.open(path, mode, encoding) else: f = open(path, mode) |