Skip to content

bpo-23867: Argument Clinic: inline parsing code for a single positional parameter. #9689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/modsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
#define _PyArg_NoPositional(funcname, args) \
((args) == NULL || _PyArg_NoPositional((funcname), (args)))

PyAPI_FUNC(void) _PyArg_BadArgument(const char *, const char *, PyObject *);

#endif

PyAPI_FUNC(PyObject *) Py_VaBuildValue(const char *, va_list);
Expand Down
26 changes: 18 additions & 8 deletions Lib/test/clinic.test
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ test_PyBytesObject_converter(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyBytesObject *a;

if (!PyArg_Parse(arg, "S:test_PyBytesObject_converter", &a)) {
if (!PyBytes_Check(arg)) {
_PyArg_BadArgument("test_PyBytesObject_converter", "bytes", arg);
goto exit;
}
a = (PyBytesObject *)arg;
return_value = test_PyBytesObject_converter_impl(module, a);

exit:
Expand All @@ -212,7 +214,7 @@ exit:

static PyObject *
test_PyBytesObject_converter_impl(PyObject *module, PyBytesObject *a)
/*[clinic end generated code: output=8dbf43c604ced031 input=12b10c7cb5750400]*/
/*[clinic end generated code: output=fd69d6df4d26c853 input=12b10c7cb5750400]*/

/*[clinic input]
test_PyByteArrayObject_converter
Expand All @@ -239,9 +241,11 @@ test_PyByteArrayObject_converter(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyByteArrayObject *a;

if (!PyArg_Parse(arg, "Y:test_PyByteArrayObject_converter", &a)) {
if (!PyByteArray_Check(arg)) {
_PyArg_BadArgument("test_PyByteArrayObject_converter", "bytearray", arg);
goto exit;
}
a = (PyByteArrayObject *)arg;
return_value = test_PyByteArrayObject_converter_impl(module, a);

exit:
Expand All @@ -250,7 +254,7 @@ exit:

static PyObject *
test_PyByteArrayObject_converter_impl(PyObject *module, PyByteArrayObject *a)
/*[clinic end generated code: output=ade99fc6705e7d6e input=5a657da535d194ae]*/
/*[clinic end generated code: output=d309c909182c4183 input=5a657da535d194ae]*/

/*[clinic input]
test_unicode_converter
Expand All @@ -277,9 +281,14 @@ test_unicode_converter(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyObject *a;

if (!PyArg_Parse(arg, "U:test_unicode_converter", &a)) {
if (!PyUnicode_Check(arg)) {
_PyArg_BadArgument("test_unicode_converter", "str", arg);
goto exit;
}
if (PyUnicode_READY(arg) == -1) {
goto exit;
}
a = arg;
return_value = test_unicode_converter_impl(module, a);

exit:
Expand All @@ -288,7 +297,7 @@ exit:

static PyObject *
test_unicode_converter_impl(PyObject *module, PyObject *a)
/*[clinic end generated code: output=504a2c8d00370adf input=aa33612df92aa9c5]*/
/*[clinic end generated code: output=ca603454e1f8f764 input=aa33612df92aa9c5]*/

/*[clinic input]
test_bool_converter
Expand Down Expand Up @@ -1027,7 +1036,8 @@ test_Py_complex_converter(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_complex a;

if (!PyArg_Parse(arg, "D:test_Py_complex_converter", &a)) {
a = PyComplex_AsCComplex(arg);
if (PyErr_Occurred()) {
goto exit;
}
return_value = test_Py_complex_converter_impl(module, a);
Expand All @@ -1038,7 +1048,7 @@ exit:

static PyObject *
test_Py_complex_converter_impl(PyObject *module, Py_complex a)
/*[clinic end generated code: output=27efb4ff772d6170 input=070f216a515beb79]*/
/*[clinic end generated code: output=c2ecbec2144ca540 input=070f216a515beb79]*/

/*[clinic input]
test_str_converter
Expand Down
40 changes: 34 additions & 6 deletions Modules/_io/clinic/bufferedio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ _io__BufferedIOBase_readinto(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear();
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
goto exit;
}
return_value = _io__BufferedIOBase_readinto_impl(self, &buffer);
Expand Down Expand Up @@ -50,7 +56,13 @@ _io__BufferedIOBase_readinto1(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "w*:readinto1", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear();
_PyArg_BadArgument("readinto1", "read-write bytes-like object", arg);
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto1", "contiguous buffer", arg);
goto exit;
}
return_value = _io__BufferedIOBase_readinto1_impl(self, &buffer);
Expand Down Expand Up @@ -183,7 +195,13 @@ _io__Buffered_readinto(buffered *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear();
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
goto exit;
}
return_value = _io__Buffered_readinto_impl(self, &buffer);
Expand Down Expand Up @@ -214,7 +232,13 @@ _io__Buffered_readinto1(buffered *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "w*:readinto1", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear();
_PyArg_BadArgument("readinto1", "read-write bytes-like object", arg);
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto1", "contiguous buffer", arg);
goto exit;
}
return_value = _io__Buffered_readinto1_impl(self, &buffer);
Expand Down Expand Up @@ -390,7 +414,11 @@ _io_BufferedWriter_write(buffered *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "y*:write", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_SIMPLE) != 0) {
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("write", "contiguous buffer", arg);
goto exit;
}
return_value = _io_BufferedWriter_write_impl(self, &buffer);
Expand Down Expand Up @@ -476,4 +504,4 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
/*[clinic end generated code: output=cb4bf8d50533953b input=a9049054013a1b77]*/
/*[clinic end generated code: output=40de95d461a20782 input=a9049054013a1b77]*/
10 changes: 8 additions & 2 deletions Modules/_io/clinic/bytesio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,13 @@ _io_BytesIO_readinto(bytesio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear();
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
goto exit;
}
return_value = _io_BytesIO_readinto_impl(self, &buffer);
Expand Down Expand Up @@ -444,4 +450,4 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
/*[clinic end generated code: output=89538a941ae1267a input=a9049054013a1b77]*/
/*[clinic end generated code: output=f6e720f38fc6e3cd input=a9049054013a1b77]*/
16 changes: 13 additions & 3 deletions Modules/_io/clinic/fileio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ _io_FileIO_readinto(fileio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear();
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
goto exit;
}
return_value = _io_FileIO_readinto_impl(self, &buffer);
Expand Down Expand Up @@ -245,7 +251,11 @@ _io_FileIO_write(fileio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer b = {NULL, NULL};

if (!PyArg_Parse(arg, "y*:write", &b)) {
if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) {
goto exit;
}
if (!PyBuffer_IsContiguous(&b, 'C')) {
_PyArg_BadArgument("write", "contiguous buffer", arg);
goto exit;
}
return_value = _io_FileIO_write_impl(self, &b);
Expand Down Expand Up @@ -373,4 +383,4 @@ _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO_FILEIO_TRUNCATE_METHODDEF
#define _IO_FILEIO_TRUNCATE_METHODDEF
#endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
/*[clinic end generated code: output=9d44e7035bce105d input=a9049054013a1b77]*/
/*[clinic end generated code: output=8be0ea9a5ac7aa43 input=a9049054013a1b77]*/
9 changes: 7 additions & 2 deletions Modules/_io/clinic/textio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,14 @@ _io_TextIOWrapper_write(textio *self, PyObject *arg)
PyObject *return_value = NULL;
PyObject *text;

if (!PyArg_Parse(arg, "U:write", &text)) {
if (!PyUnicode_Check(arg)) {
_PyArg_BadArgument("write", "str", arg);
goto exit;
}
if (PyUnicode_READY(arg) == -1) {
goto exit;
}
text = arg;
return_value = _io_TextIOWrapper_write_impl(self, text);

exit:
Expand Down Expand Up @@ -504,4 +509,4 @@ _io_TextIOWrapper_close(textio *self, PyObject *Py_UNUSED(ignored))
{
return _io_TextIOWrapper_close_impl(self);
}
/*[clinic end generated code: output=a811badd76bfe92e input=a9049054013a1b77]*/
/*[clinic end generated code: output=b933f08c2f2d85cd input=a9049054013a1b77]*/
16 changes: 13 additions & 3 deletions Modules/_io/clinic/winconsoleio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ _io__WindowsConsoleIO_readinto(winconsoleio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};

if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear();
_PyArg_BadArgument("readinto", "read-write bytes-like object", arg);
goto exit;
}
if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto", "contiguous buffer", arg);
goto exit;
}
return_value = _io__WindowsConsoleIO_readinto_impl(self, &buffer);
Expand Down Expand Up @@ -255,7 +261,11 @@ _io__WindowsConsoleIO_write(winconsoleio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer b = {NULL, NULL};

if (!PyArg_Parse(arg, "y*:write", &b)) {
if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) {
goto exit;
}
if (!PyBuffer_IsContiguous(&b, 'C')) {
_PyArg_BadArgument("write", "contiguous buffer", arg);
goto exit;
}
return_value = _io__WindowsConsoleIO_write_impl(self, &b);
Expand Down Expand Up @@ -328,4 +338,4 @@ _io__WindowsConsoleIO_isatty(winconsoleio *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#endif /* !defined(_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF) */
/*[clinic end generated code: output=080af41338394b49 input=a9049054013a1b77]*/
/*[clinic end generated code: output=4337e8de65915a1e input=a9049054013a1b77]*/
6 changes: 3 additions & 3 deletions Modules/_sha3/clinic/sha3module.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ _sha3_shake_128_digest(SHA3object *self, PyObject *arg)
PyObject *return_value = NULL;
unsigned long length;

if (!PyArg_Parse(arg, "O&:digest", _PyLong_UnsignedLong_Converter, &length)) {
if (!_PyLong_UnsignedLong_Converter(arg, &length)) {
goto exit;
}
return_value = _sha3_shake_128_digest_impl(self, length);
Expand All @@ -110,12 +110,12 @@ _sha3_shake_128_hexdigest(SHA3object *self, PyObject *arg)
PyObject *return_value = NULL;
unsigned long length;

if (!PyArg_Parse(arg, "O&:hexdigest", _PyLong_UnsignedLong_Converter, &length)) {
if (!_PyLong_UnsignedLong_Converter(arg, &length)) {
goto exit;
}
return_value = _sha3_shake_128_hexdigest_impl(self, length);

exit:
return return_value;
}
/*[clinic end generated code: output=bf823532a7bffe68 input=a9049054013a1b77]*/
/*[clinic end generated code: output=5b3e99b9a96471e8 input=a9049054013a1b77]*/
12 changes: 6 additions & 6 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class cache_struct_converter(CConverter):
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=49957cca130ffb63]*/

static int cache_struct_converter(PyObject *, PyObject **);
static int cache_struct_converter(PyObject *, PyStructObject **);

#include "clinic/_struct.c.h"

Expand Down Expand Up @@ -2072,7 +2072,7 @@ PyTypeObject PyStructType = {
static PyObject *cache = NULL;

static int
cache_struct_converter(PyObject *fmt, PyObject **ptr)
cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
{
PyObject * s_object;

Expand All @@ -2091,7 +2091,7 @@ cache_struct_converter(PyObject *fmt, PyObject **ptr)
s_object = PyDict_GetItem(cache, fmt);
if (s_object != NULL) {
Py_INCREF(s_object);
*ptr = s_object;
*ptr = (PyStructObject *)s_object;
return Py_CLEANUP_SUPPORTED;
}

Expand All @@ -2102,7 +2102,7 @@ cache_struct_converter(PyObject *fmt, PyObject **ptr)
/* Attempt to cache the result */
if (PyDict_SetItem(cache, fmt, s_object) == -1)
PyErr_Clear();
*ptr = s_object;
*ptr = (PyStructObject *)s_object;
return Py_CLEANUP_SUPPORTED;
}
return 0;
Expand Down Expand Up @@ -2157,7 +2157,7 @@ pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
}
format = args[0];

if (!cache_struct_converter(format, &s_object)) {
if (!cache_struct_converter(format, (PyStructObject **)&s_object)) {
return NULL;
}
result = s_pack(s_object, args + 1, nargs - 1);
Expand Down Expand Up @@ -2185,7 +2185,7 @@ pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
}
format = args[0];

if (!cache_struct_converter(format, &s_object)) {
if (!cache_struct_converter(format, (PyStructObject **)&s_object)) {
return NULL;
}
result = s_pack_into(s_object, args + 1, nargs - 1);
Expand Down
10 changes: 7 additions & 3 deletions Modules/cjkcodecs/clinic/multibytecodec.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ _multibytecodec_MultibyteIncrementalEncoder_setstate(MultibyteIncrementalEncoder
PyObject *return_value = NULL;
PyLongObject *statelong;

if (!PyArg_Parse(arg, "O!:setstate", &PyLong_Type, &statelong)) {
if (!PyLong_Check(arg)) {
_PyArg_BadArgument("setstate", "int", arg);
goto exit;
}
statelong = (PyLongObject *)arg;
return_value = _multibytecodec_MultibyteIncrementalEncoder_setstate_impl(self, statelong);

exit:
Expand Down Expand Up @@ -248,9 +250,11 @@ _multibytecodec_MultibyteIncrementalDecoder_setstate(MultibyteIncrementalDecoder
PyObject *return_value = NULL;
PyObject *state;

if (!PyArg_Parse(arg, "O!:setstate", &PyTuple_Type, &state)) {
if (!PyTuple_Check(arg)) {
_PyArg_BadArgument("setstate", "tuple", arg);
goto exit;
}
state = arg;
return_value = _multibytecodec_MultibyteIncrementalDecoder_setstate_impl(self, state);

exit:
Expand Down Expand Up @@ -418,4 +422,4 @@ PyDoc_STRVAR(_multibytecodec___create_codec__doc__,

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