Skip to content

Commit 2a39d25

Browse files
bpo-35582: Argument Clinic: Optimize the "all boring objects" case. (GH-11520)
Use _PyArg_CheckPositional() and inlined code instead of PyArg_UnpackTuple() and _PyArg_UnpackStack() if all parameters are positional and use the "object" converter.
1 parent 4fa9591 commit 2a39d25

30 files changed

+561
-408
lines changed

Lib/test/clinic.test

+8-4
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ test_objects_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs
106106
PyObject *a;
107107
PyObject *b = NULL;
108108

109-
if (!_PyArg_UnpackStack(args, nargs, "test_objects_converter",
110-
1, 2,
111-
&a, &b)) {
109+
if (!_PyArg_CheckPositional("test_objects_converter", nargs, 1, 2)) {
112110
goto exit;
113111
}
112+
a = args[0];
113+
if (nargs < 2) {
114+
goto skip_optional;
115+
}
116+
b = args[1];
117+
skip_optional:
114118
return_value = test_objects_converter_impl(module, a, b);
115119

116120
exit:
@@ -119,7 +123,7 @@ exit:
119123

120124
static PyObject *
121125
test_objects_converter_impl(PyObject *module, PyObject *a, PyObject *b)
122-
/*[clinic end generated code: output=068c25d6ae8cd1ef input=4cbb3d9edd2a36f3]*/
126+
/*[clinic end generated code: output=58009c0e42b4834e input=4cbb3d9edd2a36f3]*/
123127

124128
/*[clinic input]
125129
test_object_converter_subclass_of

Modules/_io/clinic/bufferedio.c.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,14 @@ _io__Buffered_truncate(buffered *self, PyObject *const *args, Py_ssize_t nargs)
389389
PyObject *return_value = NULL;
390390
PyObject *pos = Py_None;
391391

392-
if (!_PyArg_UnpackStack(args, nargs, "truncate",
393-
0, 1,
394-
&pos)) {
392+
if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
395393
goto exit;
396394
}
395+
if (nargs < 1) {
396+
goto skip_optional;
397+
}
398+
pos = args[0];
399+
skip_optional:
397400
return_value = _io__Buffered_truncate_impl(self, pos);
398401

399402
exit:
@@ -591,4 +594,4 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
591594
exit:
592595
return return_value;
593596
}
594-
/*[clinic end generated code: output=a85f61f495feff5c input=a9049054013a1b77]*/
597+
/*[clinic end generated code: output=b7f51040defff318 input=a9049054013a1b77]*/

Modules/_io/clinic/bytesio.c.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,14 @@ _io_BytesIO_readlines(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
282282
PyObject *return_value = NULL;
283283
PyObject *arg = Py_None;
284284

285-
if (!_PyArg_UnpackStack(args, nargs, "readlines",
286-
0, 1,
287-
&arg)) {
285+
if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
288286
goto exit;
289287
}
288+
if (nargs < 1) {
289+
goto skip_optional;
290+
}
291+
arg = args[0];
292+
skip_optional:
290293
return_value = _io_BytesIO_readlines_impl(self, arg);
291294

292295
exit:
@@ -503,4 +506,4 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
503506
exit:
504507
return return_value;
505508
}
506-
/*[clinic end generated code: output=5c68eb481fa960bf input=a9049054013a1b77]*/
509+
/*[clinic end generated code: output=a6b47dd7921abfcd input=a9049054013a1b77]*/

Modules/_io/clinic/fileio.c.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,14 @@ _io_FileIO_truncate(fileio *self, PyObject *const *args, Py_ssize_t nargs)
368368
PyObject *return_value = NULL;
369369
PyObject *posobj = NULL;
370370

371-
if (!_PyArg_UnpackStack(args, nargs, "truncate",
372-
0, 1,
373-
&posobj)) {
371+
if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
374372
goto exit;
375373
}
374+
if (nargs < 1) {
375+
goto skip_optional;
376+
}
377+
posobj = args[0];
378+
skip_optional:
376379
return_value = _io_FileIO_truncate_impl(self, posobj);
377380

378381
exit:
@@ -402,4 +405,4 @@ _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
402405
#ifndef _IO_FILEIO_TRUNCATE_METHODDEF
403406
#define _IO_FILEIO_TRUNCATE_METHODDEF
404407
#endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
405-
/*[clinic end generated code: output=4cf4e5f0cd656b11 input=a9049054013a1b77]*/
408+
/*[clinic end generated code: output=b6f327457938d4dd input=a9049054013a1b77]*/

Modules/_io/clinic/textio.c.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,14 @@ _io_TextIOWrapper_truncate(textio *self, PyObject *const *args, Py_ssize_t nargs
419419
PyObject *return_value = NULL;
420420
PyObject *pos = Py_None;
421421

422-
if (!_PyArg_UnpackStack(args, nargs, "truncate",
423-
0, 1,
424-
&pos)) {
422+
if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
425423
goto exit;
426424
}
425+
if (nargs < 1) {
426+
goto skip_optional;
427+
}
428+
pos = args[0];
429+
skip_optional:
427430
return_value = _io_TextIOWrapper_truncate_impl(self, pos);
428431

429432
exit:
@@ -548,4 +551,4 @@ _io_TextIOWrapper_close(textio *self, PyObject *Py_UNUSED(ignored))
548551
{
549552
return _io_TextIOWrapper_close_impl(self);
550553
}
551-
/*[clinic end generated code: output=8bdd1035bf878d6f input=a9049054013a1b77]*/
554+
/*[clinic end generated code: output=c3d1b2a5d2d2d429 input=a9049054013a1b77]*/

Modules/cjkcodecs/clinic/multibytecodec.c.h

+19-10
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,14 @@ _multibytecodec_MultibyteStreamReader_read(MultibyteStreamReaderObject *self, Py
296296
PyObject *return_value = NULL;
297297
PyObject *sizeobj = Py_None;
298298

299-
if (!_PyArg_UnpackStack(args, nargs, "read",
300-
0, 1,
301-
&sizeobj)) {
299+
if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
302300
goto exit;
303301
}
302+
if (nargs < 1) {
303+
goto skip_optional;
304+
}
305+
sizeobj = args[0];
306+
skip_optional:
304307
return_value = _multibytecodec_MultibyteStreamReader_read_impl(self, sizeobj);
305308

306309
exit:
@@ -325,11 +328,14 @@ _multibytecodec_MultibyteStreamReader_readline(MultibyteStreamReaderObject *self
325328
PyObject *return_value = NULL;
326329
PyObject *sizeobj = Py_None;
327330

328-
if (!_PyArg_UnpackStack(args, nargs, "readline",
329-
0, 1,
330-
&sizeobj)) {
331+
if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
331332
goto exit;
332333
}
334+
if (nargs < 1) {
335+
goto skip_optional;
336+
}
337+
sizeobj = args[0];
338+
skip_optional:
333339
return_value = _multibytecodec_MultibyteStreamReader_readline_impl(self, sizeobj);
334340

335341
exit:
@@ -354,11 +360,14 @@ _multibytecodec_MultibyteStreamReader_readlines(MultibyteStreamReaderObject *sel
354360
PyObject *return_value = NULL;
355361
PyObject *sizehintobj = Py_None;
356362

357-
if (!_PyArg_UnpackStack(args, nargs, "readlines",
358-
0, 1,
359-
&sizehintobj)) {
363+
if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
360364
goto exit;
361365
}
366+
if (nargs < 1) {
367+
goto skip_optional;
368+
}
369+
sizehintobj = args[0];
370+
skip_optional:
362371
return_value = _multibytecodec_MultibyteStreamReader_readlines_impl(self, sizehintobj);
363372

364373
exit:
@@ -422,4 +431,4 @@ PyDoc_STRVAR(_multibytecodec___create_codec__doc__,
422431

423432
#define _MULTIBYTECODEC___CREATE_CODEC_METHODDEF \
424433
{"__create_codec", (PyCFunction)_multibytecodec___create_codec, METH_O, _multibytecodec___create_codec__doc__},
425-
/*[clinic end generated code: output=2ed7030b28a79029 input=a9049054013a1b77]*/
434+
/*[clinic end generated code: output=bcd6311010557faf input=a9049054013a1b77]*/

Modules/clinic/_abc.c.h

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/_cursesmodule.c.h

+35-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/_elementtree.c.h

+21-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)