blob: 31d7abf27682d04ea1d9d4e7a66520363d90c3b9 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* System module */
12
13/*
14Various bits of information used by the interpreter are collected in
15module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +000016Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +000017- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018Data members:
19- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000021- path: module search path (list of strings)
22- argv: script arguments (list of strings)
23- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024*/
25
Guido van Rossum65bf9f21997-04-29 18:33:38 +000026#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027
Guido van Rossume2437a11992-03-23 18:20:18 +000028#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000029
Guido van Rossum1254d791997-05-20 15:57:25 +000030#ifdef HAVE_UNISTD_H
Guido van Rossumc474dea1997-04-25 15:38:31 +000031#include <unistd.h>
32#endif
33
Guido van Rossum9b38a141996-09-11 23:12:24 +000034#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000035extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000036/* A string loaded from the DLL at startup: */
37extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000038#endif
39
Guido van Rossum65bf9f21997-04-29 18:33:38 +000040PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000041PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042{
Guido van Rossum25ce5661997-08-02 03:10:38 +000043 PyThreadState *tstate = PyThreadState_Get();
44 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000045 if (sd == NULL)
46 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000047 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048}
49
50FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000051PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052{
53 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000054 PyObject *v = PySys_GetObject(name);
55 if (v != NULL && PyFile_Check(v))
56 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 if (fp == NULL)
58 fp = def;
59 return fp;
60}
61
62int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000063PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064{
Guido van Rossum25ce5661997-08-02 03:10:38 +000065 PyThreadState *tstate = PyThreadState_Get();
66 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000067 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000068 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000069 return 0;
70 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000071 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000072 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000074 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
Guido van Rossum65bf9f21997-04-29 18:33:38 +000077static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000078sys_exc_info(PyObject *self, PyObject *args)
Guido van Rossuma027efa1997-05-05 20:56:21 +000079{
80 PyThreadState *tstate;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +000081 if (!PyArg_ParseTuple(args, ":exc_info"))
Guido van Rossuma027efa1997-05-05 20:56:21 +000082 return NULL;
83 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +000084 return Py_BuildValue(
85 "(OOO)",
86 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
87 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
88 tstate->exc_traceback != NULL ?
89 tstate->exc_traceback : Py_None);
90}
91
Guido van Rossumc3bc31e1998-06-27 19:43:25 +000092static char exc_info_doc[] =
93"exc_info() -> (type, value, traceback)\n\
94\n\
95Return information about the exception that is currently being handled.\n\
96This should be called from inside an except clause only.";
97
Guido van Rossuma027efa1997-05-05 20:56:21 +000098static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000101 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000102 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000103 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104}
105
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000106static char exit_doc[] =
107"exit([status])\n\
108\n\
109Exit the interpreter by raising SystemExit(status).\n\
110If the status is omitted or None, it defaults to zero (i.e., success).\n\
111If the status numeric, it will be used as the system exit status.\n\
112If it is another kind of object, it will be printed and the system\n\
113exit status will be one (i.e., failure).";
114
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000115static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116sys_getdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000117{
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000118 if (!PyArg_ParseTuple(args, ":getdefaultencoding"))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000119 return NULL;
120 return PyString_FromString(PyUnicode_GetDefaultEncoding());
121}
122
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000123static char getdefaultencoding_doc[] =
124"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000125\n\
126Return the current default string encoding used by the Unicode \n\
127implementation.";
128
129static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000130sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000131{
132 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000133 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000134 return NULL;
135 if (PyUnicode_SetDefaultEncoding(encoding))
136 return NULL;
137 Py_INCREF(Py_None);
138 return Py_None;
139}
140
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000141static char setdefaultencoding_doc[] =
142"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000143\n\
144Set the current default string encoding used by the Unicode implementation.";
145
146static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000147sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000148{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000149 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000150 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000151 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000152 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000153 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154 Py_XDECREF(tstate->sys_tracefunc);
155 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000156 Py_INCREF(Py_None);
157 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000158}
159
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000160static char settrace_doc[] =
161"settrace(function)\n\
162\n\
163Set the global debug tracing function. It will be called on each\n\
164function call. See the debugger chapter in the library manual.";
165
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000166static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000167sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000168{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000169 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000170 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000171 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000172 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000173 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000174 Py_XDECREF(tstate->sys_profilefunc);
175 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000176 Py_INCREF(Py_None);
177 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000178}
179
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000180static char setprofile_doc[] =
181"setprofile(function)\n\
182\n\
183Set the profiling function. It will be called on each function call\n\
184and return. See the profiler chapter in the library manual.";
185
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000186static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000188{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000189 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000190 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000191 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000192 Py_INCREF(Py_None);
193 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000194}
195
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000196static char setcheckinterval_doc[] =
197"setcheckinterval(n)\n\
198\n\
199Tell the Python interpreter to check for asynchronous events every\n\
200n instructions. This also affects how often thread switches occur.";
201
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000202static PyObject *
203sys_setrecursionlimit(PyObject *self, PyObject *args)
204{
205 int new_limit;
206 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
207 return NULL;
208 if (new_limit <= 0) {
209 PyErr_SetString(PyExc_ValueError,
210 "recursion limit must be positive");
211 return NULL;
212 }
213 Py_SetRecursionLimit(new_limit);
214 Py_INCREF(Py_None);
215 return Py_None;
216}
217
218static char setrecursionlimit_doc[] =
219"setrecursionlimit(n)\n\
220\n\
221Set the maximum depth of the Python interpreter stack to n. This\n\
222limit prevents infinite recursion from causing an overflow of the C\n\
223stack and crashing Python. The highest possible limit is platform-\n\
224dependent.";
225
226static PyObject *
227sys_getrecursionlimit(PyObject *self, PyObject *args)
228{
229 if (!PyArg_ParseTuple(args, ":getrecursionlimit"))
230 return NULL;
231 return PyInt_FromLong(Py_GetRecursionLimit());
232}
233
234static char getrecursionlimit_doc[] =
235"getrecursionlimit()\n\
236\n\
237Return the current value of the recursion limit, the maximum depth\n\
238of the Python interpreter stack. This limit prevents infinite\n\
239recursion from causing an overflow of the C stack and crashing Python.";
240
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000241#ifdef USE_MALLOPT
242/* Link with -lmalloc (or -lmpc) on an SGI */
243#include <malloc.h>
244
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000245static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000247{
248 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000249 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000250 return NULL;
251 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000252 Py_INCREF(Py_None);
253 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000254}
255#endif /* USE_MALLOPT */
256
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000257static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258sys_getrefcount(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000259{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000260 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000261 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000262 return NULL;
Mark Hammond440d8982000-06-20 08:12:48 +0000263 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000264}
265
Mark Hammond440d8982000-06-20 08:12:48 +0000266#ifdef Py_TRACE_REFS
267static PyObject *
268sys_gettotalrefcount(PyObject *self, PyObject *args)
269{
270 extern long _Py_RefTotal;
271 if (!PyArg_ParseTuple(args, ":gettotalrefcount"))
272 return NULL;
273 return PyInt_FromLong(_Py_RefTotal);
274}
275
276#endif /* Py_TRACE_REFS */
277
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000278static char getrefcount_doc[] =
279"getrefcount(object) -> integer\n\
280\n\
281Return the current reference count for the object. This includes the\n\
282temporary reference in the argument list, so it is at least 2.";
283
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000284#ifdef COUNT_ALLOCS
285static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000286sys_getcounts(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000287{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000288 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000289
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000290 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000291 return NULL;
292 return get_counts();
293}
294#endif
295
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000296#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000297/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000298extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000299#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000300
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000301#ifdef DYNAMIC_EXECUTION_PROFILE
302/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000303extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000304#endif
305
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000306static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000307 /* Might as well keep this in alphabetic order */
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000308 {"exc_info", sys_exc_info, 1, exc_info_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000309 {"exit", sys_exit, 0, exit_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000310 {"getdefaultencoding", sys_getdefaultencoding, 1,
311 getdefaultencoding_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000312#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000313 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000314#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000315#ifdef DYNAMIC_EXECUTION_PROFILE
316 {"getdxp", _Py_GetDXProfile, 1},
317#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000318#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000319 {"getobjects", _Py_GetObjects, 1},
Mark Hammond440d8982000-06-20 08:12:48 +0000320 {"gettotalrefcount", sys_gettotalrefcount, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000321#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000322 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000323 {"getrecursionlimit", sys_getrecursionlimit, 1,
324 getrecursionlimit_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000325#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000326 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000327#endif
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000328 {"setdefaultencoding", sys_setdefaultencoding, 1,
329 setdefaultencoding_doc},
330 {"setcheckinterval", sys_setcheckinterval, 1,
331 setcheckinterval_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000332 {"setprofile", sys_setprofile, 0, setprofile_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000333 {"setrecursionlimit", sys_setrecursionlimit, 1,
334 setrecursionlimit_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000335 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336 {NULL, NULL} /* sentinel */
337};
338
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000339static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000341{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000342 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000343 int i;
344 if (list == NULL)
345 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000346 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000347 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000348 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000349 if (name == NULL)
350 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000351 PyList_Append(list, name);
352 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000353 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000354 if (PyList_Sort(list) != 0) {
355 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000356 list = NULL;
357 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000358 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000359 PyObject *v = PyList_AsTuple(list);
360 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000361 list = v;
362 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000363 return list;
364}
365
Guido van Rossum40552d01998-08-06 03:34:39 +0000366/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
367 Two literals concatenated works just fine. If you have a K&R compiler
368 or other abomination that however *does* understand longer strings,
369 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000370static char sys_doc[] =
371"This module provides access to some objects used or maintained by the\n\
372interpreter and to functions that interact strongly with the interpreter.\n\
373\n\
374Dynamic objects:\n\
375\n\
376argv -- command line arguments; argv[0] is the script pathname if known\n\
377path -- module search path; path[0] is the script directory, else ''\n\
378modules -- dictionary of loaded modules\n\
379exitfunc -- you may set this to a function to be called when Python exits\n\
380\n\
381stdin -- standard input file object; used by raw_input() and input()\n\
382stdout -- standard output file object; used by the print statement\n\
383stderr -- standard error object; used for error messages\n\
384 By assigning another file object (or an object that behaves like a file)\n\
385 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
386\n\
387last_type -- type of last uncaught exception\n\
388last_value -- value of last uncaught exception\n\
389last_traceback -- traceback of last uncaught exception\n\
390 These three are only available in an interactive session after a\n\
391 traceback has been printed.\n\
392\n\
393exc_type -- type of exception currently being handled\n\
394exc_value -- value of exception currently being handled\n\
395exc_traceback -- traceback of exception currently being handled\n\
396 The function exc_info() should be used instead of these three,\n\
397 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000398"
399#ifndef MS_WIN16
400/* Concatenating string here */
401"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000402Static objects:\n\
403\n\
404maxint -- the largest supported integer (the smallest is -maxint-1)\n\
405builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000406version -- the version of this interpreter as a string\n\
407version_info -- version information as a tuple\n\
408hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000409copyright -- copyright notice pertaining to this interpreter\n\
410platform -- platform identifier\n\
411executable -- pathname of this Python interpreter\n\
412prefix -- prefix used to find the Python library\n\
413exec_prefix -- prefix used to find the machine-specific Python library\n\
414dllhandle -- [Windows only] integer handle of the Python DLL\n\
415winver -- [Windows only] version number of the Python DLL\n\
416__stdin__ -- the original stdin; don't use!\n\
417__stdout__ -- the original stdout; don't use!\n\
418__stderr__ -- the original stderr; don't use!\n\
419\n\
420Functions:\n\
421\n\
422exc_info() -- return thread-safe information about the current exception\n\
423exit() -- exit the interpreter by raising SystemExit\n\
424getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000425getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000426setcheckinterval() -- control how often the interpreter checks for events\n\
427setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000428setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000429settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000430"
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000431#endif
Fred Drakeccede592000-08-14 20:59:57 +0000432/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000433
Guido van Rossum25ce5661997-08-02 03:10:38 +0000434PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000437 PyObject *m, *v, *sysdict;
438 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000439 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000440
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000441 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000442 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000443
444 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
445 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
446 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000447 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000448 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000449 PyDict_SetItemString(sysdict, "stdin", sysin);
450 PyDict_SetItemString(sysdict, "stdout", sysout);
451 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000452 /* Make backup copies for cleanup */
453 PyDict_SetItemString(sysdict, "__stdin__", sysin);
454 PyDict_SetItemString(sysdict, "__stdout__", sysout);
455 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000456 Py_XDECREF(sysin);
457 Py_XDECREF(sysout);
458 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000459 PyDict_SetItemString(sysdict, "version",
460 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000461 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000462 PyDict_SetItemString(sysdict, "hexversion",
463 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000464 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000465 /*
466 * These release level checks are mutually exclusive and cover
467 * the field, so don't get too fancy with the pre-processor!
468 */
469#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000470 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000471#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000472 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000473#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000474 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000475#elif 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);
Fred Drake099325e2000-08-14 15:47:03 +0000505 {
506 /* Assumes that longs are at least 2 bytes long.
507 Should be safe! */
508 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000509 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000510
511 s = (char *) &number;
512 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000513 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000514 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000515 value = "little";
516 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000517 v = PyString_FromString(value));
518 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000519 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000520#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000521 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000522 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000523 Py_XDECREF(v);
524 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000525 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000526 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000527#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000528 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529 return NULL;
530 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000531}
532
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000533static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000535{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536 int i, n;
537 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000538 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000539
540 n = 1;
541 p = path;
542 while ((p = strchr(p, delim)) != NULL) {
543 n++;
544 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000545 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000546 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000547 if (v == NULL)
548 return NULL;
549 for (i = 0; ; i++) {
550 p = strchr(path, delim);
551 if (p == NULL)
552 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000553 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000554 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000555 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000556 return NULL;
557 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000558 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000559 if (*p == '\0')
560 break;
561 path = p+1;
562 }
563 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564}
565
566void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000567PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000569 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000570 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000571 Py_FatalError("can't create sys.path");
572 if (PySys_SetObject("path", v) != 0)
573 Py_FatalError("can't assign sys.path");
574 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000575}
576
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000577static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000578makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000579{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000580 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000581 if (argc <= 0 || argv == NULL) {
582 /* Ensure at least one (empty) argument is seen */
583 static char *empty_argv[1] = {""};
584 argv = empty_argv;
585 argc = 1;
586 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000587 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000588 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000589 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000590 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000591 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000592 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000593 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000594 av = NULL;
595 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000596 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000597 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000598 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000599 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000600 return av;
601}
602
603void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000604PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000605{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000606 PyObject *av = makeargvobject(argc, argv);
607 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000608 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000609 Py_FatalError("no mem for sys.argv");
610 if (PySys_SetObject("argv", av) != 0)
611 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000612 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000613 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000614 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000615 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000616 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000617#ifdef HAVE_READLINK
618 char link[MAXPATHLEN+1];
619 char argv0copy[2*MAXPATHLEN+1];
620 int nr = 0;
621 if (argc > 0 && argv0 != NULL)
622 nr = readlink(argv0, link, MAXPATHLEN);
623 if (nr > 0) {
624 /* It's a symlink */
625 link[nr] = '\0';
626 if (link[0] == SEP)
627 argv0 = link; /* Link to absolute path */
628 else if (strchr(link, SEP) == NULL)
629 ; /* Link without path */
630 else {
631 /* Must join(dirname(argv0), link) */
632 char *q = strrchr(argv0, SEP);
633 if (q == NULL)
634 argv0 = link; /* argv0 without path */
635 else {
636 /* Must make a copy */
637 strcpy(argv0copy, argv0);
638 q = strrchr(argv0copy, SEP);
639 strcpy(q+1, link);
640 argv0 = argv0copy;
641 }
642 }
643 }
644#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000645#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000646 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000647 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000648 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000649 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000650 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000651 if (q != NULL)
652 p = q;
653 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000654 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000655 if (n > 1 && p[-1] != ':')
656 n--; /* Drop trailing separator */
657 }
658 }
659#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000660 if (argc > 0 && argv0 != NULL)
661 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000662 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000663 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000664#if SEP == '/' /* Special case for Unix filename syntax */
665 if (n > 1)
666 n--; /* Drop trailing separator */
667#endif /* Unix */
668 }
669#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000670 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000671 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000672 Py_FatalError("no mem for sys.path insertion");
673 if (PyList_Insert(path, 0, a) < 0)
674 Py_FatalError("sys.path.insert(0) failed");
675 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000676 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000677 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678}
Guido van Rossuma890e681998-05-12 14:59:24 +0000679
680
681/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
682 Adapted from code submitted by Just van Rossum.
683
684 PySys_WriteStdout(format, ...)
685 PySys_WriteStderr(format, ...)
686
687 The first function writes to sys.stdout; the second to sys.stderr. When
688 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000689 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000690
691 Both take a printf-style format string as their first argument followed
692 by a variable length argument list determined by the format string.
693
694 *** WARNING ***
695
696 The format should limit the total size of the formatted output string to
697 1000 bytes. In particular, this means that no unrestricted "%s" formats
698 should occur; these should be limited using "%.<N>s where <N> is a
699 decimal number calculated so that <N> plus the maximum size of other
700 formatted text does not exceed 1000 bytes. Also watch out for "%f",
701 which can print hundreds of digits for very large numbers.
702
703 */
704
705static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000706mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +0000707{
708 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000709 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000710
Guido van Rossum8442af31998-10-12 18:22:10 +0000711 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000712 file = PySys_GetObject(name);
713 if (file == NULL || PyFile_AsFile(file) == fp)
714 vfprintf(fp, format, va);
715 else {
716 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000717 if (vsprintf(buffer, format, va) >= sizeof(buffer))
718 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000719 if (PyFile_WriteString(buffer, file) != 0) {
720 PyErr_Clear();
721 fputs(buffer, fp);
722 }
723 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000724 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000725}
726
727void
Guido van Rossuma890e681998-05-12 14:59:24 +0000728PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000729{
730 va_list va;
731
Guido van Rossuma890e681998-05-12 14:59:24 +0000732 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000733 mywrite("stdout", stdout, format, va);
734 va_end(va);
735}
736
737void
Guido van Rossuma890e681998-05-12 14:59:24 +0000738PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000739{
740 va_list va;
741
Guido van Rossuma890e681998-05-12 14:59:24 +0000742 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000743 mywrite("stderr", stderr, format, va);
744 va_end(va);
745}