diff options
Diffstat (limited to 'xattr/compat.py')
-rw-r--r-- | xattr/compat.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/xattr/compat.py b/xattr/compat.py index b3094e3..16fbccc 100644 --- a/xattr/compat.py +++ b/xattr/compat.py @@ -1,6 +1,9 @@ """Python 3 compatibility shims """ +import os import sys +import codecs + if sys.version_info[0] < 3: integer_types = (int, long) text_type = unicode @@ -9,3 +12,20 @@ else: integer_types = (int,) text_type = str binary_type = bytes + +fs_encoding = sys.getfilesystemencoding() +fs_errors = 'strict' +if fs_encoding != 'mbcs': + try: + codecs.lookup('surrogateescape') + fs_errors = 'surrogateescape' + except LookupError: + pass +try: + fs_encode = os.fsencode +except AttributeError: + def fs_encode(val): + if not isinstance(val, bytes): + return val.encode(fs_encoding, fs_errors) + else: + return val |