|
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_SetDeferredRefcount |
9 | 10 | #include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches
|
10 | 11 | #include "pycore_opcode_utils.h" // RESUME_AT_FUNC_START
|
11 | 12 | #include "pycore_pystate.h" // _PyInterpreterState_GET()
|
@@ -557,13 +558,22 @@ _PyCode_New(struct _PyCodeConstructor *con)
|
557 | 558 | }
|
558 | 559 |
|
559 | 560 | Py_ssize_t size = PyBytes_GET_SIZE(con->code) / sizeof(_Py_CODEUNIT);
|
560 |
| - PyCodeObject *co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size); |
| 561 | + PyCodeObject *co; |
| 562 | +#ifdef Py_GIL_DISABLED |
| 563 | + co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size); |
| 564 | +#else |
| 565 | + co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size); |
| 566 | +#endif |
561 | 567 | if (co == NULL) {
|
562 | 568 | Py_XDECREF(replacement_locations);
|
563 | 569 | PyErr_NoMemory();
|
564 | 570 | return NULL;
|
565 | 571 | }
|
566 | 572 | init_code(co, con);
|
| 573 | +#ifdef Py_GIL_DISABLED |
| 574 | + _PyObject_SetDeferredRefcount((PyObject *)co); |
| 575 | + _PyObject_GC_TRACK(co); |
| 576 | +#endif |
567 | 577 | Py_XDECREF(replacement_locations);
|
568 | 578 | return co;
|
569 | 579 | }
|
@@ -1710,6 +1720,10 @@ code_dealloc(PyCodeObject *co)
|
1710 | 1720 | }
|
1711 | 1721 | Py_SET_REFCNT(co, 0);
|
1712 | 1722 |
|
| 1723 | +#ifdef Py_GIL_DISABLED |
| 1724 | + PyObject_GC_UnTrack(co); |
| 1725 | +#endif |
| 1726 | + |
1713 | 1727 | _PyFunction_ClearCodeByVersion(co->co_version);
|
1714 | 1728 | if (co->co_extra != NULL) {
|
1715 | 1729 | PyInterpreterState *interp = _PyInterpreterState_GET();
|
@@ -1752,6 +1766,15 @@ code_dealloc(PyCodeObject *co)
|
1752 | 1766 | PyObject_Free(co);
|
1753 | 1767 | }
|
1754 | 1768 |
|
| 1769 | +#ifdef Py_GIL_DISABLED |
| 1770 | +static int |
| 1771 | +code_traverse(PyCodeObject *co, visitproc visit, void *arg) |
| 1772 | +{ |
| 1773 | + Py_VISIT(co->co_consts); |
| 1774 | + return 0; |
| 1775 | +} |
| 1776 | +#endif |
| 1777 | + |
1755 | 1778 | static PyObject *
|
1756 | 1779 | code_repr(PyCodeObject *co)
|
1757 | 1780 | {
|
@@ -2196,9 +2219,17 @@ PyTypeObject PyCode_Type = {
|
2196 | 2219 | PyObject_GenericGetAttr, /* tp_getattro */
|
2197 | 2220 | 0, /* tp_setattro */
|
2198 | 2221 | 0, /* tp_as_buffer */
|
| 2222 | +#ifdef Py_GIL_DISABLED |
| 2223 | + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2224 | +#else |
2199 | 2225 | Py_TPFLAGS_DEFAULT, /* tp_flags */
|
| 2226 | +#endif |
2200 | 2227 | code_new__doc__, /* tp_doc */
|
| 2228 | +#ifdef Py_GIL_DISABLED |
| 2229 | + (traverseproc)code_traverse, /* tp_traverse */ |
| 2230 | +#else |
2201 | 2231 | 0, /* tp_traverse */
|
| 2232 | +#endif |
2202 | 2233 | 0, /* tp_clear */
|
2203 | 2234 | code_richcompare, /* tp_richcompare */
|
2204 | 2235 | offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
|
|
0 commit comments