|
6 | 6 | #include "pycore_code.h" // _PyCodeConstructor
|
7 | 7 | #include "pycore_frame.h" // FRAME_SPECIALS_SIZE
|
8 | 8 | #include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs
|
| 9 | +#include "pycore_object.h" // _PyObject_SET_DEFERRED_REFCOUNT |
9 | 10 | #include "pycore_opcode.h" // _PyOpcode_Deopt
|
10 | 11 | #include "pycore_pystate.h" // _PyInterpreterState_GET()
|
11 | 12 | #include "pycore_tuple.h" // _PyTuple_ITEMS()
|
@@ -550,13 +551,15 @@ _PyCode_New(struct _PyCodeConstructor *con)
|
550 | 551 | }
|
551 | 552 |
|
552 | 553 | Py_ssize_t size = PyBytes_GET_SIZE(con->code) / sizeof(_Py_CODEUNIT);
|
553 |
| - PyCodeObject *co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size); |
| 554 | + PyCodeObject *co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size); |
554 | 555 | if (co == NULL) {
|
555 | 556 | Py_XDECREF(replacement_locations);
|
556 | 557 | PyErr_NoMemory();
|
557 | 558 | return NULL;
|
558 | 559 | }
|
559 | 560 | init_code(co, con);
|
| 561 | + _PyObject_SET_DEFERRED_REFCOUNT(co); |
| 562 | + _PyObject_GC_TRACK(co); |
560 | 563 | Py_XDECREF(replacement_locations);
|
561 | 564 | return co;
|
562 | 565 | }
|
@@ -1668,6 +1671,7 @@ code_dealloc(PyCodeObject *co)
|
1668 | 1671 | {
|
1669 | 1672 | notify_code_watchers(PY_CODE_EVENT_DESTROY, co);
|
1670 | 1673 |
|
| 1674 | + _PyObject_GC_UNTRACK(co); |
1671 | 1675 | if (co->co_extra != NULL) {
|
1672 | 1676 | PyInterpreterState *interp = _PyInterpreterState_GET();
|
1673 | 1677 | _PyCodeObjectExtra *co_extra = co->co_extra;
|
@@ -1705,7 +1709,14 @@ code_dealloc(PyCodeObject *co)
|
1705 | 1709 | if (co->_co_linearray) {
|
1706 | 1710 | PyMem_Free(co->_co_linearray);
|
1707 | 1711 | }
|
1708 |
| - PyObject_Free(co); |
| 1712 | + PyObject_GC_Del(co); |
| 1713 | +} |
| 1714 | + |
| 1715 | +static int |
| 1716 | +code_traverse(PyCodeObject *co, visitproc visit, void *arg) |
| 1717 | +{ |
| 1718 | + Py_VISIT(co->co_consts); |
| 1719 | + return 0; |
1709 | 1720 | }
|
1710 | 1721 |
|
1711 | 1722 | static PyObject *
|
@@ -2114,9 +2125,9 @@ PyTypeObject PyCode_Type = {
|
2114 | 2125 | PyObject_GenericGetAttr, /* tp_getattro */
|
2115 | 2126 | 0, /* tp_setattro */
|
2116 | 2127 | 0, /* tp_as_buffer */
|
2117 |
| - Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2128 | + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
2118 | 2129 | code_new__doc__, /* tp_doc */
|
2119 |
| - 0, /* tp_traverse */ |
| 2130 | + (traverseproc)code_traverse, /* tp_traverse */ |
2120 | 2131 | 0, /* tp_clear */
|
2121 | 2132 | code_richcompare, /* tp_richcompare */
|
2122 | 2133 | offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
|
|
0 commit comments