Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* System module */ |
| 26 | |
| 27 | /* |
| 28 | Various bits of information used by the interpreter are collected in |
| 29 | module 'sys'. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | Function member: |
Guido van Rossum | cc8914f | 1995-03-20 15:09:40 +0000 | [diff] [blame] | 31 | - exit(sts): raise SystemExit |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | Data members: |
| 33 | - stdin, stdout, stderr: standard file objects |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | - modules: the table of modules (dictionary) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | - path: module search path (list of strings) |
| 36 | - argv: script arguments (list of strings) |
| 37 | - ps1, ps2: optional primary and secondary prompts (strings) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | #include "sysmodule.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | #include "import.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | #include "modsupport.h" |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 45 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 47 | object *sys_trace, *sys_profile; |
Guido van Rossum | 9a1f388 | 1995-03-30 11:01:58 +0000 | [diff] [blame] | 48 | int sys_checkinterval = 10; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | |
| 50 | static object *sysdict; |
| 51 | |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 52 | #ifdef NT |
| 53 | extern void *PyWin_DLLhModule; |
| 54 | #endif |
| 55 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | object * |
| 57 | sysget(name) |
| 58 | char *name; |
| 59 | { |
| 60 | return dictlookup(sysdict, name); |
| 61 | } |
| 62 | |
| 63 | FILE * |
| 64 | sysgetfile(name, def) |
| 65 | char *name; |
| 66 | FILE *def; |
| 67 | { |
| 68 | FILE *fp = NULL; |
| 69 | object *v = sysget(name); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 70 | if (v != NULL && is_fileobject(v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | fp = getfilefile(v); |
| 72 | if (fp == NULL) |
| 73 | fp = def; |
| 74 | return fp; |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | sysset(name, v) |
| 79 | char *name; |
| 80 | object *v; |
| 81 | { |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 82 | if (v == NULL) { |
| 83 | if (dictlookup(sysdict, name) == NULL) |
| 84 | return 0; |
| 85 | else |
| 86 | return dictremove(sysdict, name); |
| 87 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | else |
| 89 | return dictinsert(sysdict, name, v); |
| 90 | } |
| 91 | |
| 92 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | sys_exit(self, args) |
| 94 | object *self; |
| 95 | object *args; |
| 96 | { |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 97 | /* Raise SystemExit so callers may catch it or clean up. */ |
| 98 | err_setval(SystemExit, args); |
| 99 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 102 | static object * |
| 103 | sys_settrace(self, args) |
| 104 | object *self; |
| 105 | object *args; |
| 106 | { |
| 107 | if (args == None) |
| 108 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 109 | else |
Guido van Rossum | 5dc8eb0 | 1992-06-19 13:57:27 +0000 | [diff] [blame] | 110 | XINCREF(args); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 111 | XDECREF(sys_trace); |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 112 | sys_trace = args; |
| 113 | INCREF(None); |
| 114 | return None; |
| 115 | } |
| 116 | |
| 117 | static object * |
| 118 | sys_setprofile(self, args) |
| 119 | object *self; |
| 120 | object *args; |
| 121 | { |
| 122 | if (args == None) |
| 123 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 124 | else |
Guido van Rossum | 5dc8eb0 | 1992-06-19 13:57:27 +0000 | [diff] [blame] | 125 | XINCREF(args); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 126 | XDECREF(sys_profile); |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 127 | sys_profile = args; |
| 128 | INCREF(None); |
| 129 | return None; |
| 130 | } |
| 131 | |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 132 | static object * |
| 133 | sys_setcheckinterval(self, args) |
| 134 | object *self; |
| 135 | object *args; |
| 136 | { |
| 137 | if (!newgetargs(args, "i", &sys_checkinterval)) |
| 138 | return NULL; |
| 139 | INCREF(None); |
| 140 | return None; |
| 141 | } |
| 142 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 143 | #ifdef USE_MALLOPT |
| 144 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 145 | #include <malloc.h> |
| 146 | |
| 147 | static object * |
| 148 | sys_mdebug(self, args) |
| 149 | object *self; |
| 150 | object *args; |
| 151 | { |
| 152 | int flag; |
| 153 | if (!getargs(args, "i", &flag)) |
| 154 | return NULL; |
| 155 | mallopt(M_DEBUG, flag); |
| 156 | INCREF(None); |
| 157 | return None; |
| 158 | } |
| 159 | #endif /* USE_MALLOPT */ |
| 160 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 161 | static object * |
| 162 | sys_getrefcount(self, args) |
| 163 | object *self; |
| 164 | object *args; |
| 165 | { |
| 166 | object *arg; |
| 167 | if (!getargs(args, "O", &arg)) |
| 168 | return NULL; |
| 169 | return newintobject((long) arg->ob_refcnt); |
| 170 | } |
| 171 | |
| 172 | #ifdef COUNT_ALLOCS |
| 173 | static PyObject * |
| 174 | sys_getcounts(self, args) |
| 175 | PyObject *self, *args; |
| 176 | { |
| 177 | extern PyObject *get_counts Py_PROTO((void)); |
| 178 | |
| 179 | if (!PyArg_Parse(args, "")) |
| 180 | return NULL; |
| 181 | return get_counts(); |
| 182 | } |
| 183 | #endif |
| 184 | |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 185 | #ifdef Py_TRACE_REFS |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 186 | extern PyObject *getobjects Py_PROTO((PyObject *, PyObject *)); |
| 187 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 188 | static struct methodlist sys_methods[] = { |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 189 | {"exit", sys_exit, 0}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 190 | {"getrefcount", sys_getrefcount, 0}, |
| 191 | #ifdef COUNT_ALLOCS |
| 192 | {"getcounts", sys_getcounts, 0}, |
| 193 | #endif |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 194 | #ifdef Py_TRACE_REFS |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 195 | {"getobjects", getobjects, 1}, |
| 196 | #endif |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 197 | #ifdef USE_MALLOPT |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 198 | {"mdebug", sys_mdebug, 0}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 199 | #endif |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 200 | {"setprofile", sys_setprofile, 0}, |
| 201 | {"settrace", sys_settrace, 0}, |
Guido van Rossum | b0352fa | 1996-01-12 01:15:01 +0000 | [diff] [blame] | 202 | {"setcheckinterval", sys_setcheckinterval, 1}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 203 | {NULL, NULL} /* sentinel */ |
| 204 | }; |
| 205 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | static object *sysin, *sysout, *syserr; |
| 207 | |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 208 | static object * |
| 209 | list_builtin_module_names() |
| 210 | { |
| 211 | object *list = newlistobject(0); |
| 212 | int i; |
| 213 | if (list == NULL) |
| 214 | return NULL; |
| 215 | for (i = 0; inittab[i].name != NULL; i++) { |
| 216 | object *name = newstringobject(inittab[i].name); |
| 217 | if (name == NULL) |
| 218 | break; |
| 219 | addlistitem(list, name); |
| 220 | DECREF(name); |
| 221 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 222 | if (sortlist(list) != 0) { |
| 223 | DECREF(list); |
| 224 | list = NULL; |
| 225 | } |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 226 | return list; |
| 227 | } |
| 228 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 230 | initsys() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 232 | extern long getmaxint PROTO((void)); |
| 233 | extern char *getversion PROTO((void)); |
| 234 | extern char *getcopyright PROTO((void)); |
Guido van Rossum | aaf1839 | 1995-07-07 22:45:02 +0000 | [diff] [blame] | 235 | extern char *getplatform PROTO((void)); |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 236 | extern int fclose PROTO((FILE *)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 237 | object *m = initmodule("sys", sys_methods); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 238 | object *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 239 | sysdict = getmoduledict(m); |
| 240 | INCREF(sysdict); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | /* NB keep an extra ref to the std files to avoid closing them |
| 242 | when the user deletes them */ |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 243 | sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
| 244 | sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
| 245 | syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 246 | if (err_occurred()) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 247 | fatal("can't initialize sys.std{in,out,err}"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 248 | dictinsert(sysdict, "stdin", sysin); |
| 249 | dictinsert(sysdict, "stdout", sysout); |
| 250 | dictinsert(sysdict, "stderr", syserr); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 251 | dictinsert(sysdict, "version", v = newstringobject(getversion())); |
| 252 | XDECREF(v); |
| 253 | dictinsert(sysdict, "copyright", v = newstringobject(getcopyright())); |
| 254 | XDECREF(v); |
Guido van Rossum | aaf1839 | 1995-07-07 22:45:02 +0000 | [diff] [blame] | 255 | dictinsert(sysdict, "platform", v = newstringobject(getplatform())); |
| 256 | XDECREF(v); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 257 | dictinsert(sysdict, "maxint", v = newintobject(getmaxint())); |
| 258 | XDECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 259 | dictinsert(sysdict, "modules", get_modules()); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 260 | dictinsert(sysdict, "builtin_module_names", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 261 | v = list_builtin_module_names()); |
| 262 | XDECREF(v); |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 263 | #ifdef NT |
| 264 | dictinsert(sysdict, "dllhandle", v = newintobject((int)PyWin_DLLhModule)); |
| 265 | XDECREF(v); |
| 266 | dictinsert(sysdict, "winver", v = newstringobject(WIN32_PATCH_LEVEL)); |
| 267 | XDECREF(v); |
| 268 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 269 | if (err_occurred()) |
| 270 | fatal("can't insert sys.* objects in sys dict"); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 273 | static object * |
| 274 | makepathobject(path, delim) |
| 275 | char *path; |
| 276 | int delim; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 277 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 278 | int i, n; |
| 279 | char *p; |
| 280 | object *v, *w; |
| 281 | |
| 282 | n = 1; |
| 283 | p = path; |
| 284 | while ((p = strchr(p, delim)) != NULL) { |
| 285 | n++; |
| 286 | p++; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 287 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 288 | v = newlistobject(n); |
| 289 | if (v == NULL) |
| 290 | return NULL; |
| 291 | for (i = 0; ; i++) { |
| 292 | p = strchr(path, delim); |
| 293 | if (p == NULL) |
| 294 | p = strchr(path, '\0'); /* End of string */ |
| 295 | w = newsizedstringobject(path, (int) (p - path)); |
| 296 | if (w == NULL) { |
| 297 | DECREF(v); |
| 298 | return NULL; |
| 299 | } |
| 300 | setlistitem(v, i, w); |
| 301 | if (*p == '\0') |
| 302 | break; |
| 303 | path = p+1; |
| 304 | } |
| 305 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 309 | setpythonpath(path) |
| 310 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 312 | object *v; |
| 313 | if ((v = makepathobject(path, DELIM)) == NULL) |
| 314 | fatal("can't create sys.path"); |
| 315 | if (sysset("path", v) != 0) |
| 316 | fatal("can't assign sys.path"); |
| 317 | DECREF(v); |
| 318 | } |
| 319 | |
| 320 | static object * |
| 321 | makeargvobject(argc, argv) |
| 322 | int argc; |
| 323 | char **argv; |
| 324 | { |
| 325 | object *av; |
Guido van Rossum | ee3a299 | 1992-01-14 18:42:53 +0000 | [diff] [blame] | 326 | if (argc <= 0 || argv == NULL) { |
| 327 | /* Ensure at least one (empty) argument is seen */ |
| 328 | static char *empty_argv[1] = {""}; |
| 329 | argv = empty_argv; |
| 330 | argc = 1; |
| 331 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 332 | av = newlistobject(argc); |
| 333 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 334 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 335 | for (i = 0; i < argc; i++) { |
| 336 | object *v = newstringobject(argv[i]); |
| 337 | if (v == NULL) { |
| 338 | DECREF(av); |
| 339 | av = NULL; |
| 340 | break; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 341 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 342 | setlistitem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 343 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 344 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 345 | return av; |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | setpythonargv(argc, argv) |
| 350 | int argc; |
| 351 | char **argv; |
| 352 | { |
| 353 | object *av = makeargvobject(argc, argv); |
| 354 | if (av == NULL) |
| 355 | fatal("no mem for sys.argv"); |
| 356 | if (sysset("argv", av) != 0) |
| 357 | fatal("can't assign sys.argv"); |
| 358 | DECREF(av); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 359 | } |