File: _compat.py

package info (click to toggle)
python-pkginfo 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 416 kB
  • ctags: 354
  • sloc: python: 1,487; makefile: 76
file content (34 lines) | stat: -rw-r--r-- 885 bytes parent folder | download | duplicates (5)
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
try:
    STRING_TYPES = (str, unicode)
except NameError:           #pragma NO COVER Python >= 3.0
    STRING_TYPES = (str,)

try:
    u = unicode
except NameError:           #pragma NO COVER Python >= 3.0
    u = str
    b = bytes
else:                       #pragma NO COVER Python < 3.0
    b = str

try:
    from StringIO import StringIO
except ImportError:         #pragma NO COVER Python >= 3.0
    from io import StringIO
    from io import BytesIO
else:                       #pragma NO COVER Python < 3.0
    BytesIO = StringIO


def must_decode(value):     #pragma NO COVER
    if type(value) is bytes:
        try:
            return value.decode('utf-8')
        except UnicodeDecodeError:
            return value.decode('latin1')
    return value

def must_encode(value):     #pragma NO COVER
    if type(value) is u:
        return value.encode('utf-8')
    return value