diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-09-03 14:28:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-03 14:28:14 (GMT) |
commit | 1796c191b43ed0787d83c07be7de8118fb10e8b0 (patch) | |
tree | a49fc1fa54d8d6a375516f6e6d1cd6648caf5a78 /Modules | |
parent | 55846099b155833320bc6d64b03d902028bad439 (diff) | |
download | cpython-1796c191b43ed0787d83c07be7de8118fb10e8b0.zip cpython-1796c191b43ed0787d83c07be7de8118fb10e8b0.tar.gz cpython-1796c191b43ed0787d83c07be7de8118fb10e8b0.tar.bz2 |
gh-108494: Argument Clinic: inline parsing code for positional-only parameters in the limited C API (GH-108622)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_io/clinic/bufferedio.c.h | 6 | ||||
-rw-r--r-- | Modules/_io/clinic/bytesio.c.h | 3 | ||||
-rw-r--r-- | Modules/_io/clinic/fileio.c.h | 3 | ||||
-rw-r--r-- | Modules/_io/clinic/winconsoleio.c.h | 3 | ||||
-rw-r--r-- | Modules/_multiprocessing/multiprocessing.c | 9 | ||||
-rw-r--r-- | Modules/_posixsubprocess.c | 9 | ||||
-rw-r--r-- | Modules/_struct.c | 12 | ||||
-rw-r--r-- | Modules/clinic/_testclinic_limited.c.h | 22 | ||||
-rw-r--r-- | Modules/overlapped.c | 16 | ||||
-rw-r--r-- | Modules/resource.c | 9 |
10 files changed, 51 insertions, 41 deletions
diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h index b2c52cf..7577bde 100644 --- a/Modules/_io/clinic/bufferedio.c.h +++ b/Modules/_io/clinic/bufferedio.c.h @@ -26,7 +26,6 @@ _io__BufferedIOBase_readinto(PyObject *self, PyObject *arg) Py_buffer buffer = {NULL, NULL}; if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) { - PyErr_Clear(); _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg); goto exit; } @@ -63,7 +62,6 @@ _io__BufferedIOBase_readinto1(PyObject *self, PyObject *arg) Py_buffer buffer = {NULL, NULL}; if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) { - PyErr_Clear(); _PyArg_BadArgument("readinto1", "argument", "read-write bytes-like object", arg); goto exit; } @@ -590,7 +588,6 @@ _io__Buffered_readinto(buffered *self, PyObject *arg) Py_buffer buffer = {NULL, NULL}; if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) { - PyErr_Clear(); _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg); goto exit; } @@ -627,7 +624,6 @@ _io__Buffered_readinto1(buffered *self, PyObject *arg) Py_buffer buffer = {NULL, NULL}; if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) { - PyErr_Clear(); _PyArg_BadArgument("readinto1", "argument", "read-write bytes-like object", arg); goto exit; } @@ -1098,4 +1094,4 @@ skip_optional_pos: exit: return return_value; } -/*[clinic end generated code: output=9e09091995ae02b0 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f940cea085f0bf91 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/bytesio.c.h b/Modules/_io/clinic/bytesio.c.h index d736477..d42ab48 100644 --- a/Modules/_io/clinic/bytesio.c.h +++ b/Modules/_io/clinic/bytesio.c.h @@ -328,7 +328,6 @@ _io_BytesIO_readinto(bytesio *self, PyObject *arg) Py_buffer buffer = {NULL, NULL}; if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) { - PyErr_Clear(); _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg); goto exit; } @@ -538,4 +537,4 @@ skip_optional_pos: exit: return return_value; } -/*[clinic end generated code: output=8ab65edc03edbfe0 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=b753fdf1ba36c461 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/fileio.c.h b/Modules/_io/clinic/fileio.c.h index 29f8cf6..deb99fa 100644 --- a/Modules/_io/clinic/fileio.c.h +++ b/Modules/_io/clinic/fileio.c.h @@ -245,7 +245,6 @@ _io_FileIO_readinto(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_s goto exit; } if (PyObject_GetBuffer(args[0], &buffer, PyBUF_WRITABLE) < 0) { - PyErr_Clear(); _PyArg_BadArgument("readinto", "argument 1", "read-write bytes-like object", args[0]); goto exit; } @@ -536,4 +535,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=238dd48819076434 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=2ce6ce923ccef86e input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/winconsoleio.c.h b/Modules/_io/clinic/winconsoleio.c.h index 0683eec..ecc71e5 100644 --- a/Modules/_io/clinic/winconsoleio.c.h +++ b/Modules/_io/clinic/winconsoleio.c.h @@ -243,7 +243,6 @@ _io__WindowsConsoleIO_readinto(winconsoleio *self, PyTypeObject *cls, PyObject * goto exit; } if (PyObject_GetBuffer(args[0], &buffer, PyBUF_WRITABLE) < 0) { - PyErr_Clear(); _PyArg_BadArgument("readinto", "argument 1", "read-write bytes-like object", args[0]); goto exit; } @@ -465,4 +464,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=7be51d48ddb7c8c8 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=37febc4c96732b3b input=a9049054013a1b77]*/ diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c index 16b5cb5..2e6d8eb 100644 --- a/Modules/_multiprocessing/multiprocessing.c +++ b/Modules/_multiprocessing/multiprocessing.c @@ -14,16 +14,17 @@ class HANDLE_converter(CConverter): type = "HANDLE" format_unit = '"F_HANDLE"' - def parse_arg(self, argname, displayname): - return """ + def parse_arg(self, argname, displayname, *, limited_capi): + return self.format_code(""" {paramname} = PyLong_AsVoidPtr({argname}); if (!{paramname} && PyErr_Occurred()) {{{{ goto exit; }}}} - """.format(argname=argname, paramname=self.parser_name) + """, + argname=argname) [python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=3e537d244034affb]*/ +/*[python end generated code: output=da39a3ee5e6b4b0d input=3cf0318efc6a8772]*/ /*[clinic input] module _multiprocessing diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c index ef76d26..2898eed 100644 --- a/Modules/_posixsubprocess.c +++ b/Modules/_posixsubprocess.c @@ -87,15 +87,16 @@ class pid_t_converter(CConverter): type = 'pid_t' format_unit = '" _Py_PARSE_PID "' - def parse_arg(self, argname, displayname): - return """ + def parse_arg(self, argname, displayname, *, limited_capi): + return self.format_code(""" {paramname} = PyLong_AsPid({argname}); if ({paramname} == -1 && PyErr_Occurred()) {{{{ goto exit; }}}} - """.format(argname=argname, paramname=self.parser_name) + """, + argname=argname) [python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=5af1c116d56cbb5a]*/ +/*[python end generated code: output=da39a3ee5e6b4b0d input=c94349aa1aad151d]*/ #include "clinic/_posixsubprocess.c.h" diff --git a/Modules/_struct.c b/Modules/_struct.c index 4ae21cc..be4c23a 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -110,18 +110,20 @@ class cache_struct_converter(CConverter): c_default = "NULL" broken_limited_capi = True - def parse_arg(self, argname, displayname): - return """ + def parse_arg(self, argname, displayname, *, limited_capi): + assert not limited_capi + return self.format_code(""" if (!{converter}(module, {argname}, &{paramname})) {{{{ goto exit; }}}} - """.format(argname=argname, paramname=self.name, - converter=self.converter) + """, + argname=argname, + converter=self.converter) def cleanup(self): return "Py_XDECREF(%s);\n" % self.name [python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=14e83804f599ed8f]*/ +/*[python end generated code: output=da39a3ee5e6b4b0d input=c33b27d6b06006c6]*/ static int cache_struct_converter(PyObject *, PyObject *, PyStructObject **); diff --git a/Modules/clinic/_testclinic_limited.c.h b/Modules/clinic/_testclinic_limited.c.h index 93e7d7f..eff72ce 100644 --- a/Modules/clinic/_testclinic_limited.c.h +++ b/Modules/clinic/_testclinic_limited.c.h @@ -37,7 +37,8 @@ my_int_func(PyObject *module, PyObject *arg_) int arg; int _return_value; - if (!PyArg_Parse(arg_, "i:my_int_func", &arg)) { + arg = PyLong_AsInt(arg_); + if (arg == -1 && PyErr_Occurred()) { goto exit; } _return_value = my_int_func_impl(module, arg); @@ -56,22 +57,31 @@ PyDoc_STRVAR(my_int_sum__doc__, "\n"); #define MY_INT_SUM_METHODDEF \ - {"my_int_sum", (PyCFunction)my_int_sum, METH_VARARGS, my_int_sum__doc__}, + {"my_int_sum", (PyCFunction)(void(*)(void))my_int_sum, METH_FASTCALL, my_int_sum__doc__}, static int my_int_sum_impl(PyObject *module, int x, int y); static PyObject * -my_int_sum(PyObject *module, PyObject *args) +my_int_sum(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; int x; int y; int _return_value; - if (!PyArg_ParseTuple(args, "ii:my_int_sum", - &x, &y)) + if (nargs != 2) { + PyErr_Format(PyExc_TypeError, "my_int_sum expected 2 arguments, got %zd", nargs); goto exit; + } + x = PyLong_AsInt(args[0]); + if (x == -1 && PyErr_Occurred()) { + goto exit; + } + y = PyLong_AsInt(args[1]); + if (y == -1 && PyErr_Occurred()) { + goto exit; + } _return_value = my_int_sum_impl(module, x, y); if ((_return_value == -1) && PyErr_Occurred()) { goto exit; @@ -81,4 +91,4 @@ my_int_sum(PyObject *module, PyObject *args) exit: return return_value; } -/*[clinic end generated code: output=dcd5203d0d29df3a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5cf64baf978d2288 input=a9049054013a1b77]*/ diff --git a/Modules/overlapped.c b/Modules/overlapped.c index c682e6a..e23db22 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -38,13 +38,14 @@ class pointer_converter(CConverter): format_unit = '"F_POINTER"' - def parse_arg(self, argname, displayname): - return """ + def parse_arg(self, argname, displayname, *, limited_capi): + return self.format_code(""" {paramname} = PyLong_AsVoidPtr({argname}); if (!{paramname} && PyErr_Occurred()) {{{{ goto exit; }}}} - """.format(argname=argname, paramname=self.parser_name) + """, + argname=argname) class OVERLAPPED_converter(pointer_converter): type = 'OVERLAPPED *' @@ -55,13 +56,14 @@ class HANDLE_converter(pointer_converter): class ULONG_PTR_converter(pointer_converter): type = 'ULONG_PTR' - def parse_arg(self, argname, displayname): - return """ + def parse_arg(self, argname, displayname, *, limited_capi): + return self.format_code(""" {paramname} = (uintptr_t)PyLong_AsVoidPtr({argname}); if (!{paramname} && PyErr_Occurred()) {{{{ goto exit; }}}} - """.format(argname=argname, paramname=self.parser_name) + """, + argname=argname) class DWORD_converter(unsigned_long_converter): type = 'DWORD' @@ -69,7 +71,7 @@ class DWORD_converter(unsigned_long_converter): class BOOL_converter(int_converter): type = 'BOOL' [python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=8a07ea3018f4cec8]*/ +/*[python end generated code: output=da39a3ee5e6b4b0d input=436f4440630a304c]*/ /*[clinic input] module _overlapped diff --git a/Modules/resource.c b/Modules/resource.c index 9e302a3..d65509e 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -23,15 +23,16 @@ class pid_t_converter(CConverter): type = 'pid_t' format_unit = '" _Py_PARSE_PID "' - def parse_arg(self, argname, displayname): - return """ + def parse_arg(self, argname, displayname, *, limited_capi): + return self.format_code(""" {paramname} = PyLong_AsPid({argname}); if ({paramname} == -1 && PyErr_Occurred()) {{{{ goto exit; }}}} - """.format(argname=argname, paramname=self.parser_name) + """, + argname=argname) [python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=5af1c116d56cbb5a]*/ +/*[python end generated code: output=da39a3ee5e6b4b0d input=c94349aa1aad151d]*/ #include "clinic/resource.c.h" |