blob: 33d71ac1d8f6c4ac7c862deaf558c85f6fd4af03 [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 *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071sys_exc_info(PyObject *self, PyObject *args)
Guido van Rossuma027efa1997-05-05 20:56:21 +000072{
73 PyThreadState *tstate;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +000074 if (!PyArg_ParseTuple(args, ":exc_info"))
Guido van Rossuma027efa1997-05-05 20:56:21 +000075 return NULL;
76 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +000077 return Py_BuildValue(
78 "(OOO)",
79 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
80 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
81 tstate->exc_traceback != NULL ?
82 tstate->exc_traceback : Py_None);
83}
84
Guido van Rossumc3bc31e1998-06-27 19:43:25 +000085static char exc_info_doc[] =
86"exc_info() -> (type, value, traceback)\n\
87\n\
88Return information about the exception that is currently being handled.\n\
89This should be called from inside an except clause only.";
90
Guido van Rossuma027efa1997-05-05 20:56:21 +000091static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000092sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Guido van Rossum6a468bf1991-12-31 13:15:35 +000094 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +000095 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +000096 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097}
98
Guido van Rossumc3bc31e1998-06-27 19:43:25 +000099static char exit_doc[] =
100"exit([status])\n\
101\n\
102Exit the interpreter by raising SystemExit(status).\n\
103If the status is omitted or None, it defaults to zero (i.e., success).\n\
104If the status numeric, it will be used as the system exit status.\n\
105If it is another kind of object, it will be printed and the system\n\
106exit status will be one (i.e., failure).";
107
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000108static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000109sys_getdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000110{
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000111 if (!PyArg_ParseTuple(args, ":getdefaultencoding"))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000112 return NULL;
113 return PyString_FromString(PyUnicode_GetDefaultEncoding());
114}
115
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000116static char getdefaultencoding_doc[] =
117"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000118\n\
119Return the current default string encoding used by the Unicode \n\
120implementation.";
121
122static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000123sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000124{
125 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000126 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000127 return NULL;
128 if (PyUnicode_SetDefaultEncoding(encoding))
129 return NULL;
130 Py_INCREF(Py_None);
131 return Py_None;
132}
133
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000134static char setdefaultencoding_doc[] =
135"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000136\n\
137Set the current default string encoding used by the Unicode implementation.";
138
139static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000140sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000141{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000142 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000143 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000144 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000145 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000146 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000147 Py_XDECREF(tstate->sys_tracefunc);
148 tstate->sys_tracefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000149 Py_INCREF(Py_None);
150 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000151}
152
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000153static char settrace_doc[] =
154"settrace(function)\n\
155\n\
156Set the global debug tracing function. It will be called on each\n\
157function call. See the debugger chapter in the library manual.";
158
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000159static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000161{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000162 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000163 if (args == Py_None)
Guido van Rossume2437a11992-03-23 18:20:18 +0000164 args = NULL;
Guido van Rossume765f7d1992-04-05 14:17:55 +0000165 else
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000166 Py_XINCREF(args);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167 Py_XDECREF(tstate->sys_profilefunc);
168 tstate->sys_profilefunc = args;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000169 Py_INCREF(Py_None);
170 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000171}
172
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000173static char setprofile_doc[] =
174"setprofile(function)\n\
175\n\
176Set the profiling function. It will be called on each function call\n\
177and return. See the profiler chapter in the library manual.";
178
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000179static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000180sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000181{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000182 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000183 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000184 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000185 Py_INCREF(Py_None);
186 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000187}
188
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000189static char setcheckinterval_doc[] =
190"setcheckinterval(n)\n\
191\n\
192Tell the Python interpreter to check for asynchronous events every\n\
193n instructions. This also affects how often thread switches occur.";
194
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000195static PyObject *
196sys_setrecursionlimit(PyObject *self, PyObject *args)
197{
198 int new_limit;
199 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
200 return NULL;
201 if (new_limit <= 0) {
202 PyErr_SetString(PyExc_ValueError,
203 "recursion limit must be positive");
204 return NULL;
205 }
206 Py_SetRecursionLimit(new_limit);
207 Py_INCREF(Py_None);
208 return Py_None;
209}
210
211static char setrecursionlimit_doc[] =
212"setrecursionlimit(n)\n\
213\n\
214Set the maximum depth of the Python interpreter stack to n. This\n\
215limit prevents infinite recursion from causing an overflow of the C\n\
216stack and crashing Python. The highest possible limit is platform-\n\
217dependent.";
218
219static PyObject *
220sys_getrecursionlimit(PyObject *self, PyObject *args)
221{
222 if (!PyArg_ParseTuple(args, ":getrecursionlimit"))
223 return NULL;
224 return PyInt_FromLong(Py_GetRecursionLimit());
225}
226
227static char getrecursionlimit_doc[] =
228"getrecursionlimit()\n\
229\n\
230Return the current value of the recursion limit, the maximum depth\n\
231of the Python interpreter stack. This limit prevents infinite\n\
232recursion from causing an overflow of the C stack and crashing Python.";
233
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000234#ifdef USE_MALLOPT
235/* Link with -lmalloc (or -lmpc) on an SGI */
236#include <malloc.h>
237
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000238static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000240{
241 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000242 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000243 return NULL;
244 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000245 Py_INCREF(Py_None);
246 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000247}
248#endif /* USE_MALLOPT */
249
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000250static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251sys_getrefcount(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000252{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000253 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000254 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000255 return NULL;
Mark Hammond440d8982000-06-20 08:12:48 +0000256 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000257}
258
Mark Hammond440d8982000-06-20 08:12:48 +0000259#ifdef Py_TRACE_REFS
260static PyObject *
261sys_gettotalrefcount(PyObject *self, PyObject *args)
262{
263 extern long _Py_RefTotal;
264 if (!PyArg_ParseTuple(args, ":gettotalrefcount"))
265 return NULL;
266 return PyInt_FromLong(_Py_RefTotal);
267}
268
269#endif /* Py_TRACE_REFS */
270
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000271static char getrefcount_doc[] =
272"getrefcount(object) -> integer\n\
273\n\
274Return the current reference count for the object. This includes the\n\
275temporary reference in the argument list, so it is at least 2.";
276
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000277#ifdef COUNT_ALLOCS
278static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000279sys_getcounts(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000280{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000281 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000282
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000283 if (!PyArg_ParseTuple(args, ":getcounts"))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000284 return NULL;
285 return get_counts();
286}
287#endif
288
Barry Warsawb6a54d22000-12-06 21:47:46 +0000289static char getframe_doc[] =
290"_getframe([depth]) -> frameobject\n\
291\n\
292Return a frame object from the call stack. If optional integer depth is\n\
293given, return the frame object that many calls below the top of the stack.\n\
294If that is deeper than the call stack, ValueError is raised. The default\n\
295for depth is zero, returning the frame at the top of the call stack.\n\
296\n\
297This function should be used for internal and specialized\n\
298purposes only.";
299
300static PyObject *
301sys_getframe(PyObject *self, PyObject *args)
302{
303 PyFrameObject *f = PyThreadState_Get()->frame;
304 int depth = -1;
305
306 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
307 return NULL;
308
309 while (depth > 0 && f != NULL) {
310 f = f->f_back;
311 --depth;
312 }
313 if (f == NULL) {
314 PyErr_SetString(PyExc_ValueError,
315 "call stack is not deep enough");
316 return NULL;
317 }
318 Py_INCREF(f);
319 return (PyObject*)f;
320}
321
322
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000323#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000324/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000325extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000326#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000327
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000328#ifdef DYNAMIC_EXECUTION_PROFILE
329/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000330extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000331#endif
332
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000333static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000334 /* Might as well keep this in alphabetic order */
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000335 {"exc_info", sys_exc_info, 1, exc_info_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000336 {"exit", sys_exit, 0, exit_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000337 {"getdefaultencoding", sys_getdefaultencoding, 1,
338 getdefaultencoding_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000339#ifdef COUNT_ALLOCS
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000340 {"getcounts", sys_getcounts, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000341#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000342#ifdef DYNAMIC_EXECUTION_PROFILE
343 {"getdxp", _Py_GetDXProfile, 1},
344#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000345#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000346 {"getobjects", _Py_GetObjects, 1},
Mark Hammond440d8982000-06-20 08:12:48 +0000347 {"gettotalrefcount", sys_gettotalrefcount, 1},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000348#endif
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000349 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000350 {"getrecursionlimit", sys_getrecursionlimit, 1,
351 getrecursionlimit_doc},
Barry Warsawb6a54d22000-12-06 21:47:46 +0000352 {"_getframe", sys_getframe, 1, getframe_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000353#ifdef USE_MALLOPT
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000354 {"mdebug", sys_mdebug, 1},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000355#endif
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000356 {"setdefaultencoding", sys_setdefaultencoding, 1,
357 setdefaultencoding_doc},
358 {"setcheckinterval", sys_setcheckinterval, 1,
359 setcheckinterval_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000360 {"setprofile", sys_setprofile, 0, setprofile_doc},
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000361 {"setrecursionlimit", sys_setrecursionlimit, 1,
362 setrecursionlimit_doc},
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000363 {"settrace", sys_settrace, 0, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000364 {NULL, NULL} /* sentinel */
365};
366
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000367static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000368list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000369{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000370 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000371 int i;
372 if (list == NULL)
373 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000374 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000375 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000376 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000377 if (name == NULL)
378 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000379 PyList_Append(list, name);
380 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000381 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000382 if (PyList_Sort(list) != 0) {
383 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000384 list = NULL;
385 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000386 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000387 PyObject *v = PyList_AsTuple(list);
388 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000389 list = v;
390 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000391 return list;
392}
393
Guido van Rossum23fff912000-12-15 22:02:05 +0000394static PyObject *warnoptions = NULL;
395
396void
397PySys_ResetWarnOptions(void)
398{
399 if (warnoptions == NULL || !PyList_Check(warnoptions))
400 return;
401 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
402}
403
404void
405PySys_AddWarnOption(char *s)
406{
407 PyObject *str;
408
409 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
410 Py_XDECREF(warnoptions);
411 warnoptions = PyList_New(0);
412 if (warnoptions == NULL)
413 return;
414 }
415 str = PyString_FromString(s);
416 if (str != NULL) {
417 PyList_Append(warnoptions, str);
418 Py_DECREF(str);
419 }
420}
421
Guido van Rossum40552d01998-08-06 03:34:39 +0000422/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
423 Two literals concatenated works just fine. If you have a K&R compiler
424 or other abomination that however *does* understand longer strings,
425 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000426static char sys_doc[] =
427"This module provides access to some objects used or maintained by the\n\
428interpreter and to functions that interact strongly with the interpreter.\n\
429\n\
430Dynamic objects:\n\
431\n\
432argv -- command line arguments; argv[0] is the script pathname if known\n\
433path -- module search path; path[0] is the script directory, else ''\n\
434modules -- dictionary of loaded modules\n\
435exitfunc -- you may set this to a function to be called when Python exits\n\
436\n\
437stdin -- standard input file object; used by raw_input() and input()\n\
438stdout -- standard output file object; used by the print statement\n\
439stderr -- standard error object; used for error messages\n\
440 By assigning another file object (or an object that behaves like a file)\n\
441 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
442\n\
443last_type -- type of last uncaught exception\n\
444last_value -- value of last uncaught exception\n\
445last_traceback -- traceback of last uncaught exception\n\
446 These three are only available in an interactive session after a\n\
447 traceback has been printed.\n\
448\n\
449exc_type -- type of exception currently being handled\n\
450exc_value -- value of exception currently being handled\n\
451exc_traceback -- traceback of exception currently being handled\n\
452 The function exc_info() should be used instead of these three,\n\
453 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000454"
455#ifndef MS_WIN16
456/* Concatenating string here */
457"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000458Static objects:\n\
459\n\
460maxint -- the largest supported integer (the smallest is -maxint-1)\n\
461builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000462version -- the version of this interpreter as a string\n\
463version_info -- version information as a tuple\n\
464hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000465copyright -- copyright notice pertaining to this interpreter\n\
466platform -- platform identifier\n\
467executable -- pathname of this Python interpreter\n\
468prefix -- prefix used to find the Python library\n\
469exec_prefix -- prefix used to find the machine-specific Python library\n\
470dllhandle -- [Windows only] integer handle of the Python DLL\n\
471winver -- [Windows only] version number of the Python DLL\n\
472__stdin__ -- the original stdin; don't use!\n\
473__stdout__ -- the original stdout; don't use!\n\
474__stderr__ -- the original stderr; don't use!\n\
475\n\
476Functions:\n\
477\n\
478exc_info() -- return thread-safe information about the current exception\n\
479exit() -- exit the interpreter by raising SystemExit\n\
480getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000481getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000482setcheckinterval() -- control how often the interpreter checks for events\n\
483setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000484setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000485settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000486"
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000487#endif
Fred Drakeccede592000-08-14 20:59:57 +0000488/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000489
Guido van Rossum25ce5661997-08-02 03:10:38 +0000490PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000491_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000492{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000493 PyObject *m, *v, *sysdict;
494 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000495 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000496
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000497 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000498 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000499
500 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
501 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
502 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000503 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000504 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000505 PyDict_SetItemString(sysdict, "stdin", sysin);
506 PyDict_SetItemString(sysdict, "stdout", sysout);
507 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000508 /* Make backup copies for cleanup */
509 PyDict_SetItemString(sysdict, "__stdin__", sysin);
510 PyDict_SetItemString(sysdict, "__stdout__", sysout);
511 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000512 Py_XDECREF(sysin);
513 Py_XDECREF(sysout);
514 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000515 PyDict_SetItemString(sysdict, "version",
516 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000517 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000518 PyDict_SetItemString(sysdict, "hexversion",
519 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000520 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000521 /*
522 * These release level checks are mutually exclusive and cover
523 * the field, so don't get too fancy with the pre-processor!
524 */
525#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000526 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000527#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000528 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000529#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000530 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000531#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000532 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000533#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000534 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000535 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000536 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000537 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000538 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000539 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000540 PyDict_SetItemString(sysdict, "copyright",
541 v = PyString_FromString(Py_GetCopyright()));
542 Py_XDECREF(v);
543 PyDict_SetItemString(sysdict, "platform",
544 v = PyString_FromString(Py_GetPlatform()));
545 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000546 PyDict_SetItemString(sysdict, "executable",
547 v = PyString_FromString(Py_GetProgramFullPath()));
548 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000549 PyDict_SetItemString(sysdict, "prefix",
550 v = PyString_FromString(Py_GetPrefix()));
551 Py_XDECREF(v);
552 PyDict_SetItemString(sysdict, "exec_prefix",
553 v = PyString_FromString(Py_GetExecPrefix()));
554 Py_XDECREF(v);
555 PyDict_SetItemString(sysdict, "maxint",
556 v = PyInt_FromLong(PyInt_GetMax()));
557 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000558 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000560 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000561 {
562 /* Assumes that longs are at least 2 bytes long.
563 Should be safe! */
564 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000565 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000566
567 s = (char *) &number;
568 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000569 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000570 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000571 value = "little";
572 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000573 v = PyString_FromString(value));
574 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000575 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000576#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000577 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000578 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000579 Py_XDECREF(v);
580 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000581 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000582 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000583#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000584 if (warnoptions == NULL) {
585 warnoptions = PyList_New(0);
586 }
587 else {
588 Py_INCREF(warnoptions);
589 }
590 if (warnoptions != NULL) {
591 PyDict_SetItemString(sysdict, "warnoptions", v = warnoptions);
592 Py_DECREF(v);
593 }
594
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000595 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000596 return NULL;
597 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000598}
599
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000600static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000601makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000602{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000603 int i, n;
604 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000605 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000606
607 n = 1;
608 p = path;
609 while ((p = strchr(p, delim)) != NULL) {
610 n++;
611 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000612 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000613 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000614 if (v == NULL)
615 return NULL;
616 for (i = 0; ; i++) {
617 p = strchr(path, delim);
618 if (p == NULL)
619 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000620 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000621 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000622 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000623 return NULL;
624 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000625 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000626 if (*p == '\0')
627 break;
628 path = p+1;
629 }
630 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631}
632
633void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000634PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000636 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000637 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000638 Py_FatalError("can't create sys.path");
639 if (PySys_SetObject("path", v) != 0)
640 Py_FatalError("can't assign sys.path");
641 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000642}
643
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000644static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000646{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000647 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000648 if (argc <= 0 || argv == NULL) {
649 /* Ensure at least one (empty) argument is seen */
650 static char *empty_argv[1] = {""};
651 argv = empty_argv;
652 argc = 1;
653 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000654 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000655 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000656 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000657 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000658 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000659 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000660 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000661 av = NULL;
662 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000663 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000664 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000665 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000666 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000667 return av;
668}
669
670void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000671PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000672{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000673 PyObject *av = makeargvobject(argc, argv);
674 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000675 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000676 Py_FatalError("no mem for sys.argv");
677 if (PySys_SetObject("argv", av) != 0)
678 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000679 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000680 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000681 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000682 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000683 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000684#ifdef HAVE_READLINK
685 char link[MAXPATHLEN+1];
686 char argv0copy[2*MAXPATHLEN+1];
687 int nr = 0;
688 if (argc > 0 && argv0 != NULL)
689 nr = readlink(argv0, link, MAXPATHLEN);
690 if (nr > 0) {
691 /* It's a symlink */
692 link[nr] = '\0';
693 if (link[0] == SEP)
694 argv0 = link; /* Link to absolute path */
695 else if (strchr(link, SEP) == NULL)
696 ; /* Link without path */
697 else {
698 /* Must join(dirname(argv0), link) */
699 char *q = strrchr(argv0, SEP);
700 if (q == NULL)
701 argv0 = link; /* argv0 without path */
702 else {
703 /* Must make a copy */
704 strcpy(argv0copy, argv0);
705 q = strrchr(argv0copy, SEP);
706 strcpy(q+1, link);
707 argv0 = argv0copy;
708 }
709 }
710 }
711#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000712#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000713 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000714 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000715 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000716 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000717 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000718 if (q != NULL)
719 p = q;
720 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000721 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000722 if (n > 1 && p[-1] != ':')
723 n--; /* Drop trailing separator */
724 }
725 }
726#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000727 if (argc > 0 && argv0 != NULL)
728 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000729 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000730 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000731#if SEP == '/' /* Special case for Unix filename syntax */
732 if (n > 1)
733 n--; /* Drop trailing separator */
734#endif /* Unix */
735 }
736#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000737 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000738 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000739 Py_FatalError("no mem for sys.path insertion");
740 if (PyList_Insert(path, 0, a) < 0)
741 Py_FatalError("sys.path.insert(0) failed");
742 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000743 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000744 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745}
Guido van Rossuma890e681998-05-12 14:59:24 +0000746
747
748/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
749 Adapted from code submitted by Just van Rossum.
750
751 PySys_WriteStdout(format, ...)
752 PySys_WriteStderr(format, ...)
753
754 The first function writes to sys.stdout; the second to sys.stderr. When
755 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000756 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000757
758 Both take a printf-style format string as their first argument followed
759 by a variable length argument list determined by the format string.
760
761 *** WARNING ***
762
763 The format should limit the total size of the formatted output string to
764 1000 bytes. In particular, this means that no unrestricted "%s" formats
765 should occur; these should be limited using "%.<N>s where <N> is a
766 decimal number calculated so that <N> plus the maximum size of other
767 formatted text does not exceed 1000 bytes. Also watch out for "%f",
768 which can print hundreds of digits for very large numbers.
769
770 */
771
772static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000773mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +0000774{
775 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +0000776 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +0000777
Guido van Rossum8442af31998-10-12 18:22:10 +0000778 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000779 file = PySys_GetObject(name);
780 if (file == NULL || PyFile_AsFile(file) == fp)
781 vfprintf(fp, format, va);
782 else {
783 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +0000784 if (vsprintf(buffer, format, va) >= sizeof(buffer))
785 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +0000786 if (PyFile_WriteString(buffer, file) != 0) {
787 PyErr_Clear();
788 fputs(buffer, fp);
789 }
790 }
Guido van Rossum8442af31998-10-12 18:22:10 +0000791 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +0000792}
793
794void
Guido van Rossuma890e681998-05-12 14:59:24 +0000795PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000796{
797 va_list va;
798
Guido van Rossuma890e681998-05-12 14:59:24 +0000799 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000800 mywrite("stdout", stdout, format, va);
801 va_end(va);
802}
803
804void
Guido van Rossuma890e681998-05-12 14:59:24 +0000805PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +0000806{
807 va_list va;
808
Guido van Rossuma890e681998-05-12 14:59:24 +0000809 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +0000810 mywrite("stderr", stderr, format, va);
811 va_end(va);
812}