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

"""

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

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')
    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 getexception():
    return sys.exc_info()[1]