Skip to content

Commit ce1e910

Browse files
committed
pythongh-119057: Use better error message for x // 0
1 parent d8e0e00 commit ce1e910

File tree

7 files changed

+22
-12
lines changed

7 files changed

+22
-12
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: integer division by zero|'Traceback (most recent call last):\n File "logtest7.py", line 30, in main\n x = 1 / 0\nZeroDivisionError: integer 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/test/test_builtin.py

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

664664
self.assertRaises(TypeError, divmod)
665+
self.assertRaisesRegex(
666+
ZeroDivisionError,
667+
"integer division or modulo by zero",
668+
divmod, 1, 0,
669+
)
665670

666671
def test_eval(self):
667672
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: integer 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: integer 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: integer 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: integer 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: integer 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: integer division by zero
227227
>>> next(g)
228228
Traceback (most recent call last):
229229
File "<pyshell#38>", line 1, in -toplevel-
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve error message when using ``// 0`` from "integer division or modulo
2+
by zero" to "integer division by zero" which better represents the division.

Objects/longobject.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,14 +3103,17 @@ static PyObject *long_long(PyObject *v);
31033103

31043104
static int
31053105
long_divrem(PyLongObject *a, PyLongObject *b,
3106-
PyLongObject **pdiv, PyLongObject **prem)
3106+
PyLongObject **pdiv, PyLongObject **prem, int divonly)
31073107
{
31083108
Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
31093109
PyLongObject *z;
31103110

31113111
if (size_b == 0) {
3112-
PyErr_SetString(PyExc_ZeroDivisionError,
3113-
"integer division or modulo by zero");
3112+
PyErr_SetString(
3113+
PyExc_ZeroDivisionError,
3114+
divonly ?
3115+
"integer division by zero" :
3116+
"integer division or modulo by zero");
31143117
return -1;
31153118
}
31163119
if (size_a < size_b ||
@@ -4367,7 +4370,7 @@ l_divmod(PyLongObject *v, PyLongObject *w,
43674370
return pylong_int_divmod(v, w, pdiv, pmod);
43684371
}
43694372
#endif
4370-
if (long_divrem(v, w, &div, &mod) < 0)
4373+
if (long_divrem(v, w, &div, &mod, pmod == NULL) < 0)
43714374
return -1;
43724375
if ((_PyLong_IsNegative(mod) && _PyLong_IsPositive(w)) ||
43734376
(_PyLong_IsPositive(mod) && _PyLong_IsNegative(w))) {
@@ -5978,7 +5981,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
59785981
/* Do a and b have different signs? If so, quotient is negative. */
59795982
quo_is_neg = (_PyLong_IsNegative((PyLongObject *)a)) != (_PyLong_IsNegative((PyLongObject *)b));
59805983

5981-
if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0)
5984+
if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem, 0) < 0)
59825985
goto error;
59835986

59845987
/* compare twice the remainder with the divisor, to see

0 commit comments

Comments
 (0)