blob: 843df8dda317887e2cb121cff67f32b6c101ae02 [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 *
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000146sys_getdefaultencoding(self, args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000147 PyObject *self;
148 PyObject *args;
149{
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000150 if (!PyArg_ParseTuple(args, ":getdefaultencoding"))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000151 return NULL;
152 return PyString_FromString(PyUnicode_GetDefaultEncoding());
153}
154
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000155static char getdefaultencoding_doc[] =
156"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000157\n\
158Return the current default string encoding used by the Unicode \n\
159implementation.";
160
161static PyObject *
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000162sys_setdefaultencoding(self, args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000163 PyObject *self;
164 PyObject *args;
165{
166 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000167 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000168 return NULL;
169 if (PyUnicode_SetDefaultEncoding(encoding))
170 return NULL;
171 Py_INCREF(Py_None);
172 return Py_None;
173}
174
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000175static char setdefaultencoding_doc[] =
176"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000177\n\
178Set the current default string encoding used by the Unicode implementation.";
179
180static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000181sys_settrace(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000182 PyObject *self;
183 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000184{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000186 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000187 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000188 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000189 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000190 Py_XDECREF(tstate->sys_tracefunc);
191 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000192 Py_INCREF(Py_None);
193 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000194}
195
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000196static char settrace_doc[] =
197"settrace(function)\n\
198\n\
199Set the global debug tracing function. It will be called on each\n\
200function call. See the debugger chapter in the library manual.";
201
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000202static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000203sys_setprofile(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000204 PyObject *self;
205 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000206{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000207 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000208 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000209 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000210 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000211 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000212 Py_XDECREF(tstate->sys_profilefunc);
213 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000216}
217
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000218static char setprofile_doc[] =
219"setprofile(function)\n\
220\n\
221Set the profiling function. It will be called on each function call\n\
222and return. See the profiler chapter in the library manual.";
223
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000224static PyObject *
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000225sys_setcheckinterval(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000226 PyObject *self;
227 PyObject *args;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000228{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000229 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000230 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000231 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000232 Py_INCREF(Py_None);
233 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000234}
235
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000236static char setcheckinterval_doc[] =
237"setcheckinterval(n)\n\
238\n\
239Tell the Python interpreter to check for asynchronous events every\n\
240n instructions. This also affects how often thread switches occur.";
241
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000242#ifdef USE_MALLOPT
243/* Link with -lmalloc (or -lmpc) on an SGI */
244#include <malloc.h>
245
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000246static PyObject *
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000247sys_mdebug(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000248 PyObject *self;
249 PyObject *args;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000250{
251 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000252 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000253 return NULL;
254 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000255 Py_INCREF(Py_None);
256 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000257}
258#endif /* USE_MALLOPT */
259
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000260static PyObject *
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000261sys_getrefcount(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000262 PyObject *self;
263 PyObject *args;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000264{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000265 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000266 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000267 return NULL;
Mark Hammond440d8982000-06-20 08:12:48 +0000268 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000269}
270
Mark Hammond440d8982000-06-20 08:12:48 +0000271#ifdef Py_TRACE_REFS
272static PyObject *
273sys_gettotalrefcount(PyObject *self, PyObject *args)
274{
275 extern long _Py_RefTotal;
276 if (!PyArg_ParseTuple(args, ":gettotalrefcount"))
277 return NULL;
278 return PyInt_FromLong(_Py_RefTotal);
279}
280
281#endif /* Py_TRACE_REFS */
282
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000283static char getrefcount_doc[] =
284"getrefcount(object) -> integer\n\
285\n\
286Return the current reference count for the object. This includes the\n\
287temporary reference in the argument list, so it is at least 2.";
288
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000289#ifdef COUNT_ALLOCS
290static PyObject *
291sys_getcounts(self, args)
292 PyObject *self, *args;
293{
294 extern PyObject *get_counts Py_PROTO((void));
295
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000296 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000297 return NULL;
298 return get_counts();
299}
300#endif
301
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000302#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000303/* Defined in objects.c because it uses static globals if that file */
304extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000305#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000306
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000307#ifdef DYNAMIC_EXECUTION_PROFILE
308/* Defined in ceval.c because it uses static globals if that file */
309extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *, PyObject *));
310#endif
311
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000312static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000313 /* Might as well keep this in alphabetic order */
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000314 {"exc_info", sys_exc_info, 1, exc_info_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000315 {"exit", sys_exit, 0, exit_doc},
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000316 {"getdefaultencoding", sys_getdefaultencoding, 1, getdefaultencoding_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000317#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000318 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000319#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000320#ifdef DYNAMIC_EXECUTION_PROFILE
321 {"getdxp", _Py_GetDXProfile, 1},
322#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000323#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000324 {"getobjects", _Py_GetObjects, 1},
Mark Hammond440d8982000-06-20 08:12:48 +0000325 {"gettotalrefcount", sys_gettotalrefcount, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000326#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000327 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000328#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000329 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000330#endif
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000331 {"setdefaultencoding", sys_setdefaultencoding, 1, setdefaultencoding_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000332 {"setcheckinterval", sys_setcheckinterval, 1, setcheckinterval_doc},
333 {"setprofile", sys_setprofile, 0, setprofile_doc},
334 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335 {NULL, NULL} /* sentinel */
336};
337
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000338static PyObject *
Guido van Rossum34679b71993-01-26 13:33:44 +0000339list_builtin_module_names()
340{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000341 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000342 int i;
343 if (list == NULL)
344 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000345 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000346 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000347 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000348 if (name == NULL)
349 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000350 PyList_Append(list, name);
351 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000352 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000353 if (PyList_Sort(list) != 0) {
354 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000355 list = NULL;
356 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000357 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000358 PyObject *v = PyList_AsTuple(list);
359 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000360 list = v;
361 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000362 return list;
363}
364
Guido van Rossum40552d01998-08-06 03:34:39 +0000365/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
366 Two literals concatenated works just fine. If you have a K&R compiler
367 or other abomination that however *does* understand longer strings,
368 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000369static char sys_doc[] =
370"This module provides access to some objects used or maintained by the\n\
371interpreter and to functions that interact strongly with the interpreter.\n\
372\n\
373Dynamic objects:\n\
374\n\
375argv -- command line arguments; argv[0] is the script pathname if known\n\
376path -- module search path; path[0] is the script directory, else ''\n\
377modules -- dictionary of loaded modules\n\
378exitfunc -- you may set this to a function to be called when Python exits\n\
379\n\
380stdin -- standard input file object; used by raw_input() and input()\n\
381stdout -- standard output file object; used by the print statement\n\
382stderr -- standard error object; used for error messages\n\
383 By assigning another file object (or an object that behaves like a file)\n\
384 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
385\n\
386last_type -- type of last uncaught exception\n\
387last_value -- value of last uncaught exception\n\
388last_traceback -- traceback of last uncaught exception\n\
389 These three are only available in an interactive session after a\n\
390 traceback has been printed.\n\
391\n\
392exc_type -- type of exception currently being handled\n\
393exc_value -- value of exception currently being handled\n\
394exc_traceback -- traceback of exception currently being handled\n\
395 The function exc_info() should be used instead of these three,\n\
396 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000397"
398#ifndef MS_WIN16
399/* Concatenating string here */
400"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000401Static objects:\n\
402\n\
403maxint -- the largest supported integer (the smallest is -maxint-1)\n\
404builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000405version -- the version of this interpreter as a string\n\
406version_info -- version information as a tuple\n\
407hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000408copyright -- copyright notice pertaining to this interpreter\n\
409platform -- platform identifier\n\
410executable -- pathname of this Python interpreter\n\
411prefix -- prefix used to find the Python library\n\
412exec_prefix -- prefix used to find the machine-specific Python library\n\
413dllhandle -- [Windows only] integer handle of the Python DLL\n\
414winver -- [Windows only] version number of the Python DLL\n\
415__stdin__ -- the original stdin; don't use!\n\
416__stdout__ -- the original stdout; don't use!\n\
417__stderr__ -- the original stderr; don't use!\n\
418\n\
419Functions:\n\
420\n\
421exc_info() -- return thread-safe information about the current exception\n\
422exit() -- exit the interpreter by raising SystemExit\n\
423getrefcount() -- return the reference count for an object (plus one :-)\n\
424setcheckinterval() -- control how often the interpreter checks for events\n\
425setprofile() -- set the global profiling function\n\
426settrace() -- set the global debug tracing function\n\
427";
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000428#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000429
Guido van Rossum25ce5661997-08-02 03:10:38 +0000430PyObject *
431_PySys_Init()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000433 extern int fclose Py_PROTO((FILE *));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000434 PyObject *m, *v, *sysdict;
435 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000436 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000437
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000438 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000439 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000440
441 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
442 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
443 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000444 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000445 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000446 PyDict_SetItemString(sysdict, "stdin", sysin);
447 PyDict_SetItemString(sysdict, "stdout", sysout);
448 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000449 /* Make backup copies for cleanup */
450 PyDict_SetItemString(sysdict, "__stdin__", sysin);
451 PyDict_SetItemString(sysdict, "__stdout__", sysout);
452 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000453 Py_XDECREF(sysin);
454 Py_XDECREF(sysout);
455 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000456 PyDict_SetItemString(sysdict, "version",
457 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000458 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000459 PyDict_SetItemString(sysdict, "hexversion",
460 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000461 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000462 /*
463 * These release level checks are mutually exclusive and cover
464 * the field, so don't get too fancy with the pre-processor!
465 */
466#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000467 s = "alpha";
Fred Drake93a20bf2000-04-13 17:44:51 +0000468#endif
469#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000470 s = "beta";
Fred Drake93a20bf2000-04-13 17:44:51 +0000471#endif
472#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000473 s = "candidate";
Fred Drake93a20bf2000-04-13 17:44:51 +0000474#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000475#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000476 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000477#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000478 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000479 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000480 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000481 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000482 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000483 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000484 PyDict_SetItemString(sysdict, "copyright",
485 v = PyString_FromString(Py_GetCopyright()));
486 Py_XDECREF(v);
487 PyDict_SetItemString(sysdict, "platform",
488 v = PyString_FromString(Py_GetPlatform()));
489 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000490 PyDict_SetItemString(sysdict, "executable",
491 v = PyString_FromString(Py_GetProgramFullPath()));
492 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000493 PyDict_SetItemString(sysdict, "prefix",
494 v = PyString_FromString(Py_GetPrefix()));
495 Py_XDECREF(v);
496 PyDict_SetItemString(sysdict, "exec_prefix",
497 v = PyString_FromString(Py_GetExecPrefix()));
498 Py_XDECREF(v);
499 PyDict_SetItemString(sysdict, "maxint",
500 v = PyInt_FromLong(PyInt_GetMax()));
501 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000502 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000503 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000504 Py_XDECREF(v);
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000505#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000506 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000507 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000508 Py_XDECREF(v);
509 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000510 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000511 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000512#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000513 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000514 return NULL;
515 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000516}
517
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000518static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000519makepathobject(path, delim)
520 char *path;
521 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000522{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000523 int i, n;
524 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000525 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526
527 n = 1;
528 p = path;
529 while ((p = strchr(p, delim)) != NULL) {
530 n++;
531 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000532 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000533 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000534 if (v == NULL)
535 return NULL;
536 for (i = 0; ; i++) {
537 p = strchr(path, delim);
538 if (p == NULL)
539 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000540 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000541 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000542 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000543 return NULL;
544 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000545 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000546 if (*p == '\0')
547 break;
548 path = p+1;
549 }
550 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551}
552
553void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000554PySys_SetPath(path)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000555 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000557 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000558 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000559 Py_FatalError("can't create sys.path");
560 if (PySys_SetObject("path", v) != 0)
561 Py_FatalError("can't assign sys.path");
562 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000563}
564
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000565static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000566makeargvobject(argc, argv)
567 int argc;
568 char **argv;
569{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000570 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000571 if (argc <= 0 || argv == NULL) {
572 /* Ensure at least one (empty) argument is seen */
573 static char *empty_argv[1] = {""};
574 argv = empty_argv;
575 argc = 1;
576 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000577 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000578 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000579 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000580 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000581 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000582 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000583 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000584 av = NULL;
585 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000586 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000587 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000588 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000589 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000590 return av;
591}
592
593void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000594PySys_SetArgv(argc, argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000595 int argc;
596 char **argv;
597{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000598 PyObject *av = makeargvobject(argc, argv);
599 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000600 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000601 Py_FatalError("no mem for sys.argv");
602 if (PySys_SetObject("argv", av) != 0)
603 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000604 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000605 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000606 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000607 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000608 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000609#ifdef HAVE_READLINK
610 char link[MAXPATHLEN+1];
611 char argv0copy[2*MAXPATHLEN+1];
612 int nr = 0;
613 if (argc > 0 && argv0 != NULL)
614 nr = readlink(argv0, link, MAXPATHLEN);
615 if (nr > 0) {
616 /* It's a symlink */
617 link[nr] = '\0';
618 if (link[0] == SEP)
619 argv0 = link; /* Link to absolute path */
620 else if (strchr(link, SEP) == NULL)
621 ; /* Link without path */
622 else {
623 /* Must join(dirname(argv0), link) */
624 char *q = strrchr(argv0, SEP);
625 if (q == NULL)
626 argv0 = link; /* argv0 without path */
627 else {
628 /* Must make a copy */
629 strcpy(argv0copy, argv0);
630 q = strrchr(argv0copy, SEP);
631 strcpy(q+1, link);
632 argv0 = argv0copy;
633 }
634 }
635 }
636#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000637#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000638 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000639 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000640 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000641 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000642 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000643 if (q != NULL)
644 p = q;
645 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000646 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000647 if (n > 1 && p[-1] != ':')
648 n--; /* Drop trailing separator */
649 }
650 }
651#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000652 if (argc > 0 && argv0 != NULL)
653 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000654 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000655 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000656#if SEP == '/' /* Special case for Unix filename syntax */
657 if (n > 1)
658 n--; /* Drop trailing separator */
659#endif /* Unix */
660 }
661#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000662 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000663 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000664 Py_FatalError("no mem for sys.path insertion");
665 if (PyList_Insert(path, 0, a) < 0)
666 Py_FatalError("sys.path.insert(0) failed");
667 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000668 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000669 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670}
Guido van Rossuma890e681998-05-12 14:59:24 +0000671
672
673/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
674 Adapted from code submitted by Just van Rossum.
675
676 PySys_WriteStdout(format, ...)
677 PySys_WriteStderr(format, ...)
678
679 The first function writes to sys.stdout; the second to sys.stderr. When
680 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000681 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000682
683 Both take a printf-style format string as their first argument followed
684 by a variable length argument list determined by the format string.
685
686 *** WARNING ***
687
688 The format should limit the total size of the formatted output string to
689 1000 bytes. In particular, this means that no unrestricted "%s" formats
690 should occur; these should be limited using "%.<N>s where <N> is a
691 decimal number calculated so that <N> plus the maximum size of other
692 formatted text does not exceed 1000 bytes. Also watch out for "%f",
693 which can print hundreds of digits for very large numbers.
694
695 */
696
697static void
698mywrite(name, fp, format, va)
699 char *name;
700 FILE *fp;
701 const char *format;
702 va_list va;
703{
704 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000705 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000706
Guido van Rossum8442af31998-10-12 18:22:10 +0000707 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000708 file = PySys_GetObject(name);
709 if (file == NULL || PyFile_AsFile(file) == fp)
710 vfprintf(fp, format, va);
711 else {
712 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000713 if (vsprintf(buffer, format, va) >= sizeof(buffer))
714 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000715 if (PyFile_WriteString(buffer, file) != 0) {
716 PyErr_Clear();
717 fputs(buffer, fp);
718 }
719 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000720 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000721}
722
723void
724#ifdef HAVE_STDARG_PROTOTYPES
725PySys_WriteStdout(const char *format, ...)
726#else
727PySys_WriteStdout(va_alist)
728 va_dcl
729#endif
730{
731 va_list va;
732
733#ifdef HAVE_STDARG_PROTOTYPES
734 va_start(va, format);
735#else
736 char *format;
737 va_start(va);
738 format = va_arg(va, char *);
739#endif
740 mywrite("stdout", stdout, format, va);
741 va_end(va);
742}
743
744void
745#ifdef HAVE_STDARG_PROTOTYPES
746PySys_WriteStderr(const char *format, ...)
747#else
748PySys_WriteStderr(va_alist)
749 va_dcl
750#endif
751{
752 va_list va;
753
754#ifdef HAVE_STDARG_PROTOTYPES
755 va_start(va, format);
756#else
757 char *format;
758 va_start(va);
759 format = va_arg(va, char *);
760#endif
761 mywrite("stderr", stderr, format, va);
762 va_end(va);
763}