gh-101409: Improve generated clinic code for self type checks (#101411)

This commit is contained in:
Erlend E. Aasland 2023-01-31 21:42:03 +01:00 committed by GitHub
parent 0062f538d9
commit 2753cf2ed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 80 additions and 78 deletions

View File

@ -601,12 +601,13 @@ static int
_io_BufferedRWPair___init__(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
PyTypeObject *base_tp = &PyBufferedRWPair_Type;
PyObject *reader;
PyObject *writer;
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
if ((Py_IS_TYPE(self, &PyBufferedRWPair_Type) ||
Py_TYPE(self)->tp_new == PyBufferedRWPair_Type.tp_new) &&
if ((Py_IS_TYPE(self, base_tp) ||
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
!_PyArg_NoKeywords("BufferedRWPair", kwargs)) {
goto exit;
}
@ -713,4 +714,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=ca87adcfff6a810b input=a9049054013a1b77]*/
/*[clinic end generated code: output=953f1577e96e8d86 input=a9049054013a1b77]*/

View File

@ -16,10 +16,11 @@ static int
pysqlite_cursor_init(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
PyTypeObject *base_tp = clinic_state()->CursorType;
pysqlite_Connection *connection;
if ((Py_IS_TYPE(self, clinic_state()->CursorType) ||
Py_TYPE(self)->tp_new == clinic_state()->CursorType->tp_new) &&
if ((Py_IS_TYPE(self, base_tp) ||
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
!_PyArg_NoKeywords("Cursor", kwargs)) {
goto exit;
}
@ -318,4 +319,4 @@ pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
{
return pysqlite_cursor_close_impl(self);
}
/*[clinic end generated code: output=e53e75a32a9d92bd input=a9049054013a1b77]*/
/*[clinic end generated code: output=1f82e3c9791bb9a5 input=a9049054013a1b77]*/

View File

@ -16,11 +16,11 @@ static PyObject *
pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = clinic_state()->RowType;
pysqlite_Cursor *cursor;
PyObject *data;
if ((type == clinic_state()->RowType ||
type->tp_init == clinic_state()->RowType->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("Row", kwargs)) {
goto exit;
}
@ -60,4 +60,4 @@ pysqlite_row_keys(pysqlite_Row *self, PyObject *Py_UNUSED(ignored))
{
return pysqlite_row_keys_impl(self);
}
/*[clinic end generated code: output=87b91f234633702e input=a9049054013a1b77]*/
/*[clinic end generated code: output=157b31ac3f6af1ba input=a9049054013a1b77]*/

View File

@ -46,11 +46,11 @@ static PyObject *
tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &tuplegetter_type;
Py_ssize_t index;
PyObject *doc;
if ((type == &tuplegetter_type ||
type->tp_init == tuplegetter_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("_tuplegetter", kwargs)) {
goto exit;
}
@ -75,4 +75,4 @@ tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
/*[clinic end generated code: output=12168d58a11a4fb9 input=a9049054013a1b77]*/
/*[clinic end generated code: output=91a0f221c7b1f96c input=a9049054013a1b77]*/

View File

@ -21,14 +21,13 @@ static PyObject *
simplequeue_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = simplequeue_get_state_by_type(type)->SimpleQueueType;
if ((type == simplequeue_get_state_by_type(type)->SimpleQueueType ||
type->tp_init == simplequeue_get_state_by_type(type)->SimpleQueueType->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoPositional("SimpleQueue", args)) {
goto exit;
}
if ((type == simplequeue_get_state_by_type(type)->SimpleQueueType ||
type->tp_init == simplequeue_get_state_by_type(type)->SimpleQueueType->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("SimpleQueue", kwargs)) {
goto exit;
}
@ -332,4 +331,4 @@ _queue_SimpleQueue_qsize(simplequeueobject *self, PyObject *Py_UNUSED(ignored))
exit:
return return_value;
}
/*[clinic end generated code: output=628e992d38f50aac input=a9049054013a1b77]*/
/*[clinic end generated code: output=9a72a8d1b5767f6a input=a9049054013a1b77]*/

View File

@ -435,10 +435,10 @@ static PyObject *
_ssl__SSLContext(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = get_state_type(type)->PySSLContext_Type;
int proto_version;
if ((type == get_state_type(type)->PySSLContext_Type ||
type->tp_init == get_state_type(type)->PySSLContext_Type->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("_SSLContext", kwargs)) {
goto exit;
}
@ -1028,14 +1028,13 @@ static PyObject *
_ssl_MemoryBIO(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = get_state_type(type)->PySSLMemoryBIO_Type;
if ((type == get_state_type(type)->PySSLMemoryBIO_Type ||
type->tp_init == get_state_type(type)->PySSLMemoryBIO_Type->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoPositional("MemoryBIO", args)) {
goto exit;
}
if ((type == get_state_type(type)->PySSLMemoryBIO_Type ||
type->tp_init == get_state_type(type)->PySSLMemoryBIO_Type->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("MemoryBIO", kwargs)) {
goto exit;
}
@ -1543,4 +1542,4 @@ exit:
#ifndef _SSL_ENUM_CRLS_METHODDEF
#define _SSL_ENUM_CRLS_METHODDEF
#endif /* !defined(_SSL_ENUM_CRLS_METHODDEF) */
/*[clinic end generated code: output=a3d97a19163bb044 input=a9049054013a1b77]*/
/*[clinic end generated code: output=4d9b81fa81f520f0 input=a9049054013a1b77]*/

View File

@ -102,10 +102,10 @@ static PyObject *
pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &pairwise_type;
PyObject *iterable;
if ((type == &pairwise_type ||
type->tp_init == pairwise_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("pairwise", kwargs)) {
goto exit;
}
@ -195,11 +195,11 @@ static PyObject *
itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &_grouper_type;
PyObject *parent;
PyObject *tgtkey;
if ((type == &_grouper_type ||
type->tp_init == _grouper_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("_grouper", kwargs)) {
goto exit;
}
@ -232,12 +232,12 @@ static PyObject *
itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &teedataobject_type;
PyObject *it;
PyObject *values;
PyObject *next;
if ((type == &teedataobject_type ||
type->tp_init == teedataobject_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("teedataobject", kwargs)) {
goto exit;
}
@ -270,10 +270,10 @@ static PyObject *
itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &tee_type;
PyObject *iterable;
if ((type == &tee_type ||
type->tp_init == tee_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("_tee", kwargs)) {
goto exit;
}
@ -345,10 +345,10 @@ static PyObject *
itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &cycle_type;
PyObject *iterable;
if ((type == &cycle_type ||
type->tp_init == cycle_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("cycle", kwargs)) {
goto exit;
}
@ -377,11 +377,11 @@ static PyObject *
itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &dropwhile_type;
PyObject *func;
PyObject *seq;
if ((type == &dropwhile_type ||
type->tp_init == dropwhile_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("dropwhile", kwargs)) {
goto exit;
}
@ -409,11 +409,11 @@ static PyObject *
itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &takewhile_type;
PyObject *func;
PyObject *seq;
if ((type == &takewhile_type ||
type->tp_init == takewhile_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("takewhile", kwargs)) {
goto exit;
}
@ -441,11 +441,11 @@ static PyObject *
itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &starmap_type;
PyObject *func;
PyObject *seq;
if ((type == &starmap_type ||
type->tp_init == starmap_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("starmap", kwargs)) {
goto exit;
}
@ -821,11 +821,11 @@ static PyObject *
itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &filterfalse_type;
PyObject *func;
PyObject *seq;
if ((type == &filterfalse_type ||
type->tp_init == filterfalse_type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("filterfalse", kwargs)) {
goto exit;
}
@ -913,4 +913,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=0229ebd72962f130 input=a9049054013a1b77]*/
/*[clinic end generated code: output=47c8c8ccec8740d7 input=a9049054013a1b77]*/

View File

@ -1061,14 +1061,13 @@ static PyObject *
select_kqueue(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = _selectstate_by_type(type)->kqueue_queue_Type;
if ((type == _selectstate_by_type(type)->kqueue_queue_Type ||
type->tp_init == _selectstate_by_type(type)->kqueue_queue_Type->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoPositional("kqueue", args)) {
goto exit;
}
if ((type == _selectstate_by_type(type)->kqueue_queue_Type ||
type->tp_init == _selectstate_by_type(type)->kqueue_queue_Type->tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("kqueue", kwargs)) {
goto exit;
}
@ -1310,4 +1309,4 @@ exit:
#ifndef SELECT_KQUEUE_CONTROL_METHODDEF
#define SELECT_KQUEUE_CONTROL_METHODDEF
#endif /* !defined(SELECT_KQUEUE_CONTROL_METHODDEF) */
/*[clinic end generated code: output=9556c7d6cd5192d1 input=a9049054013a1b77]*/
/*[clinic end generated code: output=64516114287e894d input=a9049054013a1b77]*/

View File

@ -38,11 +38,11 @@ static PyObject *
method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &PyMethod_Type;
PyObject *function;
PyObject *instance;
if ((type == &PyMethod_Type ||
type->tp_init == PyMethod_Type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("method", kwargs)) {
goto exit;
}
@ -70,10 +70,10 @@ static PyObject *
instancemethod_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &PyInstanceMethod_Type;
PyObject *function;
if ((type == &PyInstanceMethod_Type ||
type->tp_init == PyInstanceMethod_Type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("instancemethod", kwargs)) {
goto exit;
}
@ -86,4 +86,4 @@ instancemethod_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
/*[clinic end generated code: output=e3294c26a71d456d input=a9049054013a1b77]*/
/*[clinic end generated code: output=2a5e7fa5947a86cb input=a9049054013a1b77]*/

View File

@ -30,6 +30,7 @@ static PyObject *
code_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &PyCode_Type;
int argcount;
int posonlyargcount;
int kwonlyargcount;
@ -49,8 +50,7 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *freevars = NULL;
PyObject *cellvars = NULL;
if ((type == &PyCode_Type ||
type->tp_init == PyCode_Type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("code", kwargs)) {
goto exit;
}
@ -488,4 +488,4 @@ code__varname_from_oparg(PyCodeObject *self, PyObject *const *args, Py_ssize_t n
exit:
return return_value;
}
/*[clinic end generated code: output=b6c98f17c60ace53 input=a9049054013a1b77]*/
/*[clinic end generated code: output=f1fab6e71c785182 input=a9049054013a1b77]*/

View File

@ -91,10 +91,10 @@ static PyObject *
reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &PyReversed_Type;
PyObject *seq;
if ((type == &PyReversed_Type ||
type->tp_init == PyReversed_Type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("reversed", kwargs)) {
goto exit;
}
@ -107,4 +107,4 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
/*[clinic end generated code: output=683261097bfd794a input=a9049054013a1b77]*/
/*[clinic end generated code: output=aba0ddbeab1601e3 input=a9049054013a1b77]*/

View File

@ -212,10 +212,10 @@ static PyObject *
float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &PyFloat_Type;
PyObject *x = NULL;
if ((type == &PyFloat_Type ||
type->tp_init == PyFloat_Type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("float", kwargs)) {
goto exit;
}
@ -327,4 +327,4 @@ float___format__(PyObject *self, PyObject *arg)
exit:
return return_value;
}
/*[clinic end generated code: output=ac6374ac606a505e input=a9049054013a1b77]*/
/*[clinic end generated code: output=74bc91bb44014df9 input=a9049054013a1b77]*/

View File

@ -326,10 +326,11 @@ static int
list___init__(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
PyTypeObject *base_tp = &PyList_Type;
PyObject *iterable = NULL;
if ((Py_IS_TYPE(self, &PyList_Type) ||
Py_TYPE(self)->tp_new == PyList_Type.tp_new) &&
if ((Py_IS_TYPE(self, base_tp) ||
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
!_PyArg_NoKeywords("list", kwargs)) {
goto exit;
}
@ -382,4 +383,4 @@ list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored))
{
return list___reversed___impl(self);
}
/*[clinic end generated code: output=4e6f38b655394564 input=a9049054013a1b77]*/
/*[clinic end generated code: output=2ca109d8acc775bc input=a9049054013a1b77]*/

View File

@ -81,10 +81,10 @@ static PyObject *
tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyTypeObject *base_tp = &PyTuple_Type;
PyObject *iterable = NULL;
if ((type == &PyTuple_Type ||
type->tp_init == PyTuple_Type.tp_init) &&
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("tuple", kwargs)) {
goto exit;
}
@ -118,4 +118,4 @@ tuple___getnewargs__(PyTupleObject *self, PyObject *Py_UNUSED(ignored))
{
return tuple___getnewargs___impl(self);
}
/*[clinic end generated code: output=441d2b880e865f87 input=a9049054013a1b77]*/
/*[clinic end generated code: output=48a9e0834b300ac3 input=a9049054013a1b77]*/

View File

@ -1174,6 +1174,7 @@ class CLanguage(Language):
raise ValueError("Slot methods cannot access their defining class.")
if not parses_keywords:
declarations = '{base_type_ptr}'
fields.insert(0, normalize_snippet("""
if ({self_type_check}!_PyArg_NoKeywords("{name}", kwargs)) {{
goto exit;
@ -1187,7 +1188,7 @@ class CLanguage(Language):
""", indent=4))
parser_definition = parser_body(parser_prototype, *fields,
declarations=parser_body_declarations)
declarations=declarations)
if flags in ('METH_NOARGS', 'METH_O', 'METH_VARARGS'):
@ -3839,21 +3840,22 @@ class self_converter(CConverter):
cls = self.function.cls
if ((kind in (METHOD_NEW, METHOD_INIT)) and cls and cls.typedef):
type_object = self.function.cls.type_object
prefix = (type_object[1:] + '.' if type_object[0] == '&' else
type_object + '->')
if kind == METHOD_NEW:
type_check = ('({0} == {1} ||\n '
' {0}->tp_init == {2}tp_init)'
).format(self.name, type_object, prefix)
type_check = (
'({0} == base_tp || {0}->tp_init == base_tp->tp_init)'
).format(self.name)
else:
type_check = ('(Py_IS_TYPE({0}, {1}) ||\n '
' Py_TYPE({0})->tp_new == {2}tp_new)'
).format(self.name, type_object, prefix)
type_check = ('(Py_IS_TYPE({0}, base_tp) ||\n '
' Py_TYPE({0})->tp_new == base_tp->tp_new)'
).format(self.name)
line = '{} &&\n '.format(type_check)
template_dict['self_type_check'] = line
type_object = self.function.cls.type_object
type_ptr = f'PyTypeObject *base_tp = {type_object};'
template_dict['base_type_ptr'] = type_ptr
def add_c_return_converter(f, name=None):