Skip to content

Commit 1d4c2e4

Browse files
authored
gh-119057: Use better error messages for zero division (#119066)
1 parent 153b118 commit 1d4c2e4

File tree

11 files changed

+32
-20
lines changed

11 files changed

+32
-20
lines changed

Doc/howto/logging-cookbook.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2950,7 +2950,7 @@ When run, this produces a file with exactly two lines:
29502950
.. code-block:: none
29512951
29522952
28/01/2015 07:21:23|INFO|Sample message|
2953-
28/01/2015 07:21:23|ERROR|ZeroDivisionError: integer division or modulo by zero|'Traceback (most recent call last):\n File "logtest7.py", line 30, in main\n x = 1 / 0\nZeroDivisionError: integer division or modulo by zero'|
2953+
28/01/2015 07:21:23|ERROR|ZeroDivisionError: division by zero|'Traceback (most recent call last):\n File "logtest7.py", line 30, in main\n x = 1 / 0\nZeroDivisionError: division by zero'|
29542954
29552955
While the above treatment is simplistic, it points the way to how exception
29562956
information can be formatted to your liking. The :mod:`traceback` module may be

Lib/_pylong.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def int_divmod(a, b):
530530
Its time complexity is O(n**1.58), where n = #bits(a) + #bits(b).
531531
"""
532532
if b == 0:
533-
raise ZeroDivisionError
533+
raise ZeroDivisionError('division by zero')
534534
elif b < 0:
535535
q, r = int_divmod(-a, -b)
536536
return q, -r

Lib/test/mathdata/ieee754.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ inf
116116
>>> 0 ** -1
117117
Traceback (most recent call last):
118118
...
119-
ZeroDivisionError: 0.0 cannot be raised to a negative power
119+
ZeroDivisionError: zero to a negative power
120120
>>> pow(0, NAN)
121121
nan
122122

Lib/test/test_builtin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,16 @@ def test_divmod(self):
662662
self.assertAlmostEqual(result[1], exp_result[1])
663663

664664
self.assertRaises(TypeError, divmod)
665+
self.assertRaisesRegex(
666+
ZeroDivisionError,
667+
"division by zero",
668+
divmod, 1, 0,
669+
)
670+
self.assertRaisesRegex(
671+
ZeroDivisionError,
672+
"division by zero",
673+
divmod, 0.0, 0,
674+
)
665675

666676
def test_eval(self):
667677
self.assertEqual(eval('1+1'), 2)

Lib/test/test_doctest/test_doctest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ def exceptions(): r"""
10351035
... >>> x = 12
10361036
... >>> print(x//0)
10371037
... Traceback (most recent call last):
1038-
... ZeroDivisionError: integer division or modulo by zero
1038+
... ZeroDivisionError: division by zero
10391039
... '''
10401040
>>> test = doctest.DocTestFinder().find(f)[0]
10411041
>>> doctest.DocTestRunner(verbose=False).run(test)
@@ -1052,7 +1052,7 @@ def exceptions(): r"""
10521052
... >>> print('pre-exception output', x//0)
10531053
... pre-exception output
10541054
... Traceback (most recent call last):
1055-
... ZeroDivisionError: integer division or modulo by zero
1055+
... ZeroDivisionError: division by zero
10561056
... '''
10571057
>>> test = doctest.DocTestFinder().find(f)[0]
10581058
>>> doctest.DocTestRunner(verbose=False).run(test)
@@ -1063,7 +1063,7 @@ def exceptions(): r"""
10631063
print('pre-exception output', x//0)
10641064
Exception raised:
10651065
...
1066-
ZeroDivisionError: integer division or modulo by zero
1066+
ZeroDivisionError: division by zero
10671067
TestResults(failed=1, attempted=2)
10681068
10691069
Exception messages may contain newlines:
@@ -1258,7 +1258,7 @@ def exceptions(): r"""
12581258
Exception raised:
12591259
Traceback (most recent call last):
12601260
...
1261-
ZeroDivisionError: integer division or modulo by zero
1261+
ZeroDivisionError: division by zero
12621262
TestResults(failed=1, attempted=1)
12631263
12641264
>>> _colorize.COLORIZE = save_colorize

Lib/test/test_generators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ def b():
907907
File "<stdin>", line 1, in ?
908908
File "<stdin>", line 2, in g
909909
File "<stdin>", line 2, in f
910-
ZeroDivisionError: integer division or modulo by zero
910+
ZeroDivisionError: division by zero
911911
>>> next(k) # and the generator cannot be resumed
912912
Traceback (most recent call last):
913913
File "<stdin>", line 1, in ?

Lib/test/test_genexps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
next(g)
224224
File "<pyshell#35>", line 1, in <generator expression>
225225
g = (10 // i for i in (5, 0, 2))
226-
ZeroDivisionError: integer division or modulo by zero
226+
ZeroDivisionError: division by zero
227227
>>> next(g)
228228
Traceback (most recent call last):
229229
File "<pyshell#38>", line 1, in -toplevel-
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve :exc:`ZeroDivisionError` error message.
2+
Now, all error messages are harmonized: all ``/``, ``//``, and ``%``
3+
operations just use "division by zero" message.
4+
And ``0 ** -1`` operation uses "zero to a negative power".

Objects/complexobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ complex_div(PyObject *v, PyObject *w)
523523
errno = 0;
524524
quot = _Py_c_quot(a, b);
525525
if (errno == EDOM) {
526-
PyErr_SetString(PyExc_ZeroDivisionError, "complex division by zero");
526+
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
527527
return NULL;
528528
}
529529
return PyComplex_FromCComplex(quot);
@@ -554,7 +554,7 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
554554
_Py_ADJUST_ERANGE2(p.real, p.imag);
555555
if (errno == EDOM) {
556556
PyErr_SetString(PyExc_ZeroDivisionError,
557-
"0.0 to a negative or complex power");
557+
"zero to a negative or complex power");
558558
return NULL;
559559
}
560560
else if (errno == ERANGE) {

Objects/floatobject.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ float_div(PyObject *v, PyObject *w)
623623
CONVERT_TO_DOUBLE(w, b);
624624
if (b == 0.0) {
625625
PyErr_SetString(PyExc_ZeroDivisionError,
626-
"float division by zero");
626+
"division by zero");
627627
return NULL;
628628
}
629629
a = a / b;
@@ -639,7 +639,7 @@ float_rem(PyObject *v, PyObject *w)
639639
CONVERT_TO_DOUBLE(w, wx);
640640
if (wx == 0.0) {
641641
PyErr_SetString(PyExc_ZeroDivisionError,
642-
"float modulo by zero");
642+
"division by zero");
643643
return NULL;
644644
}
645645
mod = fmod(vx, wx);
@@ -704,7 +704,7 @@ float_divmod(PyObject *v, PyObject *w)
704704
CONVERT_TO_DOUBLE(v, vx);
705705
CONVERT_TO_DOUBLE(w, wx);
706706
if (wx == 0.0) {
707-
PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
707+
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
708708
return NULL;
709709
}
710710
_float_div_mod(vx, wx, &floordiv, &mod);
@@ -719,7 +719,7 @@ float_floor_div(PyObject *v, PyObject *w)
719719
CONVERT_TO_DOUBLE(v, vx);
720720
CONVERT_TO_DOUBLE(w, wx);
721721
if (wx == 0.0) {
722-
PyErr_SetString(PyExc_ZeroDivisionError, "float floor division by zero");
722+
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
723723
return NULL;
724724
}
725725
_float_div_mod(vx, wx, &floordiv, &mod);
@@ -788,8 +788,7 @@ float_pow(PyObject *v, PyObject *w, PyObject *z)
788788
int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw);
789789
if (iw < 0.0) {
790790
PyErr_SetString(PyExc_ZeroDivisionError,
791-
"0.0 cannot be raised to a "
792-
"negative power");
791+
"zero to a negative power");
793792
return NULL;
794793
}
795794
/* use correct sign if iw is odd */

Objects/longobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,8 +3121,7 @@ long_divrem(PyLongObject *a, PyLongObject *b,
31213121
PyLongObject *z;
31223122

31233123
if (size_b == 0) {
3124-
PyErr_SetString(PyExc_ZeroDivisionError,
3125-
"integer division or modulo by zero");
3124+
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
31263125
return -1;
31273126
}
31283127
if (size_a < size_b ||
@@ -3185,7 +3184,7 @@ long_rem(PyLongObject *a, PyLongObject *b, PyLongObject **prem)
31853184

31863185
if (size_b == 0) {
31873186
PyErr_SetString(PyExc_ZeroDivisionError,
3188-
"integer modulo by zero");
3187+
"division by zero");
31893188
return -1;
31903189
}
31913190
if (size_a < size_b ||

0 commit comments

Comments
 (0)