Mercurial > cpython-lru_cache-weakref
annotate 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 |
| rev | line source |
|---|---|
| 6 | 1 /* System module */ |
| 2 | |
| 3 /* | |
| 4 Various bits of information used by the interpreter are collected in | |
| 5 module 'sys'. | |
| 6 Data members: | |
| 7 - stdin, stdout, stderr: standard file objects | |
| 8 - ps1, ps2: primary and secondary prompts (strings) | |
| 9 - path: module search path (list of strings) | |
| 10 - modules: the table of modules (dictionary) | |
| 11 Function members: | |
| 12 - exit(sts): call exit() | |
| 13 */ | |
| 14 | |
| 15 #include <stdio.h> | |
| 16 | |
| 17 #include "PROTO.h" | |
| 18 #include "object.h" | |
| 19 #include "stringobject.h" | |
| 20 #include "listobject.h" | |
| 21 #include "dictobject.h" | |
| 22 #include "fileobject.h" | |
| 23 #include "moduleobject.h" | |
| 24 #include "sysmodule.h" | |
| 25 #include "node.h" /* For context.h */ | |
| 26 #include "context.h" /* For import.h */ | |
| 27 #include "import.h" | |
| 28 #include "methodobject.h" | |
| 29 #include "modsupport.h" | |
| 30 #include "errors.h" | |
| 31 | |
| 32 static object *sysdict; | |
| 33 | |
| 34 object * | |
| 35 sysget(name) | |
| 36 char *name; | |
| 37 { | |
| 38 return dictlookup(sysdict, name); | |
| 39 } | |
| 40 | |
| 41 FILE * | |
| 42 sysgetfile(name, def) | |
| 43 char *name; | |
| 44 FILE *def; | |
| 45 { | |
| 46 FILE *fp = NULL; | |
| 47 object *v = sysget(name); | |
| 48 if (v != NULL) | |
| 49 fp = getfilefile(v); | |
| 50 if (fp == NULL) | |
| 51 fp = def; | |
| 52 return fp; | |
| 53 } | |
| 54 | |
| 55 int | |
| 56 sysset(name, v) | |
| 57 char *name; | |
| 58 object *v; | |
| 59 { | |
| 60 if (v == NULL) | |
| 61 return dictremove(sysdict, name); | |
| 62 else | |
| 63 return dictinsert(sysdict, name, v); | |
| 64 } | |
| 65 | |
| 66 static object * | |
| 67 makeargv(argc, argv) | |
| 68 int argc; | |
| 69 char **argv; | |
| 70 { | |
| 71 int i; | |
| 72 object *av, *v; | |
| 73 if (argc < 0 || argv == NULL) | |
| 74 argc = 0; | |
| 75 av = newlistobject(argc); | |
| 76 if (av != NULL) { | |
| 77 for (i = 0; i < argc; i++) { | |
| 78 v = newstringobject(argv[i]); | |
| 79 if (v == NULL) { | |
| 80 DECREF(av); | |
| 81 av = NULL; | |
| 82 break; | |
| 83 } | |
| 84 setlistitem(av, i, v); | |
| 85 } | |
| 86 } | |
| 87 if (av == NULL) | |
| 88 fatal("no mem for sys.argv"); | |
| 89 return av; | |
| 90 } | |
| 91 | |
| 92 /* sys.exit method */ | |
| 93 | |
| 94 static object * | |
| 95 sys_exit(self, args) | |
| 96 object *self; | |
| 97 object *args; | |
| 98 { | |
| 99 int sts; | |
| 100 if (!getintarg(args, &sts)) | |
| 101 return NULL; | |
| 102 goaway(sts); | |
| 103 exit(sts); /* Just in case */ | |
| 104 /* NOTREACHED */ | |
| 105 } | |
| 106 | |
| 107 static object *sysin, *sysout, *syserr; | |
| 108 | |
| 109 void | |
| 110 initsys(argc, argv) | |
| 111 int argc; | |
| 112 char **argv; | |
| 113 { | |
| 114 object *v; | |
| 115 object *exit; | |
| 116 if ((sysdict = newdictobject()) == NULL) | |
| 117 fatal("can't create sys dict"); | |
| 118 /* NB keep an extra ref to the std files to avoid closing them | |
| 119 when the user deletes them */ | |
| 120 sysin = newopenfileobject(stdin, "<stdin>", "r"); | |
| 121 sysout = newopenfileobject(stdout, "<stdout>", "w"); | |
| 122 syserr = newopenfileobject(stderr, "<stderr>", "w"); | |
| 123 v = makeargv(argc, argv); | |
| 124 exit = newmethodobject("exit", sys_exit, (object *)NULL); | |
| 125 if (err_occurred()) | |
| 126 fatal("can't create sys.* objects"); | |
| 127 dictinsert(sysdict, "stdin", sysin); | |
| 128 dictinsert(sysdict, "stdout", sysout); | |
| 129 dictinsert(sysdict, "stderr", syserr); | |
| 130 dictinsert(sysdict, "argv", v); | |
| 131 dictinsert(sysdict, "exit", exit); | |
| 132 if (err_occurred()) | |
| 133 fatal("can't insert sys.* objects in sys dict"); | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
134 DECREF(exit); |
| 6 | 135 DECREF(v); |
| 136 /* The other symbols are added elsewhere */ | |
| 137 | |
| 138 /* Only now can we initialize the import stuff, after which | |
| 139 we can turn ourselves into a module */ | |
| 140 initimport(); | |
| 141 if ((v = new_module("sys")) == NULL) | |
| 142 fatal("can't create sys module"); | |
| 143 if (setmoduledict(v, sysdict) != 0) | |
| 144 fatal("can't assign sys dict to sys module"); | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
145 DECREF(v); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
146 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
147 |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
148 static void |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
149 cleardict(d) |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
150 object *d; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
151 { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
152 int i; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
153 for (i = getdictsize(d); --i >= 0; ) { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
154 char *k; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
155 k = getdictkey(d, i); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
156 if (k != NULL) { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
157 (void) dictremove(d, k); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
158 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
159 } |
| 6 | 160 } |
| 161 | |
| 162 void | |
| 163 closesys() | |
| 164 { | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
165 object *modules; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
166 modules = sysget("modules"); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
167 if (modules != NULL && is_dictobject(modules)) { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
168 int i; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
169 /* Explicitly erase all modules; this is the safest way |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
170 to get rid of at least *some* circular dependencies */ |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
171 INCREF(modules); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
172 for (i = getdictsize(modules); --i >= 0; ) { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
173 char *k; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
174 k = getdictkey(modules, i); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
175 if (k != NULL) { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
176 object *m; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
177 m = dictlookup(modules, k); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
178 if (m != NULL && is_moduleobject(m)) { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
179 object *d; |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
180 d = getmoduledict(m); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
181 if (d != NULL && is_dictobject(d)) { |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
182 cleardict(d); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
183 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
184 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
185 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
186 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
187 cleardict(modules); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
188 DECREF(modules); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
189 } |
| 6 | 190 DECREF(sysdict); |
| 191 } |
