Visitar URL original
Stackless issue #202: remove the STACKLESS_DECLARE_METHOD magic · akruis/cpython@b8a98a8 · GitHub
Skip to content

Commit b8a98a8

Browse files
author
Anselm Kruis
authored
Stackless issue python#202: remove the STACKLESS_DECLARE_METHOD magic
Now that we have C99 designated initializers, we don't need the STACKLESS_DECLARE_METHOD magic any longer. Replacing it with explicit initializers has several advantages: - faster startup time - less magic, code is simpler to reason about - no memory leak - no initialization race for sub-types between init_stackless_methods() and PyType_Ready().
1 parent e989973 commit b8a98a8

16 files changed

Lines changed: 92 additions & 205 deletions

‎Objects/classobject.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
332332
return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
333333
}
334334

335+
#ifdef STACKLESS
336+
static PyMappingMethods method_as_mapping = {
337+
.slpflags.tp_call = -1,
338+
};
339+
#endif
340+
335341
PyTypeObject PyMethod_Type = {
336342
PyVarObject_HEAD_INIT(&PyType_Type, 0)
337343
"method",
@@ -345,14 +351,15 @@ PyTypeObject PyMethod_Type = {
345351
(reprfunc)method_repr, /* tp_repr */
346352
0, /* tp_as_number */
347353
0, /* tp_as_sequence */
348-
0, /* tp_as_mapping */
354+
SLP_TP_AS_MAPPING(method_as_mapping), /* tp_as_mapping */
349355
(hashfunc)method_hash, /* tp_hash */
350356
method_call, /* tp_call */
351357
0, /* tp_str */
352358
method_getattro, /* tp_getattro */
353359
PyObject_GenericSetAttr, /* tp_setattro */
354360
0, /* tp_as_buffer */
355-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
361+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
362+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
356363
method_doc, /* tp_doc */
357364
(traverseproc)method_traverse, /* tp_traverse */
358365
0, /* tp_clear */
@@ -373,8 +380,6 @@ PyTypeObject PyMethod_Type = {
373380
method_new, /* tp_new */
374381
};
375382

376-
STACKLESS_DECLARE_METHOD(&PyMethod_Type, tp_call)
377-
378383
/* Clear out the free list */
379384

380385
int

‎Objects/descrobject.c‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ descr_traverse(PyObject *self, visitproc visit, void *arg)
556556
return 0;
557557
}
558558

559+
#ifdef STACKLESS
560+
static PyMappingMethods descr_as_mapping = {
561+
.slpflags.tp_call = -1,
562+
};
563+
#endif
564+
559565
PyTypeObject PyMethodDescr_Type = {
560566
PyVarObject_HEAD_INIT(&PyType_Type, 0)
561567
"method_descriptor",
@@ -569,14 +575,15 @@ PyTypeObject PyMethodDescr_Type = {
569575
(reprfunc)method_repr, /* tp_repr */
570576
0, /* tp_as_number */
571577
0, /* tp_as_sequence */
572-
0, /* tp_as_mapping */
578+
SLP_TP_AS_MAPPING(descr_as_mapping), /* tp_as_mapping */
573579
0, /* tp_hash */
574580
(ternaryfunc)methoddescr_call, /* tp_call */
575581
0, /* tp_str */
576582
PyObject_GenericGetAttr, /* tp_getattro */
577583
0, /* tp_setattro */
578584
0, /* tp_as_buffer */
579-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
585+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
586+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
580587
0, /* tp_doc */
581588
descr_traverse, /* tp_traverse */
582589
0, /* tp_clear */
@@ -593,7 +600,6 @@ PyTypeObject PyMethodDescr_Type = {
593600
0, /* tp_descr_set */
594601
};
595602

596-
STACKLESS_DECLARE_METHOD(&PyMethodDescr_Type, tp_call)
597603

598604
/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
599605
PyTypeObject PyClassMethodDescr_Type = {
@@ -609,14 +615,15 @@ PyTypeObject PyClassMethodDescr_Type = {
609615
(reprfunc)method_repr, /* tp_repr */
610616
0, /* tp_as_number */
611617
0, /* tp_as_sequence */
612-
0, /* tp_as_mapping */
618+
SLP_TP_AS_MAPPING(descr_as_mapping), /* tp_as_mapping */
613619
0, /* tp_hash */
614620
(ternaryfunc)classmethoddescr_call, /* tp_call */
615621
0, /* tp_str */
616622
PyObject_GenericGetAttr, /* tp_getattro */
617623
0, /* tp_setattro */
618624
0, /* tp_as_buffer */
619-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
625+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
626+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
620627
0, /* tp_doc */
621628
descr_traverse, /* tp_traverse */
622629
0, /* tp_clear */
@@ -633,7 +640,6 @@ PyTypeObject PyClassMethodDescr_Type = {
633640
0, /* tp_descr_set */
634641
};
635642

636-
STACKLESS_DECLARE_METHOD(&PyClassMethodDescr_Type, tp_call)
637643

638644
PyTypeObject PyMemberDescr_Type = {
639645
PyVarObject_HEAD_INIT(&PyType_Type, 0)
@@ -722,14 +728,15 @@ PyTypeObject PyWrapperDescr_Type = {
722728
(reprfunc)wrapperdescr_repr, /* tp_repr */
723729
0, /* tp_as_number */
724730
0, /* tp_as_sequence */
725-
0, /* tp_as_mapping */
731+
SLP_TP_AS_MAPPING(descr_as_mapping), /* tp_as_mapping */
726732
0, /* tp_hash */
727733
(ternaryfunc)wrapperdescr_call, /* tp_call */
728734
0, /* tp_str */
729735
PyObject_GenericGetAttr, /* tp_getattro */
730736
0, /* tp_setattro */
731737
0, /* tp_as_buffer */
732-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
738+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
739+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
733740
0, /* tp_doc */
734741
descr_traverse, /* tp_traverse */
735742
0, /* tp_clear */
@@ -746,7 +753,6 @@ PyTypeObject PyWrapperDescr_Type = {
746753
0, /* tp_descr_set */
747754
};
748755

749-
STACKLESS_DECLARE_METHOD(&PyWrapperDescr_Type, tp_call)
750756

751757
static PyDescrObject *
752758
descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
@@ -1210,14 +1216,15 @@ PyTypeObject _PyMethodWrapper_Type = {
12101216
(reprfunc)wrapper_repr, /* tp_repr */
12111217
0, /* tp_as_number */
12121218
0, /* tp_as_sequence */
1213-
0, /* tp_as_mapping */
1219+
SLP_TP_AS_MAPPING(descr_as_mapping), /* tp_as_mapping */
12141220
(hashfunc)wrapper_hash, /* tp_hash */
12151221
(ternaryfunc)wrapper_call, /* tp_call */
12161222
0, /* tp_str */
12171223
PyObject_GenericGetAttr, /* tp_getattro */
12181224
0, /* tp_setattro */
12191225
0, /* tp_as_buffer */
1220-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1226+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1227+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
12211228
0, /* tp_doc */
12221229
wrapper_traverse, /* tp_traverse */
12231230
0, /* tp_clear */
@@ -1234,7 +1241,6 @@ PyTypeObject _PyMethodWrapper_Type = {
12341241
0, /* tp_descr_set */
12351242
};
12361243

1237-
STACKLESS_DECLARE_METHOD(&_PyMethodWrapper_Type, tp_call)
12381244

12391245
PyObject *
12401246
PyWrapper_New(PyObject *d, PyObject *self)

‎Objects/funcobject.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
601601
return PyMethod_New(func, obj);
602602
}
603603

604+
#ifdef STACKLESS
605+
static PyMappingMethods func_as_mapping = {
606+
.slpflags.tp_call = -1,
607+
};
608+
#endif
609+
604610
PyTypeObject PyFunction_Type = {
605611
PyVarObject_HEAD_INIT(&PyType_Type, 0)
606612
"function",
@@ -614,14 +620,15 @@ PyTypeObject PyFunction_Type = {
614620
(reprfunc)func_repr, /* tp_repr */
615621
0, /* tp_as_number */
616622
0, /* tp_as_sequence */
617-
0, /* tp_as_mapping */
623+
SLP_TP_AS_MAPPING(func_as_mapping), /* tp_as_mapping */
618624
0, /* tp_hash */
619625
function_call, /* tp_call */
620626
0, /* tp_str */
621627
0, /* tp_getattro */
622628
0, /* tp_setattro */
623629
0, /* tp_as_buffer */
624-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
630+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
631+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
625632
func_new__doc__, /* tp_doc */
626633
(traverseproc)func_traverse, /* tp_traverse */
627634
0, /* tp_clear */
@@ -642,7 +649,6 @@ PyTypeObject PyFunction_Type = {
642649
func_new, /* tp_new */
643650
};
644651

645-
STACKLESS_DECLARE_METHOD(&PyFunction_Type, tp_call)
646652

647653
/* Class method object */
648654

‎Objects/genobject.c‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,12 @@ static PyMethodDef gen_methods[] = {
872872
{NULL, NULL} /* Sentinel */
873873
};
874874

875+
#ifdef STACKLESS
876+
static PyMappingMethods gen_as_mapping = {
877+
.slpflags.tp_iternext = -1,
878+
};
879+
#endif
880+
875881
PyTypeObject PyGen_Type = {
876882
PyVarObject_HEAD_INIT(&PyType_Type, 0)
877883
"generator", /* tp_name */
@@ -886,15 +892,16 @@ PyTypeObject PyGen_Type = {
886892
(reprfunc)gen_repr, /* tp_repr */
887893
0, /* tp_as_number */
888894
0, /* tp_as_sequence */
889-
0, /* tp_as_mapping */
895+
SLP_TP_AS_MAPPING(gen_as_mapping), /* tp_as_mapping */
890896
0, /* tp_hash */
891897
0, /* tp_call */
892898
0, /* tp_str */
893899
PyObject_GenericGetAttr, /* tp_getattro */
894900
0, /* tp_setattro */
895901
0, /* tp_as_buffer */
896902
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
897-
Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
903+
Py_TPFLAGS_HAVE_FINALIZE |
904+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
898905
0, /* tp_doc */
899906
(traverseproc)gen_traverse, /* tp_traverse */
900907
0, /* tp_clear */
@@ -926,8 +933,6 @@ PyTypeObject PyGen_Type = {
926933
_PyGen_Finalize, /* tp_finalize */
927934
};
928935

929-
STACKLESS_DECLARE_METHOD(&PyGen_Type, tp_iternext);
930-
931936
static PyObject *
932937
gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
933938
PyObject *name, PyObject *qualname)
@@ -1243,14 +1248,15 @@ PyTypeObject _PyCoroWrapper_Type = {
12431248
0, /* tp_repr */
12441249
0, /* tp_as_number */
12451250
0, /* tp_as_sequence */
1246-
0, /* tp_as_mapping */
1251+
SLP_TP_AS_MAPPING(gen_as_mapping), /* tp_as_mapping */
12471252
0, /* tp_hash */
12481253
0, /* tp_call */
12491254
0, /* tp_str */
12501255
PyObject_GenericGetAttr, /* tp_getattro */
12511256
0, /* tp_setattro */
12521257
0, /* tp_as_buffer */
1253-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1258+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1259+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
12541260
"A wrapper object implementing __await__ for coroutines.",
12551261
(traverseproc)coro_wrapper_traverse, /* tp_traverse */
12561262
0, /* tp_clear */
@@ -1272,8 +1278,6 @@ PyTypeObject _PyCoroWrapper_Type = {
12721278
0, /* tp_free */
12731279
};
12741280

1275-
STACKLESS_DECLARE_METHOD(&_PyCoroWrapper_Type, tp_iternext);
1276-
12771281
static PyObject *
12781282
compute_cr_origin(int origin_depth)
12791283
{
@@ -1812,14 +1816,15 @@ PyTypeObject _PyAsyncGenASend_Type = {
18121816
0, /* tp_repr */
18131817
0, /* tp_as_number */
18141818
0, /* tp_as_sequence */
1815-
0, /* tp_as_mapping */
1819+
SLP_TP_AS_MAPPING(gen_as_mapping), /* tp_as_mapping */
18161820
0, /* tp_hash */
18171821
0, /* tp_call */
18181822
0, /* tp_str */
18191823
PyObject_GenericGetAttr, /* tp_getattro */
18201824
0, /* tp_setattro */
18211825
0, /* tp_as_buffer */
1822-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1826+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1827+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
18231828
0, /* tp_doc */
18241829
(traverseproc)async_gen_asend_traverse, /* tp_traverse */
18251830
0, /* tp_clear */
@@ -1840,8 +1845,6 @@ PyTypeObject _PyAsyncGenASend_Type = {
18401845
0, /* tp_new */
18411846
};
18421847

1843-
STACKLESS_DECLARE_METHOD(&_PyAsyncGenASend_Type, tp_iternext);
1844-
18451848

18461849
static PyObject *
18471850
async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)

‎Objects/methodobject.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ meth_hash(PyCFunctionObject *a)
269269
}
270270

271271

272+
#ifdef STACKLESS
273+
static PyMappingMethods meth_as_mapping = {
274+
.slpflags.tp_call = -1,
275+
};
276+
#endif
277+
272278
PyTypeObject PyCFunction_Type = {
273279
PyVarObject_HEAD_INIT(&PyType_Type, 0)
274280
"builtin_function_or_method",
@@ -282,14 +288,15 @@ PyTypeObject PyCFunction_Type = {
282288
(reprfunc)meth_repr, /* tp_repr */
283289
0, /* tp_as_number */
284290
0, /* tp_as_sequence */
285-
0, /* tp_as_mapping */
291+
SLP_TP_AS_MAPPING(meth_as_mapping), /* tp_as_mapping */
286292
(hashfunc)meth_hash, /* tp_hash */
287293
PyCFunction_Call, /* tp_call */
288294
0, /* tp_str */
289295
PyObject_GenericGetAttr, /* tp_getattro */
290296
0, /* tp_setattro */
291297
0, /* tp_as_buffer */
292-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
298+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
299+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
293300
0, /* tp_doc */
294301
(traverseproc)meth_traverse, /* tp_traverse */
295302
0, /* tp_clear */
@@ -304,8 +311,6 @@ PyTypeObject PyCFunction_Type = {
304311
0, /* tp_dict */
305312
};
306313

307-
STACKLESS_DECLARE_METHOD(&PyCFunction_Type, tp_call)
308-
309314
/* Clear out the free list */
310315

311316
int

‎Objects/typeobject.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,6 +3590,12 @@ type_is_gc(PyTypeObject *type)
35903590
return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
35913591
}
35923592

3593+
#ifdef STACKLESS
3594+
static PyMappingMethods type_as_mapping = {
3595+
.slpflags.tp_call = -1,
3596+
};
3597+
#endif
3598+
35933599
PyTypeObject PyType_Type = {
35943600
PyVarObject_HEAD_INIT(&PyType_Type, 0)
35953601
"type", /* tp_name */
@@ -3603,15 +3609,16 @@ PyTypeObject PyType_Type = {
36033609
(reprfunc)type_repr, /* tp_repr */
36043610
0, /* tp_as_number */
36053611
0, /* tp_as_sequence */
3606-
0, /* tp_as_mapping */
3612+
SLP_TP_AS_MAPPING(type_as_mapping), /* tp_as_mapping */
36073613
0, /* tp_hash */
36083614
(ternaryfunc)type_call, /* tp_call */
36093615
0, /* tp_str */
36103616
(getattrofunc)type_getattro, /* tp_getattro */
36113617
(setattrofunc)type_setattro, /* tp_setattro */
36123618
0, /* tp_as_buffer */
36133619
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3614-
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
3620+
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS |
3621+
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, /* tp_flags */
36153622
type_doc, /* tp_doc */
36163623
(traverseproc)type_traverse, /* tp_traverse */
36173624
(inquiry)type_clear, /* tp_clear */
@@ -3634,8 +3641,6 @@ PyTypeObject PyType_Type = {
36343641
(inquiry)type_is_gc, /* tp_is_gc */
36353642
};
36363643

3637-
STACKLESS_DECLARE_METHOD(&PyType_Type, tp_call)
3638-
36393644
/* The base type of all types (eventually)... except itself. */
36403645

36413646
/* You may wonder why object.__new__() only complains about arguments

‎PCbuild/pythoncore.vcxproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@
216216
<ClInclude Include="..\Stackless\core\cframeobject.h" />
217217
<ClInclude Include="..\Stackless\core\slp_exttype.h" />
218218
<ClInclude Include="..\Stackless\core\stackless_impl.h" />
219-
<ClInclude Include="..\Stackless\core\stackless_methods.h" />
220219
<ClInclude Include="..\Stackless\core\stackless_structs.h" />
221220
<ClInclude Include="..\Stackless\core\stackless_tstate.h" />
222221
<ClInclude Include="..\Stackless\core\stackless_pystate.h" />

‎PCbuild/pythoncore.vcxproj.filters‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,6 @@
465465
<ClInclude Include="..\Stackless\core\stackless_impl.h">
466466
<Filter>Stackless\core</Filter>
467467
</ClInclude>
468-
<ClInclude Include="..\Stackless\core\stackless_methods.h">
469-
<Filter>Stackless\core</Filter>
470-
</ClInclude>
471468
<ClInclude Include="..\Stackless\core\stackless_structs.h">
472469
<Filter>Stackless\core</Filter>
473470
</ClInclude>

0 commit comments

Comments
 (0)