bpo-30600: Fix error messages (condition order in Argument Clinic) (#2051)

The function '_PyArg_ParseStack()' and
'_PyArg_UnpackStack' were failing (with error
"XXX() takes Y argument (Z given)") before
the function '_PyArg_NoStackKeywords()' was called.
Thus, the latter did not raise its more meaningful
error : "XXX() takes no keyword arguments".
diff --git a/Objects/clinic/bytearrayobject.c.h b/Objects/clinic/bytearrayobject.c.h
index a181d9a..8363dbd 100644
--- a/Objects/clinic/bytearrayobject.c.h
+++ b/Objects/clinic/bytearrayobject.c.h
@@ -100,12 +100,12 @@
     Py_buffer frm = {NULL, NULL};
     Py_buffer to = {NULL, NULL};
 
-    if (!_PyArg_ParseStack(args, nargs, "y*y*:maketrans",
-        &frm, &to)) {
+    if (!_PyArg_NoStackKeywords("maketrans", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("maketrans", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "y*y*:maketrans",
+        &frm, &to)) {
         goto exit;
     }
     return_value = bytearray_maketrans_impl(&frm, &to);
@@ -151,12 +151,12 @@
     Py_buffer new = {NULL, NULL};
     Py_ssize_t count = -1;
 
-    if (!_PyArg_ParseStack(args, nargs, "y*y*|n:replace",
-        &old, &new, &count)) {
+    if (!_PyArg_NoStackKeywords("replace", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("replace", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "y*y*|n:replace",
+        &old, &new, &count)) {
         goto exit;
     }
     return_value = bytearray_replace_impl(self, &old, &new, count);
@@ -330,12 +330,12 @@
     Py_ssize_t index;
     int item;
 
-    if (!_PyArg_ParseStack(args, nargs, "nO&:insert",
-        &index, _getbytevalue, &item)) {
+    if (!_PyArg_NoStackKeywords("insert", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("insert", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "nO&:insert",
+        &index, _getbytevalue, &item)) {
         goto exit;
     }
     return_value = bytearray_insert_impl(self, index, item);
@@ -410,12 +410,12 @@
     PyObject *return_value = NULL;
     Py_ssize_t index = -1;
 
-    if (!_PyArg_ParseStack(args, nargs, "|n:pop",
-        &index)) {
+    if (!_PyArg_NoStackKeywords("pop", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("pop", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "|n:pop",
+        &index)) {
         goto exit;
     }
     return_value = bytearray_pop_impl(self, index);
@@ -474,13 +474,13 @@
     PyObject *return_value = NULL;
     PyObject *bytes = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "strip",
-        0, 1,
-        &bytes)) {
+    if (!_PyArg_NoStackKeywords("strip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("strip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "strip",
+        0, 1,
+        &bytes)) {
         goto exit;
     }
     return_value = bytearray_strip_impl(self, bytes);
@@ -509,13 +509,13 @@
     PyObject *return_value = NULL;
     PyObject *bytes = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "lstrip",
-        0, 1,
-        &bytes)) {
+    if (!_PyArg_NoStackKeywords("lstrip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("lstrip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "lstrip",
+        0, 1,
+        &bytes)) {
         goto exit;
     }
     return_value = bytearray_lstrip_impl(self, bytes);
@@ -544,13 +544,13 @@
     PyObject *return_value = NULL;
     PyObject *bytes = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "rstrip",
-        0, 1,
-        &bytes)) {
+    if (!_PyArg_NoStackKeywords("rstrip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("rstrip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "rstrip",
+        0, 1,
+        &bytes)) {
         goto exit;
     }
     return_value = bytearray_rstrip_impl(self, bytes);
@@ -712,12 +712,12 @@
     PyObject *return_value = NULL;
     int proto = 0;
 
-    if (!_PyArg_ParseStack(args, nargs, "|i:__reduce_ex__",
-        &proto)) {
+    if (!_PyArg_NoStackKeywords("__reduce_ex__", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("__reduce_ex__", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "|i:__reduce_ex__",
+        &proto)) {
         goto exit;
     }
     return_value = bytearray_reduce_ex_impl(self, proto);
@@ -743,4 +743,4 @@
 {
     return bytearray_sizeof_impl(self);
 }
-/*[clinic end generated code: output=f5c364927425fae8 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=f0079d8ee82614f7 input=a9049054013a1b77]*/
diff --git a/Objects/clinic/bytesobject.c.h b/Objects/clinic/bytesobject.c.h
index c73b560..da17cf8 100644
--- a/Objects/clinic/bytesobject.c.h
+++ b/Objects/clinic/bytesobject.c.h
@@ -195,13 +195,13 @@
     PyObject *return_value = NULL;
     PyObject *bytes = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "strip",
-        0, 1,
-        &bytes)) {
+    if (!_PyArg_NoStackKeywords("strip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("strip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "strip",
+        0, 1,
+        &bytes)) {
         goto exit;
     }
     return_value = bytes_strip_impl(self, bytes);
@@ -230,13 +230,13 @@
     PyObject *return_value = NULL;
     PyObject *bytes = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "lstrip",
-        0, 1,
-        &bytes)) {
+    if (!_PyArg_NoStackKeywords("lstrip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("lstrip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "lstrip",
+        0, 1,
+        &bytes)) {
         goto exit;
     }
     return_value = bytes_lstrip_impl(self, bytes);
@@ -265,13 +265,13 @@
     PyObject *return_value = NULL;
     PyObject *bytes = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "rstrip",
-        0, 1,
-        &bytes)) {
+    if (!_PyArg_NoStackKeywords("rstrip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("rstrip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "rstrip",
+        0, 1,
+        &bytes)) {
         goto exit;
     }
     return_value = bytes_rstrip_impl(self, bytes);
@@ -342,12 +342,12 @@
     Py_buffer frm = {NULL, NULL};
     Py_buffer to = {NULL, NULL};
 
-    if (!_PyArg_ParseStack(args, nargs, "y*y*:maketrans",
-        &frm, &to)) {
+    if (!_PyArg_NoStackKeywords("maketrans", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("maketrans", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "y*y*:maketrans",
+        &frm, &to)) {
         goto exit;
     }
     return_value = bytes_maketrans_impl(&frm, &to);
@@ -393,12 +393,12 @@
     Py_buffer new = {NULL, NULL};
     Py_ssize_t count = -1;
 
-    if (!_PyArg_ParseStack(args, nargs, "y*y*|n:replace",
-        &old, &new, &count)) {
+    if (!_PyArg_NoStackKeywords("replace", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("replace", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "y*y*|n:replace",
+        &old, &new, &count)) {
         goto exit;
     }
     return_value = bytes_replace_impl(self, &old, &new, count);
@@ -519,4 +519,4 @@
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=2504c1225108d348 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=a82999760469bbec input=a9049054013a1b77]*/
diff --git a/Objects/clinic/dictobject.c.h b/Objects/clinic/dictobject.c.h
index fb1e797..2e11cd2 100644
--- a/Objects/clinic/dictobject.c.h
+++ b/Objects/clinic/dictobject.c.h
@@ -21,13 +21,13 @@
     PyObject *iterable;
     PyObject *value = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "fromkeys",
-        1, 2,
-        &iterable, &value)) {
+    if (!_PyArg_NoStackKeywords("fromkeys", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("fromkeys", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "fromkeys",
+        1, 2,
+        &iterable, &value)) {
         goto exit;
     }
     return_value = dict_fromkeys_impl(type, iterable, value);
@@ -64,13 +64,13 @@
     PyObject *key;
     PyObject *default_value = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "get",
-        1, 2,
-        &key, &default_value)) {
+    if (!_PyArg_NoStackKeywords("get", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("get", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "get",
+        1, 2,
+        &key, &default_value)) {
         goto exit;
     }
     return_value = dict_get_impl(self, key, default_value);
@@ -101,13 +101,13 @@
     PyObject *key;
     PyObject *default_value = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "setdefault",
-        1, 2,
-        &key, &default_value)) {
+    if (!_PyArg_NoStackKeywords("setdefault", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("setdefault", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "setdefault",
+        1, 2,
+        &key, &default_value)) {
         goto exit;
     }
     return_value = dict_setdefault_impl(self, key, default_value);
@@ -115,4 +115,4 @@
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=4d57df133cf66e53 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=49e03ab4360f5be0 input=a9049054013a1b77]*/
diff --git a/Objects/clinic/floatobject.c.h b/Objects/clinic/floatobject.c.h
index d9ac39d..893629b 100644
--- a/Objects/clinic/floatobject.c.h
+++ b/Objects/clinic/floatobject.c.h
@@ -58,13 +58,13 @@
     PyObject *return_value = NULL;
     PyObject *o_ndigits = NULL;
 
-    if (!_PyArg_UnpackStack(args, nargs, "__round__",
-        0, 1,
-        &o_ndigits)) {
+    if (!_PyArg_NoStackKeywords("__round__", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("__round__", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "__round__",
+        0, 1,
+        &o_ndigits)) {
         goto exit;
     }
     return_value = float___round___impl(self, o_ndigits);
@@ -273,12 +273,12 @@
     const char *typestr;
     const char *fmt;
 
-    if (!_PyArg_ParseStack(args, nargs, "ss:__set_format__",
-        &typestr, &fmt)) {
+    if (!_PyArg_NoStackKeywords("__set_format__", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("__set_format__", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "ss:__set_format__",
+        &typestr, &fmt)) {
         goto exit;
     }
     return_value = float___set_format___impl(type, typestr, fmt);
@@ -313,4 +313,4 @@
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=dc6b0b67a7e40c93 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=b2271e7413b36162 input=a9049054013a1b77]*/
diff --git a/Objects/clinic/listobject.c.h b/Objects/clinic/listobject.c.h
index 43d5599..d5b9b1e 100644
--- a/Objects/clinic/listobject.c.h
+++ b/Objects/clinic/listobject.c.h
@@ -21,12 +21,12 @@
     Py_ssize_t index;
     PyObject *object;
 
-    if (!_PyArg_ParseStack(args, nargs, "nO:insert",
-        &index, &object)) {
+    if (!_PyArg_NoStackKeywords("insert", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("insert", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "nO:insert",
+        &index, &object)) {
         goto exit;
     }
     return_value = list_insert_impl(self, index, object);
@@ -109,12 +109,12 @@
     PyObject *return_value = NULL;
     Py_ssize_t index = -1;
 
-    if (!_PyArg_ParseStack(args, nargs, "|n:pop",
-        &index)) {
+    if (!_PyArg_NoStackKeywords("pop", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("pop", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "|n:pop",
+        &index)) {
         goto exit;
     }
     return_value = list_pop_impl(self, index);
@@ -195,12 +195,12 @@
     Py_ssize_t start = 0;
     Py_ssize_t stop = PY_SSIZE_T_MAX;
 
-    if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index",
-        &value, _PyEval_SliceIndexNotNone, &start, _PyEval_SliceIndexNotNone, &stop)) {
+    if (!_PyArg_NoStackKeywords("index", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("index", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index",
+        &value, _PyEval_SliceIndexNotNone, &start, _PyEval_SliceIndexNotNone, &stop)) {
         goto exit;
     }
     return_value = list_index_impl(self, value, start, stop);
@@ -297,4 +297,4 @@
 {
     return list___reversed___impl(self);
 }
-/*[clinic end generated code: output=71deae70ca0e6799 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=63cbe6d6320e916f input=a9049054013a1b77]*/
diff --git a/Objects/clinic/tupleobject.c.h b/Objects/clinic/tupleobject.c.h
index 880b258..a6eedff 100644
--- a/Objects/clinic/tupleobject.c.h
+++ b/Objects/clinic/tupleobject.c.h
@@ -25,12 +25,12 @@
     Py_ssize_t start = 0;
     Py_ssize_t stop = PY_SSIZE_T_MAX;
 
-    if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index",
-        &value, _PyEval_SliceIndexNotNone, &start, _PyEval_SliceIndexNotNone, &stop)) {
+    if (!_PyArg_NoStackKeywords("index", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("index", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index",
+        &value, _PyEval_SliceIndexNotNone, &start, _PyEval_SliceIndexNotNone, &stop)) {
         goto exit;
     }
     return_value = tuple_index_impl(self, value, start, stop);
@@ -99,4 +99,4 @@
 {
     return tuple___getnewargs___impl(self);
 }
-/*[clinic end generated code: output=145bcfff64e8c809 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=70b4de94a0002ec3 input=a9049054013a1b77]*/
diff --git a/Objects/clinic/unicodeobject.c.h b/Objects/clinic/unicodeobject.c.h
index 509405e..7ae7a99 100644
--- a/Objects/clinic/unicodeobject.c.h
+++ b/Objects/clinic/unicodeobject.c.h
@@ -83,12 +83,12 @@
     Py_ssize_t width;
     Py_UCS4 fillchar = ' ';
 
-    if (!_PyArg_ParseStack(args, nargs, "n|O&:center",
-        &width, convert_uc, &fillchar)) {
+    if (!_PyArg_NoStackKeywords("center", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("center", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "n|O&:center",
+        &width, convert_uc, &fillchar)) {
         goto exit;
     }
     return_value = unicode_center_impl(self, width, fillchar);
@@ -435,12 +435,12 @@
     Py_ssize_t width;
     Py_UCS4 fillchar = ' ';
 
-    if (!_PyArg_ParseStack(args, nargs, "n|O&:ljust",
-        &width, convert_uc, &fillchar)) {
+    if (!_PyArg_NoStackKeywords("ljust", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("ljust", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "n|O&:ljust",
+        &width, convert_uc, &fillchar)) {
         goto exit;
     }
     return_value = unicode_ljust_impl(self, width, fillchar);
@@ -487,13 +487,13 @@
     PyObject *return_value = NULL;
     PyObject *chars = Py_None;
 
-    if (!_PyArg_UnpackStack(args, nargs, "strip",
-        0, 1,
-        &chars)) {
+    if (!_PyArg_NoStackKeywords("strip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("strip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "strip",
+        0, 1,
+        &chars)) {
         goto exit;
     }
     return_value = unicode_strip_impl(self, chars);
@@ -522,13 +522,13 @@
     PyObject *return_value = NULL;
     PyObject *chars = NULL;
 
-    if (!_PyArg_UnpackStack(args, nargs, "lstrip",
-        0, 1,
-        &chars)) {
+    if (!_PyArg_NoStackKeywords("lstrip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("lstrip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "lstrip",
+        0, 1,
+        &chars)) {
         goto exit;
     }
     return_value = unicode_lstrip_impl(self, chars);
@@ -557,13 +557,13 @@
     PyObject *return_value = NULL;
     PyObject *chars = NULL;
 
-    if (!_PyArg_UnpackStack(args, nargs, "rstrip",
-        0, 1,
-        &chars)) {
+    if (!_PyArg_NoStackKeywords("rstrip", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("rstrip", kwnames)) {
+    if (!_PyArg_UnpackStack(args, nargs, "rstrip",
+        0, 1,
+        &chars)) {
         goto exit;
     }
     return_value = unicode_rstrip_impl(self, chars);
@@ -600,12 +600,12 @@
     PyObject *new;
     Py_ssize_t count = -1;
 
-    if (!_PyArg_ParseStack(args, nargs, "UU|n:replace",
-        &old, &new, &count)) {
+    if (!_PyArg_NoStackKeywords("replace", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("replace", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "UU|n:replace",
+        &old, &new, &count)) {
         goto exit;
     }
     return_value = unicode_replace_impl(self, old, new, count);
@@ -635,12 +635,12 @@
     Py_ssize_t width;
     Py_UCS4 fillchar = ' ';
 
-    if (!_PyArg_ParseStack(args, nargs, "n|O&:rjust",
-        &width, convert_uc, &fillchar)) {
+    if (!_PyArg_NoStackKeywords("rjust", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("rjust", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "n|O&:rjust",
+        &width, convert_uc, &fillchar)) {
         goto exit;
     }
     return_value = unicode_rjust_impl(self, width, fillchar);
@@ -840,12 +840,12 @@
     PyObject *y = NULL;
     PyObject *z = NULL;
 
-    if (!_PyArg_ParseStack(args, nargs, "O|UU:maketrans",
-        &x, &y, &z)) {
+    if (!_PyArg_NoStackKeywords("maketrans", kwnames)) {
         goto exit;
     }
 
-    if (!_PyArg_NoStackKeywords("maketrans", kwnames)) {
+    if (!_PyArg_ParseStack(args, nargs, "O|UU:maketrans",
+        &x, &y, &z)) {
         goto exit;
     }
     return_value = unicode_maketrans_impl(x, y, z);
@@ -962,4 +962,4 @@
 {
     return unicode_sizeof_impl(self);
 }
-/*[clinic end generated code: output=88b06f61edd282f9 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=339a83c0c100dd17 input=a9049054013a1b77]*/