blob: 62e08414d8c9d93f3ce2977032ab89d87d27ffbb [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* System module */
3
4/*
5Various bits of information used by the interpreter are collected in
6module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +00007Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +00008- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009Data members:
10- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000012- path: module search path (list of strings)
13- argv: script arguments (list of strings)
14- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015*/
16
Guido van Rossum65bf9f21997-04-29 18:33:38 +000017#include "Python.h"
Barry Warsawb6a54d22000-12-06 21:47:46 +000018#include "compile.h"
19#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossume2437a11992-03-23 18:20:18 +000021#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
Guido van Rossum1254d791997-05-20 15:57:25 +000023#ifdef HAVE_UNISTD_H
Guido van Rossumc474dea1997-04-25 15:38:31 +000024#include <unistd.h>
25#endif
26
Guido van Rossum9b38a141996-09-11 23:12:24 +000027#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000028extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000029/* A string loaded from the DLL at startup: */
30extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000031#endif
32
Guido van Rossum65bf9f21997-04-29 18:33:38 +000033PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000034PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035{
Guido van Rossum25ce5661997-08-02 03:10:38 +000036 PyThreadState *tstate = PyThreadState_Get();
37 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000038 if (sd == NULL)
39 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000040 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041}
42
43FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000044PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045{
46 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000047 PyObject *v = PySys_GetObject(name);
48 if (v != NULL && PyFile_Check(v))
49 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050 if (fp == NULL)
51 fp = def;
52 return fp;
53}
54
55int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000056PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Guido van Rossum25ce5661997-08-02 03:10:38 +000058 PyThreadState *tstate = PyThreadState_Get();
59 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000060 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000061 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000062 return 0;
63 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000064 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000065 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000067 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossum65bf9f21997-04-29 18:33:38 +000070static PyObject *
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000071sys_displayhook(PyObject *self, PyObject *args)
72{
Greg Steinceb9b7c2001-01-11 09:27:34 +000073 PyObject *o, *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000074 PyInterpreterState *interp = PyThreadState_Get()->interp;
75 PyObject *modules = interp->modules;
76 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
77
78 /* parse arguments */
79 if (!PyArg_ParseTuple(args, "O:displayhook", &o))
80 return NULL;
81
82 /* Print value except if None */
83 /* After printing, also assign to '_' */
84 /* Before, set '_' to None to avoid recursion */
85 if (o == Py_None) {
86 Py_INCREF(Py_None);
87 return Py_None;
88 }
89 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
90 return NULL;
91 if (Py_FlushLine() != 0)
92 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000093 outf = PySys_GetObject("stdout");
94 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000095 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
96 return NULL;
97 }
Greg Steinceb9b7c2001-01-11 09:27:34 +000098 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000099 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000100 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000101 if (Py_FlushLine() != 0)
102 return NULL;
103 if (PyObject_SetAttrString(builtins, "_", o) != 0)
104 return NULL;
105 Py_INCREF(Py_None);
106 return Py_None;
107}
108
109static char displayhook_doc[] =
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000110"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000111"\n"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000112"Print an object to sys.stdout and also save it in __builtin__._\n";
113
114static PyObject *
115sys_excepthook(PyObject* self, PyObject* args)
116{
117 PyObject *exc, *value, *tb;
118 if (!PyArg_ParseTuple(args, "OOO:excepthook", &exc, &value, &tb))
119 return NULL;
120 PyErr_Display(exc, value, tb);
121 Py_INCREF(Py_None);
122 return Py_None;
123}
124
125static char excepthook_doc[] =
126"excepthook(exctype, value, traceback) -> None\n"
127"\n"
128"Handle an exception by displaying it with a traceback on sys.stderr.\n";
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000129
130static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131sys_exc_info(PyObject *self, PyObject *args)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132{
133 PyThreadState *tstate;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000134 if (!PyArg_ParseTuple(args, ":exc_info"))
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135 return NULL;
136 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137 return Py_BuildValue(
138 "(OOO)",
139 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
140 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
141 tstate->exc_traceback != NULL ?
142 tstate->exc_traceback : Py_None);
143}
144
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000145static char exc_info_doc[] =
146"exc_info() -> (type, value, traceback)\n\
147\n\
148Return information about the exception that is currently being handled.\n\
149This should be called from inside an except clause only.";
150
Guido van Rossuma027efa1997-05-05 20:56:21 +0000151static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000152sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000154 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000155 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000156 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157}
158
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000159static char exit_doc[] =
160"exit([status])\n\
161\n\
162Exit the interpreter by raising SystemExit(status).\n\
163If the status is omitted or None, it defaults to zero (i.e., success).\n\
164If the status numeric, it will be used as the system exit status.\n\
165If it is another kind of object, it will be printed and the system\n\
166exit status will be one (i.e., failure).";
167
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000168static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000169sys_getdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000170{
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000171 if (!PyArg_ParseTuple(args, ":getdefaultencoding"))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000172 return NULL;
173 return PyString_FromString(PyUnicode_GetDefaultEncoding());
174}
175
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000176static char getdefaultencoding_doc[] =
177"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000178\n\
179Return the current default string encoding used by the Unicode \n\
180implementation.";
181
182static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000183sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000184{
185 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000186 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000187 return NULL;
188 if (PyUnicode_SetDefaultEncoding(encoding))
189 return NULL;
190 Py_INCREF(Py_None);
191 return Py_None;
192}
193
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000194static char setdefaultencoding_doc[] =
195"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000196\n\
197Set the current default string encoding used by the Unicode implementation.";
198
Fred Draked0838392001-06-16 21:02:31 +0000199extern int _PyTrace_Init(void);
200
Fred Drake8b4d01d2000-05-09 19:57:01 +0000201static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000203{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000204 PyThreadState *tstate = PyThreadState_Get();
Fred Draked0838392001-06-16 21:02:31 +0000205 if (_PyTrace_Init() == -1)
206 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000207 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000208 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000209 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000210 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000211 Py_XDECREF(tstate->sys_tracefunc);
212 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000215}
216
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000217static char settrace_doc[] =
218"settrace(function)\n\
219\n\
220Set the global debug tracing function. It will be called on each\n\
221function call. See the debugger chapter in the library manual.";
222
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000223static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000225{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000226 PyThreadState *tstate = PyThreadState_Get();
Fred Draked0838392001-06-16 21:02:31 +0000227 if (_PyTrace_Init() == -1)
228 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000229 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000230 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000231 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000232 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000233 Py_XDECREF(tstate->sys_profilefunc);
234 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000235 Py_INCREF(Py_None);
236 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000237}
238
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000239static char setprofile_doc[] =
240"setprofile(function)\n\
241\n\
242Set the profiling function. It will be called on each function call\n\
243and return. See the profiler chapter in the library manual.";
244
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000245static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000247{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000248 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000249 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000250 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000251 Py_INCREF(Py_None);
252 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000253}
254
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000255static char setcheckinterval_doc[] =
256"setcheckinterval(n)\n\
257\n\
258Tell the Python interpreter to check for asynchronous events every\n\
259n instructions. This also affects how often thread switches occur.";
260
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000261static PyObject *
262sys_setrecursionlimit(PyObject *self, PyObject *args)
263{
264 int new_limit;
265 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
266 return NULL;
267 if (new_limit <= 0) {
268 PyErr_SetString(PyExc_ValueError,
269 "recursion limit must be positive");
270 return NULL;
271 }
272 Py_SetRecursionLimit(new_limit);
273 Py_INCREF(Py_None);
274 return Py_None;
275}
276
277static char setrecursionlimit_doc[] =
278"setrecursionlimit(n)\n\
279\n\
280Set the maximum depth of the Python interpreter stack to n. This\n\
281limit prevents infinite recursion from causing an overflow of the C\n\
282stack and crashing Python. The highest possible limit is platform-\n\
283dependent.";
284
285static PyObject *
286sys_getrecursionlimit(PyObject *self, PyObject *args)
287{
288 if (!PyArg_ParseTuple(args, ":getrecursionlimit"))
289 return NULL;
290 return PyInt_FromLong(Py_GetRecursionLimit());
291}
292
293static char getrecursionlimit_doc[] =
294"getrecursionlimit()\n\
295\n\
296Return the current value of the recursion limit, the maximum depth\n\
297of the Python interpreter stack. This limit prevents infinite\n\
298recursion from causing an overflow of the C stack and crashing Python.";
299
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000300#ifdef USE_MALLOPT
301/* Link with -lmalloc (or -lmpc) on an SGI */
302#include <malloc.h>
303
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000304static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000306{
307 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000308 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000309 return NULL;
310 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000311 Py_INCREF(Py_None);
312 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000313}
314#endif /* USE_MALLOPT */
315
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000316static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000317sys_getrefcount(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000318{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000319 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000320 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000321 return NULL;
Mark Hammond440d8982000-06-20 08:12:48 +0000322 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000323}
324
Mark Hammond440d8982000-06-20 08:12:48 +0000325#ifdef Py_TRACE_REFS
326static PyObject *
327sys_gettotalrefcount(PyObject *self, PyObject *args)
328{
329 extern long _Py_RefTotal;
330 if (!PyArg_ParseTuple(args, ":gettotalrefcount"))
331 return NULL;
332 return PyInt_FromLong(_Py_RefTotal);
333}
334
335#endif /* Py_TRACE_REFS */
336
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000337static char getrefcount_doc[] =
338"getrefcount(object) -> integer\n\
339\n\
340Return the current reference count for the object. This includes the\n\
341temporary reference in the argument list, so it is at least 2.";
342
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000343#ifdef COUNT_ALLOCS
344static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345sys_getcounts(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000346{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000347 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000348
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000349 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000350 return NULL;
351 return get_counts();
352}
353#endif
354
Barry Warsawb6a54d22000-12-06 21:47:46 +0000355static char getframe_doc[] =
356"_getframe([depth]) -> frameobject\n\
357\n\
358Return a frame object from the call stack. If optional integer depth is\n\
359given, return the frame object that many calls below the top of the stack.\n\
360If that is deeper than the call stack, ValueError is raised. The default\n\
361for depth is zero, returning the frame at the top of the call stack.\n\
362\n\
363This function should be used for internal and specialized\n\
364purposes only.";
365
366static PyObject *
367sys_getframe(PyObject *self, PyObject *args)
368{
369 PyFrameObject *f = PyThreadState_Get()->frame;
370 int depth = -1;
371
372 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
373 return NULL;
374
375 while (depth > 0 && f != NULL) {
376 f = f->f_back;
377 --depth;
378 }
379 if (f == NULL) {
380 PyErr_SetString(PyExc_ValueError,
381 "call stack is not deep enough");
382 return NULL;
383 }
384 Py_INCREF(f);
385 return (PyObject*)f;
386}
387
388
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000389#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000390/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000391extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000392#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000393
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000394#ifdef DYNAMIC_EXECUTION_PROFILE
395/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000396extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000397#endif
398
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000399static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000400 /* Might as well keep this in alphabetic order */
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000401 {"displayhook", sys_displayhook, 1, displayhook_doc},
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000402 {"exc_info", sys_exc_info, 1, exc_info_doc},
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000403 {"excepthook", sys_excepthook, 1, excepthook_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000404 {"exit", sys_exit, 0, exit_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000405 {"getdefaultencoding", sys_getdefaultencoding, 1,
406 getdefaultencoding_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000407#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000408 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000409#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000410#ifdef DYNAMIC_EXECUTION_PROFILE
411 {"getdxp", _Py_GetDXProfile, 1},
412#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000413#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000414 {"getobjects", _Py_GetObjects, 1},
Mark Hammond440d8982000-06-20 08:12:48 +0000415 {"gettotalrefcount", sys_gettotalrefcount, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000416#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000417 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000418 {"getrecursionlimit", sys_getrecursionlimit, 1,
419 getrecursionlimit_doc},
Barry Warsawb6a54d22000-12-06 21:47:46 +0000420 {"_getframe", sys_getframe, 1, getframe_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000421#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000422 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000423#endif
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000424 {"setdefaultencoding", sys_setdefaultencoding, 1,
425 setdefaultencoding_doc},
426 {"setcheckinterval", sys_setcheckinterval, 1,
427 setcheckinterval_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000428 {"setprofile", sys_setprofile, 0, setprofile_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000429 {"setrecursionlimit", sys_setrecursionlimit, 1,
430 setrecursionlimit_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000431 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000432 {NULL, NULL} /* sentinel */
433};
434
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000435static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000437{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000438 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000439 int i;
440 if (list == NULL)
441 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000442 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000443 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000444 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000445 if (name == NULL)
446 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000447 PyList_Append(list, name);
448 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000449 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000450 if (PyList_Sort(list) != 0) {
451 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000452 list = NULL;
453 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000454 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000455 PyObject *v = PyList_AsTuple(list);
456 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000457 list = v;
458 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000459 return list;
460}
461
Guido van Rossum23fff912000-12-15 22:02:05 +0000462static PyObject *warnoptions = NULL;
463
464void
465PySys_ResetWarnOptions(void)
466{
467 if (warnoptions == NULL || !PyList_Check(warnoptions))
468 return;
469 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
470}
471
472void
473PySys_AddWarnOption(char *s)
474{
475 PyObject *str;
476
477 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
478 Py_XDECREF(warnoptions);
479 warnoptions = PyList_New(0);
480 if (warnoptions == NULL)
481 return;
482 }
483 str = PyString_FromString(s);
484 if (str != NULL) {
485 PyList_Append(warnoptions, str);
486 Py_DECREF(str);
487 }
488}
489
Guido van Rossum40552d01998-08-06 03:34:39 +0000490/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
491 Two literals concatenated works just fine. If you have a K&R compiler
492 or other abomination that however *does* understand longer strings,
493 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000494static char sys_doc[] =
495"This module provides access to some objects used or maintained by the\n\
496interpreter and to functions that interact strongly with the interpreter.\n\
497\n\
498Dynamic objects:\n\
499\n\
500argv -- command line arguments; argv[0] is the script pathname if known\n\
501path -- module search path; path[0] is the script directory, else ''\n\
502modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000503\n\
504displayhook -- called to show results in an interactive session\n\
505excepthook -- called to handle any uncaught exception other than SystemExit\n\
506 To customize printing in an interactive session or to install a custom\n\
507 top-level exception handler, assign other functions to replace these.\n\
508\n\
509exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
510 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000511\n\
512stdin -- standard input file object; used by raw_input() and input()\n\
513stdout -- standard output file object; used by the print statement\n\
514stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000515 By assigning other file objects (or objects that behave like files)\n\
516 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000517\n\
518last_type -- type of last uncaught exception\n\
519last_value -- value of last uncaught exception\n\
520last_traceback -- traceback of last uncaught exception\n\
521 These three are only available in an interactive session after a\n\
522 traceback has been printed.\n\
523\n\
524exc_type -- type of exception currently being handled\n\
525exc_value -- value of exception currently being handled\n\
526exc_traceback -- traceback of exception currently being handled\n\
527 The function exc_info() should be used instead of these three,\n\
528 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000529"
530#ifndef MS_WIN16
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000531/* concatenating string here */
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000532"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000533Static objects:\n\
534\n\
535maxint -- the largest supported integer (the smallest is -maxint-1)\n\
536builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000537version -- the version of this interpreter as a string\n\
538version_info -- version information as a tuple\n\
539hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000540copyright -- copyright notice pertaining to this interpreter\n\
541platform -- platform identifier\n\
542executable -- pathname of this Python interpreter\n\
543prefix -- prefix used to find the Python library\n\
544exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000545"
546#ifdef MS_WINDOWS
547/* concatenating string here */
548"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000549winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000550"
551#endif /* MS_WINDOWS */
552"__stdin__ -- the original stdin; don't touch!\n\
553__stdout__ -- the original stdout; don't touch!\n\
554__stderr__ -- the original stderr; don't touch!\n\
555__displayhook__ -- the original displayhook; don't touch!\n\
556__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000557\n\
558Functions:\n\
559\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000560displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000561excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000562exc_info() -- return thread-safe information about the current exception\n\
563exit() -- exit the interpreter by raising SystemExit\n\
564getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000565getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000566setcheckinterval() -- control how often the interpreter checks for events\n\
567setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000569settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000570"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000571#endif /* MS_WIN16 */
Fred Drakeccede592000-08-14 20:59:57 +0000572/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000573
Guido van Rossum25ce5661997-08-02 03:10:38 +0000574PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000575_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000577 PyObject *m, *v, *sysdict;
578 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000579 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000580
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000581 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000582 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000583
584 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
585 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
586 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000587 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000589 PyDict_SetItemString(sysdict, "stdin", sysin);
590 PyDict_SetItemString(sysdict, "stdout", sysout);
591 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000592 /* Make backup copies for cleanup */
593 PyDict_SetItemString(sysdict, "__stdin__", sysin);
594 PyDict_SetItemString(sysdict, "__stdout__", sysout);
595 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000596 PyDict_SetItemString(sysdict, "__displayhook__",
597 PyDict_GetItemString(sysdict, "displayhook"));
598 PyDict_SetItemString(sysdict, "__excepthook__",
599 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000600 Py_XDECREF(sysin);
601 Py_XDECREF(sysout);
602 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000603 PyDict_SetItemString(sysdict, "version",
604 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000605 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000606 PyDict_SetItemString(sysdict, "hexversion",
607 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000608 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000609 /*
610 * These release level checks are mutually exclusive and cover
611 * the field, so don't get too fancy with the pre-processor!
612 */
613#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000614 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000615#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000616 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000617#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000618 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000619#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000620 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000621#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000622 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000623 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000624 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000625 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000626 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000627 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000628 PyDict_SetItemString(sysdict, "copyright",
629 v = PyString_FromString(Py_GetCopyright()));
630 Py_XDECREF(v);
631 PyDict_SetItemString(sysdict, "platform",
632 v = PyString_FromString(Py_GetPlatform()));
633 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000634 PyDict_SetItemString(sysdict, "executable",
635 v = PyString_FromString(Py_GetProgramFullPath()));
636 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000637 PyDict_SetItemString(sysdict, "prefix",
638 v = PyString_FromString(Py_GetPrefix()));
639 Py_XDECREF(v);
640 PyDict_SetItemString(sysdict, "exec_prefix",
641 v = PyString_FromString(Py_GetExecPrefix()));
642 Py_XDECREF(v);
643 PyDict_SetItemString(sysdict, "maxint",
644 v = PyInt_FromLong(PyInt_GetMax()));
645 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000646 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000648 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000649 {
650 /* Assumes that longs are at least 2 bytes long.
651 Should be safe! */
652 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000653 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000654
655 s = (char *) &number;
656 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000657 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000658 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000659 value = "little";
660 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000661 v = PyString_FromString(value));
662 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000663 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000664#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000665 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000666 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000667 Py_XDECREF(v);
668 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000669 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000670 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000671#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000672 if (warnoptions == NULL) {
673 warnoptions = PyList_New(0);
674 }
675 else {
676 Py_INCREF(warnoptions);
677 }
678 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000679 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000680 }
681
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000682 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000683 return NULL;
684 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000685}
686
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000687static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000688makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000689{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000690 int i, n;
691 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000692 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000693
694 n = 1;
695 p = path;
696 while ((p = strchr(p, delim)) != NULL) {
697 n++;
698 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000699 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000700 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000701 if (v == NULL)
702 return NULL;
703 for (i = 0; ; i++) {
704 p = strchr(path, delim);
705 if (p == NULL)
706 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000707 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000708 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000709 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000710 return NULL;
711 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000712 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000713 if (*p == '\0')
714 break;
715 path = p+1;
716 }
717 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718}
719
720void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000721PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000723 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000724 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000725 Py_FatalError("can't create sys.path");
726 if (PySys_SetObject("path", v) != 0)
727 Py_FatalError("can't assign sys.path");
728 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000729}
730
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000731static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000732makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000733{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000734 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000735 if (argc <= 0 || argv == NULL) {
736 /* Ensure at least one (empty) argument is seen */
737 static char *empty_argv[1] = {""};
738 argv = empty_argv;
739 argc = 1;
740 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000741 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000742 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000743 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000744 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000745 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000746 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000747 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000748 av = NULL;
749 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000750 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000751 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000752 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000753 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000754 return av;
755}
756
757void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000758PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000759{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000760 PyObject *av = makeargvobject(argc, argv);
761 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000762 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000763 Py_FatalError("no mem for sys.argv");
764 if (PySys_SetObject("argv", av) != 0)
765 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000766 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000767 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000768 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000769 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000770 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000771#ifdef HAVE_READLINK
772 char link[MAXPATHLEN+1];
773 char argv0copy[2*MAXPATHLEN+1];
774 int nr = 0;
775 if (argc > 0 && argv0 != NULL)
776 nr = readlink(argv0, link, MAXPATHLEN);
777 if (nr > 0) {
778 /* It's a symlink */
779 link[nr] = '\0';
780 if (link[0] == SEP)
781 argv0 = link; /* Link to absolute path */
782 else if (strchr(link, SEP) == NULL)
783 ; /* Link without path */
784 else {
785 /* Must join(dirname(argv0), link) */
786 char *q = strrchr(argv0, SEP);
787 if (q == NULL)
788 argv0 = link; /* argv0 without path */
789 else {
790 /* Must make a copy */
791 strcpy(argv0copy, argv0);
792 q = strrchr(argv0copy, SEP);
793 strcpy(q+1, link);
794 argv0 = argv0copy;
795 }
796 }
797 }
798#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000799#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000800 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000801 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000802 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000803 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000804 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000805 if (q != NULL)
806 p = q;
807 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000808 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000809 if (n > 1 && p[-1] != ':')
810 n--; /* Drop trailing separator */
811 }
812 }
813#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000814 if (argc > 0 && argv0 != NULL)
815 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000816 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +0000817#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +0000818 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +0000819#else /* don't include trailing separator */
820 n = p - argv0;
821#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +0000822#if SEP == '/' /* Special case for Unix filename syntax */
823 if (n > 1)
824 n--; /* Drop trailing separator */
825#endif /* Unix */
826 }
827#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000828 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000829 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000830 Py_FatalError("no mem for sys.path insertion");
831 if (PyList_Insert(path, 0, a) < 0)
832 Py_FatalError("sys.path.insert(0) failed");
833 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000834 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000835 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836}
Guido van Rossuma890e681998-05-12 14:59:24 +0000837
838
839/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
840 Adapted from code submitted by Just van Rossum.
841
842 PySys_WriteStdout(format, ...)
843 PySys_WriteStderr(format, ...)
844
845 The first function writes to sys.stdout; the second to sys.stderr. When
846 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000847 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000848
849 Both take a printf-style format string as their first argument followed
850 by a variable length argument list determined by the format string.
851
852 *** WARNING ***
853
854 The format should limit the total size of the formatted output string to
855 1000 bytes. In particular, this means that no unrestricted "%s" formats
856 should occur; these should be limited using "%.<N>s where <N> is a
857 decimal number calculated so that <N> plus the maximum size of other
858 formatted text does not exceed 1000 bytes. Also watch out for "%f",
859 which can print hundreds of digits for very large numbers.
860
861 */
862
863static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000864mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +0000865{
866 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000867 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000868
Guido van Rossum8442af31998-10-12 18:22:10 +0000869 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000870 file = PySys_GetObject(name);
871 if (file == NULL || PyFile_AsFile(file) == fp)
872 vfprintf(fp, format, va);
873 else {
874 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000875 if (vsprintf(buffer, format, va) >= sizeof(buffer))
876 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000877 if (PyFile_WriteString(buffer, file) != 0) {
878 PyErr_Clear();
879 fputs(buffer, fp);
880 }
881 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000882 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000883}
884
885void
Guido van Rossuma890e681998-05-12 14:59:24 +0000886PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000887{
888 va_list va;
889
Guido van Rossuma890e681998-05-12 14:59:24 +0000890 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000891 mywrite("stdout", stdout, format, va);
892 va_end(va);
893}
894
895void
Guido van Rossuma890e681998-05-12 14:59:24 +0000896PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000897{
898 va_list va;
899
Guido van Rossuma890e681998-05-12 14:59:24 +0000900 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000901 mywrite("stderr", stderr, format, va);
902 va_end(va);
903}