summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1990-11-18 17:41:40 (GMT)
committerGuido van Rossum <guido@python.org>1990-11-18 17:41:40 (GMT)
commit5b3138bec047cfe9d284516be3958680f4fcf4ab (patch)
tree64b0f0f2eb3984a09549c017ce922b568e0bc851 /Python
parent392ab328594bbf299f63ca9c3ba064e9a1a2deb1 (diff)
downloadcpython-5b3138bec047cfe9d284516be3958680f4fcf4ab.zip
cpython-5b3138bec047cfe9d284516be3958680f4fcf4ab.tar.gz
cpython-5b3138bec047cfe9d284516be3958680f4fcf4ab.tar.bz2
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...)
Diffstat (limited to 'Python')
-rw-r--r--Python/sysmodule.c47
1 files changed, 41 insertions, 6 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 845ccfc..f276fb4 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -131,6 +131,7 @@ initsys(argc, argv)
dictinsert(sysdict, "exit", exit);
if (err_occurred())
fatal("can't insert sys.* objects in sys dict");
+ DECREF(exit);
DECREF(v);
/* The other symbols are added elsewhere */
@@ -141,16 +142,50 @@ initsys(argc, argv)
fatal("can't create sys module");
if (setmoduledict(v, sysdict) != 0)
fatal("can't assign sys dict to sys module");
+ DECREF(v);
+}
+
+static void
+cleardict(d)
+ object *d;
+{
+ int i;
+ for (i = getdictsize(d); --i >= 0; ) {
+ char *k;
+ k = getdictkey(d, i);
+ if (k != NULL) {
+ (void) dictremove(d, k);
+ }
+ }
}
void
closesys()
{
- object *mtab;
- mtab = sysget("modules");
- if (mtab != NULL && is_dictobject(mtab))
- dictremove(mtab, "sys"); /* Get rid of recursion */
- else
- fprintf(stderr, "[module sys not found]\n");
+ object *modules;
+ modules = sysget("modules");
+ if (modules != NULL && is_dictobject(modules)) {
+ int i;
+ /* Explicitly erase all modules; this is the safest way
+ to get rid of at least *some* circular dependencies */
+ INCREF(modules);
+ for (i = getdictsize(modules); --i >= 0; ) {
+ char *k;
+ k = getdictkey(modules, i);
+ if (k != NULL) {
+ object *m;
+ m = dictlookup(modules, k);
+ if (m != NULL && is_moduleobject(m)) {
+ object *d;
+ d = getmoduledict(m);
+ if (d != NULL && is_dictobject(d)) {
+ cleardict(d);
+ }
+ }
+ }
+ }
+ cleardict(modules);
+ DECREF(modules);
+ }
DECREF(sysdict);
}