summaryrefslogtreecommitdiffstats
path: root/Objects/moduleobject.c
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-04-12 17:36:20 (GMT)
committerGitHub <noreply@github.com>2024-04-12 17:36:20 (GMT)
commit4ad8f090cce03c24fd4279ec8198a099b2d0cf97 (patch)
treeda9e5dc5aac6b0b1121eaf4a35c286783d41d2ff /Objects/moduleobject.c
parentc50cb6dd09d5a1bfdd1b896cc31ccdc96c72e561 (diff)
downloadcpython-4ad8f090cce03c24fd4279ec8198a099b2d0cf97.zip
cpython-4ad8f090cce03c24fd4279ec8198a099b2d0cf97.tar.gz
cpython-4ad8f090cce03c24fd4279ec8198a099b2d0cf97.tar.bz2
gh-117376: Partial implementation of deferred reference counting (#117696)
This marks objects as using deferred refrence counting using the `ob_gc_bits` field in the free-threaded build and collects those objects during GC.
Diffstat (limited to 'Objects/moduleobject.c')
-rw-r--r--Objects/moduleobject.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 9cd98fb..da6a276 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -88,21 +88,31 @@ new_module_notrack(PyTypeObject *mt)
m->md_weaklist = NULL;
m->md_name = NULL;
m->md_dict = PyDict_New();
- if (m->md_dict != NULL) {
- return m;
+ if (m->md_dict == NULL) {
+ Py_DECREF(m);
+ return NULL;
}
- Py_DECREF(m);
- return NULL;
+ return m;
+}
+
+static void
+track_module(PyModuleObject *m)
+{
+ _PyObject_SetDeferredRefcount(m->md_dict);
+ PyObject_GC_Track(m->md_dict);
+
+ _PyObject_SetDeferredRefcount((PyObject *)m);
+ PyObject_GC_Track(m);
}
static PyObject *
new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
{
- PyObject *m = (PyObject *)new_module_notrack(mt);
+ PyModuleObject *m = new_module_notrack(mt);
if (m != NULL) {
- PyObject_GC_Track(m);
+ track_module(m);
}
- return m;
+ return (PyObject *)m;
}
PyObject *
@@ -113,7 +123,7 @@ PyModule_NewObject(PyObject *name)
return NULL;
if (module_init_dict(m, m->md_dict, name, NULL) != 0)
goto fail;
- PyObject_GC_Track(m);
+ track_module(m);
return (PyObject *)m;
fail:
@@ -705,16 +715,7 @@ static int
module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
{
- PyObject *dict = self->md_dict;
- if (dict == NULL) {
- dict = PyDict_New();
- if (dict == NULL)
- return -1;
- self->md_dict = dict;
- }
- if (module_init_dict(self, dict, name, doc) < 0)
- return -1;
- return 0;
+ return module_init_dict(self, self->md_dict, name, doc);
}
static void