1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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.
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.
23 ******************************************************************/
28 Various bits of information used by the interpreter are collected in
31 - exit(sts): raise SystemExit
33 - stdin, stdout, stderr: standard file objects
34 - modules: the table of modules (dictionary)
35 - path: module search path (list of strings)
36 - argv: script arguments (list of strings)
37 - ps1, ps2: optional primary and secondary prompts (strings)
40 #include "allobjects.h"
42 #include "sysmodule.h"
44 #include "modsupport.h"
47 object
*sys_trace
, *sys_profile
;
48 int sys_checkinterval
;
50 static object
*sysdict
;
56 return dictlookup(sysdict
, name
);
65 object
*v
= sysget(name
);
66 if (v
!= NULL
&& is_fileobject(v
))
79 if (dictlookup(sysdict
, name
) == NULL
)
82 return dictremove(sysdict
, name
);
85 return dictinsert(sysdict
, name
, v
);
93 /* Raise SystemExit so callers may catch it or clean up. */
94 err_setval(SystemExit
, args
);
99 sys_settrace(self
, args
)
114 sys_setprofile(self
, args
)
122 XDECREF(sys_profile
);
129 sys_setcheckinterval(self
, args
)
133 if (!newgetargs(args
, "i", &sys_checkinterval
))
140 /* Link with -lmalloc (or -lmpc) on an SGI */
144 sys_mdebug(self
, args
)
149 if (!getargs(args
, "i", &flag
))
151 mallopt(M_DEBUG
, flag
);
155 #endif /* USE_MALLOPT */
157 static struct methodlist sys_methods
[] = {
158 {"exit", sys_exit
, 0},
160 {"mdebug", sys_mdebug
, 0},
162 {"setprofile", sys_setprofile
, 0},
163 {"settrace", sys_settrace
, 0},
164 {"setcheckinterval", sys_setcheckinterval
, 0},
165 {NULL
, NULL
} /* sentinel */
168 static object
*sysin
, *sysout
, *syserr
;
171 list_builtin_module_names()
173 object
*list
= newlistobject(0);
177 for (i
= 0; inittab
[i
].name
!= NULL
; i
++) {
178 object
*name
= newstringobject(inittab
[i
].name
);
181 addlistitem(list
, name
);
184 if (sortlist(list
) != 0) {
194 extern long getmaxint
PROTO((void));
195 extern char *getversion
PROTO((void));
196 extern char *getcopyright
PROTO((void));
197 extern int fclose
PROTO((FILE *));
198 object
*m
= initmodule("sys", sys_methods
);
200 sysdict
= getmoduledict(m
);
202 /* NB keep an extra ref to the std files to avoid closing them
203 when the user deletes them */
204 sysin
= newopenfileobject(stdin
, "<stdin>", "r", fclose
);
205 sysout
= newopenfileobject(stdout
, "<stdout>", "w", fclose
);
206 syserr
= newopenfileobject(stderr
, "<stderr>", "w", fclose
);
208 fatal("can't initialize sys.std{in,out,err}");
209 dictinsert(sysdict
, "stdin", sysin
);
210 dictinsert(sysdict
, "stdout", sysout
);
211 dictinsert(sysdict
, "stderr", syserr
);
212 dictinsert(sysdict
, "version", v
= newstringobject(getversion()));
214 dictinsert(sysdict
, "copyright", v
= newstringobject(getcopyright()));
216 dictinsert(sysdict
, "maxint", v
= newintobject(getmaxint()));
218 dictinsert(sysdict
, "modules", get_modules());
219 dictinsert(sysdict
, "builtin_module_names",
220 v
= list_builtin_module_names());
223 fatal("can't insert sys.* objects in sys dict");
227 makepathobject(path
, delim
)
237 while ((p
= strchr(p
, delim
)) != NULL
) {
241 v
= newlistobject(n
);
245 p
= strchr(path
, delim
);
247 p
= strchr(path
, '\0'); /* End of string */
248 w
= newsizedstringobject(path
, (int) (p
- path
));
253 setlistitem(v
, i
, w
);
266 if ((v
= makepathobject(path
, DELIM
)) == NULL
)
267 fatal("can't create sys.path");
268 if (sysset("path", v
) != 0)
269 fatal("can't assign sys.path");
274 makeargvobject(argc
, argv
)
279 if (argc
<= 0 || argv
== NULL
) {
280 /* Ensure at least one (empty) argument is seen */
281 static char *empty_argv
[1] = {""};
285 av
= newlistobject(argc
);
288 for (i
= 0; i
< argc
; i
++) {
289 object
*v
= newstringobject(argv
[i
]);
295 setlistitem(av
, i
, v
);
302 setpythonargv(argc
, argv
)
306 object
*av
= makeargvobject(argc
, argv
);
308 fatal("no mem for sys.argv");
309 if (sysset("argv", av
) != 0)
310 fatal("can't assign sys.argv");