Skip to content

Commit 32d96a2

Browse files
bpo-23867: Argument Clinic: inline parsing code for a single positional parameter. (GH-9689)
1 parent 65ce60a commit 32d96a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1677
-275
lines changed

Include/modsupport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
6666
#define _PyArg_NoPositional(funcname, args) \
6767
((args) == NULL || _PyArg_NoPositional((funcname), (args)))
6868

69+
PyAPI_FUNC(void) _PyArg_BadArgument(const char *, const char *, PyObject *);
70+
6971
#endif
7072

7173
PyAPI_FUNC(PyObject *) Py_VaBuildValue(const char *, va_list);

Lib/test/clinic.test

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,11 @@ test_PyBytesObject_converter(PyObject *module, PyObject *arg)
201201
PyObject *return_value = NULL;
202202
PyBytesObject *a;
203203

204-
if (!PyArg_Parse(arg, "S:test_PyBytesObject_converter", &a)) {
204+
if (!PyBytes_Check(arg)) {
205+
_PyArg_BadArgument("test_PyBytesObject_converter", "bytes", arg);
205206
goto exit;
206207
}
208+
a = (PyBytesObject *)arg;
207209
return_value = test_PyBytesObject_converter_impl(module, a);
208210

209211
exit:
@@ -212,7 +214,7 @@ exit:
212214

213215
static PyObject *
214216
test_PyBytesObject_converter_impl(PyObject *module, PyBytesObject *a)
215-
/*[clinic end generated code: output=8dbf43c604ced031 input=12b10c7cb5750400]*/
217+
/*[clinic end generated code: output=fd69d6df4d26c853 input=12b10c7cb5750400]*/
216218

217219
/*[clinic input]
218220
test_PyByteArrayObject_converter
@@ -239,9 +241,11 @@ test_PyByteArrayObject_converter(PyObject *module, PyObject *arg)
239241
PyObject *return_value = NULL;
240242
PyByteArrayObject *a;
241243

242-
if (!PyArg_Parse(arg, "Y:test_PyByteArrayObject_converter", &a)) {
244+
if (!PyByteArray_Check(arg)) {
245+
_PyArg_BadArgument("test_PyByteArrayObject_converter", "bytearray", arg);
243246
goto exit;
244247
}
248+
a = (PyByteArrayObject *)arg;
245249
return_value = test_PyByteArrayObject_converter_impl(module, a);
246250

247251
exit:
@@ -250,7 +254,7 @@ exit:
250254

251255
static PyObject *
252256
test_PyByteArrayObject_converter_impl(PyObject *module, PyByteArrayObject *a)
253-
/*[clinic end generated code: output=ade99fc6705e7d6e input=5a657da535d194ae]*/
257+
/*[clinic end generated code: output=d309c909182c4183 input=5a657da535d194ae]*/
254258

255259
/*[clinic input]
256260
test_unicode_converter
@@ -277,9 +281,14 @@ test_unicode_converter(PyObject *module, PyObject *arg)
277281
PyObject *return_value = NULL;
278282
PyObject *a;
279283

280-
if (!PyArg_Parse(arg, "U:test_unicode_converter", &a)) {
284+
if (!PyUnicode_Check(arg)) {
285+
_PyArg_BadArgument("test_unicode_converter", "str", arg);
281286
goto exit;
282287
}
288+
if (PyUnicode_READY(arg) == -1) {
289+
goto exit;
290+
}
291+
a = arg;
283292
return_value = test_unicode_converter_impl(module, a);
284293

285294
exit:
@@ -288,7 +297,7 @@ exit:
288297

289298
static PyObject *
290299
test_unicode_converter_impl(PyObject *module, PyObject *a)
291-
/*[clinic end generated code: output=504a2c8d00370adf input=aa33612df92aa9c5]*/
300+
/*[clinic end generated code: output=ca603454e1f8f764 input=aa33612df92aa9c5]*/
292301

293302
/*[clinic input]
294303
test_bool_converter
@@ -1027,7 +1036,8 @@ test_Py_complex_converter(PyObject *module, PyObject *arg)
10271036
PyObject *return_value = NULL;
10281037
Py_complex a;
10291038

1030-
if (!PyArg_Parse(arg, "D:test_Py_complex_converter", &a)) {
1039+
a = PyComplex_AsCComplex(arg);
1040+
if (PyErr_Occurred()) {
10311041
goto exit;
10321042
}
10331043
return_value = test_Py_complex_converter_impl(module, a);
@@ -1038,7 +1048,7 @@ exit:
10381048

10391049
static PyObject *
10401050
test_Py_complex_converter_impl(PyObject *module, Py_complex a)
1041-
/*[clinic end generated code: output=27efb4ff772d6170 input=070f216a515beb79]*/
1051+
/*[clinic end generated code: output=c2ecbec2144ca540 input=070f216a515beb79]*/
10421052

10431053
/*[clinic input]
10441054
test_str_converter

Modules/_io/clinic/bufferedio.c.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ _io__BufferedIOBase_readinto(PyObject *self, PyObject *arg)
1919
PyObject *return_value = NULL;
2020
Py_buffer buffer = {NULL, NULL};
2121

22-
if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
22+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
23+
PyErr_Clear();
24+
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
25+
goto exit;
26+
}
27+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
28+
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
2329
goto exit;
2430
}
2531
return_value = _io__BufferedIOBase_readinto_impl(self, &buffer);
@@ -50,7 +56,13 @@ _io__BufferedIOBase_readinto1(PyObject *self, PyObject *arg)
5056
PyObject *return_value = NULL;
5157
Py_buffer buffer = {NULL, NULL};
5258

53-
if (!PyArg_Parse(arg, "w*:readinto1", &buffer)) {
59+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
60+
PyErr_Clear();
61+
_PyArg_BadArgument("readinto1", "read-write bytes-like object", arg);
62+
goto exit;
63+
}
64+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
65+
_PyArg_BadArgument("readinto1", "contiguous buffer", arg);
5466
goto exit;
5567
}
5668
return_value = _io__BufferedIOBase_readinto1_impl(self, &buffer);
@@ -183,7 +195,13 @@ _io__Buffered_readinto(buffered *self, PyObject *arg)
183195
PyObject *return_value = NULL;
184196
Py_buffer buffer = {NULL, NULL};
185197

186-
if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
198+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
199+
PyErr_Clear();
200+
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
201+
goto exit;
202+
}
203+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
204+
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
187205
goto exit;
188206
}
189207
return_value = _io__Buffered_readinto_impl(self, &buffer);
@@ -214,7 +232,13 @@ _io__Buffered_readinto1(buffered *self, PyObject *arg)
214232
PyObject *return_value = NULL;
215233
Py_buffer buffer = {NULL, NULL};
216234

217-
if (!PyArg_Parse(arg, "w*:readinto1", &buffer)) {
235+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
236+
PyErr_Clear();
237+
_PyArg_BadArgument("readinto1", "read-write bytes-like object", arg);
238+
goto exit;
239+
}
240+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
241+
_PyArg_BadArgument("readinto1", "contiguous buffer", arg);
218242
goto exit;
219243
}
220244
return_value = _io__Buffered_readinto1_impl(self, &buffer);
@@ -390,7 +414,11 @@ _io_BufferedWriter_write(buffered *self, PyObject *arg)
390414
PyObject *return_value = NULL;
391415
Py_buffer buffer = {NULL, NULL};
392416

393-
if (!PyArg_Parse(arg, "y*:write", &buffer)) {
417+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_SIMPLE) != 0) {
418+
goto exit;
419+
}
420+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
421+
_PyArg_BadArgument("write", "contiguous buffer", arg);
394422
goto exit;
395423
}
396424
return_value = _io_BufferedWriter_write_impl(self, &buffer);
@@ -476,4 +504,4 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
476504
exit:
477505
return return_value;
478506
}
479-
/*[clinic end generated code: output=cb4bf8d50533953b input=a9049054013a1b77]*/
507+
/*[clinic end generated code: output=40de95d461a20782 input=a9049054013a1b77]*/

Modules/_io/clinic/bytesio.c.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,13 @@ _io_BytesIO_readinto(bytesio *self, PyObject *arg)
296296
PyObject *return_value = NULL;
297297
Py_buffer buffer = {NULL, NULL};
298298

299-
if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
299+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
300+
PyErr_Clear();
301+
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
302+
goto exit;
303+
}
304+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
305+
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
300306
goto exit;
301307
}
302308
return_value = _io_BytesIO_readinto_impl(self, &buffer);
@@ -444,4 +450,4 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
444450
exit:
445451
return return_value;
446452
}
447-
/*[clinic end generated code: output=89538a941ae1267a input=a9049054013a1b77]*/
453+
/*[clinic end generated code: output=f6e720f38fc6e3cd input=a9049054013a1b77]*/

Modules/_io/clinic/fileio.c.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,13 @@ _io_FileIO_readinto(fileio *self, PyObject *arg)
156156
PyObject *return_value = NULL;
157157
Py_buffer buffer = {NULL, NULL};
158158

159-
if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
159+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
160+
PyErr_Clear();
161+
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
162+
goto exit;
163+
}
164+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
165+
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
160166
goto exit;
161167
}
162168
return_value = _io_FileIO_readinto_impl(self, &buffer);
@@ -245,7 +251,11 @@ _io_FileIO_write(fileio *self, PyObject *arg)
245251
PyObject *return_value = NULL;
246252
Py_buffer b = {NULL, NULL};
247253

248-
if (!PyArg_Parse(arg, "y*:write", &b)) {
254+
if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) {
255+
goto exit;
256+
}
257+
if (!PyBuffer_IsContiguous(&b, 'C')) {
258+
_PyArg_BadArgument("write", "contiguous buffer", arg);
249259
goto exit;
250260
}
251261
return_value = _io_FileIO_write_impl(self, &b);
@@ -373,4 +383,4 @@ _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
373383
#ifndef _IO_FILEIO_TRUNCATE_METHODDEF
374384
#define _IO_FILEIO_TRUNCATE_METHODDEF
375385
#endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
376-
/*[clinic end generated code: output=9d44e7035bce105d input=a9049054013a1b77]*/
386+
/*[clinic end generated code: output=8be0ea9a5ac7aa43 input=a9049054013a1b77]*/

Modules/_io/clinic/textio.c.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,14 @@ _io_TextIOWrapper_write(textio *self, PyObject *arg)
250250
PyObject *return_value = NULL;
251251
PyObject *text;
252252

253-
if (!PyArg_Parse(arg, "U:write", &text)) {
253+
if (!PyUnicode_Check(arg)) {
254+
_PyArg_BadArgument("write", "str", arg);
254255
goto exit;
255256
}
257+
if (PyUnicode_READY(arg) == -1) {
258+
goto exit;
259+
}
260+
text = arg;
256261
return_value = _io_TextIOWrapper_write_impl(self, text);
257262

258263
exit:
@@ -504,4 +509,4 @@ _io_TextIOWrapper_close(textio *self, PyObject *Py_UNUSED(ignored))
504509
{
505510
return _io_TextIOWrapper_close_impl(self);
506511
}
507-
/*[clinic end generated code: output=a811badd76bfe92e input=a9049054013a1b77]*/
512+
/*[clinic end generated code: output=b933f08c2f2d85cd input=a9049054013a1b77]*/

Modules/_io/clinic/winconsoleio.c.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,13 @@ _io__WindowsConsoleIO_readinto(winconsoleio *self, PyObject *arg)
156156
PyObject *return_value = NULL;
157157
Py_buffer buffer = {NULL, NULL};
158158

159-
if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
159+
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
160+
PyErr_Clear();
161+
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
162+
goto exit;
163+
}
164+
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
165+
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
160166
goto exit;
161167
}
162168
return_value = _io__WindowsConsoleIO_readinto_impl(self, &buffer);
@@ -255,7 +261,11 @@ _io__WindowsConsoleIO_write(winconsoleio *self, PyObject *arg)
255261
PyObject *return_value = NULL;
256262
Py_buffer b = {NULL, NULL};
257263

258-
if (!PyArg_Parse(arg, "y*:write", &b)) {
264+
if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) {
265+
goto exit;
266+
}
267+
if (!PyBuffer_IsContiguous(&b, 'C')) {
268+
_PyArg_BadArgument("write", "contiguous buffer", arg);
259269
goto exit;
260270
}
261271
return_value = _io__WindowsConsoleIO_write_impl(self, &b);
@@ -328,4 +338,4 @@ _io__WindowsConsoleIO_isatty(winconsoleio *self, PyObject *Py_UNUSED(ignored))
328338
#ifndef _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
329339
#define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
330340
#endif /* !defined(_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF) */
331-
/*[clinic end generated code: output=080af41338394b49 input=a9049054013a1b77]*/
341+
/*[clinic end generated code: output=4337e8de65915a1e input=a9049054013a1b77]*/

Modules/_sha3/clinic/sha3module.c.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ _sha3_shake_128_digest(SHA3object *self, PyObject *arg)
8383
PyObject *return_value = NULL;
8484
unsigned long length;
8585

86-
if (!PyArg_Parse(arg, "O&:digest", _PyLong_UnsignedLong_Converter, &length)) {
86+
if (!_PyLong_UnsignedLong_Converter(arg, &length)) {
8787
goto exit;
8888
}
8989
return_value = _sha3_shake_128_digest_impl(self, length);
@@ -110,12 +110,12 @@ _sha3_shake_128_hexdigest(SHA3object *self, PyObject *arg)
110110
PyObject *return_value = NULL;
111111
unsigned long length;
112112

113-
if (!PyArg_Parse(arg, "O&:hexdigest", _PyLong_UnsignedLong_Converter, &length)) {
113+
if (!_PyLong_UnsignedLong_Converter(arg, &length)) {
114114
goto exit;
115115
}
116116
return_value = _sha3_shake_128_hexdigest_impl(self, length);
117117

118118
exit:
119119
return return_value;
120120
}
121-
/*[clinic end generated code: output=bf823532a7bffe68 input=a9049054013a1b77]*/
121+
/*[clinic end generated code: output=5b3e99b9a96471e8 input=a9049054013a1b77]*/

Modules/_struct.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class cache_struct_converter(CConverter):
9696
[python start generated code]*/
9797
/*[python end generated code: output=da39a3ee5e6b4b0d input=49957cca130ffb63]*/
9898

99-
static int cache_struct_converter(PyObject *, PyObject **);
99+
static int cache_struct_converter(PyObject *, PyStructObject **);
100100

101101
#include "clinic/_struct.c.h"
102102

@@ -2072,7 +2072,7 @@ PyTypeObject PyStructType = {
20722072
static PyObject *cache = NULL;
20732073

20742074
static int
2075-
cache_struct_converter(PyObject *fmt, PyObject **ptr)
2075+
cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
20762076
{
20772077
PyObject * s_object;
20782078

@@ -2091,7 +2091,7 @@ cache_struct_converter(PyObject *fmt, PyObject **ptr)
20912091
s_object = PyDict_GetItem(cache, fmt);
20922092
if (s_object != NULL) {
20932093
Py_INCREF(s_object);
2094-
*ptr = s_object;
2094+
*ptr = (PyStructObject *)s_object;
20952095
return Py_CLEANUP_SUPPORTED;
20962096
}
20972097

@@ -2102,7 +2102,7 @@ cache_struct_converter(PyObject *fmt, PyObject **ptr)
21022102
/* Attempt to cache the result */
21032103
if (PyDict_SetItem(cache, fmt, s_object) == -1)
21042104
PyErr_Clear();
2105-
*ptr = s_object;
2105+
*ptr = (PyStructObject *)s_object;
21062106
return Py_CLEANUP_SUPPORTED;
21072107
}
21082108
return 0;
@@ -2157,7 +2157,7 @@ pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
21572157
}
21582158
format = args[0];
21592159

2160-
if (!cache_struct_converter(format, &s_object)) {
2160+
if (!cache_struct_converter(format, (PyStructObject **)&s_object)) {
21612161
return NULL;
21622162
}
21632163
result = s_pack(s_object, args + 1, nargs - 1);
@@ -2185,7 +2185,7 @@ pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
21852185
}
21862186
format = args[0];
21872187

2188-
if (!cache_struct_converter(format, &s_object)) {
2188+
if (!cache_struct_converter(format, (PyStructObject **)&s_object)) {
21892189
return NULL;
21902190
}
21912191
result = s_pack_into(s_object, args + 1, nargs - 1);

Modules/cjkcodecs/clinic/multibytecodec.c.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ _multibytecodec_MultibyteIncrementalEncoder_setstate(MultibyteIncrementalEncoder
150150
PyObject *return_value = NULL;
151151
PyLongObject *statelong;
152152

153-
if (!PyArg_Parse(arg, "O!:setstate", &PyLong_Type, &statelong)) {
153+
if (!PyLong_Check(arg)) {
154+
_PyArg_BadArgument("setstate", "int", arg);
154155
goto exit;
155156
}
157+
statelong = (PyLongObject *)arg;
156158
return_value = _multibytecodec_MultibyteIncrementalEncoder_setstate_impl(self, statelong);
157159

158160
exit:
@@ -248,9 +250,11 @@ _multibytecodec_MultibyteIncrementalDecoder_setstate(MultibyteIncrementalDecoder
248250
PyObject *return_value = NULL;
249251
PyObject *state;
250252

251-
if (!PyArg_Parse(arg, "O!:setstate", &PyTuple_Type, &state)) {
253+
if (!PyTuple_Check(arg)) {
254+
_PyArg_BadArgument("setstate", "tuple", arg);
252255
goto exit;
253256
}
257+
state = arg;
254258
return_value = _multibytecodec_MultibyteIncrementalDecoder_setstate_impl(self, state);
255259

256260
exit:
@@ -418,4 +422,4 @@ PyDoc_STRVAR(_multibytecodec___create_codec__doc__,
418422

419423
#define _MULTIBYTECODEC___CREATE_CODEC_METHODDEF \
420424
{"__create_codec", (PyCFunction)_multibytecodec___create_codec, METH_O, _multibytecodec___create_codec__doc__},
421-
/*[clinic end generated code: output=4c1dc8015ee5abb4 input=a9049054013a1b77]*/
425+
/*[clinic end generated code: output=a94364d0965adf1d input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)