File: compat.py

package info (click to toggle)
python-xattr 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 204 kB
  • sloc: python: 824; ansic: 489; sh: 32; makefile: 17
file content (31 lines) | stat: -rw-r--r-- 680 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Python 3 compatibility shims
"""
import os
import sys
import codecs

if sys.version_info[0] < 3:
    integer_types = (int, long)
    text_type = unicode
    binary_type = str
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