@@ -228,48 +228,101 @@ static void inherit_special(PyTypeObject *type, PyTypeObject *base) {
228
228
}
229
229
230
230
static void inherit_slots (PyTypeObject * type , PyTypeObject * base ) {
231
- PyTypeObject * basebase ;
232
-
233
- #undef SLOTDEFINED
234
- #undef COPYSLOT
235
- #undef SLOTDEFINED2
236
- #undef COPYSLOT2
237
- #undef COPYBUF
238
-
239
- #define SLOTDEFINED (SLOT ) \
240
- (PyTypeObject_##SLOT(base) != 0 && \
241
- (basebase == NULL || PyTypeObject_##SLOT(base) != PyTypeObject_##SLOT(basebase)))
242
-
243
- #define COPYSLOT (SLOT ) \
244
- if (!PyTypeObject_##SLOT(type) && SLOTDEFINED(SLOT)) set_PyTypeObject_##SLOT(type, PyTypeObject_##SLOT(base));
245
-
246
- #define SLOTDEFINED2 (SLOT , SLOT2 ) \
247
- (PyTypeObject_##SLOT(base)->SLOT2 != 0 && \
248
- (basebase == NULL || PyTypeObject_##SLOT(base) != PyTypeObject_##SLOT(basebase)))
249
-
250
- #define COPYSLOT2 (SLOT , SLOT2 ) \
251
- if (!PyTypeObject_##SLOT(type)->SLOT2 && SLOTDEFINED2(SLOT, SLOT2)) PyTypeObject_##SLOT(type)->SLOT2 = PyTypeObject_##SLOT(base)->SLOT2;
252
-
253
- #define COPYBUF (SLOT ) COPYSLOT2(tp_as_buffer, SLOT)
254
-
255
- if (type -> tp_as_buffer != NULL && base -> tp_as_buffer != NULL ) {
256
- basebase = base -> tp_base ;
257
- if (basebase -> tp_as_buffer == NULL )
258
- basebase = NULL ;
231
+ const PyTypeObject * basebase = base -> tp_base ;
232
+
233
+ #define SLOTDEFINED (BASE , BASEBASE , GETFUNC ) \
234
+ (GETFUNC(BASE) != 0 && (BASEBASE == NULL || GETFUNC(BASE) != GETFUNC(BASEBASE)))
235
+
236
+ #define COPYSLOT_GENERIC (SLOT , TYPE , BASE , BASEBASE , GETFUNC ) \
237
+ if (!TYPE->SLOT && SLOTDEFINED(BASE, BASEBASE, GETFUNC)) TYPE->SLOT = GETFUNC(BASE);
238
+
239
+ #define COPYSLOT (SLOT ) COPYSLOT_GENERIC(SLOT, type, base, basebase, PyTypeObject_##SLOT)
240
+ #define COPYSLOT_GENERIC_GROUP (SLOT , GROUP , PREFIX ) COPYSLOT_GENERIC(SLOT, type_##GROUP, base_##GROUP, basebase_##GROUP, PREFIX##_##SLOT)
241
+ #define GROUP_DECL (GROUP , TYPE ) \
242
+ TYPE* type_##GROUP = type->GROUP; \
243
+ TYPE* base_##GROUP = PyTypeObject_##GROUP(base); \
244
+ TYPE* basebase_##GROUP = basebase ? PyTypeObject_##GROUP(basebase) : NULL; \
245
+ if (type_##GROUP != NULL && base_##GROUP != NULL)
246
+ #define COPYASYNC (SLOT ) COPYSLOT_GENERIC_GROUP(SLOT, tp_as_async, PyAsyncMethods)
247
+ #define COPYNUM (SLOT ) COPYSLOT_GENERIC_GROUP(SLOT, tp_as_number, PyNumberMethods)
248
+ #define COPYSEQ (SLOT ) COPYSLOT_GENERIC_GROUP(SLOT, tp_as_sequence, PySequenceMethods)
249
+ #define COPYMAP (SLOT ) COPYSLOT_GENERIC_GROUP(SLOT, tp_as_mapping, PyMappingMethods)
250
+ #define COPYBUF (SLOT ) COPYSLOT_GENERIC_GROUP(SLOT, tp_as_buffer, PyBufferProcs)
251
+
252
+ GROUP_DECL (tp_as_number , PyNumberMethods ) {
253
+ COPYNUM (nb_add );
254
+ COPYNUM (nb_subtract );
255
+ COPYNUM (nb_multiply );
256
+ COPYNUM (nb_remainder );
257
+ COPYNUM (nb_divmod );
258
+ COPYNUM (nb_power );
259
+ COPYNUM (nb_negative );
260
+ COPYNUM (nb_positive );
261
+ COPYNUM (nb_absolute );
262
+ COPYNUM (nb_bool );
263
+ COPYNUM (nb_invert );
264
+ COPYNUM (nb_lshift );
265
+ COPYNUM (nb_rshift );
266
+ COPYNUM (nb_and );
267
+ COPYNUM (nb_xor );
268
+ COPYNUM (nb_or );
269
+ COPYNUM (nb_int );
270
+ COPYNUM (nb_float );
271
+ COPYNUM (nb_inplace_add );
272
+ COPYNUM (nb_inplace_subtract );
273
+ COPYNUM (nb_inplace_multiply );
274
+ COPYNUM (nb_inplace_remainder );
275
+ COPYNUM (nb_inplace_power );
276
+ COPYNUM (nb_inplace_lshift );
277
+ COPYNUM (nb_inplace_rshift );
278
+ COPYNUM (nb_inplace_and );
279
+ COPYNUM (nb_inplace_xor );
280
+ COPYNUM (nb_inplace_or );
281
+ COPYNUM (nb_true_divide );
282
+ COPYNUM (nb_floor_divide );
283
+ COPYNUM (nb_inplace_true_divide );
284
+ COPYNUM (nb_inplace_floor_divide );
285
+ COPYNUM (nb_index );
286
+ COPYNUM (nb_matrix_multiply );
287
+ COPYNUM (nb_inplace_matrix_multiply );
288
+ }
289
+
290
+ GROUP_DECL (tp_as_async , PyAsyncMethods ) {
291
+ COPYASYNC (am_await );
292
+ COPYASYNC (am_aiter );
293
+ COPYASYNC (am_anext );
294
+ }
295
+
296
+ GROUP_DECL (tp_as_sequence , PySequenceMethods ) {
297
+ COPYSEQ (sq_length );
298
+ COPYSEQ (sq_concat );
299
+ COPYSEQ (sq_repeat );
300
+ COPYSEQ (sq_item );
301
+ COPYSEQ (sq_ass_item );
302
+ COPYSEQ (sq_contains );
303
+ COPYSEQ (sq_inplace_concat );
304
+ COPYSEQ (sq_inplace_repeat );
305
+ }
306
+
307
+ GROUP_DECL (tp_as_mapping , PyMappingMethods ) {
308
+ COPYMAP (mp_length );
309
+ COPYMAP (mp_subscript );
310
+ COPYMAP (mp_ass_subscript );
311
+ }
312
+
313
+ GROUP_DECL (tp_as_buffer , PyBufferProcs ) {
259
314
COPYBUF (bf_getbuffer );
260
315
COPYBUF (bf_releasebuffer );
261
316
}
262
317
263
- basebase = PyTypeObject_tp_base (base );
264
-
265
318
COPYSLOT (tp_dealloc );
266
- if (PyTypeObject_tp_getattr ( type ) == NULL && PyTypeObject_tp_getattro ( type ) == NULL ) {
267
- set_PyTypeObject_tp_getattr ( type , PyTypeObject_tp_getattr (base ) );
268
- set_PyTypeObject_tp_getattro ( type , PyTypeObject_tp_getattro (base ) );
319
+ if (type -> tp_getattr == NULL && type -> tp_getattro == NULL ) {
320
+ type -> tp_getattr = PyTypeObject_tp_getattr (base );
321
+ type -> tp_getattro = PyTypeObject_tp_getattro (base );
269
322
}
270
- if (PyTypeObject_tp_setattr ( type ) == NULL && PyTypeObject_tp_setattro ( type ) == NULL ) {
271
- set_PyTypeObject_tp_setattr ( type , PyTypeObject_tp_setattr (base ) );
272
- set_PyTypeObject_tp_setattro ( type , PyTypeObject_tp_setattro (base ) );
323
+ if (type -> tp_setattr == NULL && type -> tp_setattro == NULL ) {
324
+ type -> tp_setattr = PyTypeObject_tp_setattr (base );
325
+ type -> tp_setattro = PyTypeObject_tp_setattro (base );
273
326
}
274
327
{
275
328
/* Always inherit tp_vectorcall_offset to support PyVectorcall_Call().
@@ -279,11 +332,11 @@ static void inherit_slots(PyTypeObject *type, PyTypeObject *base) {
279
332
280
333
/* Inherit _Py_TPFLAGS_HAVE_VECTORCALL for non-heap types
281
334
* if tp_call is not overridden */
282
- if (!PyTypeObject_tp_call ( type ) &&
335
+ if (!type -> tp_call &&
283
336
(PyTypeObject_tp_flags (base ) & _Py_TPFLAGS_HAVE_VECTORCALL ) &&
284
- !(PyTypeObject_tp_flags ( type ) & Py_TPFLAGS_HEAPTYPE ))
337
+ !(type -> tp_flags & Py_TPFLAGS_HEAPTYPE ))
285
338
{
286
- set_PyTypeObject_tp_flags ( type , PyTypeObject_tp_flags ( type ) | _Py_TPFLAGS_HAVE_VECTORCALL ) ;
339
+ type -> tp_flags |= _Py_TPFLAGS_HAVE_VECTORCALL ;
287
340
}
288
341
/* COPYSLOT(tp_call); */
289
342
}
@@ -293,24 +346,24 @@ static void inherit_slots(PyTypeObject *type, PyTypeObject *base) {
293
346
}
294
347
{
295
348
COPYSLOT (tp_alloc );
296
- if ((PyTypeObject_tp_flags ( type ) & Py_TPFLAGS_HAVE_FINALIZE ) &&
349
+ if ((type -> tp_flags & Py_TPFLAGS_HAVE_FINALIZE ) &&
297
350
(PyTypeObject_tp_flags (base ) & Py_TPFLAGS_HAVE_FINALIZE )) {
298
351
COPYSLOT (tp_finalize );
299
352
}
300
- if ((PyTypeObject_tp_flags ( type ) & Py_TPFLAGS_HAVE_GC ) ==
353
+ if ((type -> tp_flags & Py_TPFLAGS_HAVE_GC ) ==
301
354
(PyTypeObject_tp_flags (base ) & Py_TPFLAGS_HAVE_GC )) {
302
355
/* They agree about gc. */
303
356
COPYSLOT (tp_free );
304
357
}
305
- else if ((PyTypeObject_tp_flags ( type ) & Py_TPFLAGS_HAVE_GC ) &&
306
- PyTypeObject_tp_free ( type ) == NULL &&
358
+ else if ((type -> tp_flags & Py_TPFLAGS_HAVE_GC ) &&
359
+ type -> tp_free == NULL &&
307
360
PyTypeObject_tp_free (base ) == PyObject_Free ) {
308
361
/* A bit of magic to plug in the correct default
309
362
* tp_free function when a derived class adds gc,
310
363
* didn't define tp_free, and the base uses the
311
364
* default non-gc tp_free.
312
365
*/
313
- set_PyTypeObject_tp_free ( type , PyObject_GC_Del ) ;
366
+ type -> tp_free = PyObject_GC_Del ;
314
367
}
315
368
/* else they didn't agree about gc, and there isn't something
316
369
* obvious to be done -- the type is on its own.
0 commit comments