From: Matthijs Kooijman Date: Wed, 12 Aug 2009 20:19:58 +0000 (+0200) Subject: Add getfilesystemencoding() for python < 2.3. X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=fd4ab7a04e38a40e76388626127a6c6675a41a9a;p=matthijs%2Fupstream%2Fmobilegtd.git Add getfilesystemencoding() for python < 2.3. --- 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)