1from __future__ import unicode_literals
2
3import inspect
4import sys
5import math
6import numbers
7
8from future.utils import PY2, PY3, exec_
9
10
11if PY2:
12 from collections import Mapping
13else:
14 from collections.abc import Mapping
15
16if PY3:
17 import builtins
18 from collections.abc import Mapping
19
20 def apply(f, *args, **kw):
21 return f(*args, **kw)
22
23 from past.builtins import str as oldstr
24
25 def chr(i):
26 """
27 Return a byte-string of one character with ordinal i; 0 <= i <= 256
28 """
29 return oldstr(bytes((i,)))
30
31 def cmp(x, y):
32 """
33 cmp(x, y) -> integer
34
35 Return negative if x<y, zero if x==y, positive if x>y.
36 Python2 had looser comparison allowing cmp None and non Numerical types and collections.
37 Try to match the old behavior
38 """
39 if isinstance(x, set) and isinstance(y, set):
40 raise TypeError('cannot compare sets using cmp()',)
41 try:
42 if isinstance(x, numbers.Number) and math.isnan(x):
43 if not isinstance(y, numbers.Number):
44 raise TypeError('cannot compare float("nan"), {type_y} with cmp'.format(type_y=type(y)))
45 if isinstance(y, int):
46 return 1
47 else:
48 return -1
49 if isinstance(y, numbers.Number) and math.isnan(y):
50 if not isinstance(x, numbers.Number):
51 raise TypeError('cannot compare {type_x}, float("nan") with cmp'.format(type_x=type(x)))
52 if isinstance(x, int):
53 return -1
54 else:
55 return 1
56 return (x > y) - (x < y)
57 except TypeError:
58 if x == y:
59 return 0
60 type_order = [
61 type(None),
62 numbers.Number,
63 dict, list,
64 set,
65 (str, bytes),
66 ]
67 x_type_index = y_type_index = None
68 for i, type_match in enumerate(type_order):
69 if isinstance(x, type_match):
70 x_type_index = i
71 if isinstance(y, type_match):
72 y_type_index = i
73 if cmp(x_type_index, y_type_index) == 0:
74 if isinstance(x, bytes) and isinstance(y, str):
75 return cmp(x.decode('ascii'), y)
76 if isinstance(y, bytes) and isinstance(x, str):
77 return cmp(x, y.decode('ascii'))
78 elif isinstance(x, list):
79 # if both arguments are lists take the comparison of the first non equal value
80 for x_elem, y_elem in zip(x, y):
81 elem_cmp_val = cmp(x_elem, y_elem)
82 if elem_cmp_val != 0:
83 return elem_cmp_val
84 # if all elements are equal, return equal/0
85 return 0
86 elif isinstance(x, dict):
87 if len(x) != len(y):
88 return cmp(len(x), len(y))
89 else:
90 x_key = min(a for a in x if a not in y or x[a] != y[a])
91 y_key = min(b for b in y if b not in x or x[b] != y[b])
92 if x_key != y_key:
93 return cmp(x_key, y_key)
94 else:
95 return cmp(x[x_key], y[y_key])
96 return cmp(x_type_index, y_type_index)
97
98 from sys import intern
99
100 def oct(number):
101 """oct(number) -> string
102
103 Return the octal representation of an integer
104 """
105 return '0' + builtins.oct(number)[2:]
106
107 raw_input = input
108 # imp was deprecated in python 3.6
109 if sys.version_info >= (3, 6):
110 from importlib import reload
111 else:
112 # for python2, python3 <= 3.4
113 from imp import reload
114 unicode = str
115 unichr = chr
116 xrange = range
117else:
118 import __builtin__
119 from collections import Mapping
120 apply = __builtin__.apply
121 chr = __builtin__.chr
122 cmp = __builtin__.cmp
123 execfile = __builtin__.execfile
124 intern = __builtin__.intern
125 oct = __builtin__.oct
126 raw_input = __builtin__.raw_input
127 reload = __builtin__.reload
128 unicode = __builtin__.unicode
129 unichr = __builtin__.unichr
130 xrange = __builtin__.xrange
131
132
133if PY3:
134 def execfile(filename, myglobals=None, mylocals=None):
135 """
136 Read and execute a Python script from a file in the given namespaces.
137 The globals and locals are dictionaries, defaulting to the current
138 globals and locals. If only globals is given, locals defaults to it.
139 """
140 if myglobals is None:
141 # There seems to be no alternative to frame hacking here.
142 caller_frame = inspect.stack()[1]
143 myglobals = caller_frame[0].f_globals
144 mylocals = caller_frame[0].f_locals
145 elif mylocals is None:
146 # Only if myglobals is given do we set mylocals to it.
147 mylocals = myglobals
148 if not isinstance(myglobals, Mapping):
149 raise TypeError('globals must be a mapping')
150 if not isinstance(mylocals, Mapping):
151 raise TypeError('locals must be a mapping')
152 with open(filename, "rb") as fin:
153 source = fin.read()
154 code = compile(source, filename, "exec")
155 exec_(code, myglobals, mylocals)
156
157
158if PY3:
159 __all__ = ['apply', 'chr', 'cmp', 'execfile', 'intern', 'raw_input',
160 'reload', 'unichr', 'unicode', 'xrange']
161else:
162 __all__ = []