Skip to content

Commit 4d90ca8

Browse files
committed
Fix a couple Python C-API issues in the record module
Call PyType_Ready() on Record and its helper types. PyObject_GC_(Un)Track should be used instead of _PyObject_GC_(UN)TRACK.
1 parent a0cc7c3 commit 4d90ca8

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

asyncpg/protocol/protocol.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,6 @@ def _create_record(object mapping, tuple elems):
487487
cpython.Py_INCREF(elem)
488488
record.ApgRecord_SET_ITEM(rec, i, elem)
489489
return rec
490+
491+
492+
record.ApgRecord_InitTypes()

asyncpg/protocol/record/__init__.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
cdef extern from "record/recordobj.h":
99

10+
int ApgRecord_InitTypes()
11+
1012
int ApgRecord_CheckExact(object)
1113
object ApgRecord_New(object, int)
1214
void ApgRecord_SET_ITEM(object, int, object)

asyncpg/protocol/record/recordobj.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ApgRecord_New(PyObject *mapping, Py_ssize_t size)
5151
Py_INCREF(mapping);
5252
o->mapping = mapping;
5353
o->self_hash = -1;
54-
_PyObject_GC_TRACK(o);
54+
PyObject_GC_Track(o);
5555
return (PyObject *) o;
5656
}
5757

@@ -509,7 +509,7 @@ static PyMethodDef record_methods[] = {
509509

510510

511511
PyTypeObject ApgRecord_Type = {
512-
PyVarObject_HEAD_INIT(&PyType_Type, 0)
512+
PyVarObject_HEAD_INIT(NULL, 0)
513513
"Record", /* tp_name */
514514
sizeof(ApgRecordObject) - sizeof(PyObject *), /* tp_basic_size */
515515
sizeof(PyObject *), /* tp_itemsize */
@@ -565,7 +565,7 @@ typedef struct {
565565
static void
566566
record_iter_dealloc(ApgRecordIterObject *it)
567567
{
568-
_PyObject_GC_UNTRACK(it);
568+
PyObject_GC_UnTrack(it);
569569
Py_CLEAR(it->it_seq);
570570
PyObject_GC_Del(it);
571571
}
@@ -627,7 +627,7 @@ static PyMethodDef record_iter_methods[] = {
627627

628628

629629
PyTypeObject ApgRecordIter_Type = {
630-
PyVarObject_HEAD_INIT(&PyType_Type, 0)
630+
PyVarObject_HEAD_INIT(NULL, 0)
631631
"RecordIterator", /* tp_name */
632632
sizeof(ApgRecordIterObject), /* tp_basicsize */
633633
0, /* tp_itemsize */
@@ -675,7 +675,7 @@ record_iter(PyObject *seq)
675675
it->it_index = 0;
676676
Py_INCREF(seq);
677677
it->it_seq = (ApgRecordObject *)seq;
678-
_PyObject_GC_TRACK(it);
678+
PyObject_GC_Track(it);
679679
return (PyObject *)it;
680680
}
681681

@@ -694,7 +694,7 @@ typedef struct {
694694
static void
695695
record_items_dealloc(ApgRecordItemsObject *it)
696696
{
697-
_PyObject_GC_UNTRACK(it);
697+
PyObject_GC_UnTrack(it);
698698
Py_CLEAR(it->it_map_iter);
699699
Py_CLEAR(it->it_seq);
700700
PyObject_GC_Del(it);
@@ -784,7 +784,7 @@ static PyMethodDef record_items_methods[] = {
784784

785785

786786
PyTypeObject ApgRecordItems_Type = {
787-
PyVarObject_HEAD_INIT(&PyType_Type, 0)
787+
PyVarObject_HEAD_INIT(NULL, 0)
788788
"RecordItemsIterator", /* tp_name */
789789
sizeof(ApgRecordItemsObject), /* tp_basicsize */
790790
0, /* tp_itemsize */
@@ -841,7 +841,25 @@ record_new_items_iter(PyObject *seq)
841841
it->it_index = 0;
842842
Py_INCREF(seq);
843843
it->it_seq = (ApgRecordObject *)seq;
844-
_PyObject_GC_TRACK(it);
844+
PyObject_GC_Track(it);
845845

846846
return (PyObject *)it;
847847
}
848+
849+
850+
int ApgRecord_InitTypes(void)
851+
{
852+
if (PyType_Ready(&ApgRecord_Type) < 0) {
853+
return -1;
854+
}
855+
856+
if (PyType_Ready(&ApgRecordIter_Type) < 0) {
857+
return -1;
858+
}
859+
860+
if (PyType_Ready(&ApgRecordItems_Type) < 0) {
861+
return -1;
862+
}
863+
864+
return 0;
865+
}

asyncpg/protocol/record/recordobj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern PyTypeObject ApgRecordItems_Type;
3333
#define ApgRecord_GET_ITEM(op, i) \
3434
(((ApgRecordObject *)(op))->ob_item[i])
3535

36+
int ApgRecord_InitTypes(void);
3637
PyObject * ApgRecord_New(PyObject *, Py_ssize_t);
3738

3839
#endif

0 commit comments

Comments
 (0)