blob: 51a85e0a5dddf50092f23944edae2dca9542a227 [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
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000202#ifdef USE_MALLOPT
203/* Link with -lmalloc (or -lmpc) on an SGI */
204#include <malloc.h>
205
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000206static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000208{
209 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000210 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000211 return NULL;
212 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000215}
216#endif /* USE_MALLOPT */
217
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000218static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219sys_getrefcount(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000220{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000221 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000222 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000223 return NULL;
Mark Hammond440d8982000-06-20 08:12:48 +0000224 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000225}
226
Mark Hammond440d8982000-06-20 08:12:48 +0000227#ifdef Py_TRACE_REFS
228static PyObject *
229sys_gettotalrefcount(PyObject *self, PyObject *args)
230{
231 extern long _Py_RefTotal;
232 if (!PyArg_ParseTuple(args, ":gettotalrefcount"))
233 return NULL;
234 return PyInt_FromLong(_Py_RefTotal);
235}
236
237#endif /* Py_TRACE_REFS */
238
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000239static char getrefcount_doc[] =
240"getrefcount(object) -> integer\n\
241\n\
242Return the current reference count for the object. This includes the\n\
243temporary reference in the argument list, so it is at least 2.";
244
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000245#ifdef COUNT_ALLOCS
246static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247sys_getcounts(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000248{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000249 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000250
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000251 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000252 return NULL;
253 return get_counts();
254}
255#endif
256
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000257#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000258/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000259extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000260#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000261
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000262#ifdef DYNAMIC_EXECUTION_PROFILE
263/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000264extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000265#endif
266
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000267static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000268 /* Might as well keep this in alphabetic order */
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000269 {"exc_info", sys_exc_info, 1, exc_info_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000270 {"exit", sys_exit, 0, exit_doc},
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000271 {"getdefaultencoding", sys_getdefaultencoding, 1, getdefaultencoding_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000272#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000273 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000274#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000275#ifdef DYNAMIC_EXECUTION_PROFILE
276 {"getdxp", _Py_GetDXProfile, 1},
277#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000278#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000279 {"getobjects", _Py_GetObjects, 1},
Mark Hammond440d8982000-06-20 08:12:48 +0000280 {"gettotalrefcount", sys_gettotalrefcount, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000281#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000282 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000283#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000284 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000285#endif
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000286 {"setdefaultencoding", sys_setdefaultencoding, 1, setdefaultencoding_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000287 {"setcheckinterval", sys_setcheckinterval, 1, setcheckinterval_doc},
288 {"setprofile", sys_setprofile, 0, setprofile_doc},
289 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000290 {NULL, NULL} /* sentinel */
291};
292
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000293static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000295{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000296 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000297 int i;
298 if (list == NULL)
299 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000300 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000301 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000302 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000303 if (name == NULL)
304 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000305 PyList_Append(list, name);
306 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000307 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000308 if (PyList_Sort(list) != 0) {
309 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000310 list = NULL;
311 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000312 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000313 PyObject *v = PyList_AsTuple(list);
314 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000315 list = v;
316 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000317 return list;
318}
319
Guido van Rossum40552d01998-08-06 03:34:39 +0000320/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
321 Two literals concatenated works just fine. If you have a K&R compiler
322 or other abomination that however *does* understand longer strings,
323 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000324static char sys_doc[] =
325"This module provides access to some objects used or maintained by the\n\
326interpreter and to functions that interact strongly with the interpreter.\n\
327\n\
328Dynamic objects:\n\
329\n\
330argv -- command line arguments; argv[0] is the script pathname if known\n\
331path -- module search path; path[0] is the script directory, else ''\n\
332modules -- dictionary of loaded modules\n\
333exitfunc -- you may set this to a function to be called when Python exits\n\
334\n\
335stdin -- standard input file object; used by raw_input() and input()\n\
336stdout -- standard output file object; used by the print statement\n\
337stderr -- standard error object; used for error messages\n\
338 By assigning another file object (or an object that behaves like a file)\n\
339 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
340\n\
341last_type -- type of last uncaught exception\n\
342last_value -- value of last uncaught exception\n\
343last_traceback -- traceback of last uncaught exception\n\
344 These three are only available in an interactive session after a\n\
345 traceback has been printed.\n\
346\n\
347exc_type -- type of exception currently being handled\n\
348exc_value -- value of exception currently being handled\n\
349exc_traceback -- traceback of exception currently being handled\n\
350 The function exc_info() should be used instead of these three,\n\
351 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000352"
353#ifndef MS_WIN16
354/* Concatenating string here */
355"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000356Static objects:\n\
357\n\
358maxint -- the largest supported integer (the smallest is -maxint-1)\n\
359builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000360version -- the version of this interpreter as a string\n\
361version_info -- version information as a tuple\n\
362hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000363copyright -- copyright notice pertaining to this interpreter\n\
364platform -- platform identifier\n\
365executable -- pathname of this Python interpreter\n\
366prefix -- prefix used to find the Python library\n\
367exec_prefix -- prefix used to find the machine-specific Python library\n\
368dllhandle -- [Windows only] integer handle of the Python DLL\n\
369winver -- [Windows only] version number of the Python DLL\n\
370__stdin__ -- the original stdin; don't use!\n\
371__stdout__ -- the original stdout; don't use!\n\
372__stderr__ -- the original stderr; don't use!\n\
373\n\
374Functions:\n\
375\n\
376exc_info() -- return thread-safe information about the current exception\n\
377exit() -- exit the interpreter by raising SystemExit\n\
378getrefcount() -- return the reference count for an object (plus one :-)\n\
379setcheckinterval() -- control how often the interpreter checks for events\n\
380setprofile() -- set the global profiling function\n\
381settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000382"
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000383#endif
Fred Drakeccede592000-08-14 20:59:57 +0000384/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000385
Guido van Rossum25ce5661997-08-02 03:10:38 +0000386PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000389 PyObject *m, *v, *sysdict;
390 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000391 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000392
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000393 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000394 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000395
396 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
397 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
398 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000399 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000400 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000401 PyDict_SetItemString(sysdict, "stdin", sysin);
402 PyDict_SetItemString(sysdict, "stdout", sysout);
403 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000404 /* Make backup copies for cleanup */
405 PyDict_SetItemString(sysdict, "__stdin__", sysin);
406 PyDict_SetItemString(sysdict, "__stdout__", sysout);
407 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000408 Py_XDECREF(sysin);
409 Py_XDECREF(sysout);
410 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000411 PyDict_SetItemString(sysdict, "version",
412 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000413 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000414 PyDict_SetItemString(sysdict, "hexversion",
415 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000416 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000417 /*
418 * These release level checks are mutually exclusive and cover
419 * the field, so don't get too fancy with the pre-processor!
420 */
421#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000422 s = "alpha";
Fred Drake93a20bf2000-04-13 17:44:51 +0000423#endif
424#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000425 s = "beta";
Fred Drake93a20bf2000-04-13 17:44:51 +0000426#endif
427#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000428 s = "candidate";
Fred Drake93a20bf2000-04-13 17:44:51 +0000429#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000430#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000431 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000432#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000433 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000434 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000435 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000436 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000437 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000438 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000439 PyDict_SetItemString(sysdict, "copyright",
440 v = PyString_FromString(Py_GetCopyright()));
441 Py_XDECREF(v);
442 PyDict_SetItemString(sysdict, "platform",
443 v = PyString_FromString(Py_GetPlatform()));
444 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000445 PyDict_SetItemString(sysdict, "executable",
446 v = PyString_FromString(Py_GetProgramFullPath()));
447 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000448 PyDict_SetItemString(sysdict, "prefix",
449 v = PyString_FromString(Py_GetPrefix()));
450 Py_XDECREF(v);
451 PyDict_SetItemString(sysdict, "exec_prefix",
452 v = PyString_FromString(Py_GetExecPrefix()));
453 Py_XDECREF(v);
454 PyDict_SetItemString(sysdict, "maxint",
455 v = PyInt_FromLong(PyInt_GetMax()));
456 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000457 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000458 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000459 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000460 {
461 /* Assumes that longs are at least 2 bytes long.
462 Should be safe! */
463 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000464 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000465
466 s = (char *) &number;
467 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000468 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000469 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000470 value = "little";
471 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000472 v = PyString_FromString(value));
473 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000474 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000475#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000476 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000477 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000478 Py_XDECREF(v);
479 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000480 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000481 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000482#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000483 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000484 return NULL;
485 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000486}
487
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000488static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000489makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000490{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000491 int i, n;
492 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000493 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000494
495 n = 1;
496 p = path;
497 while ((p = strchr(p, delim)) != NULL) {
498 n++;
499 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000500 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000501 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000502 if (v == NULL)
503 return NULL;
504 for (i = 0; ; i++) {
505 p = strchr(path, delim);
506 if (p == NULL)
507 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000508 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000509 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000510 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000511 return NULL;
512 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000513 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000514 if (*p == '\0')
515 break;
516 path = p+1;
517 }
518 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519}
520
521void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000522PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000524 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000525 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000526 Py_FatalError("can't create sys.path");
527 if (PySys_SetObject("path", v) != 0)
528 Py_FatalError("can't assign sys.path");
529 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000530}
531
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000532static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000533makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000534{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000535 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000536 if (argc <= 0 || argv == NULL) {
537 /* Ensure at least one (empty) argument is seen */
538 static char *empty_argv[1] = {""};
539 argv = empty_argv;
540 argc = 1;
541 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000542 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000543 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000544 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000545 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000546 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000547 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000548 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000549 av = NULL;
550 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000551 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000552 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000553 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000554 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000555 return av;
556}
557
558void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000559PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000560{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000561 PyObject *av = makeargvobject(argc, argv);
562 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000563 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000564 Py_FatalError("no mem for sys.argv");
565 if (PySys_SetObject("argv", av) != 0)
566 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000567 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000568 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000569 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000570 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000571 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000572#ifdef HAVE_READLINK
573 char link[MAXPATHLEN+1];
574 char argv0copy[2*MAXPATHLEN+1];
575 int nr = 0;
576 if (argc > 0 && argv0 != NULL)
577 nr = readlink(argv0, link, MAXPATHLEN);
578 if (nr > 0) {
579 /* It's a symlink */
580 link[nr] = '\0';
581 if (link[0] == SEP)
582 argv0 = link; /* Link to absolute path */
583 else if (strchr(link, SEP) == NULL)
584 ; /* Link without path */
585 else {
586 /* Must join(dirname(argv0), link) */
587 char *q = strrchr(argv0, SEP);
588 if (q == NULL)
589 argv0 = link; /* argv0 without path */
590 else {
591 /* Must make a copy */
592 strcpy(argv0copy, argv0);
593 q = strrchr(argv0copy, SEP);
594 strcpy(q+1, link);
595 argv0 = argv0copy;
596 }
597 }
598 }
599#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000600#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000601 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000602 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000603 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000604 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000605 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000606 if (q != NULL)
607 p = q;
608 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000609 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000610 if (n > 1 && p[-1] != ':')
611 n--; /* Drop trailing separator */
612 }
613 }
614#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000615 if (argc > 0 && argv0 != NULL)
616 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000617 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000618 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000619#if SEP == '/' /* Special case for Unix filename syntax */
620 if (n > 1)
621 n--; /* Drop trailing separator */
622#endif /* Unix */
623 }
624#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000625 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000626 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000627 Py_FatalError("no mem for sys.path insertion");
628 if (PyList_Insert(path, 0, a) < 0)
629 Py_FatalError("sys.path.insert(0) failed");
630 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000631 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000632 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633}
Guido van Rossuma890e681998-05-12 14:59:24 +0000634
635
636/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
637 Adapted from code submitted by Just van Rossum.
638
639 PySys_WriteStdout(format, ...)
640 PySys_WriteStderr(format, ...)
641
642 The first function writes to sys.stdout; the second to sys.stderr. When
643 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000644 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000645
646 Both take a printf-style format string as their first argument followed
647 by a variable length argument list determined by the format string.
648
649 *** WARNING ***
650
651 The format should limit the total size of the formatted output string to
652 1000 bytes. In particular, this means that no unrestricted "%s" formats
653 should occur; these should be limited using "%.<N>s where <N> is a
654 decimal number calculated so that <N> plus the maximum size of other
655 formatted text does not exceed 1000 bytes. Also watch out for "%f",
656 which can print hundreds of digits for very large numbers.
657
658 */
659
660static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000661mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +0000662{
663 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000664 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000665
Guido van Rossum8442af31998-10-12 18:22:10 +0000666 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000667 file = PySys_GetObject(name);
668 if (file == NULL || PyFile_AsFile(file) == fp)
669 vfprintf(fp, format, va);
670 else {
671 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000672 if (vsprintf(buffer, format, va) >= sizeof(buffer))
673 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000674 if (PyFile_WriteString(buffer, file) != 0) {
675 PyErr_Clear();
676 fputs(buffer, fp);
677 }
678 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000679 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000680}
681
682void
Guido van Rossuma890e681998-05-12 14:59:24 +0000683PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000684{
685 va_list va;
686
Guido van Rossuma890e681998-05-12 14:59:24 +0000687 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000688 mywrite("stdout", stdout, format, va);
689 va_end(va);
690}
691
692void
Guido van Rossuma890e681998-05-12 14:59:24 +0000693PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000694{
695 va_list va;
696
Guido van Rossuma890e681998-05-12 14:59:24 +0000697 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000698 mywrite("stderr", stderr, format, va);
699 va_end(va);
700}