File: _encoding.py

package info (click to toggle)
pbbam 0.19.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,280 kB
  • sloc: cpp: 50,244; python: 1,371; ansic: 949; xml: 903; sh: 317; makefile: 185
file content (106 lines) | stat: -rw-r--r-- 2,990 bytes parent folder | download | duplicates (30)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Encoding utilities"""

import os
import sys

try:
    import builtins
except ImportError:
    import __builtin__ as builtins

__all__ = ['b', 'bchr', 'bytestype', 'envencode', 'fsdecode', 'fsencode',
           'stdoutb', 'stderrb', 'u', 'ul', 'unicodetype']

bytestype = getattr(builtins, 'bytes', str)
unicodetype = getattr(builtins, 'unicode', str)

if getattr(os, 'fsdecode', None) is not None:
    fsdecode = os.fsdecode
    fsencode = os.fsencode
elif bytestype is not str:
    if sys.platform == 'win32':
        def fsdecode(s):
            """Decode a filename from the filesystem encoding"""
            if isinstance(s, unicodetype):
                return s
            encoding = sys.getfilesystemencoding()
            if encoding == 'mbcs':
                return s.decode(encoding)
            else:
                return s.decode(encoding, 'surrogateescape')

        def fsencode(s):
            """Encode a filename to the filesystem encoding"""
            if isinstance(s, bytestype):
                return s
            encoding = sys.getfilesystemencoding()
            if encoding == 'mbcs':
                return s.encode(encoding)
            else:
                return s.encode(encoding, 'surrogateescape')
    else:
        def fsdecode(s):
            """Decode a filename from the filesystem encoding"""
            if isinstance(s, unicodetype):
                return s
            return s.decode(sys.getfilesystemencoding(), 'surrogateescape')

        def fsencode(s):
            """Encode a filename to the filesystem encoding"""
            if isinstance(s, bytestype):
                return s
            return s.encode(sys.getfilesystemencoding(), 'surrogateescape')
else:
    def fsdecode(s):
        """Decode a filename from the filesystem encoding"""
        return s

    def fsencode(s):
        """Encode a filename to the filesystem encoding"""
        return s

if bytestype is str:
    def envencode(s):
        """Encode a byte string to the os.environ encoding"""
        return s
else:
    envencode = fsdecode

if getattr(sys.stdout, 'buffer', None) is not None:
    stdoutb = sys.stdout.buffer
    stderrb = sys.stderr.buffer
else:
    stdoutb = sys.stdout
    stderrb = sys.stderr

if bytestype is str:
    def b(s):
        """Convert an ASCII string literal into a bytes object"""
        return s

    bchr = chr

    def u(s):
        """Convert an ASCII string literal into a unicode object"""
        return s.decode('ascii')
else:
    def b(s):
        """Convert an ASCII string literal into a bytes object"""
        return s.encode('ascii')

    def bchr(i):
        """Return a bytes character for a given integer value"""
        return bytestype([i])

    def u(s):
        """Convert an ASCII string literal into a unicode object"""
        return s

try:
    eval(r'u""')
except SyntaxError:
    ul = eval
else:
    def ul(e):
        """Evaluate e as a unicode string literal"""
        return eval('u' + e)