Mercurial > cpython-lru_cache-weakref
annotate Python/sysmodule.c @ 1073:2c0432fcc270 legacy-trunk
Modified most (but not yet all) I/O to always go through sys.stdout or
sys.stderr or sys.stdin, and to work with any object as long as it has
a write() (respectively readline()) methods. Some functions that took
a FILE* argument now take an object* argument.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 25 Sep 1992 21:59:05 +0000 |
| parents | a1ce5a949828 |
| children | 2937897a7d8b |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
| 145 | 3 Netherlands. |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* System module */ |
| 26 | |
| 27 /* | |
| 28 Various bits of information used by the interpreter are collected in | |
| 29 module 'sys'. | |
| 86 | 30 Function member: |
| 31 - exit(sts): call (C, POSIX) exit(sts) | |
| 6 | 32 Data members: |
| 33 - stdin, stdout, stderr: standard file objects | |
| 86 | 34 - modules: the table of modules (dictionary) |
| 6 | 35 - path: module search path (list of strings) |
| 86 | 36 - argv: script arguments (list of strings) |
| 37 - ps1, ps2: optional primary and secondary prompts (strings) | |
| 6 | 38 */ |
| 39 | |
| 86 | 40 #include "allobjects.h" |
| 41 | |
| 42 #include "sysmodule.h" | |
| 43 #include "import.h" | |
| 44 #include "modsupport.h" | |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
45 #include "osdefs.h" |
| 86 | 46 |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
47 object *sys_trace, *sys_profile; |
| 6 | 48 |
| 49 static object *sysdict; | |
| 50 | |
| 51 object * | |
| 52 sysget(name) | |
| 53 char *name; | |
| 54 { | |
| 55 return dictlookup(sysdict, name); | |
| 56 } | |
| 57 | |
| 58 FILE * | |
| 59 sysgetfile(name, def) | |
| 60 char *name; | |
| 61 FILE *def; | |
| 62 { | |
| 63 FILE *fp = NULL; | |
| 64 object *v = sysget(name); | |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1013
diff
changeset
|
65 if (v != NULL && is_fileobject(v)) |
| 6 | 66 fp = getfilefile(v); |
| 67 if (fp == NULL) | |
| 68 fp = def; | |
| 69 return fp; | |
| 70 } | |
| 71 | |
| 72 int | |
| 73 sysset(name, v) | |
| 74 char *name; | |
| 75 object *v; | |
| 76 { | |
|
631
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
77 if (v == NULL) { |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
78 if (dictlookup(sysdict, name) == NULL) |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
79 return 0; |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
80 else |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
81 return dictremove(sysdict, name); |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
82 } |
| 6 | 83 else |
| 84 return dictinsert(sysdict, name, v); | |
| 85 } | |
| 86 | |
| 87 static object * | |
| 88 sys_exit(self, args) | |
| 89 object *self; | |
| 90 object *args; | |
| 91 { | |
|
549
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
92 /* Raise SystemExit so callers may catch it or clean up. */ |
|
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
93 err_setval(SystemExit, args); |
|
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
94 return NULL; |
| 6 | 95 } |
| 96 | |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
97 static object * |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
98 sys_settrace(self, args) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
99 object *self; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
100 object *args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
101 { |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
102 if (args == None) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
103 args = NULL; |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
104 else |
|
901
fea3f594df77
sysmodule.c: calling sys.settrace() or sys.setprofile() without
Guido van Rossum <guido@python.org>
parents:
788
diff
changeset
|
105 XINCREF(args); |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
106 XDECREF(sys_trace); |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
107 sys_trace = args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
108 INCREF(None); |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
109 return None; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
110 } |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
111 |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
112 static object * |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
113 sys_setprofile(self, args) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
114 object *self; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
115 object *args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
116 { |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
117 if (args == None) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
118 args = NULL; |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
119 else |
|
901
fea3f594df77
sysmodule.c: calling sys.settrace() or sys.setprofile() without
Guido van Rossum <guido@python.org>
parents:
788
diff
changeset
|
120 XINCREF(args); |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
121 XDECREF(sys_profile); |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
122 sys_profile = args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
123 INCREF(None); |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
124 return None; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
125 } |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
126 |
|
1013
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
127 #ifdef USE_MALLOPT |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
128 /* Link with -lmalloc (or -lmpc) on an SGI */ |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
129 #include <malloc.h> |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
130 |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
131 static object * |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
132 sys_mdebug(self, args) |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
133 object *self; |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
134 object *args; |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
135 { |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
136 int flag; |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
137 if (!getargs(args, "i", &flag)) |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
138 return NULL; |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
139 mallopt(M_DEBUG, flag); |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
140 INCREF(None); |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
141 return None; |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
142 } |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
143 #endif /* USE_MALLOPT */ |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
144 |
| 86 | 145 static struct methodlist sys_methods[] = { |
| 146 {"exit", sys_exit}, | |
|
1013
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
147 #ifdef USE_MALLOPT |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
148 {"mdebug", sys_mdebug}, |
|
a1ce5a949828
Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
149 #endif |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
150 {"setprofile", sys_setprofile}, |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
151 {"settrace", sys_settrace}, |
| 86 | 152 {NULL, NULL} /* sentinel */ |
| 153 }; | |
| 154 | |
| 6 | 155 static object *sysin, *sysout, *syserr; |
| 156 | |
| 157 void | |
| 86 | 158 initsys() |
| 6 | 159 { |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
160 extern int fclose PROTO((FILE *)); |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
161 extern char version[]; |
|
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
162 object *v = newstringobject(version); |
| 86 | 163 object *m = initmodule("sys", sys_methods); |
| 164 sysdict = getmoduledict(m); | |
| 165 INCREF(sysdict); | |
| 6 | 166 /* NB keep an extra ref to the std files to avoid closing them |
| 167 when the user deletes them */ | |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
168 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
169 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
170 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
| 6 | 171 if (err_occurred()) |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
172 fatal("can't create sys.* objects"); |
| 6 | 173 dictinsert(sysdict, "stdin", sysin); |
| 174 dictinsert(sysdict, "stdout", sysout); | |
| 175 dictinsert(sysdict, "stderr", syserr); | |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
176 dictinsert(sysdict, "version", v); |
| 86 | 177 dictinsert(sysdict, "modules", get_modules()); |
| 6 | 178 if (err_occurred()) |
| 179 fatal("can't insert sys.* objects in sys dict"); | |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
180 DECREF(v); |
| 86 | 181 } |
| 182 | |
| 183 static object * | |
| 184 makepathobject(path, delim) | |
| 185 char *path; | |
| 186 int delim; | |
| 187 { | |
| 188 int i, n; | |
| 189 char *p; | |
| 190 object *v, *w; | |
| 6 | 191 |
| 86 | 192 n = 1; |
| 193 p = path; | |
| 194 while ((p = strchr(p, delim)) != NULL) { | |
| 195 n++; | |
| 196 p++; | |
| 197 } | |
| 198 v = newlistobject(n); | |
| 199 if (v == NULL) | |
| 200 return NULL; | |
| 201 for (i = 0; ; i++) { | |
| 202 p = strchr(path, delim); | |
| 203 if (p == NULL) | |
| 204 p = strchr(path, '\0'); /* End of string */ | |
| 205 w = newsizedstringobject(path, (int) (p - path)); | |
| 206 if (w == NULL) { | |
| 207 DECREF(v); | |
| 208 return NULL; | |
| 209 } | |
| 210 setlistitem(v, i, w); | |
| 211 if (*p == '\0') | |
| 212 break; | |
| 213 path = p+1; | |
| 214 } | |
| 215 return v; | |
| 216 } | |
| 217 | |
| 218 void | |
| 219 setpythonpath(path) | |
| 220 char *path; | |
| 221 { | |
| 222 object *v; | |
| 223 if ((v = makepathobject(path, DELIM)) == NULL) | |
| 224 fatal("can't create sys.path"); | |
| 225 if (sysset("path", v) != 0) | |
| 226 fatal("can't assign sys.path"); | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
227 DECREF(v); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
228 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
229 |
| 86 | 230 static object * |
| 231 makeargvobject(argc, argv) | |
| 232 int argc; | |
| 233 char **argv; | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
234 { |
| 86 | 235 object *av; |
|
596
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
236 if (argc <= 0 || argv == NULL) { |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
237 /* Ensure at least one (empty) argument is seen */ |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
238 static char *empty_argv[1] = {""}; |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
239 argv = empty_argv; |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
240 argc = 1; |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
241 } |
| 86 | 242 av = newlistobject(argc); |
| 243 if (av != NULL) { | |
| 244 int i; | |
| 245 for (i = 0; i < argc; i++) { | |
| 246 object *v = newstringobject(argv[i]); | |
| 247 if (v == NULL) { | |
| 248 DECREF(av); | |
| 249 av = NULL; | |
| 250 break; | |
| 251 } | |
| 252 setlistitem(av, i, v); | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
253 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
254 } |
| 86 | 255 return av; |
| 6 | 256 } |
| 257 | |
| 258 void | |
| 86 | 259 setpythonargv(argc, argv) |
| 260 int argc; | |
| 261 char **argv; | |
| 6 | 262 { |
| 86 | 263 object *av = makeargvobject(argc, argv); |
| 264 if (av == NULL) | |
| 265 fatal("no mem for sys.argv"); | |
| 266 if (sysset("argv", av) != 0) | |
| 267 fatal("can't assign sys.argv"); | |
| 268 DECREF(av); | |
| 6 | 269 } |
