Mercurial > cpython-lru_cache-weakref
comparison Python/sysmodule.c @ 83:f6bcf3c1d714 legacy-trunk
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...)
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sun, 18 Nov 1990 17:41:40 +0000 |
| parents | fc6fcd7df4f7 |
| children | a6934380c6e7 |
comparison
equal
deleted
inserted
replaced
| 82:554ac659b2cb | 83:f6bcf3c1d714 |
|---|---|
| 129 dictinsert(sysdict, "stderr", syserr); | 129 dictinsert(sysdict, "stderr", syserr); |
| 130 dictinsert(sysdict, "argv", v); | 130 dictinsert(sysdict, "argv", v); |
| 131 dictinsert(sysdict, "exit", exit); | 131 dictinsert(sysdict, "exit", exit); |
| 132 if (err_occurred()) | 132 if (err_occurred()) |
| 133 fatal("can't insert sys.* objects in sys dict"); | 133 fatal("can't insert sys.* objects in sys dict"); |
| 134 DECREF(exit); | |
| 134 DECREF(v); | 135 DECREF(v); |
| 135 /* The other symbols are added elsewhere */ | 136 /* The other symbols are added elsewhere */ |
| 136 | 137 |
| 137 /* Only now can we initialize the import stuff, after which | 138 /* Only now can we initialize the import stuff, after which |
| 138 we can turn ourselves into a module */ | 139 we can turn ourselves into a module */ |
| 139 initimport(); | 140 initimport(); |
| 140 if ((v = new_module("sys")) == NULL) | 141 if ((v = new_module("sys")) == NULL) |
| 141 fatal("can't create sys module"); | 142 fatal("can't create sys module"); |
| 142 if (setmoduledict(v, sysdict) != 0) | 143 if (setmoduledict(v, sysdict) != 0) |
| 143 fatal("can't assign sys dict to sys module"); | 144 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 } | |
| 144 } | 160 } |
| 145 | 161 |
| 146 void | 162 void |
| 147 closesys() | 163 closesys() |
| 148 { | 164 { |
| 149 object *mtab; | 165 object *modules; |
| 150 mtab = sysget("modules"); | 166 modules = sysget("modules"); |
| 151 if (mtab != NULL && is_dictobject(mtab)) | 167 if (modules != NULL && is_dictobject(modules)) { |
| 152 dictremove(mtab, "sys"); /* Get rid of recursion */ | 168 int i; |
| 153 else | 169 /* Explicitly erase all modules; this is the safest way |
| 154 fprintf(stderr, "[module sys not found]\n"); | 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 } | |
| 155 DECREF(sysdict); | 190 DECREF(sysdict); |
| 156 } | 191 } |
