Skip to content

Commit 3a897b0

Browse files
committed
py: float and int - check for divide by zero
1 parent 5a56e32 commit 3a897b0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

py/float.go

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func (o Float) Type() *Type {
1717

1818
// Arithmetic
1919

20+
// Errors
21+
var floatDivisionByZero = ExceptionNewf(ZeroDivisionError, "float division by zero")
22+
2023
// Convert an Object to an Float
2124
//
2225
// Retrurns ok as to whether the conversion worked or not
@@ -98,13 +101,19 @@ func (a Float) M__imul__(other Object) Object {
98101

99102
func (a Float) M__truediv__(other Object) Object {
100103
if b, ok := convertToFloat(other); ok {
104+
if b == 0 {
105+
panic(floatDivisionByZero)
106+
}
101107
return Float(a / b)
102108
}
103109
return NotImplemented
104110
}
105111

106112
func (a Float) M__rtruediv__(other Object) Object {
107113
if b, ok := convertToFloat(other); ok {
114+
if a == 0 {
115+
panic(floatDivisionByZero)
116+
}
108117
return Float(b / a)
109118
}
110119
return NotImplemented
@@ -134,6 +143,9 @@ func (a Float) M__ifloordiv__(other Object) Object {
134143

135144
// Does DivMod of two floating point numbers
136145
func floatDivMod(a, b Float) (Float, Float) {
146+
if b == 0 {
147+
panic(floatDivisionByZero)
148+
}
137149
q := Float(math.Floor(float64(a / b)))
138150
r := a - q*b
139151
return q, Float(r)

py/int.go

+18
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ func IntNew(metatype *Type, args Tuple, kwargs StringDict) Object {
8181

8282
// Arithmetic
8383

84+
// Errors
85+
var divisionByZero = ExceptionNewf(ZeroDivisionError, "division by zero")
86+
8487
// Convert an Object to an Int
8588
//
8689
// Retrurns ok as to whether the conversion worked or not
@@ -188,6 +191,9 @@ func (a Int) M__floordiv__(other Object) Object {
188191

189192
func (a Int) M__rfloordiv__(other Object) Object {
190193
if b, ok := convertToInt(other); ok {
194+
if a == 0 {
195+
panic(divisionByZero)
196+
}
191197
return Int(b / a)
192198
}
193199
return NotImplemented
@@ -199,13 +205,19 @@ func (a Int) M__ifloordiv__(other Object) Object {
199205

200206
func (a Int) M__mod__(other Object) Object {
201207
if b, ok := convertToInt(other); ok {
208+
if b == 0 {
209+
panic(divisionByZero)
210+
}
202211
return Int(a % b)
203212
}
204213
return NotImplemented
205214
}
206215

207216
func (a Int) M__rmod__(other Object) Object {
208217
if b, ok := convertToInt(other); ok {
218+
if a == 0 {
219+
panic(divisionByZero)
220+
}
209221
return Int(b % a)
210222
}
211223
return NotImplemented
@@ -217,13 +229,19 @@ func (a Int) M__imod__(other Object) Object {
217229

218230
func (a Int) M__divmod__(other Object) (Object, Object) {
219231
if b, ok := convertToInt(other); ok {
232+
if b == 0 {
233+
panic(divisionByZero)
234+
}
220235
return Int(a / b), Int(a % b)
221236
}
222237
return NotImplemented, None
223238
}
224239

225240
func (a Int) M__rdivmod__(other Object) (Object, Object) {
226241
if b, ok := convertToInt(other); ok {
242+
if a == 0 {
243+
panic(divisionByZero)
244+
}
227245
return Int(b / a), Int(b % a)
228246
}
229247
return NotImplemented, None

0 commit comments

Comments
 (0)