X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fmobilegtd.git;a=blobdiff_plain;f=src%2Finout%2Fio.py;h=f873b67d7b0f9aa4d6b196e23f66dee210d37c62;hp=68b69130999fdfa2771326449965d07ae8d4734d;hb=fd4ab7a04e38a40e76388626127a6c6675a41a9a;hpb=ab8105e7f3d6adbea1e4e9de73075a8cdcaa980c diff --git a/src/inout/io.py b/src/inout/io.py index 68b6913..f873b67 100644 --- a/src/inout/io.py +++ b/src/inout/io.py @@ -24,15 +24,30 @@ def create_file(file_path): f = file(file_name,'w') return f +def getfilesystemencoding(): + try: + # sys.getfilesystemencoding() was introduced in Python v2.3 + return sys.getfilesystemencoding() + except AttributeError: + return 'ascii' def os_encode(s): - return s.encode(sys.getfilesystemencoding()) + # Encoding can be None, meaning the default system encoding should + # be used. + enc = getfilesystemencoding() + if enc: + return s.encode(enc) + else: + return s def os_decode(s): - if type(s) == unicode: + # Encoding can be None, meaning the default system encoding should + # be used. + enc = getfilesystemencoding() + if type(s) == unicode or not enc: return s else: - return unicode(s,sys.getfilesystemencoding()) + return unicode(s,enc) def write(file_path,content): f = create_file(file_path)