From fd4ab7a04e38a40e76388626127a6c6675a41a9a Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Wed, 12 Aug 2009 22:19:58 +0200 Subject: [PATCH] Add getfilesystemencoding() for python < 2.3. --- src/inout/io.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) 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) -- 2.30.2