Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abdc634

Browse files
corona10vstinner
authored andcommitted
bpo-39200: Correct the error message for min/max builtin function (GH-17814)
Correct the error message when calling the min() or max() with no arguments.
1 parent c39b52f commit abdc634

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,12 @@ def test_max(self):
949949
self.assertEqual(max(1, 2.0, 3), 3)
950950
self.assertEqual(max(1.0, 2, 3), 3)
951951

952-
self.assertRaises(TypeError, max)
952+
with self.assertRaisesRegex(
953+
TypeError,
954+
'max expected at least 1 argument, got 0'
955+
):
956+
max()
957+
953958
self.assertRaises(TypeError, max, 42)
954959
self.assertRaises(ValueError, max, ())
955960
class BadSeq:
@@ -1003,7 +1008,12 @@ def test_min(self):
10031008
self.assertEqual(min(1, 2.0, 3), 1)
10041009
self.assertEqual(min(1.0, 2, 3), 1.0)
10051010

1006-
self.assertRaises(TypeError, min)
1011+
with self.assertRaisesRegex(
1012+
TypeError,
1013+
'min expected at least 1 argument, got 0'
1014+
):
1015+
min()
1016+
10071017
self.assertRaises(TypeError, min, 42)
10081018
self.assertRaises(ValueError, min, ())
10091019
class BadSeq:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correct the error message when calling the :func:`min` or :func:`max` with
2+
no arguments. Patch by Dong-hee Na.

Python/bltinmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,10 +1589,15 @@ min_max(PyObject *args, PyObject *kwds, int op)
15891589
const int positional = PyTuple_Size(args) > 1;
15901590
int ret;
15911591

1592-
if (positional)
1592+
if (positional) {
15931593
v = args;
1594-
else if (!PyArg_UnpackTuple(args, name, 1, 1, &v))
1594+
}
1595+
else if (!PyArg_UnpackTuple(args, name, 1, 1, &v)) {
1596+
if (PyExceptionClass_Check(PyExc_TypeError)) {
1597+
PyErr_Format(PyExc_TypeError, "%s expected at least 1 argument, got 0", name);
1598+
}
15951599
return NULL;
1600+
}
15961601

15971602
emptytuple = PyTuple_New(0);
15981603
if (emptytuple == NULL)

0 commit comments

Comments
 (0)