Skip to content

Commit dc0a5ac

Browse files
committed
RFC: redefine PyTypeObjects as heap types for Limited API compliance
1 parent dc15c42 commit dc0a5ac

1 file changed

Lines changed: 39 additions & 38 deletions

File tree

extension/wrapper.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ typedef struct {
2222
elsetrec satrec[0];
2323
} SatrecArrayObject;
2424

25+
static PyObject* SatrecType = NULL;
26+
static PyObject* SatrecArrayType = NULL;
27+
2528
/* Support routine that is used to support NumPy array broadcasting for
2629
both individual satellite objects and also arrays. */
2730

@@ -483,22 +486,13 @@ static PyGetSetDef Satrec_getset[] = {
483486
{NULL},
484487
};
485488

486-
static PyTypeObject SatrecType = {
487-
PyVarObject_HEAD_INIT(NULL, 0)
488-
/* See the module initialization function at the bottom of this file. */
489-
};
490-
491489
/* Details of the SatrecArray. */
492490

493491
static Py_ssize_t
494492
Satrec_len(PyObject *self) {
495493
return ((SatrecArrayObject*)self)->ob_base.ob_size;
496494
}
497495

498-
static PySequenceMethods SatrecArray_as_sequence = {
499-
Satrec_len
500-
};
501-
502496
static PyObject *
503497
SatrecArray_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
504498
{
@@ -557,9 +551,36 @@ static PyMethodDef SatrecArray_methods[] = {
557551
{NULL, NULL}
558552
};
559553

560-
static PyTypeObject SatrecArrayType = {
561-
PyVarObject_HEAD_INIT(NULL, sizeof(elsetrec))
562-
/* See the module initialization function at the bottom of this file. */
554+
static char doc_SatRecType[23] = "SGP4 satellite record.";
555+
static PyType_Spec SatrecType_spec = {
556+
.name = "sgp4.vallado_cpp.Satrec",
557+
.basicsize = sizeof(SatrecObject),
558+
.itemsize = 0,
559+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
560+
.slots = (PyType_Slot[]){
561+
{Py_tp_doc, doc_SatRecType},
562+
{Py_tp_methods, Satrec_methods},
563+
{Py_tp_members, Satrec_members},
564+
{Py_tp_getset, Satrec_getset},
565+
//{Py_tp_new, PyType_GenericNew},
566+
{0, NULL},
567+
},
568+
};
569+
570+
static char doc_SatRecArrayType[26] = "SGP4 array of satellites.";
571+
static PyType_Spec SatrecArrayType_spec = {
572+
.name = "sgp4.vallado_cpp.SatrecArray",
573+
.basicsize = sizeof(SatrecArrayObject),
574+
.itemsize = sizeof(elsetrec),
575+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
576+
.slots = (PyType_Slot[]){
577+
{Py_tp_doc, doc_SatRecArrayType},
578+
{Py_tp_methods, SatrecArray_methods},
579+
{Py_tp_init, (void *)SatrecArray_init},
580+
{Py_tp_new, (void *)SatrecArray_new},
581+
{Py_sq_length, (void *)Satrec_len},
582+
{0, NULL},
583+
},
563584
};
564585

565586
/* The module that ties it all together. */
@@ -575,45 +596,25 @@ static PyModuleDef module = {
575596
PyMODINIT_FUNC
576597
PyInit_vallado_cpp(void)
577598
{
578-
SatrecType.tp_name = "sgp4.vallado_cpp.Satrec";
579-
SatrecType.tp_basicsize = sizeof(SatrecObject);
580-
SatrecArrayType.tp_itemsize = 0;
581-
SatrecType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
582-
SatrecType.tp_doc = "SGP4 satellite record.";
583-
SatrecType.tp_methods = Satrec_methods;
584-
SatrecType.tp_members = Satrec_members;
585-
SatrecType.tp_getset = Satrec_getset;
586-
SatrecType.tp_new = PyType_GenericNew;
587-
588-
if (PyType_Ready(&SatrecType) < 0)
599+
SatrecType = PyType_FromSpec(&SatrecType_spec);
600+
if (SatrecType == NULL)
589601
return NULL;
590602

591-
SatrecArrayType.tp_name = "sgp4.vallado_cpp.SatrecArray";
592-
SatrecArrayType.tp_basicsize = sizeof(SatrecArrayObject);
593-
SatrecArrayType.tp_itemsize = sizeof(elsetrec);
594-
SatrecArrayType.tp_as_sequence = &SatrecArray_as_sequence;
595-
SatrecArrayType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
596-
SatrecArrayType.tp_doc = "SGP4 array of satellites.";
597-
SatrecArrayType.tp_methods = SatrecArray_methods;
598-
SatrecArrayType.tp_init = (initproc) SatrecArray_init;
599-
SatrecArrayType.tp_new = SatrecArray_new;
600-
601-
if (PyType_Ready(&SatrecArrayType) < 0)
603+
SatrecArrayType = PyType_FromSpec(&SatrecArrayType_spec);
604+
if (SatrecArrayType == NULL)
602605
return NULL;
603606

604607
PyObject *m = PyModule_Create(&module);
605608
if (m == NULL)
606609
return NULL;
607610

608-
Py_INCREF(&SatrecType);
609-
if (PyModule_AddObject(m, "Satrec", (PyObject *) &SatrecType) < 0) {
611+
if (PyModule_AddObject(m, "Satrec", SatrecType) < 0) {
610612
Py_DECREF(&SatrecType);
611613
Py_DECREF(m);
612614
return NULL;
613615
}
614616

615-
Py_INCREF(&SatrecArrayType);
616-
if (PyModule_AddObject(m, "SatrecArray", (PyObject *) &SatrecArrayType) < 0) {
617+
if (PyModule_AddObject(m, "SatrecArray", SatrecArrayType) < 0) {
617618
Py_DECREF(&SatrecArrayType);
618619
Py_DECREF(&SatrecType);
619620
Py_DECREF(m);

0 commit comments

Comments
 (0)