blob: 6ba43bd1ee632d8b15d05a5e80dc0e04b04611e7 [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 *
41PySys_GetObject(name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042 char *name;
43{
Guido van Rossum25ce5661997-08-02 03:10:38 +000044 PyThreadState *tstate = PyThreadState_Get();
45 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000046 if (sd == NULL)
47 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000048 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049}
50
51FILE *
Guido van Rossum65bf9f21997-04-29 18:33:38 +000052PySys_GetFile(name, def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053 char *name;
54 FILE *def;
55{
56 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000057 PyObject *v = PySys_GetObject(name);
58 if (v != NULL && PyFile_Check(v))
59 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060 if (fp == NULL)
61 fp = def;
62 return fp;
63}
64
65int
Guido van Rossum65bf9f21997-04-29 18:33:38 +000066PySys_SetObject(name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 char *name;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000068 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069{
Guido van Rossum25ce5661997-08-02 03:10:38 +000070 PyThreadState *tstate = PyThreadState_Get();
71 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000072 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000073 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000074 return 0;
75 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000076 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000077 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000079 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080}
81
Guido van Rossum65bf9f21997-04-29 18:33:38 +000082static PyObject *
Guido van Rossuma027efa1997-05-05 20:56:21 +000083sys_exc_info(self, args)
84 PyObject *self;
85 PyObject *args;
86{
87 PyThreadState *tstate;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +000088 if (!PyArg_ParseTuple(args, ":exc_info"))
Guido van Rossuma027efa1997-05-05 20:56:21 +000089 return NULL;
90 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +000091 return Py_BuildValue(
92 "(OOO)",
93 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
94 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
95 tstate->exc_traceback != NULL ?
96 tstate->exc_traceback : Py_None);
97}
98
Guido van Rossumc3bc31e1998-06-27 19:43:25 +000099static char exc_info_doc[] =
100"exc_info() -> (type, value, traceback)\n\
101\n\
102Return information about the exception that is currently being handled.\n\
103This should be called from inside an except clause only.";
104
Guido van Rossuma027efa1997-05-05 20:56:21 +0000105static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106sys_exit(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000107 PyObject *self;
108 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000110 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000111 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000112 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113}
114
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000115static char exit_doc[] =
116"exit([status])\n\
117\n\
118Exit the interpreter by raising SystemExit(status).\n\
119If the status is omitted or None, it defaults to zero (i.e., success).\n\
120If the status numeric, it will be used as the system exit status.\n\
121If it is another kind of object, it will be printed and the system\n\
122exit status will be one (i.e., failure).";
123
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000124static PyObject *
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000125sys_getdefaultencoding(self, args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000126 PyObject *self;
127 PyObject *args;
128{
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000129 if (!PyArg_ParseTuple(args, ":getdefaultencoding"))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000130 return NULL;
131 return PyString_FromString(PyUnicode_GetDefaultEncoding());
132}
133
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000134static char getdefaultencoding_doc[] =
135"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000136\n\
137Return the current default string encoding used by the Unicode \n\
138implementation.";
139
140static PyObject *
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000141sys_setdefaultencoding(self, args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000142 PyObject *self;
143 PyObject *args;
144{
145 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000146 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000147 return NULL;
148 if (PyUnicode_SetDefaultEncoding(encoding))
149 return NULL;
150 Py_INCREF(Py_None);
151 return Py_None;
152}
153
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000154static char setdefaultencoding_doc[] =
155"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000156\n\
157Set the current default string encoding used by the Unicode implementation.";
158
159static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000160sys_settrace(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000161 PyObject *self;
162 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000163{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000165 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000166 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000167 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000168 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000169 Py_XDECREF(tstate->sys_tracefunc);
170 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000171 Py_INCREF(Py_None);
172 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000173}
174
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000175static char settrace_doc[] =
176"settrace(function)\n\
177\n\
178Set the global debug tracing function. It will be called on each\n\
179function call. See the debugger chapter in the library manual.";
180
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000181static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000182sys_setprofile(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000183 PyObject *self;
184 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000185{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000186 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000187 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000188 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000189 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000190 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000191 Py_XDECREF(tstate->sys_profilefunc);
192 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000193 Py_INCREF(Py_None);
194 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000195}
196
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000197static char setprofile_doc[] =
198"setprofile(function)\n\
199\n\
200Set the profiling function. It will be called on each function call\n\
201and return. See the profiler chapter in the library manual.";
202
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000203static PyObject *
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000204sys_setcheckinterval(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000205 PyObject *self;
206 PyObject *args;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000207{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000209 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000210 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000211 Py_INCREF(Py_None);
212 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000213}
214
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000215static char setcheckinterval_doc[] =
216"setcheckinterval(n)\n\
217\n\
218Tell the Python interpreter to check for asynchronous events every\n\
219n instructions. This also affects how often thread switches occur.";
220
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000221#ifdef USE_MALLOPT
222/* Link with -lmalloc (or -lmpc) on an SGI */
223#include <malloc.h>
224
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000225static PyObject *
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000226sys_mdebug(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000227 PyObject *self;
228 PyObject *args;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000229{
230 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000231 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000232 return NULL;
233 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000234 Py_INCREF(Py_None);
235 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000236}
237#endif /* USE_MALLOPT */
238
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000239static PyObject *
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000240sys_getrefcount(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000241 PyObject *self;
242 PyObject *args;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000243{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000244 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000245 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000246 return NULL;
Mark Hammond440d8982000-06-20 08:12:48 +0000247 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000248}
249
Mark Hammond440d8982000-06-20 08:12:48 +0000250#ifdef Py_TRACE_REFS
251static PyObject *
252sys_gettotalrefcount(PyObject *self, PyObject *args)
253{
254 extern long _Py_RefTotal;
255 if (!PyArg_ParseTuple(args, ":gettotalrefcount"))
256 return NULL;
257 return PyInt_FromLong(_Py_RefTotal);
258}
259
260#endif /* Py_TRACE_REFS */
261
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000262static char getrefcount_doc[] =
263"getrefcount(object) -> integer\n\
264\n\
265Return the current reference count for the object. This includes the\n\
266temporary reference in the argument list, so it is at least 2.";
267
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000268#ifdef COUNT_ALLOCS
269static PyObject *
270sys_getcounts(self, args)
271 PyObject *self, *args;
272{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000273 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000274
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000275 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000276 return NULL;
277 return get_counts();
278}
279#endif
280
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000281#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000282/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000283extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000284#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000285
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000286#ifdef DYNAMIC_EXECUTION_PROFILE
287/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000288extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000289#endif
290
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000291static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000292 /* Might as well keep this in alphabetic order */
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000293 {"exc_info", sys_exc_info, 1, exc_info_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000294 {"exit", sys_exit, 0, exit_doc},
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000295 {"getdefaultencoding", sys_getdefaultencoding, 1, getdefaultencoding_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000296#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000297 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000298#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000299#ifdef DYNAMIC_EXECUTION_PROFILE
300 {"getdxp", _Py_GetDXProfile, 1},
301#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000302#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000303 {"getobjects", _Py_GetObjects, 1},
Mark Hammond440d8982000-06-20 08:12:48 +0000304 {"gettotalrefcount", sys_gettotalrefcount, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000305#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000306 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000307#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000308 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000309#endif
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000310 {"setdefaultencoding", sys_setdefaultencoding, 1, setdefaultencoding_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000311 {"setcheckinterval", sys_setcheckinterval, 1, setcheckinterval_doc},
312 {"setprofile", sys_setprofile, 0, setprofile_doc},
313 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000314 {NULL, NULL} /* sentinel */
315};
316
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000317static PyObject *
Guido van Rossum34679b71993-01-26 13:33:44 +0000318list_builtin_module_names()
319{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000320 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000321 int i;
322 if (list == NULL)
323 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000324 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000325 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000326 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000327 if (name == NULL)
328 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000329 PyList_Append(list, name);
330 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000331 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000332 if (PyList_Sort(list) != 0) {
333 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000334 list = NULL;
335 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000336 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000337 PyObject *v = PyList_AsTuple(list);
338 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000339 list = v;
340 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000341 return list;
342}
343
Guido van Rossum40552d01998-08-06 03:34:39 +0000344/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
345 Two literals concatenated works just fine. If you have a K&R compiler
346 or other abomination that however *does* understand longer strings,
347 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000348static char sys_doc[] =
349"This module provides access to some objects used or maintained by the\n\
350interpreter and to functions that interact strongly with the interpreter.\n\
351\n\
352Dynamic objects:\n\
353\n\
354argv -- command line arguments; argv[0] is the script pathname if known\n\
355path -- module search path; path[0] is the script directory, else ''\n\
356modules -- dictionary of loaded modules\n\
357exitfunc -- you may set this to a function to be called when Python exits\n\
358\n\
359stdin -- standard input file object; used by raw_input() and input()\n\
360stdout -- standard output file object; used by the print statement\n\
361stderr -- standard error object; used for error messages\n\
362 By assigning another file object (or an object that behaves like a file)\n\
363 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
364\n\
365last_type -- type of last uncaught exception\n\
366last_value -- value of last uncaught exception\n\
367last_traceback -- traceback of last uncaught exception\n\
368 These three are only available in an interactive session after a\n\
369 traceback has been printed.\n\
370\n\
371exc_type -- type of exception currently being handled\n\
372exc_value -- value of exception currently being handled\n\
373exc_traceback -- traceback of exception currently being handled\n\
374 The function exc_info() should be used instead of these three,\n\
375 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000376"
377#ifndef MS_WIN16
378/* Concatenating string here */
379"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000380Static objects:\n\
381\n\
382maxint -- the largest supported integer (the smallest is -maxint-1)\n\
383builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000384version -- the version of this interpreter as a string\n\
385version_info -- version information as a tuple\n\
386hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000387copyright -- copyright notice pertaining to this interpreter\n\
388platform -- platform identifier\n\
389executable -- pathname of this Python interpreter\n\
390prefix -- prefix used to find the Python library\n\
391exec_prefix -- prefix used to find the machine-specific Python library\n\
392dllhandle -- [Windows only] integer handle of the Python DLL\n\
393winver -- [Windows only] version number of the Python DLL\n\
394__stdin__ -- the original stdin; don't use!\n\
395__stdout__ -- the original stdout; don't use!\n\
396__stderr__ -- the original stderr; don't use!\n\
397\n\
398Functions:\n\
399\n\
400exc_info() -- return thread-safe information about the current exception\n\
401exit() -- exit the interpreter by raising SystemExit\n\
402getrefcount() -- return the reference count for an object (plus one :-)\n\
403setcheckinterval() -- control how often the interpreter checks for events\n\
404setprofile() -- set the global profiling function\n\
405settrace() -- set the global debug tracing function\n\
406";
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000407#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000408
Guido van Rossum25ce5661997-08-02 03:10:38 +0000409PyObject *
410_PySys_Init()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000412 extern int fclose(FILE *);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000413 PyObject *m, *v, *sysdict;
414 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000415 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000416
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000417 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000418 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000419
420 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
421 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
422 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000423 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000424 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000425 PyDict_SetItemString(sysdict, "stdin", sysin);
426 PyDict_SetItemString(sysdict, "stdout", sysout);
427 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000428 /* Make backup copies for cleanup */
429 PyDict_SetItemString(sysdict, "__stdin__", sysin);
430 PyDict_SetItemString(sysdict, "__stdout__", sysout);
431 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000432 Py_XDECREF(sysin);
433 Py_XDECREF(sysout);
434 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000435 PyDict_SetItemString(sysdict, "version",
436 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000437 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000438 PyDict_SetItemString(sysdict, "hexversion",
439 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000440 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000441 /*
442 * These release level checks are mutually exclusive and cover
443 * the field, so don't get too fancy with the pre-processor!
444 */
445#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000446 s = "alpha";
Fred Drake93a20bf2000-04-13 17:44:51 +0000447#endif
448#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000449 s = "beta";
Fred Drake93a20bf2000-04-13 17:44:51 +0000450#endif
451#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000452 s = "candidate";
Fred Drake93a20bf2000-04-13 17:44:51 +0000453#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000454#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000455 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000456#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000457 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000458 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000459 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000460 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000461 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000462 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000463 PyDict_SetItemString(sysdict, "copyright",
464 v = PyString_FromString(Py_GetCopyright()));
465 Py_XDECREF(v);
466 PyDict_SetItemString(sysdict, "platform",
467 v = PyString_FromString(Py_GetPlatform()));
468 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000469 PyDict_SetItemString(sysdict, "executable",
470 v = PyString_FromString(Py_GetProgramFullPath()));
471 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000472 PyDict_SetItemString(sysdict, "prefix",
473 v = PyString_FromString(Py_GetPrefix()));
474 Py_XDECREF(v);
475 PyDict_SetItemString(sysdict, "exec_prefix",
476 v = PyString_FromString(Py_GetExecPrefix()));
477 Py_XDECREF(v);
478 PyDict_SetItemString(sysdict, "maxint",
479 v = PyInt_FromLong(PyInt_GetMax()));
480 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000481 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000483 Py_XDECREF(v);
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000484#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000485 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000486 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000487 Py_XDECREF(v);
488 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000489 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000490 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000491#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000492 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000493 return NULL;
494 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000495}
496
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000497static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000498makepathobject(path, delim)
499 char *path;
500 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000501{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000502 int i, n;
503 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000504 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000505
506 n = 1;
507 p = path;
508 while ((p = strchr(p, delim)) != NULL) {
509 n++;
510 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000511 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000512 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000513 if (v == NULL)
514 return NULL;
515 for (i = 0; ; i++) {
516 p = strchr(path, delim);
517 if (p == NULL)
518 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000519 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000520 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000521 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000522 return NULL;
523 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000524 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000525 if (*p == '\0')
526 break;
527 path = p+1;
528 }
529 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530}
531
532void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000533PySys_SetPath(path)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000534 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000536 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000537 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000538 Py_FatalError("can't create sys.path");
539 if (PySys_SetObject("path", v) != 0)
540 Py_FatalError("can't assign sys.path");
541 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000542}
543
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000544static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000545makeargvobject(argc, argv)
546 int argc;
547 char **argv;
548{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000549 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000550 if (argc <= 0 || argv == NULL) {
551 /* Ensure at least one (empty) argument is seen */
552 static char *empty_argv[1] = {""};
553 argv = empty_argv;
554 argc = 1;
555 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000556 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000557 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000558 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000559 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000560 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000561 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000562 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000563 av = NULL;
564 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000565 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000566 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000567 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000568 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000569 return av;
570}
571
572void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000573PySys_SetArgv(argc, argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000574 int argc;
575 char **argv;
576{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000577 PyObject *av = makeargvobject(argc, argv);
578 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000579 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000580 Py_FatalError("no mem for sys.argv");
581 if (PySys_SetObject("argv", av) != 0)
582 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000583 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000584 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000585 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000586 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000587 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000588#ifdef HAVE_READLINK
589 char link[MAXPATHLEN+1];
590 char argv0copy[2*MAXPATHLEN+1];
591 int nr = 0;
592 if (argc > 0 && argv0 != NULL)
593 nr = readlink(argv0, link, MAXPATHLEN);
594 if (nr > 0) {
595 /* It's a symlink */
596 link[nr] = '\0';
597 if (link[0] == SEP)
598 argv0 = link; /* Link to absolute path */
599 else if (strchr(link, SEP) == NULL)
600 ; /* Link without path */
601 else {
602 /* Must join(dirname(argv0), link) */
603 char *q = strrchr(argv0, SEP);
604 if (q == NULL)
605 argv0 = link; /* argv0 without path */
606 else {
607 /* Must make a copy */
608 strcpy(argv0copy, argv0);
609 q = strrchr(argv0copy, SEP);
610 strcpy(q+1, link);
611 argv0 = argv0copy;
612 }
613 }
614 }
615#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000616#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000617 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000618 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000619 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000620 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000621 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000622 if (q != NULL)
623 p = q;
624 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000625 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000626 if (n > 1 && p[-1] != ':')
627 n--; /* Drop trailing separator */
628 }
629 }
630#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000631 if (argc > 0 && argv0 != NULL)
632 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000633 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000634 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000635#if SEP == '/' /* Special case for Unix filename syntax */
636 if (n > 1)
637 n--; /* Drop trailing separator */
638#endif /* Unix */
639 }
640#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000641 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000642 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000643 Py_FatalError("no mem for sys.path insertion");
644 if (PyList_Insert(path, 0, a) < 0)
645 Py_FatalError("sys.path.insert(0) failed");
646 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000647 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000648 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649}
Guido van Rossuma890e681998-05-12 14:59:24 +0000650
651
652/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
653 Adapted from code submitted by Just van Rossum.
654
655 PySys_WriteStdout(format, ...)
656 PySys_WriteStderr(format, ...)
657
658 The first function writes to sys.stdout; the second to sys.stderr. When
659 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000660 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000661
662 Both take a printf-style format string as their first argument followed
663 by a variable length argument list determined by the format string.
664
665 *** WARNING ***
666
667 The format should limit the total size of the formatted output string to
668 1000 bytes. In particular, this means that no unrestricted "%s" formats
669 should occur; these should be limited using "%.<N>s where <N> is a
670 decimal number calculated so that <N> plus the maximum size of other
671 formatted text does not exceed 1000 bytes. Also watch out for "%f",
672 which can print hundreds of digits for very large numbers.
673
674 */
675
676static void
677mywrite(name, fp, format, va)
678 char *name;
679 FILE *fp;
680 const char *format;
681 va_list va;
682{
683 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000684 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000685
Guido van Rossum8442af31998-10-12 18:22:10 +0000686 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000687 file = PySys_GetObject(name);
688 if (file == NULL || PyFile_AsFile(file) == fp)
689 vfprintf(fp, format, va);
690 else {
691 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000692 if (vsprintf(buffer, format, va) >= sizeof(buffer))
693 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000694 if (PyFile_WriteString(buffer, file) != 0) {
695 PyErr_Clear();
696 fputs(buffer, fp);
697 }
698 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000699 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000700}
701
702void
703#ifdef HAVE_STDARG_PROTOTYPES
704PySys_WriteStdout(const char *format, ...)
705#else
706PySys_WriteStdout(va_alist)
707 va_dcl
708#endif
709{
710 va_list va;
711
712#ifdef HAVE_STDARG_PROTOTYPES
713 va_start(va, format);
714#else
715 char *format;
716 va_start(va);
717 format = va_arg(va, char *);
718#endif
719 mywrite("stdout", stdout, format, va);
720 va_end(va);
721}
722
723void
724#ifdef HAVE_STDARG_PROTOTYPES
725PySys_WriteStderr(const char *format, ...)
726#else
727PySys_WriteStderr(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("stderr", stderr, format, va);
741 va_end(va);
742}