8000 Preserve the type substructs too. · python/cpython@e777250 · GitHub
[go: up one dir, main page]

Skip to content

Commit e777250

Browse files
Preserve the type substructs too.
1 parent 6b17676 commit e777250

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Include/internal/pycore_typeobject.h

Lines changed: 5 additions & 0 deletions

Objects/typeobject.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ extern "C" {
2626

2727
struct static_type_def {
2828
PyTypeObject type;
29+
PyNumberMethods as_number;
30+
PySequenceMethods as_sequence;
31+
PyMappingMethods as_mapping;
32+
PyBufferProcs as_buffer;
33+
PyAsyncMethods as_async;
2934
};
3035

3136
struct _types_runtime_state {
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ managed_static_type_init_def(static_type_def def, PyTypeObject *type)
179179
"tp" slots, but it's simpler to copy the whole struct. */
180180
PyTypeObject *deftype = &def->type;
181181
memcpy(deftype, type, sizeof(PyTypeObject));
182+
if (deftype->tp_as_buffer != NULL) {
183+
memcpy(&def->as_buffer, type->tp_as_buffer, sizeof(PyBufferProcs));
184+
deftype->tp_as_buffer = &def->as_buffer;
185+
}
186+
if (deftype->tp_as_sequence != NULL) {
187+
memcpy(&def->as_sequence, type->tp_as_sequence, sizeof(PySequenceMethods));
188+
deftype->tp_as_sequence = &def->as_sequence;
189+
}
190+
if (deftype->tp_as_mapping != NULL) {
191+
memcpy(&def->as_mapping, type->tp_as_mapping, sizeof(PyMappingMethods));
192+
deftype->tp_as_mapping = &def->as_mapping;
193+
}
194+
if (deftype->tp_as_number != NULL) {
195+
memcpy(&def->as_number, type->tp_as_number, sizeof(PyNumberMethods));
196+
deftype->tp_as_number = &def->as_number;
197+
}
198+
if (deftype->tp_as_async != NULL) {
199+
memcpy(&def->as_async, type->tp_as_async, sizeof(PyAsyncMethods));
200+
deftype->tp_as_async = &def->as_async;
201+
}
182202
}
183203

184204
static void
@@ -213,6 +233,21 @@ managed_static_type_fini_def(static_type_def def, PyTypeObject *type,
213233
void **typeptr = slotptr(type, p->offset);
214234
*typeptr = *defptr;
215235
}
236+
if (deftype->tp_as_buffer == NULL) {
237+
type->tp_as_buffer = NULL;
238+
}
239+
if (deftype->tp_as_sequence== NULL) {
240+
type->tp_as_sequence = NULL;
241+
}
242+
if (deftype->tp_as_mapping == NULL) {
243+
type->tp_as_mapping = NULL;
244+
}
245+
if (deftype->tp_as_number == NULL) {
246+
type->tp_as_number = NULL;
247+
}
248+
if (deftype->tp_as_async == NULL) {
249+
type->tp_as_async = NULL;
250+
}
216251

217252
/* Currently, there is a small set of cases where a static type
218253
might be used after it has been finalized. For example, some

0 commit comments

Comments
 (0)
0