| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5cefb6c commit 5eb788b
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | import unittest | |
| 2 | + from test.support import cpython_only | ||
| 2 | 3 | ||
| 3 | 4 | # The test cases here cover several paths through the function calling | |
| 4 | 5 | # code. They depend on the METH_XXX flag that is used to define a C | |
@@ -33,9 +34,6 @@ def test_varargs2_ext(self): | |||
| 33 | 34 | else: | |
| 34 | 35 | raise RuntimeError | |
| 35 | 36 | ||
| 36 | - def test_varargs0_kw(self): | ||
| 37 | - self.assertRaises(TypeError, {}.__contains__, x=2) | ||
| 38 | - | ||
| 39 | 37 | def test_varargs1_kw(self): | |
| 40 | 38 | self.assertRaises(TypeError, {}.__contains__, x=2) | |
| 41 | 39 | ||
@@ -122,5 +120,61 @@ def test_oldargs1_2_kw(self): | |||
| 122 | 120 | self.assertRaises(TypeError, [].count, x=2, y=2) | |
| 123 | 121 | ||
| 124 | 122 | ||
| 123 | + @cpython_only | ||
| 124 | + class CFunctionCallsErrorMessages(unittest.TestCase): | ||
| 125 | + | ||
| 126 | + def test_varargs0(self): | ||
| 127 | + msg = r"__contains__\(\) takes exactly one argument \(0 given\)" | ||
| 128 | + self.assertRaisesRegex(TypeError, msg, {}.__contains__) | ||
| 129 | + | ||
| 130 | + def test_varargs2(self): | ||
| 131 | + msg = r"__contains__\(\) takes exactly one argument \(2 given\)" | ||
| 132 | + self.assertRaisesRegex(TypeError, msg, {}.__contains__, 0, 1) | ||
| 133 | + | ||
| 134 | + def test_varargs1_kw(self): | ||
| 135 | + msg = r"__contains__\(\) takes no keyword arguments" | ||
| 136 | + self.assertRaisesRegex(TypeError, msg, {}.__contains__, x=2) | ||
| 137 | + | ||
| 138 | + def test_varargs2_kw(self): | ||
| 139 | + msg = r"__contains__\(\) takes no keyword arguments" | ||
| 140 | + self.assertRaisesRegex(TypeError, msg, {}.__contains__, x=2, y=2) | ||
| 141 | + | ||
| 142 | + def test_oldargs0_1(self): | ||
| 143 | + msg = r"keys\(\) takes no arguments \(1 given\)" | ||
| 144 | + self.assertRaisesRegex(TypeError, msg, {}.keys, 0) | ||
| 145 | + | ||
| 146 | + def test_oldargs0_2(self): | ||
| 147 | + msg = r"keys\(\) takes no arguments \(2 given\)" | ||
| 148 | + self.assertRaisesRegex(TypeError, msg, {}.keys, 0, 1) | ||
| 149 | + | ||
| 150 | + def test_oldargs0_1_kw(self): | ||
| 151 | + msg = r"keys\(\) takes no keyword arguments" | ||
| 152 | + self.assertRaisesRegex(TypeError, msg, {}.keys, x=2) | ||
| 153 | + | ||
| 154 | + def test_oldargs0_2_kw(self): | ||
| 155 | + msg = r"keys\(\) takes no keyword arguments" | ||
| 156 | + self.assertRaisesRegex(TypeError, msg, {}.keys, x=2, y=2) | ||
| 157 | + | ||
| 158 | + def test_oldargs1_0(self): | ||
| 159 | + msg = r"count\(\) takes exactly one argument \(0 given\)" | ||
| 160 | + self.assertRaisesRegex(TypeError, msg, [].count) | ||
| 161 | + | ||
| 162 | + def test_oldargs1_2(self): | ||
| 163 | + msg = r"count\(\) takes exactly one argument \(2 given\)" | ||
| 164 | + self.assertRaisesRegex(TypeError, msg, [].count, 1, 2) | ||
| 165 | + | ||
| 166 | + def test_oldargs1_0_kw(self): | ||
| 167 | + msg = r"count\(\) takes no keyword arguments" | ||
| 168 | + self.assertRaisesRegex(TypeError, msg, [].count, x=2) | ||
| 169 | + | ||
| 170 | + def test_oldargs1_1_kw(self): | ||
| 171 | + msg = r"count\(\) takes no keyword arguments" | ||
| 172 | + self.assertRaisesRegex(TypeError, msg, [].count, {}, x=2) | ||
| 173 | + | ||
| 174 | + def test_oldargs1_2_kw(self): | ||
| 175 | + msg = r"count\(\) takes no keyword arguments" | ||
| 176 | + self.assertRaisesRegex(TypeError, msg, [].count, x=2, y=2) | ||
| 177 | + | ||
| 178 | + | ||
| 125 | 179 | if __name__ == "__main__": | |
| 126 | 180 | unittest.main() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1996,7 +1996,7 @@ def __init__(self, newarg=None, *args): | |||
| 1996 | 1996 | Subclass(newarg=1) | |
| 1997 | 1997 | except TypeError as err: | |
| 1998 | 1998 | # we expect type errors because of wrong argument count | |
| 1999 | - self.assertNotIn("does not take keyword arguments", err.args[0]) | ||
| 1999 | + self.assertNotIn("keyword arguments", err.args[0]) | ||
| 2000 | 2000 | ||
| 2001 | 2001 | @support.cpython_only | |
| 2002 | 2002 | class SizeofTest(unittest.TestCase): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -466,38 +466,37 @@ _PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **arg | |||
| 466 | 466 | switch (flags) | |
| 467 | 467 | { | |
| 468 | 468 | case METH_NOARGS: | |
| 469 | + if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | ||
| 470 | + goto no_keyword_error; | ||
| 471 | + } | ||
| 472 | + | ||
| 469 | 473 | if (nargs != 0) { | |
| 470 | 474 | PyErr_Format(PyExc_TypeError, | |
| 471 | 475 | "%.200s() takes no arguments (%zd given)", | |
| 472 | 476 | method->ml_name, nargs); | |
| 473 | 477 | goto exit; | |
| 474 | 478 | } | |
| 475 | 479 | ||
| 476 | - if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | ||
| 477 | - goto no_keyword_error; | ||
| 478 | - } | ||
| 479 | - | ||
| 480 | 480 | result = (*meth) (self, NULL); | |
| 481 | 481 | break; | |
| 482 | 482 | ||
| 483 | 483 | case METH_O: | |
| 484 | + if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | ||
| 485 | + goto no_keyword_error; | ||
| 486 | + } | ||
| 487 | + | ||
| 484 | 488 | if (nargs != 1) { | |
| 485 | 489 | PyErr_Format(PyExc_TypeError, | |
| 486 | 490 | "%.200s() takes exactly one argument (%zd given)", | |
| 487 | 491 | method->ml_name, nargs); | |
| 488 | 492 | goto exit; | |
| 489 | 493 | } | |
| 490 | 494 | ||
| 491 | - if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | ||
| 492 | - goto no_keyword_error; | ||
| 493 | - } | ||
| 494 | - | ||
| 495 | 495 | result = (*meth) (self, args[0]); | |
| 496 | 496 | break; | |
| 497 | 497 | ||
| 498 | 498 | case METH_VARARGS: | |
| 499 | - if (!(flags & METH_KEYWORDS) | ||
| 500 | - && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | ||
| 499 | + if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | ||
| 501 | 500 | goto no_keyword_error; | |
| 502 | 501 | } | |
| 503 | 502 | /* fall through next case */ | |
@@ -592,7 +591,7 @@ _PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject * | |||
| 592 | 591 | ||
| 593 | 592 | PyCFunction meth = method->ml_meth; | |
| 594 | 593 | int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST); | |
| 595 | - Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames); | ||
| 594 | + Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames); | ||
| 596 | 595 | PyObject *result = NULL; | |
| 597 | 596 | ||
| 598 | 597 | if (Py_EnterRecursiveCall(" while calling a Python object")) { | |
@@ -602,32 +601,32 @@ _PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject * | |||
| 602 | 601 | switch (flags) | |
| 603 | 602 | { | |
| 604 | 603 | case METH_NOARGS: | |
| 604 | + if (nkwargs) { | ||
| 605 | + goto no_keyword_error; | ||
| 606 | + } | ||
| 607 | + | ||
| 605 | 608 | if (nargs != 0) { | |
| 606 | 609 | PyErr_Format(PyExc_TypeError, | |
| 607 | 610 | "%.200s() takes no arguments (%zd given)", | |
| 608 | 611 | method->ml_name, nargs); | |
| 609 | 612 | goto exit; | |
| 610 | 613 | } | |
| 611 | 614 | ||
| 612 | - if (nkwargs) { | ||
| 613 | - goto no_keyword_error; | ||
| 614 | - } | ||
| 615 | - | ||
| 616 | 615 | result = (*meth) (self, NULL); | |
| 617 | 616 | break; | |
| 618 | 617 | ||
| 619 | 618 | case METH_O: | |
| 619 | + if (nkwargs) { | ||
| 620 | + goto no_keyword_error; | ||
| 621 | + } | ||
| 622 | + | ||
| 620 | 623 | if (nargs != 1) { | |
| 621 | 624 | PyErr_Format(PyExc_TypeError, | |
| 622 | 625 | "%.200s() takes exactly one argument (%zd given)", | |
| 623 | 626 | method->ml_name, nargs); | |
| 624 | 627 | goto exit; | |
| 625 | 628 | } | |
| 626 | 629 | ||
| 627 | - if (nkwargs) { | ||
| 628 | - goto no_keyword_error; | ||
| 629 | - } | ||
| 630 | - | ||
| 631 | 630 | result = (*meth) (self, args[0]); | |
| 632 | 631 | break; | |
| 633 | 632 | ||
@@ -637,16 +636,17 @@ _PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject * | |||
| 637 | 636 | break; | |
| 638 | 637 | ||
| 639 | 638 | case METH_VARARGS: | |
| 639 | + if (nkwargs) { | ||
| 640 | + goto no_keyword_error; | ||
| 641 | + } | ||
| 642 | + /* fall through next case */ | ||
| 643 | + | ||
| 640 | 644 | case METH_VARARGS | METH_KEYWORDS: | |
| 641 | 645 | { | |
| 642 | 646 | /* Slow-path: create a temporary tuple for positional arguments | |
| 643 | 647 | and a temporary dict for keyword arguments */ | |
| 644 | 648 | PyObject *argtuple; | |
| 645 | 649 | ||
| 646 | - if (!(flags & METH_KEYWORDS) && nkwargs) { | ||
| 647 | - goto no_keyword_error; | ||
| 648 | - } | ||
| 649 | - | ||
| 650 | 650 | argtuple = _PyStack_AsTuple(args, nargs); | |
| 651 | 651 | if (argtuple == NULL) { | |
| 652 | 652 | goto exit; | |
@@ -717,6 +717,7 @@ static PyObject * | |||
| 717 | 717 | cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs) | |
| 718 | 718 | { | |
| 719 | 719 | assert(!PyErr_Occurred()); | |
| 720 | + assert(kwargs == NULL || PyDict_Check(kwargs)); | ||
| 720 | 721 | ||
| 721 | 722 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); | |
| 722 | 723 | PyObject *self = PyCFunction_GET_SELF(func); | |
@@ -732,7 +733,7 @@ cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs) | |||
| 732 | 733 | Py_LeaveRecursiveCall(); | |
| 733 | 734 | } | |
| 734 | 735 | else { | |
| 735 | - if (kwargs != NULL && PyDict_Size(kwargs) != 0) { | ||
| 736 | + if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | ||
| 736 | 737 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", | |
| 737 | 738 | ((PyCFunctionObject*)func)->m_ml->ml_name); | |
| 738 | 739 | return NULL; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1176,7 +1176,7 @@ wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds) | |||
| 1176 | 1176 | ||
| 1177 | 1177 | if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_GET_SIZE(kwds) != 0)) { | |
| 1178 | 1178 | PyErr_Format(PyExc_TypeError, | |
| 1179 | - "wrapper %s doesn't take keyword arguments", | ||
| 1179 | + "wrapper %s() takes no keyword arguments", | ||
| 1180 | 1180 | wp->descr->d_base->name); | |
| 1181 | 1181 | return NULL; | |
| 1182 | 1182 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1704,13 +1704,21 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, | |||
| 1704 | 1704 | break; | |
| 1705 | 1705 | } | |
| 1706 | 1706 | if (max < nargs) { | |
| 1707 | - PyErr_Format(PyExc_TypeError, | ||
| 1708 | - "%.200s%s takes %s %d positional arguments" | ||
| 1709 | - " (%d given)", | ||
| 1710 | - (fname == NULL) ? "function" : fname, | ||
| 1711 | - (fname == NULL) ? "" : "()", | ||
| 1712 | - (min != INT_MAX) ? "at most" : "exactly", | ||
| 1713 | - max, nargs); | ||
| 1707 | + if (max == 0) { | ||
| 1708 | + PyErr_Format(PyExc_TypeError, | ||
| 1709 | + "%.200s%s takes no positional arguments", | ||
| 1710 | + (fname == NULL) ? "function" : fname, | ||
| 1711 | + (fname == NULL) ? "" : "()"); | ||
| 1712 | + } | ||
| 1713 | + else { | ||
| 1714 | + PyErr_Format(PyExc_TypeError, | ||
| 1715 | + "%.200s%s takes %s %d positional arguments" | ||
| 1716 | + " (%d given)", | ||
| 1717 | + (fname == NULL) ? "function" : fname, | ||
| 1718 | + (fname == NULL) ? "" : "()", | ||
| 1719 | + (min != INT_MAX) ? "at most" : "exactly", | ||
| 1720 | + max, nargs); | ||
| 1721 | + } | ||
| 1714 | 1722 | return cleanreturn(0, &freelist); | |
| 1715 | 1723 | } | |
| 1716 | 1724 | } | |
@@ -2079,12 +2087,20 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs, | |||
| 2079 | 2087 | return cleanreturn(0, &freelist); | |
| 2080 | 2088 | } | |
| 2081 | 2089 | if (parser->max < nargs) { | |
| 2082 | - PyErr_Format(PyExc_TypeError, | ||
| 2083 | - "%200s%s takes %s %d positional arguments (%d given)", | ||
| 2084 | - (parser->fname == NULL) ? "function" : parser->fname, | ||
| 2085 | - (parser->fname == NULL) ? "" : "()", | ||
| 2086 | - (parser->min != INT_MAX) ? "at most" : "exactly", | ||
| 2087 | - parser->max, nargs); | ||
| 2090 | + if (parser->max == 0) { | ||
| 2091 | + PyErr_Format(PyExc_TypeError, | ||
| 2092 | + "%200s%s takes no positional arguments", | ||
| 2093 | + (parser->fname == NULL) ? "function" : parser->fname, | ||
| 2094 | + (parser->fname == NULL) ? "" : "()"); | ||
| 2095 | + } | ||
| 2096 | + else { | ||
| 2097 | + PyErr_Format(PyExc_TypeError, | ||
| 2098 | + "%200s%s takes %s %d positional arguments (%d given)", | ||
| 2099 | + (parser->fname == NULL) ? "function" : parser->fname, | ||
| 2100 | + (parser->fname == NULL) ? "" : "()", | ||
| 2101 | + (parser->min != INT_MAX) ? "at most" : "exactly", | ||
| 2102 | + parser->max, nargs); | ||
| 2103 | + } | ||
| 2088 | 2104 | return cleanreturn(0, &freelist); | |
| 2089 | 2105 | } | |
| 2090 | 2106 | ||
@@ -2489,7 +2505,7 @@ _PyArg_NoKeywords(const char *funcname, PyObject *kwargs) | |||
| 2489 | 2505 | return 1; | |
| 2490 | 2506 | } | |
| 2491 | 2507 | ||
| 2492 | - PyErr_Format(PyExc_TypeError, "%.200s does not take keyword arguments", | ||
| 2508 | + PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", | ||
| 2493 | 2509 | funcname); | |
| 2494 | 2510 | return 0; | |
| 2495 | 2511 | } | |
@@ -2506,7 +2522,7 @@ _PyArg_NoStackKeywords(const char *funcname, PyObject *kwnames) | |||
| 2506 | 2522 | return 1; | |
| 2507 | 2523 | } | |
| 2508 | 2524 | ||
| 2509 | - PyErr_Format(PyExc_TypeError, "%.200s does not take keyword arguments", | ||
| 2525 | + PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", | ||
| 2510 | 2526 | funcname); | |
| 2511 | 2527 | return 0; | |
| 2512 | 2528 | } | |
@@ -2524,7 +2540,7 @@ _PyArg_NoPositional(const char *funcname, PyObject *args) | |||
| 2524 | 2540 | if (PyTuple_GET_SIZE(args) == 0) | |
| 2525 | 2541 | return 1; | |
| 2526 | 2542 | ||
| 2527 | - PyErr_Format(PyExc_TypeError, "%.200s does not take positional arguments", | ||
| 2543 | + PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments", | ||
| 2528 | 2544 | funcname); | |
| 2529 | 2545 | return 0; | |
| 2530 | 2546 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments