File: compat.py

package info (click to toggle)
pygame 1.9.6%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,060 kB
  • sloc: ansic: 59,765; python: 31,220; objc: 334; makefile: 57; cpp: 25
file content (103 lines) | stat: -rw-r--r-- 3,113 bytes parent folder | download
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
# coding: ascii
"""Python 2.x/3.x compatibility tools"""

import sys

__all__ = ['geterror', 'long_', 'xrange_', 'ord_', 'unichr_',
           'unicode_', 'raw_input_', 'as_bytes', 'as_unicode',
           'bytes_', 'imap_', 'PY_MAJOR_VERSION']

PY_MAJOR_VERSION = sys.version_info[0]


def geterror():
    return sys.exc_info()[1]

# Python 3
if PY_MAJOR_VERSION >= 3:
    long_ = int
    xrange_ = range
    from io import StringIO
    from io import BytesIO
    unichr_ = chr
    unicode_ = str
    bytes_ = bytes
    raw_input_ = input
    imap_ = map

    # Represent escaped bytes and strings in a portable way.
    #
    # as_bytes: Allow a Python 3.x string to represent a bytes object.
    #   e.g.: as_bytes("a\x01\b") == b"a\x01b" # Python 3.x
    #         as_bytes("a\x01\b") == "a\x01b"  # Python 2.x
    # as_unicode: Allow a Python "r" string to represent a unicode string.
    #   e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
    #         as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes"  # Python 3.x
    def as_bytes(string):
        """ '<binary literal>' => b'<binary literal>' """
        return string.encode('latin-1', 'strict')

    def as_unicode(rstring):
        """ r'<Unicode literal>' => '<Unicode literal>' """
        return rstring.encode('ascii', 'strict').decode('unicode_escape',
                                                        'strict')

# Python 2
else:
    long_ = long
    xrange_ = xrange
    from cStringIO import StringIO
    BytesIO = StringIO
    unichr_ = unichr
    unicode_ = unicode
    bytes_ = str
    raw_input_ = raw_input
    from itertools import imap as imap_

    # Represent escaped bytes and strings in a portable way.
    #
    # as_bytes: Allow a Python 3.x string to represent a bytes object.
    #   e.g.: as_bytes("a\x01\b") == b"a\x01b" # Python 3.x
    #         as_bytes("a\x01\b") == "a\x01b"  # Python 2.x
    # as_unicode: Allow a Python "r" string to represent a unicode string.
    #   e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
    #         as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes"  # Python 3.x
    def as_bytes(string):
        """ '<binary literal>' => '<binary literal>' """
        return string

    def as_unicode(rstring):
        """ r'<Unicode literal>' => u'<Unicode literal>' """
        return rstring.decode('unicode_escape', 'strict')


def get_BytesIO():
    return BytesIO


def get_StringIO():
    return StringIO


def ord_(o):
    try:
        return ord(o)
    except TypeError:
        return o

if sys.platform == 'win32':
    filesystem_errors = "replace"
elif PY_MAJOR_VERSION >= 3:
    filesystem_errors = "surrogateescape"
else:
    filesystem_errors = "strict"


def filesystem_encode(u):
    fsencoding = sys.getfilesystemencoding()
    if fsencoding.lower() in ['ascii', 'ansi_x3.4-1968'] and sys.platform.startswith('linux'):
        # Don't believe Linux systems claiming ASCII-only filesystems. In
        # practice, arbitrary bytes are allowed, and most things expect UTF-8.
        fsencoding = 'utf-8'
    return u.encode(fsencoding, filesystem_errors)