]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-101409: Improve generated clinic code for self type checks (#101411)
authorErlend E. Aasland <erlend.aasland@protonmail.com>
Tue, 31 Jan 2023 20:42:03 +0000 (21:42 +0100)
committerGitHub <noreply@github.com>
Tue, 31 Jan 2023 20:42:03 +0000 (21:42 +0100)
15 files changed:
Modules/_io/clinic/bufferedio.c.h
Modules/_sqlite/clinic/cursor.c.h
Modules/_sqlite/clinic/row.c.h
Modules/clinic/_collectionsmodule.c.h
Modules/clinic/_queuemodule.c.h
Modules/clinic/_ssl.c.h
Modules/clinic/itertoolsmodule.c.h
Modules/clinic/selectmodule.c.h
Objects/clinic/classobject.c.h
Objects/clinic/codeobject.c.h
Objects/clinic/enumobject.c.h
Objects/clinic/floatobject.c.h
Objects/clinic/listobject.c.h
Objects/clinic/tupleobject.c.h
Tools/clinic/clinic.py

index 8a8f86b2eea318f1d78157aa498a22b4756d4fa4..38ea756879c122e29c659642832b36bd8eaccef6 100644 (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]*/
index 36b8d0051a2915d5c6a1be46dfbc3807f8d5bc28..43e912d1347963625eeb0a9a9eb571a76da85ee4 100644 (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]*/
index c543b398db3fcf89f2e3559dadff4723ecfb192d..89a48fd52da226889a475dfc3e60c2edea792540 100644 (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]*/
index ad4da8856ac3c3897813cbbb3601f70b2d596b8a..8ea0255b0610706d884e67b4198d70b3df9f2d27 100644 (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]*/
index f86dac3c497d641dc02d68087508e7a60d9463e6..94fb59a5b17a490cfd62a7e61df42dcadc3d3b89 100644 (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]*/
index 2d7c98c4f014a38583e871ddc6e12b0d6ab483ce..9f967ddc8e30619d9d2756c31b6ae0378a5a0af4 100644 (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]*/
index 287de524e9130795c76389f7d714efd091394aeb..70299aceb7a02cb499fc0bf878326b74c664f75f 100644 (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]*/
index fda9aaab475567fbfd5751f1db10434939ebacb9..f44ca1d70a1e86d6b3a46ab23647374f090a5f70 100644 (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]*/
index 6c449829662af3c5128bab20eb5a8131844813d5..a7bac63052bc4085b1b324e936a7a0114a97c90b 100644 (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]*/
index da33f4a6a20c1b33d57f62546ddd664a2de2f7e6..5ad4b1fed734176355e2b2c831945c64259d24bd 100644 (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]*/
index 208a9e8be1a1a2fd1fb738a03f7091de50eff7b1..adf78efd0d66f4f1bfebdd1b1c967b057d36a37a 100644 (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]*/
index 1a81e173231bfa9df9603cfea9ab1d9d0a2ce714..6bc25a0a409f97f7d9107cd4a9160fca622fe423 100644 (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]*/
index 94852e9961706061f7b98d4f07f4ea469d2e2678..e3d6ffa9f76fdb5f84e9becfd73e0842ed82fdfa 100644 (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]*/
index a4776e14fa0a9b77952b4fd8f4b54fbd0eb76657..3de95759a13f216cd5c7f80620e41390269afe14 100644 (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]*/
index abff4d2583e170a3f60dbfd6233f4ea6cddb967c..b8b2b75c749152e0f14433adbea895ccbbc25cc9 100755 (executable)
@@ -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):