blob: 6c834d0808c2a44a404ab5e094fb177774721c1f [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* System module */
33
34/*
35Various bits of information used by the interpreter are collected in
36module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +000037Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +000038- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039Data members:
40- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000042- path: module search path (list of strings)
43- argv: script arguments (list of strings)
44- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045*/
46
Guido van Rossum65bf9f21997-04-29 18:33:38 +000047#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048
Guido van Rossume2437a11992-03-23 18:20:18 +000049#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000050
Guido van Rossum1254d791997-05-20 15:57:25 +000051#ifdef HAVE_UNISTD_H
Guido van Rossumc474dea1997-04-25 15:38:31 +000052#include <unistd.h>
53#endif
54
Guido van Rossum9b38a141996-09-11 23:12:24 +000055#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000056extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000057/* A string loaded from the DLL at startup: */
58extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000059#endif
60
Guido van Rossum65bf9f21997-04-29 18:33:38 +000061PyObject *
62PySys_GetObject(name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063 char *name;
64{
Guido van Rossum25ce5661997-08-02 03:10:38 +000065 PyThreadState *tstate = PyThreadState_Get();
66 PyObject *sd = tstate->interp->sysdict;
67 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
70FILE *
Guido van Rossum65bf9f21997-04-29 18:33:38 +000071PySys_GetFile(name, def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 char *name;
73 FILE *def;
74{
75 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000076 PyObject *v = PySys_GetObject(name);
77 if (v != NULL && PyFile_Check(v))
78 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 if (fp == NULL)
80 fp = def;
81 return fp;
82}
83
84int
Guido van Rossum65bf9f21997-04-29 18:33:38 +000085PySys_SetObject(name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 char *name;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000087 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088{
Guido van Rossum25ce5661997-08-02 03:10:38 +000089 PyThreadState *tstate = PyThreadState_Get();
90 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000091 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000092 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000093 return 0;
94 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000095 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000096 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000098 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099}
100
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000101static PyObject *
Guido van Rossuma027efa1997-05-05 20:56:21 +0000102sys_exc_info(self, args)
103 PyObject *self;
104 PyObject *args;
105{
106 PyThreadState *tstate;
107 if (!PyArg_Parse(args, ""))
108 return NULL;
109 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000110 return Py_BuildValue(
111 "(OOO)",
112 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
113 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
114 tstate->exc_traceback != NULL ?
115 tstate->exc_traceback : Py_None);
116}
117
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000118static char exc_info_doc[] =
119"exc_info() -> (type, value, traceback)\n\
120\n\
121Return information about the exception that is currently being handled.\n\
122This should be called from inside an except clause only.";
123
Guido van Rossuma027efa1997-05-05 20:56:21 +0000124static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125sys_exit(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000126 PyObject *self;
127 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000129 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000130 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000131 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000134static char exit_doc[] =
135"exit([status])\n\
136\n\
137Exit the interpreter by raising SystemExit(status).\n\
138If the status is omitted or None, it defaults to zero (i.e., success).\n\
139If the status numeric, it will be used as the system exit status.\n\
140If it is another kind of object, it will be printed and the system\n\
141exit status will be one (i.e., failure).";
142
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000143static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000144sys_settrace(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000145 PyObject *self;
146 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000147{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000148 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000149 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000150 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000151 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000152 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000153 Py_XDECREF(tstate->sys_tracefunc);
154 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000155 Py_INCREF(Py_None);
156 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000157}
158
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000159static char settrace_doc[] =
160"settrace(function)\n\
161\n\
162Set the global debug tracing function. It will be called on each\n\
163function call. See the debugger chapter in the library manual.";
164
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000165static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000166sys_setprofile(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000167 PyObject *self;
168 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000169{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000171 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000172 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000173 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000174 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000175 Py_XDECREF(tstate->sys_profilefunc);
176 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000177 Py_INCREF(Py_None);
178 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000179}
180
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000181static char setprofile_doc[] =
182"setprofile(function)\n\
183\n\
184Set the profiling function. It will be called on each function call\n\
185and return. See the profiler chapter in the library manual.";
186
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000187static PyObject *
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000188sys_setcheckinterval(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000189 PyObject *self;
190 PyObject *args;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000191{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000192 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193 if (!PyArg_ParseTuple(args, "i", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000194 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000195 Py_INCREF(Py_None);
196 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000197}
198
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000199static char setcheckinterval_doc[] =
200"setcheckinterval(n)\n\
201\n\
202Tell the Python interpreter to check for asynchronous events every\n\
203n instructions. This also affects how often thread switches occur.";
204
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000205#ifdef USE_MALLOPT
206/* Link with -lmalloc (or -lmpc) on an SGI */
207#include <malloc.h>
208
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000209static PyObject *
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000210sys_mdebug(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000211 PyObject *self;
212 PyObject *args;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000213{
214 int flag;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000215 if (!PyArg_Parse(args, "i", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000216 return NULL;
217 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000218 Py_INCREF(Py_None);
219 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000220}
221#endif /* USE_MALLOPT */
222
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000223static PyObject *
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000224sys_getrefcount(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000225 PyObject *self;
226 PyObject *args;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000227{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000228 PyObject *arg;
229 if (!PyArg_Parse(args, "O", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000230 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000231 return PyInt_FromLong((long) arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000232}
233
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000234static char getrefcount_doc[] =
235"getrefcount(object) -> integer\n\
236\n\
237Return the current reference count for the object. This includes the\n\
238temporary reference in the argument list, so it is at least 2.";
239
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000240#ifdef COUNT_ALLOCS
241static PyObject *
242sys_getcounts(self, args)
243 PyObject *self, *args;
244{
245 extern PyObject *get_counts Py_PROTO((void));
246
247 if (!PyArg_Parse(args, ""))
248 return NULL;
249 return get_counts();
250}
251#endif
252
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000253#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000254/* Defined in objects.c because it uses static globals if that file */
255extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000256#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000257
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000258#ifdef DYNAMIC_EXECUTION_PROFILE
259/* Defined in ceval.c because it uses static globals if that file */
260extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *, PyObject *));
261#endif
262
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000263static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000264 /* Might as well keep this in alphabetic order */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000265 {"exc_info", sys_exc_info, 0, exc_info_doc},
266 {"exit", sys_exit, 0, exit_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000267#ifdef COUNT_ALLOCS
268 {"getcounts", sys_getcounts, 0},
269#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000270#ifdef DYNAMIC_EXECUTION_PROFILE
271 {"getdxp", _Py_GetDXProfile, 1},
272#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000273#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000274 {"getobjects", _Py_GetObjects, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000275#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000276 {"getrefcount", sys_getrefcount, 0, getrefcount_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000277#ifdef USE_MALLOPT
Guido van Rossum4f508cc1995-02-21 21:03:24 +0000278 {"mdebug", sys_mdebug, 0},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000279#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000280 {"setcheckinterval", sys_setcheckinterval, 1, setcheckinterval_doc},
281 {"setprofile", sys_setprofile, 0, setprofile_doc},
282 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000283 {NULL, NULL} /* sentinel */
284};
285
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000286static PyObject *
Guido van Rossum34679b71993-01-26 13:33:44 +0000287list_builtin_module_names()
288{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000289 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000290 int i;
291 if (list == NULL)
292 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000293 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000294 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000295 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000296 if (name == NULL)
297 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000298 PyList_Append(list, name);
299 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000300 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000301 if (PyList_Sort(list) != 0) {
302 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000303 list = NULL;
304 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000305 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000306 PyObject *v = PyList_AsTuple(list);
307 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000308 list = v;
309 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000310 return list;
311}
312
Guido van Rossum40552d01998-08-06 03:34:39 +0000313/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
314 Two literals concatenated works just fine. If you have a K&R compiler
315 or other abomination that however *does* understand longer strings,
316 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000317static char sys_doc[] =
318"This module provides access to some objects used or maintained by the\n\
319interpreter and to functions that interact strongly with the interpreter.\n\
320\n\
321Dynamic objects:\n\
322\n\
323argv -- command line arguments; argv[0] is the script pathname if known\n\
324path -- module search path; path[0] is the script directory, else ''\n\
325modules -- dictionary of loaded modules\n\
326exitfunc -- you may set this to a function to be called when Python exits\n\
327\n\
328stdin -- standard input file object; used by raw_input() and input()\n\
329stdout -- standard output file object; used by the print statement\n\
330stderr -- standard error object; used for error messages\n\
331 By assigning another file object (or an object that behaves like a file)\n\
332 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
333\n\
334last_type -- type of last uncaught exception\n\
335last_value -- value of last uncaught exception\n\
336last_traceback -- traceback of last uncaught exception\n\
337 These three are only available in an interactive session after a\n\
338 traceback has been printed.\n\
339\n\
340exc_type -- type of exception currently being handled\n\
341exc_value -- value of exception currently being handled\n\
342exc_traceback -- traceback of exception currently being handled\n\
343 The function exc_info() should be used instead of these three,\n\
344 because it is thread-safe.\n\
Guido van Rossum40552d01998-08-06 03:34:39 +0000345" /* !!! */ "\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000346Static objects:\n\
347\n\
348maxint -- the largest supported integer (the smallest is -maxint-1)\n\
349builtin_module_names -- tuple of module names built into this intepreter\n\
350version -- the version of this interpreter\n\
351copyright -- copyright notice pertaining to this interpreter\n\
352platform -- platform identifier\n\
353executable -- pathname of this Python interpreter\n\
354prefix -- prefix used to find the Python library\n\
355exec_prefix -- prefix used to find the machine-specific Python library\n\
356dllhandle -- [Windows only] integer handle of the Python DLL\n\
357winver -- [Windows only] version number of the Python DLL\n\
358__stdin__ -- the original stdin; don't use!\n\
359__stdout__ -- the original stdout; don't use!\n\
360__stderr__ -- the original stderr; don't use!\n\
361\n\
362Functions:\n\
363\n\
364exc_info() -- return thread-safe information about the current exception\n\
365exit() -- exit the interpreter by raising SystemExit\n\
366getrefcount() -- return the reference count for an object (plus one :-)\n\
367setcheckinterval() -- control how often the interpreter checks for events\n\
368setprofile() -- set the global profiling function\n\
369settrace() -- set the global debug tracing function\n\
370";
371
Guido van Rossum25ce5661997-08-02 03:10:38 +0000372PyObject *
373_PySys_Init()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000375 extern int fclose Py_PROTO((FILE *));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000376 PyObject *m, *v, *sysdict;
377 PyObject *sysin, *sysout, *syserr;
378
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000379 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000380 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000381
382 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
383 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
384 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000385 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000386 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000387 PyDict_SetItemString(sysdict, "stdin", sysin);
388 PyDict_SetItemString(sysdict, "stdout", sysout);
389 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000390 /* Make backup copies for cleanup */
391 PyDict_SetItemString(sysdict, "__stdin__", sysin);
392 PyDict_SetItemString(sysdict, "__stdout__", sysout);
393 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000394 Py_XDECREF(sysin);
395 Py_XDECREF(sysout);
396 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000397 PyDict_SetItemString(sysdict, "version",
398 v = PyString_FromString(Py_GetVersion()));
399 Py_XDECREF(v);
400 PyDict_SetItemString(sysdict, "copyright",
401 v = PyString_FromString(Py_GetCopyright()));
402 Py_XDECREF(v);
403 PyDict_SetItemString(sysdict, "platform",
404 v = PyString_FromString(Py_GetPlatform()));
405 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000406 PyDict_SetItemString(sysdict, "executable",
407 v = PyString_FromString(Py_GetProgramFullPath()));
408 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000409 PyDict_SetItemString(sysdict, "prefix",
410 v = PyString_FromString(Py_GetPrefix()));
411 Py_XDECREF(v);
412 PyDict_SetItemString(sysdict, "exec_prefix",
413 v = PyString_FromString(Py_GetExecPrefix()));
414 Py_XDECREF(v);
415 PyDict_SetItemString(sysdict, "maxint",
416 v = PyInt_FromLong(PyInt_GetMax()));
417 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000418 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000419 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000420 Py_XDECREF(v);
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000421#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000422 PyDict_SetItemString(sysdict, "dllhandle",
423 v = PyInt_FromLong((int)PyWin_DLLhModule));
424 Py_XDECREF(v);
425 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000426 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000427 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000428#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000429 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000430 return NULL;
431 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000432}
433
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000434static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000435makepathobject(path, delim)
436 char *path;
437 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000438{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000439 int i, n;
440 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000441 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000442
443 n = 1;
444 p = path;
445 while ((p = strchr(p, delim)) != NULL) {
446 n++;
447 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000448 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000449 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000450 if (v == NULL)
451 return NULL;
452 for (i = 0; ; i++) {
453 p = strchr(path, delim);
454 if (p == NULL)
455 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000456 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000457 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000458 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000459 return NULL;
460 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000461 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000462 if (*p == '\0')
463 break;
464 path = p+1;
465 }
466 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467}
468
469void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000470PySys_SetPath(path)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000471 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000473 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000474 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000475 Py_FatalError("can't create sys.path");
476 if (PySys_SetObject("path", v) != 0)
477 Py_FatalError("can't assign sys.path");
478 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000479}
480
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000481static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000482makeargvobject(argc, argv)
483 int argc;
484 char **argv;
485{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000486 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000487 if (argc <= 0 || argv == NULL) {
488 /* Ensure at least one (empty) argument is seen */
489 static char *empty_argv[1] = {""};
490 argv = empty_argv;
491 argc = 1;
492 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000493 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000494 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000495 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000496 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000497 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000498 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000499 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000500 av = NULL;
501 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000502 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000503 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000504 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000505 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000506 return av;
507}
508
509void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000510PySys_SetArgv(argc, argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000511 int argc;
512 char **argv;
513{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000514 PyObject *av = makeargvobject(argc, argv);
515 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000517 Py_FatalError("no mem for sys.argv");
518 if (PySys_SetObject("argv", av) != 0)
519 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000520 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000521 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000522 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000523 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000524 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000525#ifdef HAVE_READLINK
526 char link[MAXPATHLEN+1];
527 char argv0copy[2*MAXPATHLEN+1];
528 int nr = 0;
529 if (argc > 0 && argv0 != NULL)
530 nr = readlink(argv0, link, MAXPATHLEN);
531 if (nr > 0) {
532 /* It's a symlink */
533 link[nr] = '\0';
534 if (link[0] == SEP)
535 argv0 = link; /* Link to absolute path */
536 else if (strchr(link, SEP) == NULL)
537 ; /* Link without path */
538 else {
539 /* Must join(dirname(argv0), link) */
540 char *q = strrchr(argv0, SEP);
541 if (q == NULL)
542 argv0 = link; /* argv0 without path */
543 else {
544 /* Must make a copy */
545 strcpy(argv0copy, argv0);
546 q = strrchr(argv0copy, SEP);
547 strcpy(q+1, link);
548 argv0 = argv0copy;
549 }
550 }
551 }
552#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000553#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000554 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000555 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000556 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000557 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000558 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000559 if (q != NULL)
560 p = q;
561 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000562 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000563 if (n > 1 && p[-1] != ':')
564 n--; /* Drop trailing separator */
565 }
566 }
567#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000568 if (argc > 0 && argv0 != NULL)
569 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000570 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000571 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000572#if SEP == '/' /* Special case for Unix filename syntax */
573 if (n > 1)
574 n--; /* Drop trailing separator */
575#endif /* Unix */
576 }
577#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000578 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000579 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000580 Py_FatalError("no mem for sys.path insertion");
581 if (PyList_Insert(path, 0, a) < 0)
582 Py_FatalError("sys.path.insert(0) failed");
583 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000584 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000585 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586}
Guido van Rossuma890e681998-05-12 14:59:24 +0000587
588
589/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
590 Adapted from code submitted by Just van Rossum.
591
592 PySys_WriteStdout(format, ...)
593 PySys_WriteStderr(format, ...)
594
595 The first function writes to sys.stdout; the second to sys.stderr. When
596 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000597 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000598
599 Both take a printf-style format string as their first argument followed
600 by a variable length argument list determined by the format string.
601
602 *** WARNING ***
603
604 The format should limit the total size of the formatted output string to
605 1000 bytes. In particular, this means that no unrestricted "%s" formats
606 should occur; these should be limited using "%.<N>s where <N> is a
607 decimal number calculated so that <N> plus the maximum size of other
608 formatted text does not exceed 1000 bytes. Also watch out for "%f",
609 which can print hundreds of digits for very large numbers.
610
611 */
612
613static void
614mywrite(name, fp, format, va)
615 char *name;
616 FILE *fp;
617 const char *format;
618 va_list va;
619{
620 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000621 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000622
Guido van Rossum8442af31998-10-12 18:22:10 +0000623 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000624 file = PySys_GetObject(name);
625 if (file == NULL || PyFile_AsFile(file) == fp)
626 vfprintf(fp, format, va);
627 else {
628 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000629 if (vsprintf(buffer, format, va) >= sizeof(buffer))
630 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000631 if (PyFile_WriteString(buffer, file) != 0) {
632 PyErr_Clear();
633 fputs(buffer, fp);
634 }
635 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000636 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000637}
638
639void
640#ifdef HAVE_STDARG_PROTOTYPES
641PySys_WriteStdout(const char *format, ...)
642#else
643PySys_WriteStdout(va_alist)
644 va_dcl
645#endif
646{
647 va_list va;
648
649#ifdef HAVE_STDARG_PROTOTYPES
650 va_start(va, format);
651#else
652 char *format;
653 va_start(va);
654 format = va_arg(va, char *);
655#endif
656 mywrite("stdout", stdout, format, va);
657 va_end(va);
658}
659
660void
661#ifdef HAVE_STDARG_PROTOTYPES
662PySys_WriteStderr(const char *format, ...)
663#else
664PySys_WriteStderr(va_alist)
665 va_dcl
666#endif
667{
668 va_list va;
669
670#ifdef HAVE_STDARG_PROTOTYPES
671 va_start(va, format);
672#else
673 char *format;
674 va_start(va);
675 format = va_arg(va, char *);
676#endif
677 mywrite("stderr", stderr, format, va);
678 va_end(va);
679}