blob: 34f7c96daff779440d569a31a7eb3c1fcefff9fc [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;
Guido van Rossumbe203361999-10-05 22:17:41 +000067 if (sd == NULL)
68 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000069 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
72FILE *
Guido van Rossum65bf9f21997-04-29 18:33:38 +000073PySys_GetFile(name, def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 char *name;
75 FILE *def;
76{
77 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000078 PyObject *v = PySys_GetObject(name);
79 if (v != NULL && PyFile_Check(v))
80 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081 if (fp == NULL)
82 fp = def;
83 return fp;
84}
85
86int
Guido van Rossum65bf9f21997-04-29 18:33:38 +000087PySys_SetObject(name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 char *name;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000089 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossum25ce5661997-08-02 03:10:38 +000091 PyThreadState *tstate = PyThreadState_Get();
92 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000093 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000094 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000095 return 0;
96 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000097 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000098 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 else
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101}
102
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000103static PyObject *
Guido van Rossuma027efa1997-05-05 20:56:21 +0000104sys_exc_info(self, args)
105 PyObject *self;
106 PyObject *args;
107{
108 PyThreadState *tstate;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000109 if (!PyArg_ParseTuple(args, ":exc_info"))
Guido van Rossuma027efa1997-05-05 20:56:21 +0000110 return NULL;
111 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000112 return Py_BuildValue(
113 "(OOO)",
114 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
115 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
116 tstate->exc_traceback != NULL ?
117 tstate->exc_traceback : Py_None);
118}
119
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000120static char exc_info_doc[] =
121"exc_info() -> (type, value, traceback)\n\
122\n\
123Return information about the exception that is currently being handled.\n\
124This should be called from inside an except clause only.";
125
Guido van Rossuma027efa1997-05-05 20:56:21 +0000126static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127sys_exit(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000128 PyObject *self;
129 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000131 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000132 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000133 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000136static char exit_doc[] =
137"exit([status])\n\
138\n\
139Exit the interpreter by raising SystemExit(status).\n\
140If the status is omitted or None, it defaults to zero (i.e., success).\n\
141If the status numeric, it will be used as the system exit status.\n\
142If it is another kind of object, it will be printed and the system\n\
143exit status will be one (i.e., failure).";
144
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000145static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000146sys_settrace(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000147 PyObject *self;
148 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000149{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000150 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000151 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000152 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000153 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000154 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000155 Py_XDECREF(tstate->sys_tracefunc);
156 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000157 Py_INCREF(Py_None);
158 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000159}
160
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000161static char settrace_doc[] =
162"settrace(function)\n\
163\n\
164Set the global debug tracing function. It will be called on each\n\
165function call. See the debugger chapter in the library manual.";
166
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000167static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000168sys_setprofile(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000169 PyObject *self;
170 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000171{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000172 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000173 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000174 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000175 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000176 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177 Py_XDECREF(tstate->sys_profilefunc);
178 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000179 Py_INCREF(Py_None);
180 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000181}
182
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000183static char setprofile_doc[] =
184"setprofile(function)\n\
185\n\
186Set the profiling function. It will be called on each function call\n\
187and return. See the profiler chapter in the library manual.";
188
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000189static PyObject *
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000190sys_setcheckinterval(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000191 PyObject *self;
192 PyObject *args;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000193{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000195 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000196 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000197 Py_INCREF(Py_None);
198 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000199}
200
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000201static char setcheckinterval_doc[] =
202"setcheckinterval(n)\n\
203\n\
204Tell the Python interpreter to check for asynchronous events every\n\
205n instructions. This also affects how often thread switches occur.";
206
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000207#ifdef USE_MALLOPT
208/* Link with -lmalloc (or -lmpc) on an SGI */
209#include <malloc.h>
210
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000211static PyObject *
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000212sys_mdebug(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000213 PyObject *self;
214 PyObject *args;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000215{
216 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000217 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000218 return NULL;
219 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000220 Py_INCREF(Py_None);
221 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000222}
223#endif /* USE_MALLOPT */
224
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000225static PyObject *
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000226sys_getrefcount(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000227 PyObject *self;
228 PyObject *args;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000229{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000230 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000231 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000232 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000233 return PyInt_FromLong((long) arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000234}
235
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000236static char getrefcount_doc[] =
237"getrefcount(object) -> integer\n\
238\n\
239Return the current reference count for the object. This includes the\n\
240temporary reference in the argument list, so it is at least 2.";
241
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000242#ifdef COUNT_ALLOCS
243static PyObject *
244sys_getcounts(self, args)
245 PyObject *self, *args;
246{
247 extern PyObject *get_counts Py_PROTO((void));
248
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000249 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000250 return NULL;
251 return get_counts();
252}
253#endif
254
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000255#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000256/* Defined in objects.c because it uses static globals if that file */
257extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000258#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000259
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000260#ifdef DYNAMIC_EXECUTION_PROFILE
261/* Defined in ceval.c because it uses static globals if that file */
262extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *, PyObject *));
263#endif
264
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000265static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000266 /* Might as well keep this in alphabetic order */
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000267 {"exc_info", sys_exc_info, 1, exc_info_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000268 {"exit", sys_exit, 0, exit_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000269#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000270 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000271#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000272#ifdef DYNAMIC_EXECUTION_PROFILE
273 {"getdxp", _Py_GetDXProfile, 1},
274#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000275#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000276 {"getobjects", _Py_GetObjects, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000277#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000278 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000279#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000280 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000281#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000282 {"setcheckinterval", sys_setcheckinterval, 1, setcheckinterval_doc},
283 {"setprofile", sys_setprofile, 0, setprofile_doc},
284 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000285 {NULL, NULL} /* sentinel */
286};
287
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000288static PyObject *
Guido van Rossum34679b71993-01-26 13:33:44 +0000289list_builtin_module_names()
290{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000291 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000292 int i;
293 if (list == NULL)
294 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000295 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000296 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000297 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000298 if (name == NULL)
299 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000300 PyList_Append(list, name);
301 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000302 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000303 if (PyList_Sort(list) != 0) {
304 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000305 list = NULL;
306 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000307 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000308 PyObject *v = PyList_AsTuple(list);
309 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000310 list = v;
311 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000312 return list;
313}
314
Guido van Rossum40552d01998-08-06 03:34:39 +0000315/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
316 Two literals concatenated works just fine. If you have a K&R compiler
317 or other abomination that however *does* understand longer strings,
318 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000319static char sys_doc[] =
320"This module provides access to some objects used or maintained by the\n\
321interpreter and to functions that interact strongly with the interpreter.\n\
322\n\
323Dynamic objects:\n\
324\n\
325argv -- command line arguments; argv[0] is the script pathname if known\n\
326path -- module search path; path[0] is the script directory, else ''\n\
327modules -- dictionary of loaded modules\n\
328exitfunc -- you may set this to a function to be called when Python exits\n\
329\n\
330stdin -- standard input file object; used by raw_input() and input()\n\
331stdout -- standard output file object; used by the print statement\n\
332stderr -- standard error object; used for error messages\n\
333 By assigning another file object (or an object that behaves like a file)\n\
334 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
335\n\
336last_type -- type of last uncaught exception\n\
337last_value -- value of last uncaught exception\n\
338last_traceback -- traceback of last uncaught exception\n\
339 These three are only available in an interactive session after a\n\
340 traceback has been printed.\n\
341\n\
342exc_type -- type of exception currently being handled\n\
343exc_value -- value of exception currently being handled\n\
344exc_traceback -- traceback of exception currently being handled\n\
345 The function exc_info() should be used instead of these three,\n\
346 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000347"
348#ifndef MS_WIN16
349/* Concatenating string here */
350"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000351Static objects:\n\
352\n\
353maxint -- the largest supported integer (the smallest is -maxint-1)\n\
354builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000355version -- the version of this interpreter as a string\n\
356version_info -- version information as a tuple\n\
357hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000358copyright -- copyright notice pertaining to this interpreter\n\
359platform -- platform identifier\n\
360executable -- pathname of this Python interpreter\n\
361prefix -- prefix used to find the Python library\n\
362exec_prefix -- prefix used to find the machine-specific Python library\n\
363dllhandle -- [Windows only] integer handle of the Python DLL\n\
364winver -- [Windows only] version number of the Python DLL\n\
365__stdin__ -- the original stdin; don't use!\n\
366__stdout__ -- the original stdout; don't use!\n\
367__stderr__ -- the original stderr; don't use!\n\
368\n\
369Functions:\n\
370\n\
371exc_info() -- return thread-safe information about the current exception\n\
372exit() -- exit the interpreter by raising SystemExit\n\
373getrefcount() -- return the reference count for an object (plus one :-)\n\
374setcheckinterval() -- control how often the interpreter checks for events\n\
375setprofile() -- set the global profiling function\n\
376settrace() -- set the global debug tracing function\n\
377";
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000378#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000379
Guido van Rossum25ce5661997-08-02 03:10:38 +0000380PyObject *
381_PySys_Init()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000383 extern int fclose Py_PROTO((FILE *));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000384 PyObject *m, *v, *sysdict;
385 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000386 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000387
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000388 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000389 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000390
391 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
392 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
393 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000394 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000395 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000396 PyDict_SetItemString(sysdict, "stdin", sysin);
397 PyDict_SetItemString(sysdict, "stdout", sysout);
398 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000399 /* Make backup copies for cleanup */
400 PyDict_SetItemString(sysdict, "__stdin__", sysin);
401 PyDict_SetItemString(sysdict, "__stdout__", sysout);
402 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000403 Py_XDECREF(sysin);
404 Py_XDECREF(sysout);
405 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000406 PyDict_SetItemString(sysdict, "version",
407 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000408 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000409 PyDict_SetItemString(sysdict, "hexversion",
410 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000411 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000412 /*
413 * These release level checks are mutually exclusive and cover
414 * the field, so don't get too fancy with the pre-processor!
415 */
416#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000417 s = "alpha";
Fred Drake93a20bf2000-04-13 17:44:51 +0000418#endif
419#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000420 s = "beta";
Fred Drake93a20bf2000-04-13 17:44:51 +0000421#endif
422#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000423 s = "candidate";
Fred Drake93a20bf2000-04-13 17:44:51 +0000424#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000425#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000426 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000427#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000428 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000429 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000430 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000431 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000432 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000433 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000434 PyDict_SetItemString(sysdict, "copyright",
435 v = PyString_FromString(Py_GetCopyright()));
436 Py_XDECREF(v);
437 PyDict_SetItemString(sysdict, "platform",
438 v = PyString_FromString(Py_GetPlatform()));
439 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000440 PyDict_SetItemString(sysdict, "executable",
441 v = PyString_FromString(Py_GetProgramFullPath()));
442 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000443 PyDict_SetItemString(sysdict, "prefix",
444 v = PyString_FromString(Py_GetPrefix()));
445 Py_XDECREF(v);
446 PyDict_SetItemString(sysdict, "exec_prefix",
447 v = PyString_FromString(Py_GetExecPrefix()));
448 Py_XDECREF(v);
449 PyDict_SetItemString(sysdict, "maxint",
450 v = PyInt_FromLong(PyInt_GetMax()));
451 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000452 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000453 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000454 Py_XDECREF(v);
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000455#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000456 PyDict_SetItemString(sysdict, "dllhandle",
457 v = PyInt_FromLong((int)PyWin_DLLhModule));
458 Py_XDECREF(v);
459 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000460 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000461 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000462#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000463 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000464 return NULL;
465 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000466}
467
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000468static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000469makepathobject(path, delim)
470 char *path;
471 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000472{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000473 int i, n;
474 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000475 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000476
477 n = 1;
478 p = path;
479 while ((p = strchr(p, delim)) != NULL) {
480 n++;
481 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000482 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000483 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000484 if (v == NULL)
485 return NULL;
486 for (i = 0; ; i++) {
487 p = strchr(path, delim);
488 if (p == NULL)
489 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000490 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000491 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000492 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000493 return NULL;
494 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000495 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000496 if (*p == '\0')
497 break;
498 path = p+1;
499 }
500 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501}
502
503void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000504PySys_SetPath(path)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000505 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000507 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000508 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000509 Py_FatalError("can't create sys.path");
510 if (PySys_SetObject("path", v) != 0)
511 Py_FatalError("can't assign sys.path");
512 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000513}
514
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000515static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516makeargvobject(argc, argv)
517 int argc;
518 char **argv;
519{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000520 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000521 if (argc <= 0 || argv == NULL) {
522 /* Ensure at least one (empty) argument is seen */
523 static char *empty_argv[1] = {""};
524 argv = empty_argv;
525 argc = 1;
526 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000527 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000528 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000529 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000530 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000531 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000532 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000533 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000534 av = NULL;
535 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000536 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000537 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000538 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000539 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000540 return av;
541}
542
543void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000544PySys_SetArgv(argc, argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000545 int argc;
546 char **argv;
547{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000548 PyObject *av = makeargvobject(argc, argv);
549 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000550 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000551 Py_FatalError("no mem for sys.argv");
552 if (PySys_SetObject("argv", av) != 0)
553 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000554 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000555 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000556 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000557 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000558 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000559#ifdef HAVE_READLINK
560 char link[MAXPATHLEN+1];
561 char argv0copy[2*MAXPATHLEN+1];
562 int nr = 0;
563 if (argc > 0 && argv0 != NULL)
564 nr = readlink(argv0, link, MAXPATHLEN);
565 if (nr > 0) {
566 /* It's a symlink */
567 link[nr] = '\0';
568 if (link[0] == SEP)
569 argv0 = link; /* Link to absolute path */
570 else if (strchr(link, SEP) == NULL)
571 ; /* Link without path */
572 else {
573 /* Must join(dirname(argv0), link) */
574 char *q = strrchr(argv0, SEP);
575 if (q == NULL)
576 argv0 = link; /* argv0 without path */
577 else {
578 /* Must make a copy */
579 strcpy(argv0copy, argv0);
580 q = strrchr(argv0copy, SEP);
581 strcpy(q+1, link);
582 argv0 = argv0copy;
583 }
584 }
585 }
586#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000587#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000588 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000589 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000590 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000591 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000592 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000593 if (q != NULL)
594 p = q;
595 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000596 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000597 if (n > 1 && p[-1] != ':')
598 n--; /* Drop trailing separator */
599 }
600 }
601#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000602 if (argc > 0 && argv0 != NULL)
603 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000604 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000605 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000606#if SEP == '/' /* Special case for Unix filename syntax */
607 if (n > 1)
608 n--; /* Drop trailing separator */
609#endif /* Unix */
610 }
611#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000612 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000613 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000614 Py_FatalError("no mem for sys.path insertion");
615 if (PyList_Insert(path, 0, a) < 0)
616 Py_FatalError("sys.path.insert(0) failed");
617 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000618 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000619 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620}
Guido van Rossuma890e681998-05-12 14:59:24 +0000621
622
623/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
624 Adapted from code submitted by Just van Rossum.
625
626 PySys_WriteStdout(format, ...)
627 PySys_WriteStderr(format, ...)
628
629 The first function writes to sys.stdout; the second to sys.stderr. When
630 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000631 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000632
633 Both take a printf-style format string as their first argument followed
634 by a variable length argument list determined by the format string.
635
636 *** WARNING ***
637
638 The format should limit the total size of the formatted output string to
639 1000 bytes. In particular, this means that no unrestricted "%s" formats
640 should occur; these should be limited using "%.<N>s where <N> is a
641 decimal number calculated so that <N> plus the maximum size of other
642 formatted text does not exceed 1000 bytes. Also watch out for "%f",
643 which can print hundreds of digits for very large numbers.
644
645 */
646
647static void
648mywrite(name, fp, format, va)
649 char *name;
650 FILE *fp;
651 const char *format;
652 va_list va;
653{
654 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000655 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000656
Guido van Rossum8442af31998-10-12 18:22:10 +0000657 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000658 file = PySys_GetObject(name);
659 if (file == NULL || PyFile_AsFile(file) == fp)
660 vfprintf(fp, format, va);
661 else {
662 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000663 if (vsprintf(buffer, format, va) >= sizeof(buffer))
664 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000665 if (PyFile_WriteString(buffer, file) != 0) {
666 PyErr_Clear();
667 fputs(buffer, fp);
668 }
669 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000670 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000671}
672
673void
674#ifdef HAVE_STDARG_PROTOTYPES
675PySys_WriteStdout(const char *format, ...)
676#else
677PySys_WriteStdout(va_alist)
678 va_dcl
679#endif
680{
681 va_list va;
682
683#ifdef HAVE_STDARG_PROTOTYPES
684 va_start(va, format);
685#else
686 char *format;
687 va_start(va);
688 format = va_arg(va, char *);
689#endif
690 mywrite("stdout", stdout, format, va);
691 va_end(va);
692}
693
694void
695#ifdef HAVE_STDARG_PROTOTYPES
696PySys_WriteStderr(const char *format, ...)
697#else
698PySys_WriteStderr(va_alist)
699 va_dcl
700#endif
701{
702 va_list va;
703
704#ifdef HAVE_STDARG_PROTOTYPES
705 va_start(va, format);
706#else
707 char *format;
708 va_start(va);
709 format = va_arg(va, char *);
710#endif
711 mywrite("stderr", stderr, format, va);
712 va_end(va);
713}