Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Various richly-typed exceptions, that also help us deal with string formatting 

3in python where it's easier. 

4 

5By putting the formatting in `__str__`, we also avoid paying the cost for 

6users who silence the exceptions. 

7""" 

8from numpy.core.overrides import set_module 

9 

10def _unpack_tuple(tup): 

11 if len(tup) == 1: 

12 return tup[0] 

13 else: 

14 return tup 

15 

16 

17def _display_as_base(cls): 

18 """ 

19 A decorator that makes an exception class look like its base. 

20 

21 We use this to hide subclasses that are implementation details - the user 

22 should catch the base type, which is what the traceback will show them. 

23 

24 Classes decorated with this decorator are subject to removal without a 

25 deprecation warning. 

26 """ 

27 assert issubclass(cls, Exception) 

28 cls.__name__ = cls.__base__.__name__ 

29 cls.__qualname__ = cls.__base__.__qualname__ 

30 set_module(cls.__base__.__module__)(cls) 

31 return cls 

32 

33 

34class UFuncTypeError(TypeError): 

35 """ Base class for all ufunc exceptions """ 

36 def __init__(self, ufunc): 

37 self.ufunc = ufunc 

38 

39 

40@_display_as_base 

41class _UFuncBinaryResolutionError(UFuncTypeError): 

42 """ Thrown when a binary resolution fails """ 

43 def __init__(self, ufunc, dtypes): 

44 super().__init__(ufunc) 

45 self.dtypes = tuple(dtypes) 

46 assert len(self.dtypes) == 2 

47 

48 def __str__(self): 

49 return ( 

50 "ufunc {!r} cannot use operands with types {!r} and {!r}" 

51 ).format( 

52 self.ufunc.__name__, *self.dtypes 

53 ) 

54 

55 

56@_display_as_base 

57class _UFuncNoLoopError(UFuncTypeError): 

58 """ Thrown when a ufunc loop cannot be found """ 

59 def __init__(self, ufunc, dtypes): 

60 super().__init__(ufunc) 

61 self.dtypes = tuple(dtypes) 

62 

63 def __str__(self): 

64 return ( 

65 "ufunc {!r} did not contain a loop with signature matching types " 

66 "{!r} -> {!r}" 

67 ).format( 

68 self.ufunc.__name__, 

69 _unpack_tuple(self.dtypes[:self.ufunc.nin]), 

70 _unpack_tuple(self.dtypes[self.ufunc.nin:]) 

71 ) 

72 

73 

74@_display_as_base 

75class _UFuncCastingError(UFuncTypeError): 

76 def __init__(self, ufunc, casting, from_, to): 

77 super().__init__(ufunc) 

78 self.casting = casting 

79 self.from_ = from_ 

80 self.to = to 

81 

82 

83@_display_as_base 

84class _UFuncInputCastingError(_UFuncCastingError): 

85 """ Thrown when a ufunc input cannot be casted """ 

86 def __init__(self, ufunc, casting, from_, to, i): 

87 super().__init__(ufunc, casting, from_, to) 

88 self.in_i = i 

89 

90 def __str__(self): 

91 # only show the number if more than one input exists 

92 i_str = "{} ".format(self.in_i) if self.ufunc.nin != 1 else "" 

93 return ( 

94 "Cannot cast ufunc {!r} input {}from {!r} to {!r} with casting " 

95 "rule {!r}" 

96 ).format( 

97 self.ufunc.__name__, i_str, self.from_, self.to, self.casting 

98 ) 

99 

100 

101@_display_as_base 

102class _UFuncOutputCastingError(_UFuncCastingError): 

103 """ Thrown when a ufunc output cannot be casted """ 

104 def __init__(self, ufunc, casting, from_, to, i): 

105 super().__init__(ufunc, casting, from_, to) 

106 self.out_i = i 

107 

108 def __str__(self): 

109 # only show the number if more than one output exists 

110 i_str = "{} ".format(self.out_i) if self.ufunc.nout != 1 else "" 

111 return ( 

112 "Cannot cast ufunc {!r} output {}from {!r} to {!r} with casting " 

113 "rule {!r}" 

114 ).format( 

115 self.ufunc.__name__, i_str, self.from_, self.to, self.casting 

116 ) 

117 

118 

119# Exception used in shares_memory() 

120@set_module('numpy') 

121class TooHardError(RuntimeError): 

122 pass 

123 

124 

125@set_module('numpy') 

126class AxisError(ValueError, IndexError): 

127 """ Axis supplied was invalid. """ 

128 def __init__(self, axis, ndim=None, msg_prefix=None): 

129 # single-argument form just delegates to base class 

130 if ndim is None and msg_prefix is None: 

131 msg = axis 

132 

133 # do the string formatting here, to save work in the C code 

134 else: 

135 msg = ("axis {} is out of bounds for array of dimension {}" 

136 .format(axis, ndim)) 

137 if msg_prefix is not None: 

138 msg = "{}: {}".format(msg_prefix, msg) 

139 

140 super(AxisError, self).__init__(msg) 

141 

142 

143@_display_as_base 

144class _ArrayMemoryError(MemoryError): 

145 """ Thrown when an array cannot be allocated""" 

146 def __init__(self, shape, dtype): 

147 self.shape = shape 

148 self.dtype = dtype 

149 

150 @property 

151 def _total_size(self): 

152 num_bytes = self.dtype.itemsize 

153 for dim in self.shape: 

154 num_bytes *= dim 

155 return num_bytes 

156 

157 @staticmethod 

158 def _size_to_string(num_bytes): 

159 """ Convert a number of bytes into a binary size string """ 

160 

161 # https://en.wikipedia.org/wiki/Binary_prefix 

162 LOG2_STEP = 10 

163 STEP = 1024 

164 units = ['bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'] 

165 

166 unit_i = max(num_bytes.bit_length() - 1, 1) // LOG2_STEP 

167 unit_val = 1 << (unit_i * LOG2_STEP) 

168 n_units = num_bytes / unit_val 

169 del unit_val 

170 

171 # ensure we pick a unit that is correct after rounding 

172 if round(n_units) == STEP: 

173 unit_i += 1 

174 n_units /= STEP 

175 

176 # deal with sizes so large that we don't have units for them 

177 if unit_i >= len(units): 

178 new_unit_i = len(units) - 1 

179 n_units *= 1 << ((unit_i - new_unit_i) * LOG2_STEP) 

180 unit_i = new_unit_i 

181 

182 unit_name = units[unit_i] 

183 # format with a sensible number of digits 

184 if unit_i == 0: 

185 # no decimal point on bytes 

186 return '{:.0f} {}'.format(n_units, unit_name) 

187 elif round(n_units) < 1000: 

188 # 3 significant figures, if none are dropped to the left of the . 

189 return '{:#.3g} {}'.format(n_units, unit_name) 

190 else: 

191 # just give all the digits otherwise 

192 return '{:#.0f} {}'.format(n_units, unit_name) 

193 

194 def __str__(self): 

195 size_str = self._size_to_string(self._total_size) 

196 return ( 

197 "Unable to allocate {} for an array with shape {} and data type {}" 

198 .format(size_str, self.shape, self.dtype) 

199 )