blob: 50b99127a014cf33a667d5ae39d9f9a09717de0c [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 Rossuma12fe4e2003-04-09 19:06:21 +000020#include "eval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossume2437a11992-03-23 18:20:18 +000022#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000023
Mark Hammond8696ebc2002-10-08 02:44:31 +000024#ifdef MS_WINDOWS
25#define WIN32_LEAN_AND_MEAN
26#include "windows.h"
27#endif /* MS_WINDOWS */
28
Guido van Rossum9b38a141996-09-11 23:12:24 +000029#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000030extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000031/* A string loaded from the DLL at startup: */
32extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000033#endif
34
Guido van Rossum65bf9f21997-04-29 18:33:38 +000035PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000036PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037{
Guido van Rossum25ce5661997-08-02 03:10:38 +000038 PyThreadState *tstate = PyThreadState_Get();
39 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000040 if (sd == NULL)
41 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000042 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043}
44
45FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000046PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
48 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000049 PyObject *v = PySys_GetObject(name);
50 if (v != NULL && PyFile_Check(v))
51 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052 if (fp == NULL)
53 fp = def;
54 return fp;
55}
56
57int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000058PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059{
Guido van Rossum25ce5661997-08-02 03:10:38 +000060 PyThreadState *tstate = PyThreadState_Get();
61 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000062 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000063 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000064 return 0;
65 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000066 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000067 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000069 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Guido van Rossum65bf9f21997-04-29 18:33:38 +000072static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000073sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000074{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000075 PyObject *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000076 PyInterpreterState *interp = PyThreadState_Get()->interp;
77 PyObject *modules = interp->modules;
78 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
79
Moshe Zadka03897ea2001-07-23 13:32:43 +000080 if (builtins == NULL) {
81 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
82 return NULL;
83 }
84
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000085 /* Print value except if None */
86 /* After printing, also assign to '_' */
87 /* Before, set '_' to None to avoid recursion */
88 if (o == Py_None) {
89 Py_INCREF(Py_None);
90 return Py_None;
91 }
92 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
93 return NULL;
94 if (Py_FlushLine() != 0)
95 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000096 outf = PySys_GetObject("stdout");
97 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000098 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
99 return NULL;
100 }
Greg Steinceb9b7c2001-01-11 09:27:34 +0000101 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000102 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000103 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000104 if (Py_FlushLine() != 0)
105 return NULL;
106 if (PyObject_SetAttrString(builtins, "_", o) != 0)
107 return NULL;
108 Py_INCREF(Py_None);
109 return Py_None;
110}
111
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000112PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000113"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000114"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000115"Print an object to sys.stdout and also save it in __builtin__._\n"
116);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000117
118static PyObject *
119sys_excepthook(PyObject* self, PyObject* args)
120{
121 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000122 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000123 return NULL;
124 PyErr_Display(exc, value, tb);
125 Py_INCREF(Py_None);
126 return Py_None;
127}
128
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000129PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000130"excepthook(exctype, value, traceback) -> None\n"
131"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000132"Handle an exception by displaying it with a traceback on sys.stderr.\n"
133);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000134
135static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000136sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137{
138 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000139 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000140 return Py_BuildValue(
141 "(OOO)",
142 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
143 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
144 tstate->exc_traceback != NULL ?
145 tstate->exc_traceback : Py_None);
146}
147
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000148PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000149"exc_info() -> (type, value, traceback)\n\
150\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000151Return information about the most recent exception caught by an except\n\
152clause in the current stack frame or in an older stack frame."
153);
154
155static PyObject *
156sys_exc_clear(PyObject *self, PyObject *noargs)
157{
158 PyThreadState *tstate = PyThreadState_Get();
159 PyObject *tmp_type, *tmp_value, *tmp_tb;
160 tmp_type = tstate->exc_type;
161 tmp_value = tstate->exc_value;
162 tmp_tb = tstate->exc_traceback;
163 tstate->exc_type = NULL;
164 tstate->exc_value = NULL;
165 tstate->exc_traceback = NULL;
166 Py_XDECREF(tmp_type);
167 Py_XDECREF(tmp_value);
168 Py_XDECREF(tmp_tb);
169 /* For b/w compatibility */
170 PySys_SetObject("exc_type", Py_None);
171 PySys_SetObject("exc_value", Py_None);
172 PySys_SetObject("exc_traceback", Py_None);
173 Py_INCREF(Py_None);
174 return Py_None;
175}
176
177PyDoc_STRVAR(exc_clear_doc,
178"exc_clear() -> None\n\
179\n\
180Clear global information on the current exception. Subsequent calls to\n\
181exc_info() will return (None,None,None) until another exception is raised\n\
182in the current thread or the execution stack returns to a frame where\n\
183another exception is being handled."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000184);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000185
Guido van Rossuma027efa1997-05-05 20:56:21 +0000186static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000189 PyObject *exit_code = 0;
190 if (!PyArg_ParseTuple(args, "|O:exit", &exit_code))
191 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000192 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000193 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000194 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195}
196
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000197PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000198"exit([status])\n\
199\n\
200Exit the interpreter by raising SystemExit(status).\n\
201If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000202If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000203If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000204exit status will be one (i.e., failure)."
205);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000206
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000207#ifdef Py_USING_UNICODE
208
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000209static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000210sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000211{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000212 return PyString_FromString(PyUnicode_GetDefaultEncoding());
213}
214
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000215PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000216"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000217\n\
218Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000219implementation."
220);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000221
222static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000224{
225 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000226 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000227 return NULL;
228 if (PyUnicode_SetDefaultEncoding(encoding))
229 return NULL;
230 Py_INCREF(Py_None);
231 return Py_None;
232}
233
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000234PyDoc_STRVAR(setdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000235"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000236\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000237Set the current default string encoding used by the Unicode implementation."
238);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000239
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000240static PyObject *
241sys_getfilesystemencoding(PyObject *self)
242{
243 if (Py_FileSystemDefaultEncoding)
244 return PyString_FromString(Py_FileSystemDefaultEncoding);
245 Py_INCREF(Py_None);
246 return Py_None;
247}
248
249PyDoc_STRVAR(getfilesystemencoding_doc,
250"getfilesystemencoding() -> string\n\
251\n\
252Return the encoding used to convert Unicode filenames in\n\
253operating system filenames."
254);
255
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000256#endif
257
Fred Drake5755ce62001-06-27 19:19:46 +0000258/*
259 * Cached interned string objects used for calling the profile and
260 * trace functions. Initialized by trace_init().
261 */
262static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
263
264static int
265trace_init(void)
266{
267 static char *whatnames[4] = {"call", "exception", "line", "return"};
268 PyObject *name;
269 int i;
270 for (i = 0; i < 4; ++i) {
271 if (whatstrings[i] == NULL) {
272 name = PyString_InternFromString(whatnames[i]);
273 if (name == NULL)
274 return -1;
275 whatstrings[i] = name;
276 }
277 }
278 return 0;
279}
280
281
282static PyObject *
283call_trampoline(PyThreadState *tstate, PyObject* callback,
284 PyFrameObject *frame, int what, PyObject *arg)
285{
286 PyObject *args = PyTuple_New(3);
287 PyObject *whatstr;
288 PyObject *result;
289
290 if (args == NULL)
291 return NULL;
292 Py_INCREF(frame);
293 whatstr = whatstrings[what];
294 Py_INCREF(whatstr);
295 if (arg == NULL)
296 arg = Py_None;
297 Py_INCREF(arg);
298 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
299 PyTuple_SET_ITEM(args, 1, whatstr);
300 PyTuple_SET_ITEM(args, 2, arg);
301
302 /* call the Python-level function */
303 PyFrame_FastToLocals(frame);
304 result = PyEval_CallObject(callback, args);
305 PyFrame_LocalsToFast(frame, 1);
306 if (result == NULL)
307 PyTraceBack_Here(frame);
308
309 /* cleanup */
310 Py_DECREF(args);
311 return result;
312}
313
314static int
315profile_trampoline(PyObject *self, PyFrameObject *frame,
316 int what, PyObject *arg)
317{
318 PyThreadState *tstate = frame->f_tstate;
319 PyObject *result;
320
Fred Drake8f51f542001-10-04 14:48:42 +0000321 if (arg == NULL)
322 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000323 result = call_trampoline(tstate, self, frame, what, arg);
324 if (result == NULL) {
325 PyEval_SetProfile(NULL, NULL);
326 return -1;
327 }
328 Py_DECREF(result);
329 return 0;
330}
331
332static int
333trace_trampoline(PyObject *self, PyFrameObject *frame,
334 int what, PyObject *arg)
335{
336 PyThreadState *tstate = frame->f_tstate;
337 PyObject *callback;
338 PyObject *result;
339
340 if (what == PyTrace_CALL)
341 callback = self;
342 else
343 callback = frame->f_trace;
344 if (callback == NULL)
345 return 0;
346 result = call_trampoline(tstate, callback, frame, what, arg);
347 if (result == NULL) {
348 PyEval_SetTrace(NULL, NULL);
349 Py_XDECREF(frame->f_trace);
350 frame->f_trace = NULL;
351 return -1;
352 }
353 if (result != Py_None) {
354 PyObject *temp = frame->f_trace;
355 frame->f_trace = NULL;
356 Py_XDECREF(temp);
357 frame->f_trace = result;
358 }
359 else {
360 Py_DECREF(result);
361 }
362 return 0;
363}
Fred Draked0838392001-06-16 21:02:31 +0000364
Fred Drake8b4d01d2000-05-09 19:57:01 +0000365static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000366sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000367{
Fred Drake5755ce62001-06-27 19:19:46 +0000368 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000369 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000370 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000371 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000372 else
Fred Drake5755ce62001-06-27 19:19:46 +0000373 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000374 Py_INCREF(Py_None);
375 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000376}
377
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000378PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000379"settrace(function)\n\
380\n\
381Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000382function call. See the debugger chapter in the library manual."
383);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000384
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000385static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000387{
Fred Drake5755ce62001-06-27 19:19:46 +0000388 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000389 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000390 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000391 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000392 else
Fred Drake5755ce62001-06-27 19:19:46 +0000393 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000394 Py_INCREF(Py_None);
395 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000396}
397
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000398PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000399"setprofile(function)\n\
400\n\
401Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000402and return. See the profiler chapter in the library manual."
403);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000404
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000405static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000406sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000407{
Skip Montanarod581d772002-09-03 20:10:45 +0000408 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000409 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000410 Py_INCREF(Py_None);
411 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000412}
413
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000414PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000415"setcheckinterval(n)\n\
416\n\
417Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000418n instructions. This also affects how often thread switches occur."
419);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000420
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000421static PyObject *
422sys_setrecursionlimit(PyObject *self, PyObject *args)
423{
424 int new_limit;
425 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
426 return NULL;
427 if (new_limit <= 0) {
428 PyErr_SetString(PyExc_ValueError,
429 "recursion limit must be positive");
430 return NULL;
431 }
432 Py_SetRecursionLimit(new_limit);
433 Py_INCREF(Py_None);
434 return Py_None;
435}
436
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000437PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000438"setrecursionlimit(n)\n\
439\n\
440Set the maximum depth of the Python interpreter stack to n. This\n\
441limit prevents infinite recursion from causing an overflow of the C\n\
442stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000443dependent."
444);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000445
446static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000447sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000448{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000449 return PyInt_FromLong(Py_GetRecursionLimit());
450}
451
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000452PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000453"getrecursionlimit()\n\
454\n\
455Return the current value of the recursion limit, the maximum depth\n\
456of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000457recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000458);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000459
Mark Hammond8696ebc2002-10-08 02:44:31 +0000460#ifdef MS_WINDOWS
461PyDoc_STRVAR(getwindowsversion_doc,
462"getwindowsversion()\n\
463\n\
464Return information about the running version of Windows.\n\
465The result is a tuple of (major, minor, build, platform, text)\n\
466All elements are numbers, except text which is a string.\n\
467Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
468"
469);
470
471static PyObject *
472sys_getwindowsversion(PyObject *self)
473{
474 OSVERSIONINFO ver;
475 ver.dwOSVersionInfoSize = sizeof(ver);
476 if (!GetVersionEx(&ver))
477 return PyErr_SetFromWindowsErr(0);
478 return Py_BuildValue("HHHHs",
479 ver.dwMajorVersion,
480 ver.dwMinorVersion,
481 ver.dwBuildNumber,
482 ver.dwPlatformId,
483 ver.szCSDVersion);
484}
485
486#endif /* MS_WINDOWS */
487
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000488#ifdef HAVE_DLOPEN
489static PyObject *
490sys_setdlopenflags(PyObject *self, PyObject *args)
491{
492 int new_val;
493 PyThreadState *tstate = PyThreadState_Get();
494 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
495 return NULL;
496 if (!tstate)
497 return NULL;
498 tstate->interp->dlopenflags = new_val;
499 Py_INCREF(Py_None);
500 return Py_None;
501}
502
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000503PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000504"setdlopenflags(n) -> None\n\
505\n\
506Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000507things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000508a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000509To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000510sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
511);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000512
513static PyObject *
514sys_getdlopenflags(PyObject *self, PyObject *args)
515{
516 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000517 if (!tstate)
518 return NULL;
519 return PyInt_FromLong(tstate->interp->dlopenflags);
520}
521
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000522PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000523"getdlopenflags() -> int\n\
524\n\
525Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000526calls. The flag constants are defined in the dl module."
527);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000528#endif
529
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000530#ifdef USE_MALLOPT
531/* Link with -lmalloc (or -lmpc) on an SGI */
532#include <malloc.h>
533
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000534static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000536{
537 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000538 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000539 return NULL;
540 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000541 Py_INCREF(Py_None);
542 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000543}
544#endif /* USE_MALLOPT */
545
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000546static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000547sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000548{
Mark Hammond440d8982000-06-20 08:12:48 +0000549 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000550}
551
Tim Peters4be93d02002-07-07 19:59:50 +0000552#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000553static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000554sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000555{
Mark Hammond440d8982000-06-20 08:12:48 +0000556 return PyInt_FromLong(_Py_RefTotal);
557}
558
559#endif /* Py_TRACE_REFS */
560
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000561PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000562"getrefcount(object) -> integer\n\
563\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000564Return the reference count of object. The count returned is generally\n\
565one higher than you might expect, because it includes the (temporary)\n\
566reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000567);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000568
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000569#ifdef COUNT_ALLOCS
570static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000571sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000572{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000573 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000574
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000575 return get_counts();
576}
577#endif
578
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000579PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000580"_getframe([depth]) -> frameobject\n\
581\n\
582Return a frame object from the call stack. If optional integer depth is\n\
583given, return the frame object that many calls below the top of the stack.\n\
584If that is deeper than the call stack, ValueError is raised. The default\n\
585for depth is zero, returning the frame at the top of the call stack.\n\
586\n\
587This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000588purposes only."
589);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000590
591static PyObject *
592sys_getframe(PyObject *self, PyObject *args)
593{
594 PyFrameObject *f = PyThreadState_Get()->frame;
595 int depth = -1;
596
597 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
598 return NULL;
599
600 while (depth > 0 && f != NULL) {
601 f = f->f_back;
602 --depth;
603 }
604 if (f == NULL) {
605 PyErr_SetString(PyExc_ValueError,
606 "call stack is not deep enough");
607 return NULL;
608 }
609 Py_INCREF(f);
610 return (PyObject*)f;
611}
612
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000613PyDoc_STRVAR(call_tracing_doc,
614"call_tracing(func, args) -> object\n\
615\n\
616Call func(*args), while tracing is enabled. The tracing state is\n\
617saved, and restored afterwards. This is intended to be called from\n\
618a debugger from a checkpoint, to recursively debug some other code."
619);
620
621static PyObject *
622sys_call_tracing(PyObject *self, PyObject *args)
623{
624 PyObject *func, *funcargs;
625 if (!PyArg_ParseTuple(args, "OO:call_tracing", &func, &funcargs))
626 return NULL;
627 return _PyEval_CallTracing(func, funcargs);
628}
629
Jeremy Hylton985eba52003-02-05 23:13:00 +0000630PyDoc_STRVAR(callstats_doc,
631"callstats() -> tuple of integers\n\
632\n\
633Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
634when Python was built. Otherwise, return None.\n\
635\n\
636When enabled, this function returns detailed, implementation-specific\n\
637details about the number of function calls executed. The return value is\n\
638a 11-tuple where the entries in the tuple are counts of:\n\
6390. all function calls\n\
6401. calls to PyFunction_Type objects\n\
6412. PyFunction calls that do not create an argument tuple\n\
6423. PyFunction calls that do not create an argument tuple\n\
643 and bypass PyEval_EvalCodeEx()\n\
6444. PyMethod calls\n\
6455. PyMethod calls on bound methods\n\
6466. PyType calls\n\
6477. PyCFunction calls\n\
6488. generator calls\n\
6499. All other calls\n\
65010. Number of stack pops performed by call_function()"
651);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000652
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000653#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000654/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000655extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000656#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000657
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000658#ifdef DYNAMIC_EXECUTION_PROFILE
659/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000660extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000661#endif
662
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000663static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000664 /* Might as well keep this in alphabetic order */
Jeremy Hylton985eba52003-02-05 23:13:00 +0000665 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
666 callstats_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000667 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000668 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
669 {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000670 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000671 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000672#ifdef Py_USING_UNICODE
Jeremy Hylton985eba52003-02-05 23:13:00 +0000673 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
674 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000675#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000676#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000677 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
678 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000679#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000680#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000681 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000682#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000683#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000684 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000685#endif
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000686#ifdef Py_USING_UNICODE
687 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
688 METH_NOARGS, getfilesystemencoding_doc},
689#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000690#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000691 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000692#endif
693#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000694 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000695#endif
Fred Drakea7688822001-10-24 20:47:48 +0000696 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000697 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000699 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000700#ifdef MS_WINDOWS
701 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
702 getwindowsversion_doc},
703#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000704#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000705 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000706#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000707#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000708 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000709 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000710#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000711 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000712 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000713#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000714 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
715 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000716#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000717 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000718 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000719 setrecursionlimit_doc},
Neal Norwitz290d31e2002-03-03 15:12:58 +0000720 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000721 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000722 {NULL, NULL} /* sentinel */
723};
724
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000725static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000726list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000727{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000728 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000729 int i;
730 if (list == NULL)
731 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000732 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000733 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000734 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000735 if (name == NULL)
736 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000737 PyList_Append(list, name);
738 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000739 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000740 if (PyList_Sort(list) != 0) {
741 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000742 list = NULL;
743 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000744 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000745 PyObject *v = PyList_AsTuple(list);
746 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000747 list = v;
748 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000749 return list;
750}
751
Guido van Rossum23fff912000-12-15 22:02:05 +0000752static PyObject *warnoptions = NULL;
753
754void
755PySys_ResetWarnOptions(void)
756{
757 if (warnoptions == NULL || !PyList_Check(warnoptions))
758 return;
759 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
760}
761
762void
763PySys_AddWarnOption(char *s)
764{
765 PyObject *str;
766
767 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
768 Py_XDECREF(warnoptions);
769 warnoptions = PyList_New(0);
770 if (warnoptions == NULL)
771 return;
772 }
773 str = PyString_FromString(s);
774 if (str != NULL) {
775 PyList_Append(warnoptions, str);
776 Py_DECREF(str);
777 }
778}
779
Guido van Rossum40552d01998-08-06 03:34:39 +0000780/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
781 Two literals concatenated works just fine. If you have a K&R compiler
782 or other abomination that however *does* understand longer strings,
783 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000784PyDoc_VAR(sys_doc) =
785PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000786"This module provides access to some objects used or maintained by the\n\
787interpreter and to functions that interact strongly with the interpreter.\n\
788\n\
789Dynamic objects:\n\
790\n\
791argv -- command line arguments; argv[0] is the script pathname if known\n\
792path -- module search path; path[0] is the script directory, else ''\n\
793modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000794\n\
795displayhook -- called to show results in an interactive session\n\
796excepthook -- called to handle any uncaught exception other than SystemExit\n\
797 To customize printing in an interactive session or to install a custom\n\
798 top-level exception handler, assign other functions to replace these.\n\
799\n\
800exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
801 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000802\n\
803stdin -- standard input file object; used by raw_input() and input()\n\
804stdout -- standard output file object; used by the print statement\n\
805stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000806 By assigning other file objects (or objects that behave like files)\n\
807 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000808\n\
809last_type -- type of last uncaught exception\n\
810last_value -- value of last uncaught exception\n\
811last_traceback -- traceback of last uncaught exception\n\
812 These three are only available in an interactive session after a\n\
813 traceback has been printed.\n\
814\n\
815exc_type -- type of exception currently being handled\n\
816exc_value -- value of exception currently being handled\n\
817exc_traceback -- traceback of exception currently being handled\n\
818 The function exc_info() should be used instead of these three,\n\
819 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000820"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000821)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000822/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000823PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000824"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000825Static objects:\n\
826\n\
827maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000828maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000829builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000830version -- the version of this interpreter as a string\n\
831version_info -- version information as a tuple\n\
832hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000833copyright -- copyright notice pertaining to this interpreter\n\
834platform -- platform identifier\n\
835executable -- pathname of this Python interpreter\n\
836prefix -- prefix used to find the Python library\n\
837exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000838"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000839)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000840#ifdef MS_WINDOWS
841/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000842PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000843"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000844winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000845"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000846)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000847#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000848PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000849"__stdin__ -- the original stdin; don't touch!\n\
850__stdout__ -- the original stdout; don't touch!\n\
851__stderr__ -- the original stderr; don't touch!\n\
852__displayhook__ -- the original displayhook; don't touch!\n\
853__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000854\n\
855Functions:\n\
856\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000857displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000858excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000859exc_info() -- return thread-safe information about the current exception\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000860exc_clear() -- clear the exception state for the current thread\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000861exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000862getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000863getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000864getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000865setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000866setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000867setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000868setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000869settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000870"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000871)
Fred Drakeccede592000-08-14 20:59:57 +0000872/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000873
Guido van Rossum25ce5661997-08-02 03:10:38 +0000874PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000875_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000876{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000877 PyObject *m, *v, *sysdict;
878 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000879 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000880
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000881 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000882 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000883
884 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
885 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
886 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000887 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000888 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000889 PyDict_SetItemString(sysdict, "stdin", sysin);
890 PyDict_SetItemString(sysdict, "stdout", sysout);
891 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000892 /* Make backup copies for cleanup */
893 PyDict_SetItemString(sysdict, "__stdin__", sysin);
894 PyDict_SetItemString(sysdict, "__stdout__", sysout);
895 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000896 PyDict_SetItemString(sysdict, "__displayhook__",
897 PyDict_GetItemString(sysdict, "displayhook"));
898 PyDict_SetItemString(sysdict, "__excepthook__",
899 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000900 Py_XDECREF(sysin);
901 Py_XDECREF(sysout);
902 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000903 PyDict_SetItemString(sysdict, "version",
904 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000905 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000906 PyDict_SetItemString(sysdict, "hexversion",
907 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000908 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000909 /*
910 * These release level checks are mutually exclusive and cover
911 * the field, so don't get too fancy with the pre-processor!
912 */
913#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000914 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000915#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000916 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000917#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000918 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000919#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000920 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000921#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000922 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000923 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000924 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000925 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000926 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000927 Py_XDECREF(v);
Skip Montanaro8e790e72002-09-03 13:25:17 +0000928 PyDict_SetItemString(sysdict, "api_version",
929 v = PyInt_FromLong(PYTHON_API_VERSION));
930 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000931 PyDict_SetItemString(sysdict, "copyright",
932 v = PyString_FromString(Py_GetCopyright()));
933 Py_XDECREF(v);
934 PyDict_SetItemString(sysdict, "platform",
935 v = PyString_FromString(Py_GetPlatform()));
936 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000937 PyDict_SetItemString(sysdict, "executable",
938 v = PyString_FromString(Py_GetProgramFullPath()));
939 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000940 PyDict_SetItemString(sysdict, "prefix",
941 v = PyString_FromString(Py_GetPrefix()));
942 Py_XDECREF(v);
943 PyDict_SetItemString(sysdict, "exec_prefix",
944 v = PyString_FromString(Py_GetExecPrefix()));
945 Py_XDECREF(v);
946 PyDict_SetItemString(sysdict, "maxint",
947 v = PyInt_FromLong(PyInt_GetMax()));
948 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000949#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000950 PyDict_SetItemString(sysdict, "maxunicode",
951 v = PyInt_FromLong(PyUnicode_GetMax()));
952 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000953#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000954 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000955 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000956 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000957 {
958 /* Assumes that longs are at least 2 bytes long.
959 Should be safe! */
960 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000961 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000962
963 s = (char *) &number;
964 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000965 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000966 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000967 value = "little";
968 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000969 v = PyString_FromString(value));
970 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000971 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000972#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000973 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000974 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000975 Py_XDECREF(v);
976 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000977 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000978 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000979#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000980 if (warnoptions == NULL) {
981 warnoptions = PyList_New(0);
982 }
983 else {
984 Py_INCREF(warnoptions);
985 }
986 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000987 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000988 }
989
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000990 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000991 return NULL;
992 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000993}
994
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000995static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000996makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000997{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000998 int i, n;
999 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001000 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001001
1002 n = 1;
1003 p = path;
1004 while ((p = strchr(p, delim)) != NULL) {
1005 n++;
1006 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001007 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001008 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001009 if (v == NULL)
1010 return NULL;
1011 for (i = 0; ; i++) {
1012 p = strchr(path, delim);
1013 if (p == NULL)
1014 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001015 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001016 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001017 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001018 return NULL;
1019 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001020 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001021 if (*p == '\0')
1022 break;
1023 path = p+1;
1024 }
1025 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001026}
1027
1028void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001029PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001030{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001031 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001032 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001033 Py_FatalError("can't create sys.path");
1034 if (PySys_SetObject("path", v) != 0)
1035 Py_FatalError("can't assign sys.path");
1036 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001037}
1038
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001039static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001040makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001041{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001042 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +00001043 if (argc <= 0 || argv == NULL) {
1044 /* Ensure at least one (empty) argument is seen */
1045 static char *empty_argv[1] = {""};
1046 argv = empty_argv;
1047 argc = 1;
1048 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001049 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001050 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001051 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001052 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001053 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001054 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001055 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001056 av = NULL;
1057 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001058 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001059 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001060 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001061 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001062 return av;
1063}
1064
1065void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001066PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001067{
Guido van Rossum162e38c2003-02-19 15:25:10 +00001068#if defined(HAVE_REALPATH)
1069 char fullpath[MAXPATHLEN];
1070#elif defined(MS_WINDOWS)
Thomas Heller27bb71e2003-01-08 14:33:48 +00001071 char fullpath[MAX_PATH];
1072#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001073 PyObject *av = makeargvobject(argc, argv);
1074 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001075 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001076 Py_FatalError("no mem for sys.argv");
1077 if (PySys_SetObject("argv", av) != 0)
1078 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001079 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001080 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001081 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +00001082 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001083 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001084#ifdef HAVE_READLINK
1085 char link[MAXPATHLEN+1];
1086 char argv0copy[2*MAXPATHLEN+1];
1087 int nr = 0;
1088 if (argc > 0 && argv0 != NULL)
1089 nr = readlink(argv0, link, MAXPATHLEN);
1090 if (nr > 0) {
1091 /* It's a symlink */
1092 link[nr] = '\0';
1093 if (link[0] == SEP)
1094 argv0 = link; /* Link to absolute path */
1095 else if (strchr(link, SEP) == NULL)
1096 ; /* Link without path */
1097 else {
1098 /* Must join(dirname(argv0), link) */
1099 char *q = strrchr(argv0, SEP);
1100 if (q == NULL)
1101 argv0 = link; /* argv0 without path */
1102 else {
1103 /* Must make a copy */
1104 strcpy(argv0copy, argv0);
1105 q = strrchr(argv0copy, SEP);
1106 strcpy(q+1, link);
1107 argv0 = argv0copy;
1108 }
1109 }
1110 }
1111#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001112#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001113 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001114 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001115#ifdef MS_WINDOWS
1116 char *ptemp;
1117 if (GetFullPathName(argv0,
1118 sizeof(fullpath),
1119 fullpath,
1120 &ptemp)) {
1121 argv0 = fullpath;
1122 }
1123#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001124 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001125 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001126 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001127 if (q != NULL)
1128 p = q;
1129 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001130 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001131 if (n > 1 && p[-1] != ':')
1132 n--; /* Drop trailing separator */
1133 }
1134 }
1135#else /* All other filename syntaxes */
Guido van Rossum162e38c2003-02-19 15:25:10 +00001136 if (argc > 0 && argv0 != NULL) {
1137#if defined(HAVE_REALPATH)
1138 if (realpath(argv0, fullpath)) {
1139 argv0 = fullpath;
1140 }
1141#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001142 p = strrchr(argv0, SEP);
Guido van Rossum162e38c2003-02-19 15:25:10 +00001143 }
Guido van Rossumcc883411996-09-10 14:44:21 +00001144 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001145#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001146 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001147#else /* don't include trailing separator */
1148 n = p - argv0;
1149#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001150#if SEP == '/' /* Special case for Unix filename syntax */
1151 if (n > 1)
1152 n--; /* Drop trailing separator */
1153#endif /* Unix */
1154 }
1155#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001156 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001157 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001158 Py_FatalError("no mem for sys.path insertion");
1159 if (PyList_Insert(path, 0, a) < 0)
1160 Py_FatalError("sys.path.insert(0) failed");
1161 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001162 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001163 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001164}
Guido van Rossuma890e681998-05-12 14:59:24 +00001165
1166
1167/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1168 Adapted from code submitted by Just van Rossum.
1169
1170 PySys_WriteStdout(format, ...)
1171 PySys_WriteStderr(format, ...)
1172
1173 The first function writes to sys.stdout; the second to sys.stderr. When
1174 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001175 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001176
1177 Both take a printf-style format string as their first argument followed
1178 by a variable length argument list determined by the format string.
1179
1180 *** WARNING ***
1181
1182 The format should limit the total size of the formatted output string to
1183 1000 bytes. In particular, this means that no unrestricted "%s" formats
1184 should occur; these should be limited using "%.<N>s where <N> is a
1185 decimal number calculated so that <N> plus the maximum size of other
1186 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1187 which can print hundreds of digits for very large numbers.
1188
1189 */
1190
1191static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001192mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001193{
1194 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001195 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001196
Guido van Rossum8442af31998-10-12 18:22:10 +00001197 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001198 file = PySys_GetObject(name);
1199 if (file == NULL || PyFile_AsFile(file) == fp)
1200 vfprintf(fp, format, va);
1201 else {
1202 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001203 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1204 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001205 if (PyFile_WriteString(buffer, file) != 0) {
1206 PyErr_Clear();
1207 fputs(buffer, fp);
1208 }
Tim Petersfaad5ad2001-12-03 00:43:33 +00001209 if (written < 0 || written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001210 const char *truncated = "... truncated";
1211 if (PyFile_WriteString(truncated, file) != 0) {
1212 PyErr_Clear();
1213 fputs(truncated, fp);
1214 }
1215 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001216 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001217 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001218}
1219
1220void
Guido van Rossuma890e681998-05-12 14:59:24 +00001221PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001222{
1223 va_list va;
1224
Guido van Rossuma890e681998-05-12 14:59:24 +00001225 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001226 mywrite("stdout", stdout, format, va);
1227 va_end(va);
1228}
1229
1230void
Guido van Rossuma890e681998-05-12 14:59:24 +00001231PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001232{
1233 va_list va;
1234
Guido van Rossuma890e681998-05-12 14:59:24 +00001235 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001236 mywrite("stderr", stderr, format, va);
1237 va_end(va);
1238}