blob: 17845f06197260eb63c748ec332c5332bac69956 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* System module */
26
27/*
28Various bits of information used by the interpreter are collected in
29module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +000030Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +000031- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032Data members:
33- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035- 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 Rossum85a5fbb1990-10-14 12:07:46 +000038*/
39
Guido van Rossum3f5da241990-12-20 15:06:42 +000040#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042#include "sysmodule.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043#include "import.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044#include "modsupport.h"
Guido van Rossume2437a11992-03-23 18:20:18 +000045#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000046
Guido van Rossume2437a11992-03-23 18:20:18 +000047object *sys_trace, *sys_profile;
Guido van Rossum9a1f3881995-03-30 11:01:58 +000048int sys_checkinterval = 10;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049
50static object *sysdict;
51
Guido van Rossumc606fe11996-04-09 02:37:57 +000052#ifdef NT
53extern void *PyWin_DLLhModule;
54#endif
55
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056object *
57sysget(name)
58 char *name;
59{
60 return dictlookup(sysdict, name);
61}
62
63FILE *
64sysgetfile(name, def)
65 char *name;
66 FILE *def;
67{
68 FILE *fp = NULL;
69 object *v = sysget(name);
Guido van Rossum3165fe61992-09-25 21:59:05 +000070 if (v != NULL && is_fileobject(v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 fp = getfilefile(v);
72 if (fp == NULL)
73 fp = def;
74 return fp;
75}
76
77int
78sysset(name, v)
79 char *name;
80 object *v;
81{
Guido van Rossum5ad58c61992-01-26 18:15:48 +000082 if (v == NULL) {
83 if (dictlookup(sysdict, name) == NULL)
84 return 0;
85 else
86 return dictremove(sysdict, name);
87 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 else
89 return dictinsert(sysdict, name, v);
90}
91
92static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093sys_exit(self, args)
94 object *self;
95 object *args;
96{
Guido van Rossum6a468bf1991-12-31 13:15:35 +000097 /* Raise SystemExit so callers may catch it or clean up. */
98 err_setval(SystemExit, args);
99 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Guido van Rossume2437a11992-03-23 18:20:18 +0000102static object *
103sys_settrace(self, args)
104 object *self;
105 object *args;
106{
107 if (args == None)
108 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000109 else
Guido van Rossum5dc8eb01992-06-19 13:57:27 +0000110 XINCREF(args);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000111 XDECREF(sys_trace);
Guido van Rossume2437a11992-03-23 18:20:18 +0000112 sys_trace = args;
113 INCREF(None);
114 return None;
115}
116
117static object *
118sys_setprofile(self, args)
119 object *self;
120 object *args;
121{
122 if (args == None)
123 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000124 else
Guido van Rossum5dc8eb01992-06-19 13:57:27 +0000125 XINCREF(args);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000126 XDECREF(sys_profile);
Guido van Rossume2437a11992-03-23 18:20:18 +0000127 sys_profile = args;
128 INCREF(None);
129 return None;
130}
131
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000132static object *
133sys_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 Rossum14b4adb1992-09-03 20:25:30 +0000143#ifdef USE_MALLOPT
144/* Link with -lmalloc (or -lmpc) on an SGI */
145#include <malloc.h>
146
147static object *
148sys_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 Mullender6ec3c651995-08-29 09:18:14 +0000161static object *
162sys_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
173static PyObject *
174sys_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 Rossum7f3f2c11996-05-23 22:45:41 +0000185#ifdef Py_TRACE_REFS
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000186extern PyObject *getobjects Py_PROTO((PyObject *, PyObject *));
187#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000188static struct methodlist sys_methods[] = {
Guido van Rossum4f508cc1995-02-21 21:03:24 +0000189 {"exit", sys_exit, 0},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000190 {"getrefcount", sys_getrefcount, 0},
191#ifdef COUNT_ALLOCS
192 {"getcounts", sys_getcounts, 0},
193#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000194#ifdef Py_TRACE_REFS
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000195 {"getobjects", getobjects, 1},
196#endif
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000197#ifdef USE_MALLOPT
Guido van Rossum4f508cc1995-02-21 21:03:24 +0000198 {"mdebug", sys_mdebug, 0},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000199#endif
Guido van Rossum4f508cc1995-02-21 21:03:24 +0000200 {"setprofile", sys_setprofile, 0},
201 {"settrace", sys_settrace, 0},
Guido van Rossumb0352fa1996-01-12 01:15:01 +0000202 {"setcheckinterval", sys_setcheckinterval, 1},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000203 {NULL, NULL} /* sentinel */
204};
205
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206static object *sysin, *sysout, *syserr;
207
Guido van Rossum34679b71993-01-26 13:33:44 +0000208static object *
209list_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 Rossum1d5735e1994-08-30 08:27:36 +0000222 if (sortlist(list) != 0) {
223 DECREF(list);
224 list = NULL;
225 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000226 return list;
227}
228
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000230initsys()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000232 extern long getmaxint PROTO((void));
233 extern char *getversion PROTO((void));
234 extern char *getcopyright PROTO((void));
Guido van Rossumaaf18391995-07-07 22:45:02 +0000235 extern char *getplatform PROTO((void));
Guido van Rossum3caa6e31991-06-04 19:40:59 +0000236 extern int fclose PROTO((FILE *));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000237 object *m = initmodule("sys", sys_methods);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000238 object *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000239 sysdict = getmoduledict(m);
240 INCREF(sysdict);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241 /* NB keep an extra ref to the std files to avoid closing them
242 when the user deletes them */
Guido van Rossum3caa6e31991-06-04 19:40:59 +0000243 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose);
244 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose);
245 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246 if (err_occurred())
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000247 fatal("can't initialize sys.std{in,out,err}");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248 dictinsert(sysdict, "stdin", sysin);
249 dictinsert(sysdict, "stdout", sysout);
250 dictinsert(sysdict, "stderr", syserr);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000251 dictinsert(sysdict, "version", v = newstringobject(getversion()));
252 XDECREF(v);
253 dictinsert(sysdict, "copyright", v = newstringobject(getcopyright()));
254 XDECREF(v);
Guido van Rossumaaf18391995-07-07 22:45:02 +0000255 dictinsert(sysdict, "platform", v = newstringobject(getplatform()));
256 XDECREF(v);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000257 dictinsert(sysdict, "maxint", v = newintobject(getmaxint()));
258 XDECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000259 dictinsert(sysdict, "modules", get_modules());
Guido van Rossum34679b71993-01-26 13:33:44 +0000260 dictinsert(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000261 v = list_builtin_module_names());
262 XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000263#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 Rossum85a5fbb1990-10-14 12:07:46 +0000269 if (err_occurred())
270 fatal("can't insert sys.* objects in sys dict");
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000271}
272
Guido van Rossum3f5da241990-12-20 15:06:42 +0000273static object *
274makepathobject(path, delim)
275 char *path;
276 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000277{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000278 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 Rossum5b3138b1990-11-18 17:41:40 +0000287 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000288 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 Rossum85a5fbb1990-10-14 12:07:46 +0000306}
307
308void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000309setpythonpath(path)
310 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000312 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
320static object *
321makeargvobject(argc, argv)
322 int argc;
323 char **argv;
324{
325 object *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000326 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 Rossum3f5da241990-12-20 15:06:42 +0000332 av = newlistobject(argc);
333 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000334 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335 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 Rossum5b3138b1990-11-18 17:41:40 +0000341 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000342 setlistitem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000343 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000344 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000345 return av;
346}
347
348void
349setpythonargv(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 Rossum85a5fbb1990-10-14 12:07:46 +0000359}