Skip to content

Commit 5b3138b

Browse files
committed
Empty all modules' symbol tables, so most circular references are
cleared up. (A function definition references its module's symbol table but the symbol table of course references the function...)
1 parent 392ab32 commit 5b3138b

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

Python/sysmodule.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ initsys(argc, argv)
131131
dictinsert(sysdict, "exit", exit);
132132
if (err_occurred())
133133
fatal("can't insert sys.* objects in sys dict");
134+
DECREF(exit);
134135
DECREF(v);
135136
/* The other symbols are added elsewhere */
136137

@@ -141,16 +142,50 @@ initsys(argc, argv)
141142
fatal("can't create sys module");
142143
if (setmoduledict(v, sysdict) != 0)
143144
fatal("can't assign sys dict to sys module");
145+
DECREF(v);
146+
}
147+
148+
static void
149+
cleardict(d)
150+
object *d;
151+
{
152+
int i;
153+
for (i = getdictsize(d); --i >= 0; ) {
154+
char *k;
155+
k = getdictkey(d, i);
156+
if (k != NULL) {
157+
(void) dictremove(d, k);
158+
}
159+
}
144160
}
145161

146162
void
147163
closesys()
148164
{
149-
object *mtab;
150-
mtab = sysget("modules");
151-
if (mtab != NULL && is_dictobject(mtab))
152-
dictremove(mtab, "sys"); /* Get rid of recursion */
153-
else
154-
fprintf(stderr, "[module sys not found]\n");
165+
object *modules;
166+
modules = sysget("modules");
167+
if (modules != NULL && is_dictobject(modules)) {
168+
int i;
169+
/* Explicitly erase all modules; this is the safest way
170+
to get rid of at least *some* circular dependencies */
171+
INCREF(modules);
172+
for (i = getdictsize(modules); --i >= 0; ) {
173+
char *k;
174+
k = getdictkey(modules, i);
175+
if (k != NULL) {
176+
object *m;
177+
m = dictlookup(modules, k);
178+
if (m != NULL && is_moduleobject(m)) {
179+
object *d;
180+
d = getmoduledict(m);
181+
if (d != NULL && is_dictobject(d)) {
182+
cleardict(d);
183+
}
184+
}
185+
}
186+
}
187+
cleardict(modules);
188+
DECREF(modules);
189+
}
155190
DECREF(sysdict);
156191
}

0 commit comments

Comments
 (0)