@@ -81,6 +81,9 @@ func IntNew(metatype *Type, args Tuple, kwargs StringDict) Object {
81
81
82
82
// Arithmetic
83
83
84
+ // Errors
85
+ var divisionByZero = ExceptionNewf (ZeroDivisionError , "division by zero" )
86
+
84
87
// Convert an Object to an Int
85
88
//
86
89
// Retrurns ok as to whether the conversion worked or not
@@ -188,6 +191,9 @@ func (a Int) M__floordiv__(other Object) Object {
188
191
189
192
func (a Int ) M__rfloordiv__ (other Object ) Object {
190
193
if b , ok := convertToInt (other ); ok {
194
+ if a == 0 {
195
+ panic (divisionByZero )
196
+ }
191
197
return Int (b / a )
192
198
}
193
199
return NotImplemented
@@ -199,13 +205,19 @@ func (a Int) M__ifloordiv__(other Object) Object {
199
205
200
206
func (a Int ) M__mod__ (other Object ) Object {
201
207
if b , ok := convertToInt (other ); ok {
208
+ if b == 0 {
209
+ panic (divisionByZero )
210
+ }
202
211
return Int (a % b )
203
212
}
204
213
return NotImplemented
205
214
}
206
215
207
216
func (a Int ) M__rmod__ (other Object ) Object {
208
217
if b , ok := convertToInt (other ); ok {
218
+ if a == 0 {
219
+ panic (divisionByZero )
220
+ }
209
221
return Int (b % a )
210
222
}
211
223
return NotImplemented
@@ -217,13 +229,19 @@ func (a Int) M__imod__(other Object) Object {
217
229
218
230
func (a Int ) M__divmod__ (other Object ) (Object , Object ) {
219
231
if b , ok := convertToInt (other ); ok {
232
+ if b == 0 {
233
+ panic (divisionByZero )
234
+ }
220
235
return Int (a / b ), Int (a % b )
221
236
}
222
237
return NotImplemented , None
223
238
}
224
239
225
240
func (a Int ) M__rdivmod__ (other Object ) (Object , Object ) {
226
241
if b , ok := convertToInt (other ); ok {
242
+ if a == 0 {
243
+ panic (divisionByZero )
244
+ }
227
245
return Int (b / a ), Int (b % a )
228
246
}
229
247
return NotImplemented , None
0 commit comments