comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/compat/py3k.py @ 87:2a2c65a20a8b

Add Python libs and headers
author Chris Cannam
date Wed, 25 Feb 2015 14:05:22 +0000
parents
children
comparison
equal deleted inserted replaced
86:413a9d26189e 87:2a2c65a20a8b
1 """
2 Python 3 compatibility tools.
3
4 """
5 from __future__ import division, absolute_import, print_function
6
7 __all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
8 'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',
9 'asstr', 'open_latin1', 'long', 'basestring', 'sixu',
10 'integer_types']
11
12 import sys
13
14 if sys.version_info[0] >= 3:
15 import io
16
17 long = int
18 integer_types = (int,)
19 basestring = str
20 unicode = str
21 bytes = bytes
22
23 def asunicode(s):
24 if isinstance(s, bytes):
25 return s.decode('latin1')
26 return str(s)
27
28 def asbytes(s):
29 if isinstance(s, bytes):
30 return s
31 return str(s).encode('latin1')
32
33 def asstr(s):
34 if isinstance(s, bytes):
35 return s.decode('latin1')
36 return str(s)
37
38 def isfileobj(f):
39 return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
40
41 def open_latin1(filename, mode='r'):
42 return open(filename, mode=mode, encoding='iso-8859-1')
43
44 def sixu(s):
45 return s
46
47 strchar = 'U'
48
49
50 else:
51 bytes = str
52 long = long
53 basestring = basestring
54 unicode = unicode
55 integer_types = (int, long)
56 asbytes = str
57 asstr = str
58 strchar = 'S'
59
60
61 def isfileobj(f):
62 return isinstance(f, file)
63
64 def asunicode(s):
65 if isinstance(s, unicode):
66 return s
67 return str(s).decode('ascii')
68
69 def open_latin1(filename, mode='r'):
70 return open(filename, mode=mode)
71
72 def sixu(s):
73 return unicode(s, 'unicode_escape')
74
75
76 def getexception():
77 return sys.exc_info()[1]
78
79 def asbytes_nested(x):
80 if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
81 return [asbytes_nested(y) for y in x]
82 else:
83 return asbytes(x)
84
85 def asunicode_nested(x):
86 if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
87 return [asunicode_nested(y) for y in x]
88 else:
89 return asunicode(x)