Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/past/builtins/misc.py: 25%

106 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:53 +0000

1from __future__ import unicode_literals 

2 

3import inspect 

4import math 

5import numbers 

6 

7from future.utils import PY2, PY3, exec_ 

8 

9if PY2: 

10 from collections import Mapping 

11else: 

12 from collections.abc import Mapping 

13 

14if PY3: 

15 import builtins 

16 from collections.abc import Mapping 

17 

18 def apply(f, *args, **kw): 

19 return f(*args, **kw) 

20 

21 from past.builtins import str as oldstr 

22 

23 def chr(i): 

24 """ 

25 Return a byte-string of one character with ordinal i; 0 <= i <= 256 

26 """ 

27 return oldstr(bytes((i,))) 

28 

29 def cmp(x, y): 

30 """ 

31 cmp(x, y) -> integer 

32 

33 Return negative if x<y, zero if x==y, positive if x>y. 

34 Python2 had looser comparison allowing cmp None and non Numerical types and collections. 

35 Try to match the old behavior 

36 """ 

37 if isinstance(x, set) and isinstance(y, set): 

38 raise TypeError('cannot compare sets using cmp()',) 

39 try: 

40 if isinstance(x, numbers.Number) and math.isnan(x): 

41 if not isinstance(y, numbers.Number): 

42 raise TypeError('cannot compare float("nan"), {type_y} with cmp'.format(type_y=type(y))) 

43 if isinstance(y, int): 

44 return 1 

45 else: 

46 return -1 

47 if isinstance(y, numbers.Number) and math.isnan(y): 

48 if not isinstance(x, numbers.Number): 

49 raise TypeError('cannot compare {type_x}, float("nan") with cmp'.format(type_x=type(x))) 

50 if isinstance(x, int): 

51 return -1 

52 else: 

53 return 1 

54 return (x > y) - (x < y) 

55 except TypeError: 

56 if x == y: 

57 return 0 

58 type_order = [ 

59 type(None), 

60 numbers.Number, 

61 dict, list, 

62 set, 

63 (str, bytes), 

64 ] 

65 x_type_index = y_type_index = None 

66 for i, type_match in enumerate(type_order): 

67 if isinstance(x, type_match): 

68 x_type_index = i 

69 if isinstance(y, type_match): 

70 y_type_index = i 

71 if cmp(x_type_index, y_type_index) == 0: 

72 if isinstance(x, bytes) and isinstance(y, str): 

73 return cmp(x.decode('ascii'), y) 

74 if isinstance(y, bytes) and isinstance(x, str): 

75 return cmp(x, y.decode('ascii')) 

76 elif isinstance(x, list): 

77 # if both arguments are lists take the comparison of the first non equal value 

78 for x_elem, y_elem in zip(x, y): 

79 elem_cmp_val = cmp(x_elem, y_elem) 

80 if elem_cmp_val != 0: 

81 return elem_cmp_val 

82 # if all elements are equal, return equal/0 

83 return 0 

84 elif isinstance(x, dict): 

85 if len(x) != len(y): 

86 return cmp(len(x), len(y)) 

87 else: 

88 x_key = min(a for a in x if a not in y or x[a] != y[a]) 

89 y_key = min(b for b in y if b not in x or x[b] != y[b]) 

90 if x_key != y_key: 

91 return cmp(x_key, y_key) 

92 else: 

93 return cmp(x[x_key], y[y_key]) 

94 return cmp(x_type_index, y_type_index) 

95 

96 from sys import intern 

97 

98 def oct(number): 

99 """oct(number) -> string 

100 

101 Return the octal representation of an integer 

102 """ 

103 return '0' + builtins.oct(number)[2:] 

104 

105 raw_input = input 

106 

107 try: 

108 from importlib import reload 

109 except ImportError: 

110 # for python2, python3 <= 3.4 

111 from imp import reload 

112 

113 unicode = str 

114 unichr = chr 

115 xrange = range 

116else: 

117 import __builtin__ 

118 from collections import Mapping 

119 apply = __builtin__.apply 

120 chr = __builtin__.chr 

121 cmp = __builtin__.cmp 

122 execfile = __builtin__.execfile 

123 intern = __builtin__.intern 

124 oct = __builtin__.oct 

125 raw_input = __builtin__.raw_input 

126 reload = __builtin__.reload 

127 unicode = __builtin__.unicode 

128 unichr = __builtin__.unichr 

129 xrange = __builtin__.xrange 

130 

131 

132if PY3: 

133 def execfile(filename, myglobals=None, mylocals=None): 

134 """ 

135 Read and execute a Python script from a file in the given namespaces. 

136 The globals and locals are dictionaries, defaulting to the current 

137 globals and locals. If only globals is given, locals defaults to it. 

138 """ 

139 if myglobals is None: 

140 # There seems to be no alternative to frame hacking here. 

141 caller_frame = inspect.stack()[1] 

142 myglobals = caller_frame[0].f_globals 

143 mylocals = caller_frame[0].f_locals 

144 elif mylocals is None: 

145 # Only if myglobals is given do we set mylocals to it. 

146 mylocals = myglobals 

147 if not isinstance(myglobals, Mapping): 

148 raise TypeError('globals must be a mapping') 

149 if not isinstance(mylocals, Mapping): 

150 raise TypeError('locals must be a mapping') 

151 with open(filename, "rb") as fin: 

152 source = fin.read() 

153 code = compile(source, filename, "exec") 

154 exec_(code, myglobals, mylocals) 

155 

156 

157if PY3: 

158 __all__ = ['apply', 'chr', 'cmp', 'execfile', 'intern', 'raw_input', 

159 'reload', 'unichr', 'unicode', 'xrange'] 

160else: 

161 __all__ = []