summaryrefslogtreecommitdiff
path: root/numpy/compat/py3k.py
blob: b6113b6610a779395ef0a28783d3c9bd47b06c05 (plain)
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
"""
Python 3 compatibility tools.

"""

__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
           'asunicode']

import sys

if sys.version_info[0] >= 3:
    import io
    bytes = bytes
    def asbytes(s):
        if isinstance(s, bytes):
            return s
        return s.encode('iso-8859-1')
    asunicode = str
    def isfileobj(f):
        return isinstance(f, io.IOBase)
    strchar = 'U'
else:
    bytes = str
    asbytes = str
    strchar = 'S'
    def isfileobj(f):
        return isinstance(f, file)
    def asunicode(s):
        if isinstance(s, unicode):
            return s
        return s.decode('iso-8859-1')

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