blob: fa235fd9a9d1f4530e30a3ba6aa4a82870a4c3b8 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* System module */
33
34/*
35Various bits of information used by the interpreter are collected in
36module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +000037Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +000038- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039Data members:
40- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000042- path: module search path (list of strings)
43- argv: script arguments (list of strings)
44- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045*/
46
Guido van Rossum65bf9f21997-04-29 18:33:38 +000047#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048
Guido van Rossume2437a11992-03-23 18:20:18 +000049#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000050
Guido van Rossum1254d791997-05-20 15:57:25 +000051#ifdef HAVE_UNISTD_H
Guido van Rossumc474dea1997-04-25 15:38:31 +000052#include <unistd.h>
53#endif
54
Guido van Rossum9b38a141996-09-11 23:12:24 +000055#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000056extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000057/* A string loaded from the DLL at startup: */
58extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000059#endif
60
Guido van Rossum65bf9f21997-04-29 18:33:38 +000061PyObject *
62PySys_GetObject(name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063 char *name;
64{
Guido van Rossum25ce5661997-08-02 03:10:38 +000065 PyThreadState *tstate = PyThreadState_Get();
66 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000067 if (sd == NULL)
68 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000069 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
72FILE *
Guido van Rossum65bf9f21997-04-29 18:33:38 +000073PySys_GetFile(name, def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 char *name;
75 FILE *def;
76{
77 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000078 PyObject *v = PySys_GetObject(name);
79 if (v != NULL && PyFile_Check(v))
80 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081 if (fp == NULL)
82 fp = def;
83 return fp;
84}
85
86int
Guido van Rossum65bf9f21997-04-29 18:33:38 +000087PySys_SetObject(name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 char *name;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000089 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossum25ce5661997-08-02 03:10:38 +000091 PyThreadState *tstate = PyThreadState_Get();
92 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000093 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000094 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000095 return 0;
96 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000097 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000098 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 else
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101}
102
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000103static PyObject *
Guido van Rossuma027efa1997-05-05 20:56:21 +0000104sys_exc_info(self, args)
105 PyObject *self;
106 PyObject *args;
107{
108 PyThreadState *tstate;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000109 if (!PyArg_ParseTuple(args, ":exc_info"))
Guido van Rossuma027efa1997-05-05 20:56:21 +0000110 return NULL;
111 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000112 return Py_BuildValue(
113 "(OOO)",
114 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
115 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
116 tstate->exc_traceback != NULL ?
117 tstate->exc_traceback : Py_None);
118}
119
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000120static char exc_info_doc[] =
121"exc_info() -> (type, value, traceback)\n\
122\n\
123Return information about the exception that is currently being handled.\n\
124This should be called from inside an except clause only.";
125
Guido van Rossuma027efa1997-05-05 20:56:21 +0000126static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127sys_exit(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000128 PyObject *self;
129 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000131 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000132 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000133 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000136static char exit_doc[] =
137"exit([status])\n\
138\n\
139Exit the interpreter by raising SystemExit(status).\n\
140If the status is omitted or None, it defaults to zero (i.e., success).\n\
141If the status numeric, it will be used as the system exit status.\n\
142If it is another kind of object, it will be printed and the system\n\
143exit status will be one (i.e., failure).";
144
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000145static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000146sys_settrace(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000147 PyObject *self;
148 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000149{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000150 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000151 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000152 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000153 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000154 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000155 Py_XDECREF(tstate->sys_tracefunc);
156 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000157 Py_INCREF(Py_None);
158 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000159}
160
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000161static char settrace_doc[] =
162"settrace(function)\n\
163\n\
164Set the global debug tracing function. It will be called on each\n\
165function call. See the debugger chapter in the library manual.";
166
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000167static PyObject *
Guido van Rossume2437a11992-03-23 18:20:18 +0000168sys_setprofile(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000169 PyObject *self;
170 PyObject *args;
Guido van Rossume2437a11992-03-23 18:20:18 +0000171{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000172 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000173 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000174 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000175 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000176 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177 Py_XDECREF(tstate->sys_profilefunc);
178 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000179 Py_INCREF(Py_None);
180 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000181}
182
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000183static char setprofile_doc[] =
184"setprofile(function)\n\
185\n\
186Set the profiling function. It will be called on each function call\n\
187and return. See the profiler chapter in the library manual.";
188
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000189static PyObject *
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000190sys_setcheckinterval(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000191 PyObject *self;
192 PyObject *args;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000193{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000195 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000196 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000197 Py_INCREF(Py_None);
198 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000199}
200
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000201static char setcheckinterval_doc[] =
202"setcheckinterval(n)\n\
203\n\
204Tell the Python interpreter to check for asynchronous events every\n\
205n instructions. This also affects how often thread switches occur.";
206
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000207#ifdef USE_MALLOPT
208/* Link with -lmalloc (or -lmpc) on an SGI */
209#include <malloc.h>
210
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000211static PyObject *
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000212sys_mdebug(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000213 PyObject *self;
214 PyObject *args;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000215{
216 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000217 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000218 return NULL;
219 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000220 Py_INCREF(Py_None);
221 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000222}
223#endif /* USE_MALLOPT */
224
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000225static PyObject *
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000226sys_getrefcount(self, args)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000227 PyObject *self;
228 PyObject *args;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000229{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000230 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000231 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000232 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000233 return PyInt_FromLong((long) arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000234}
235
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000236static char getrefcount_doc[] =
237"getrefcount(object) -> integer\n\
238\n\
239Return the current reference count for the object. This includes the\n\
240temporary reference in the argument list, so it is at least 2.";
241
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000242#ifdef COUNT_ALLOCS
243static PyObject *
244sys_getcounts(self, args)
245 PyObject *self, *args;
246{
247 extern PyObject *get_counts Py_PROTO((void));
248
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000249 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000250 return NULL;
251 return get_counts();
252}
253#endif
254
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000255#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000256/* Defined in objects.c because it uses static globals if that file */
257extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000258#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000259
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000260#ifdef DYNAMIC_EXECUTION_PROFILE
261/* Defined in ceval.c because it uses static globals if that file */
262extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *, PyObject *));
263#endif
264
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000265static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000266 /* Might as well keep this in alphabetic order */
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000267 {"exc_info", sys_exc_info, 1, exc_info_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000268 {"exit", sys_exit, 0, exit_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000269#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000270 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000271#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000272#ifdef DYNAMIC_EXECUTION_PROFILE
273 {"getdxp", _Py_GetDXProfile, 1},
274#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000275#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000276 {"getobjects", _Py_GetObjects, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000277#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000278 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000279#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000280 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000281#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000282 {"setcheckinterval", sys_setcheckinterval, 1, setcheckinterval_doc},
283 {"setprofile", sys_setprofile, 0, setprofile_doc},
284 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000285 {NULL, NULL} /* sentinel */
286};
287
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000288static PyObject *
Guido van Rossum34679b71993-01-26 13:33:44 +0000289list_builtin_module_names()
290{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000291 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000292 int i;
293 if (list == NULL)
294 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000295 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000296 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000297 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000298 if (name == NULL)
299 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000300 PyList_Append(list, name);
301 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000302 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000303 if (PyList_Sort(list) != 0) {
304 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000305 list = NULL;
306 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000307 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000308 PyObject *v = PyList_AsTuple(list);
309 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000310 list = v;
311 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000312 return list;
313}
314
Guido van Rossum40552d01998-08-06 03:34:39 +0000315/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
316 Two literals concatenated works just fine. If you have a K&R compiler
317 or other abomination that however *does* understand longer strings,
318 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000319static char sys_doc[] =
320"This module provides access to some objects used or maintained by the\n\
321interpreter and to functions that interact strongly with the interpreter.\n\
322\n\
323Dynamic objects:\n\
324\n\
325argv -- command line arguments; argv[0] is the script pathname if known\n\
326path -- module search path; path[0] is the script directory, else ''\n\
327modules -- dictionary of loaded modules\n\
328exitfunc -- you may set this to a function to be called when Python exits\n\
329\n\
330stdin -- standard input file object; used by raw_input() and input()\n\
331stdout -- standard output file object; used by the print statement\n\
332stderr -- standard error object; used for error messages\n\
333 By assigning another file object (or an object that behaves like a file)\n\
334 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
335\n\
336last_type -- type of last uncaught exception\n\
337last_value -- value of last uncaught exception\n\
338last_traceback -- traceback of last uncaught exception\n\
339 These three are only available in an interactive session after a\n\
340 traceback has been printed.\n\
341\n\
342exc_type -- type of exception currently being handled\n\
343exc_value -- value of exception currently being handled\n\
344exc_traceback -- traceback of exception currently being handled\n\
345 The function exc_info() should be used instead of these three,\n\
346 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000347"
348#ifndef MS_WIN16
349/* Concatenating string here */
350"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000351Static objects:\n\
352\n\
353maxint -- the largest supported integer (the smallest is -maxint-1)\n\
354builtin_module_names -- tuple of module names built into this intepreter\n\
355version -- the version of this interpreter\n\
356copyright -- copyright notice pertaining to this interpreter\n\
357platform -- platform identifier\n\
358executable -- pathname of this Python interpreter\n\
359prefix -- prefix used to find the Python library\n\
360exec_prefix -- prefix used to find the machine-specific Python library\n\
361dllhandle -- [Windows only] integer handle of the Python DLL\n\
362winver -- [Windows only] version number of the Python DLL\n\
363__stdin__ -- the original stdin; don't use!\n\
364__stdout__ -- the original stdout; don't use!\n\
365__stderr__ -- the original stderr; don't use!\n\
366\n\
367Functions:\n\
368\n\
369exc_info() -- return thread-safe information about the current exception\n\
370exit() -- exit the interpreter by raising SystemExit\n\
371getrefcount() -- return the reference count for an object (plus one :-)\n\
372setcheckinterval() -- control how often the interpreter checks for events\n\
373setprofile() -- set the global profiling function\n\
374settrace() -- set the global debug tracing function\n\
375";
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000376#endif
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000377
Guido van Rossum25ce5661997-08-02 03:10:38 +0000378PyObject *
379_PySys_Init()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000381 extern int fclose Py_PROTO((FILE *));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000382 PyObject *m, *v, *sysdict;
383 PyObject *sysin, *sysout, *syserr;
384
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000385 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000386 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000387
388 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
389 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
390 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000391 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000392 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000393 PyDict_SetItemString(sysdict, "stdin", sysin);
394 PyDict_SetItemString(sysdict, "stdout", sysout);
395 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000396 /* Make backup copies for cleanup */
397 PyDict_SetItemString(sysdict, "__stdin__", sysin);
398 PyDict_SetItemString(sysdict, "__stdout__", sysout);
399 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000400 Py_XDECREF(sysin);
401 Py_XDECREF(sysout);
402 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000403 PyDict_SetItemString(sysdict, "version",
404 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000405 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000406 PyDict_SetItemString(sysdict, "hexversion",
407 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000408 Py_XDECREF(v);
409 PyDict_SetItemString(sysdict, "copyright",
410 v = PyString_FromString(Py_GetCopyright()));
411 Py_XDECREF(v);
412 PyDict_SetItemString(sysdict, "platform",
413 v = PyString_FromString(Py_GetPlatform()));
414 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000415 PyDict_SetItemString(sysdict, "executable",
416 v = PyString_FromString(Py_GetProgramFullPath()));
417 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000418 PyDict_SetItemString(sysdict, "prefix",
419 v = PyString_FromString(Py_GetPrefix()));
420 Py_XDECREF(v);
421 PyDict_SetItemString(sysdict, "exec_prefix",
422 v = PyString_FromString(Py_GetExecPrefix()));
423 Py_XDECREF(v);
424 PyDict_SetItemString(sysdict, "maxint",
425 v = PyInt_FromLong(PyInt_GetMax()));
426 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000427 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000428 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000429 Py_XDECREF(v);
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000430#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000431 PyDict_SetItemString(sysdict, "dllhandle",
432 v = PyInt_FromLong((int)PyWin_DLLhModule));
433 Py_XDECREF(v);
434 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000435 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000436 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000437#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000438 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000439 return NULL;
440 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000441}
442
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000443static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000444makepathobject(path, delim)
445 char *path;
446 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000447{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000448 int i, n;
449 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000450 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000451
452 n = 1;
453 p = path;
454 while ((p = strchr(p, delim)) != NULL) {
455 n++;
456 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000457 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000458 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000459 if (v == NULL)
460 return NULL;
461 for (i = 0; ; i++) {
462 p = strchr(path, delim);
463 if (p == NULL)
464 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000465 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000466 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000467 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000468 return NULL;
469 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000470 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000471 if (*p == '\0')
472 break;
473 path = p+1;
474 }
475 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476}
477
478void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000479PySys_SetPath(path)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000480 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000482 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000483 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000484 Py_FatalError("can't create sys.path");
485 if (PySys_SetObject("path", v) != 0)
486 Py_FatalError("can't assign sys.path");
487 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000488}
489
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000490static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000491makeargvobject(argc, argv)
492 int argc;
493 char **argv;
494{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000495 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000496 if (argc <= 0 || argv == NULL) {
497 /* Ensure at least one (empty) argument is seen */
498 static char *empty_argv[1] = {""};
499 argv = empty_argv;
500 argc = 1;
501 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000502 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000503 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000504 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000505 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000506 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000507 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000508 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000509 av = NULL;
510 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000511 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000512 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000513 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000514 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000515 return av;
516}
517
518void
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000519PySys_SetArgv(argc, argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000520 int argc;
521 char **argv;
522{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000523 PyObject *av = makeargvobject(argc, argv);
524 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000525 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000526 Py_FatalError("no mem for sys.argv");
527 if (PySys_SetObject("argv", av) != 0)
528 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000529 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000530 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000531 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000532 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000533 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000534#ifdef HAVE_READLINK
535 char link[MAXPATHLEN+1];
536 char argv0copy[2*MAXPATHLEN+1];
537 int nr = 0;
538 if (argc > 0 && argv0 != NULL)
539 nr = readlink(argv0, link, MAXPATHLEN);
540 if (nr > 0) {
541 /* It's a symlink */
542 link[nr] = '\0';
543 if (link[0] == SEP)
544 argv0 = link; /* Link to absolute path */
545 else if (strchr(link, SEP) == NULL)
546 ; /* Link without path */
547 else {
548 /* Must join(dirname(argv0), link) */
549 char *q = strrchr(argv0, SEP);
550 if (q == NULL)
551 argv0 = link; /* argv0 without path */
552 else {
553 /* Must make a copy */
554 strcpy(argv0copy, argv0);
555 q = strrchr(argv0copy, SEP);
556 strcpy(q+1, link);
557 argv0 = argv0copy;
558 }
559 }
560 }
561#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000562#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000563 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000564 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000565 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000566 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000567 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000568 if (q != NULL)
569 p = q;
570 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000571 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000572 if (n > 1 && p[-1] != ':')
573 n--; /* Drop trailing separator */
574 }
575 }
576#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000577 if (argc > 0 && argv0 != NULL)
578 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000579 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000580 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000581#if SEP == '/' /* Special case for Unix filename syntax */
582 if (n > 1)
583 n--; /* Drop trailing separator */
584#endif /* Unix */
585 }
586#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000587 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000588 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000589 Py_FatalError("no mem for sys.path insertion");
590 if (PyList_Insert(path, 0, a) < 0)
591 Py_FatalError("sys.path.insert(0) failed");
592 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000593 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000594 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595}
Guido van Rossuma890e681998-05-12 14:59:24 +0000596
597
598/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
599 Adapted from code submitted by Just van Rossum.
600
601 PySys_WriteStdout(format, ...)
602 PySys_WriteStderr(format, ...)
603
604 The first function writes to sys.stdout; the second to sys.stderr. When
605 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000606 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000607
608 Both take a printf-style format string as their first argument followed
609 by a variable length argument list determined by the format string.
610
611 *** WARNING ***
612
613 The format should limit the total size of the formatted output string to
614 1000 bytes. In particular, this means that no unrestricted "%s" formats
615 should occur; these should be limited using "%.<N>s where <N> is a
616 decimal number calculated so that <N> plus the maximum size of other
617 formatted text does not exceed 1000 bytes. Also watch out for "%f",
618 which can print hundreds of digits for very large numbers.
619
620 */
621
622static void
623mywrite(name, fp, format, va)
624 char *name;
625 FILE *fp;
626 const char *format;
627 va_list va;
628{
629 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000630 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000631
Guido van Rossum8442af31998-10-12 18:22:10 +0000632 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000633 file = PySys_GetObject(name);
634 if (file == NULL || PyFile_AsFile(file) == fp)
635 vfprintf(fp, format, va);
636 else {
637 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000638 if (vsprintf(buffer, format, va) >= sizeof(buffer))
639 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000640 if (PyFile_WriteString(buffer, file) != 0) {
641 PyErr_Clear();
642 fputs(buffer, fp);
643 }
644 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000645 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000646}
647
648void
649#ifdef HAVE_STDARG_PROTOTYPES
650PySys_WriteStdout(const char *format, ...)
651#else
652PySys_WriteStdout(va_alist)
653 va_dcl
654#endif
655{
656 va_list va;
657
658#ifdef HAVE_STDARG_PROTOTYPES
659 va_start(va, format);
660#else
661 char *format;
662 va_start(va);
663 format = va_arg(va, char *);
664#endif
665 mywrite("stdout", stdout, format, va);
666 va_end(va);
667}
668
669void
670#ifdef HAVE_STDARG_PROTOTYPES
671PySys_WriteStderr(const char *format, ...)
672#else
673PySys_WriteStderr(va_alist)
674 va_dcl
675#endif
676{
677 va_list va;
678
679#ifdef HAVE_STDARG_PROTOTYPES
680 va_start(va, format);
681#else
682 char *format;
683 va_start(va);
684 format = va_arg(va, char *);
685#endif
686 mywrite("stderr", stderr, format, va);
687 va_end(va);
688}