blob: fa7f3c4a544a0eaa96a91932b64539081f60df9b [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
Mark Hammond8696ebc2002-10-08 02:44:31 +000023#ifdef MS_WINDOWS
24#define WIN32_LEAN_AND_MEAN
25#include "windows.h"
26#endif /* MS_WINDOWS */
27
Guido van Rossum9b38a141996-09-11 23:12:24 +000028#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000029extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000030/* A string loaded from the DLL at startup: */
31extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000032#endif
33
Guido van Rossum65bf9f21997-04-29 18:33:38 +000034PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000035PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036{
Guido van Rossum25ce5661997-08-02 03:10:38 +000037 PyThreadState *tstate = PyThreadState_Get();
38 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000039 if (sd == NULL)
40 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000041 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042}
43
44FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000045PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046{
47 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000048 PyObject *v = PySys_GetObject(name);
49 if (v != NULL && PyFile_Check(v))
50 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 if (fp == NULL)
52 fp = def;
53 return fp;
54}
55
56int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000057PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058{
Guido van Rossum25ce5661997-08-02 03:10:38 +000059 PyThreadState *tstate = PyThreadState_Get();
60 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000061 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000062 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000063 return 0;
64 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000065 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000066 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000068 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069}
70
Guido van Rossum65bf9f21997-04-29 18:33:38 +000071static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000072sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000073{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000074 PyObject *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000075 PyInterpreterState *interp = PyThreadState_Get()->interp;
76 PyObject *modules = interp->modules;
77 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
78
Moshe Zadka03897ea2001-07-23 13:32:43 +000079 if (builtins == NULL) {
80 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
81 return NULL;
82 }
83
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000084 /* Print value except if None */
85 /* After printing, also assign to '_' */
86 /* Before, set '_' to None to avoid recursion */
87 if (o == Py_None) {
88 Py_INCREF(Py_None);
89 return Py_None;
90 }
91 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
92 return NULL;
93 if (Py_FlushLine() != 0)
94 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000095 outf = PySys_GetObject("stdout");
96 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000097 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
98 return NULL;
99 }
Greg Steinceb9b7c2001-01-11 09:27:34 +0000100 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000101 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000102 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000103 if (Py_FlushLine() != 0)
104 return NULL;
105 if (PyObject_SetAttrString(builtins, "_", o) != 0)
106 return NULL;
107 Py_INCREF(Py_None);
108 return Py_None;
109}
110
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000111PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000112"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000113"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000114"Print an object to sys.stdout and also save it in __builtin__._\n"
115);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000116
117static PyObject *
118sys_excepthook(PyObject* self, PyObject* args)
119{
120 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000121 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000122 return NULL;
123 PyErr_Display(exc, value, tb);
124 Py_INCREF(Py_None);
125 return Py_None;
126}
127
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000128PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000129"excepthook(exctype, value, traceback) -> None\n"
130"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000131"Handle an exception by displaying it with a traceback on sys.stderr.\n"
132);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000133
134static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000135sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000136{
137 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000139 return Py_BuildValue(
140 "(OOO)",
141 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
142 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
143 tstate->exc_traceback != NULL ?
144 tstate->exc_traceback : Py_None);
145}
146
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000147PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000148"exc_info() -> (type, value, traceback)\n\
149\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000150Return information about the most recent exception caught by an except\n\
151clause in the current stack frame or in an older stack frame."
152);
153
154static PyObject *
155sys_exc_clear(PyObject *self, PyObject *noargs)
156{
157 PyThreadState *tstate = PyThreadState_Get();
158 PyObject *tmp_type, *tmp_value, *tmp_tb;
159 tmp_type = tstate->exc_type;
160 tmp_value = tstate->exc_value;
161 tmp_tb = tstate->exc_traceback;
162 tstate->exc_type = NULL;
163 tstate->exc_value = NULL;
164 tstate->exc_traceback = NULL;
165 Py_XDECREF(tmp_type);
166 Py_XDECREF(tmp_value);
167 Py_XDECREF(tmp_tb);
168 /* For b/w compatibility */
169 PySys_SetObject("exc_type", Py_None);
170 PySys_SetObject("exc_value", Py_None);
171 PySys_SetObject("exc_traceback", Py_None);
172 Py_INCREF(Py_None);
173 return Py_None;
174}
175
176PyDoc_STRVAR(exc_clear_doc,
177"exc_clear() -> None\n\
178\n\
179Clear global information on the current exception. Subsequent calls to\n\
180exc_info() will return (None,None,None) until another exception is raised\n\
181in the current thread or the execution stack returns to a frame where\n\
182another exception is being handled."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000183);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000184
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000186sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000188 PyObject *exit_code = 0;
189 if (!PyArg_ParseTuple(args, "|O:exit", &exit_code))
190 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000191 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000192 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000193 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194}
195
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000196PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000197"exit([status])\n\
198\n\
199Exit the interpreter by raising SystemExit(status).\n\
200If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000201If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000202If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000203exit status will be one (i.e., failure)."
204);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000205
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000206#ifdef Py_USING_UNICODE
207
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000208static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000209sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000210{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000211 return PyString_FromString(PyUnicode_GetDefaultEncoding());
212}
213
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000214PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000215"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000216\n\
217Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000218implementation."
219);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000220
221static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000222sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000223{
224 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000225 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000226 return NULL;
227 if (PyUnicode_SetDefaultEncoding(encoding))
228 return NULL;
229 Py_INCREF(Py_None);
230 return Py_None;
231}
232
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000233PyDoc_STRVAR(setdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000234"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000235\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000236Set the current default string encoding used by the Unicode implementation."
237);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000238
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000239static PyObject *
240sys_getfilesystemencoding(PyObject *self)
241{
242 if (Py_FileSystemDefaultEncoding)
243 return PyString_FromString(Py_FileSystemDefaultEncoding);
244 Py_INCREF(Py_None);
245 return Py_None;
246}
247
248PyDoc_STRVAR(getfilesystemencoding_doc,
249"getfilesystemencoding() -> string\n\
250\n\
251Return the encoding used to convert Unicode filenames in\n\
252operating system filenames."
253);
254
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000255#endif
256
Fred Drake5755ce62001-06-27 19:19:46 +0000257/*
258 * Cached interned string objects used for calling the profile and
259 * trace functions. Initialized by trace_init().
260 */
261static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
262
263static int
264trace_init(void)
265{
266 static char *whatnames[4] = {"call", "exception", "line", "return"};
267 PyObject *name;
268 int i;
269 for (i = 0; i < 4; ++i) {
270 if (whatstrings[i] == NULL) {
271 name = PyString_InternFromString(whatnames[i]);
272 if (name == NULL)
273 return -1;
274 whatstrings[i] = name;
275 }
276 }
277 return 0;
278}
279
280
281static PyObject *
282call_trampoline(PyThreadState *tstate, PyObject* callback,
283 PyFrameObject *frame, int what, PyObject *arg)
284{
285 PyObject *args = PyTuple_New(3);
286 PyObject *whatstr;
287 PyObject *result;
288
289 if (args == NULL)
290 return NULL;
291 Py_INCREF(frame);
292 whatstr = whatstrings[what];
293 Py_INCREF(whatstr);
294 if (arg == NULL)
295 arg = Py_None;
296 Py_INCREF(arg);
297 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
298 PyTuple_SET_ITEM(args, 1, whatstr);
299 PyTuple_SET_ITEM(args, 2, arg);
300
301 /* call the Python-level function */
302 PyFrame_FastToLocals(frame);
303 result = PyEval_CallObject(callback, args);
304 PyFrame_LocalsToFast(frame, 1);
305 if (result == NULL)
306 PyTraceBack_Here(frame);
307
308 /* cleanup */
309 Py_DECREF(args);
310 return result;
311}
312
313static int
314profile_trampoline(PyObject *self, PyFrameObject *frame,
315 int what, PyObject *arg)
316{
317 PyThreadState *tstate = frame->f_tstate;
318 PyObject *result;
319
Fred Drake8f51f542001-10-04 14:48:42 +0000320 if (arg == NULL)
321 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000322 result = call_trampoline(tstate, self, frame, what, arg);
323 if (result == NULL) {
324 PyEval_SetProfile(NULL, NULL);
325 return -1;
326 }
327 Py_DECREF(result);
328 return 0;
329}
330
331static int
332trace_trampoline(PyObject *self, PyFrameObject *frame,
333 int what, PyObject *arg)
334{
335 PyThreadState *tstate = frame->f_tstate;
336 PyObject *callback;
337 PyObject *result;
338
339 if (what == PyTrace_CALL)
340 callback = self;
341 else
342 callback = frame->f_trace;
343 if (callback == NULL)
344 return 0;
345 result = call_trampoline(tstate, callback, frame, what, arg);
346 if (result == NULL) {
347 PyEval_SetTrace(NULL, NULL);
348 Py_XDECREF(frame->f_trace);
349 frame->f_trace = NULL;
350 return -1;
351 }
352 if (result != Py_None) {
353 PyObject *temp = frame->f_trace;
354 frame->f_trace = NULL;
355 Py_XDECREF(temp);
356 frame->f_trace = result;
357 }
358 else {
359 Py_DECREF(result);
360 }
361 return 0;
362}
Fred Draked0838392001-06-16 21:02:31 +0000363
Fred Drake8b4d01d2000-05-09 19:57:01 +0000364static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000365sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000366{
Fred Drake5755ce62001-06-27 19:19:46 +0000367 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000368 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000369 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000370 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000371 else
Fred Drake5755ce62001-06-27 19:19:46 +0000372 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000373 Py_INCREF(Py_None);
374 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000375}
376
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000377PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000378"settrace(function)\n\
379\n\
380Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000381function call. See the debugger chapter in the library manual."
382);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000383
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000384static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000386{
Fred Drake5755ce62001-06-27 19:19:46 +0000387 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000388 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000389 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000390 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000391 else
Fred Drake5755ce62001-06-27 19:19:46 +0000392 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000393 Py_INCREF(Py_None);
394 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000395}
396
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000397PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000398"setprofile(function)\n\
399\n\
400Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000401and return. See the profiler chapter in the library manual."
402);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000403
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000404static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000406{
Skip Montanarod581d772002-09-03 20:10:45 +0000407 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000408 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000409 Py_INCREF(Py_None);
410 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000411}
412
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000413PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000414"setcheckinterval(n)\n\
415\n\
416Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000417n instructions. This also affects how often thread switches occur."
418);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000419
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000420static PyObject *
421sys_setrecursionlimit(PyObject *self, PyObject *args)
422{
423 int new_limit;
424 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
425 return NULL;
426 if (new_limit <= 0) {
427 PyErr_SetString(PyExc_ValueError,
428 "recursion limit must be positive");
429 return NULL;
430 }
431 Py_SetRecursionLimit(new_limit);
432 Py_INCREF(Py_None);
433 return Py_None;
434}
435
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000436PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000437"setrecursionlimit(n)\n\
438\n\
439Set the maximum depth of the Python interpreter stack to n. This\n\
440limit prevents infinite recursion from causing an overflow of the C\n\
441stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000442dependent."
443);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000444
445static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000446sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000447{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000448 return PyInt_FromLong(Py_GetRecursionLimit());
449}
450
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000451PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000452"getrecursionlimit()\n\
453\n\
454Return the current value of the recursion limit, the maximum depth\n\
455of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000456recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000457);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000458
Mark Hammond8696ebc2002-10-08 02:44:31 +0000459#ifdef MS_WINDOWS
460PyDoc_STRVAR(getwindowsversion_doc,
461"getwindowsversion()\n\
462\n\
463Return information about the running version of Windows.\n\
464The result is a tuple of (major, minor, build, platform, text)\n\
465All elements are numbers, except text which is a string.\n\
466Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
467"
468);
469
470static PyObject *
471sys_getwindowsversion(PyObject *self)
472{
473 OSVERSIONINFO ver;
474 ver.dwOSVersionInfoSize = sizeof(ver);
475 if (!GetVersionEx(&ver))
476 return PyErr_SetFromWindowsErr(0);
477 return Py_BuildValue("HHHHs",
478 ver.dwMajorVersion,
479 ver.dwMinorVersion,
480 ver.dwBuildNumber,
481 ver.dwPlatformId,
482 ver.szCSDVersion);
483}
484
485#endif /* MS_WINDOWS */
486
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000487#ifdef HAVE_DLOPEN
488static PyObject *
489sys_setdlopenflags(PyObject *self, PyObject *args)
490{
491 int new_val;
492 PyThreadState *tstate = PyThreadState_Get();
493 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
494 return NULL;
495 if (!tstate)
496 return NULL;
497 tstate->interp->dlopenflags = new_val;
498 Py_INCREF(Py_None);
499 return Py_None;
500}
501
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000502PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000503"setdlopenflags(n) -> None\n\
504\n\
505Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000506things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000507a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000508To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000509sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
510);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000511
512static PyObject *
513sys_getdlopenflags(PyObject *self, PyObject *args)
514{
515 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000516 if (!tstate)
517 return NULL;
518 return PyInt_FromLong(tstate->interp->dlopenflags);
519}
520
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000521PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000522"getdlopenflags() -> int\n\
523\n\
524Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000525calls. The flag constants are defined in the dl module."
526);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000527#endif
528
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000529#ifdef USE_MALLOPT
530/* Link with -lmalloc (or -lmpc) on an SGI */
531#include <malloc.h>
532
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000533static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000535{
536 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000537 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000538 return NULL;
539 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000540 Py_INCREF(Py_None);
541 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000542}
543#endif /* USE_MALLOPT */
544
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000545static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000546sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000547{
Mark Hammond440d8982000-06-20 08:12:48 +0000548 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000549}
550
Tim Peters4be93d02002-07-07 19:59:50 +0000551#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000552static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000553sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000554{
Mark Hammond440d8982000-06-20 08:12:48 +0000555 return PyInt_FromLong(_Py_RefTotal);
556}
557
558#endif /* Py_TRACE_REFS */
559
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000560PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000561"getrefcount(object) -> integer\n\
562\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000563Return the reference count of object. The count returned is generally\n\
564one higher than you might expect, because it includes the (temporary)\n\
565reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000566);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000567
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000568#ifdef COUNT_ALLOCS
569static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000570sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000571{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000572 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000573
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000574 return get_counts();
575}
576#endif
577
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000578PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000579"_getframe([depth]) -> frameobject\n\
580\n\
581Return a frame object from the call stack. If optional integer depth is\n\
582given, return the frame object that many calls below the top of the stack.\n\
583If that is deeper than the call stack, ValueError is raised. The default\n\
584for depth is zero, returning the frame at the top of the call stack.\n\
585\n\
586This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000587purposes only."
588);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000589
590static PyObject *
591sys_getframe(PyObject *self, PyObject *args)
592{
593 PyFrameObject *f = PyThreadState_Get()->frame;
594 int depth = -1;
595
596 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
597 return NULL;
598
599 while (depth > 0 && f != NULL) {
600 f = f->f_back;
601 --depth;
602 }
603 if (f == NULL) {
604 PyErr_SetString(PyExc_ValueError,
605 "call stack is not deep enough");
606 return NULL;
607 }
608 Py_INCREF(f);
609 return (PyObject*)f;
610}
611
Jeremy Hylton985eba52003-02-05 23:13:00 +0000612PyDoc_STRVAR(callstats_doc,
613"callstats() -> tuple of integers\n\
614\n\
615Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
616when Python was built. Otherwise, return None.\n\
617\n\
618When enabled, this function returns detailed, implementation-specific\n\
619details about the number of function calls executed. The return value is\n\
620a 11-tuple where the entries in the tuple are counts of:\n\
6210. all function calls\n\
6221. calls to PyFunction_Type objects\n\
6232. PyFunction calls that do not create an argument tuple\n\
6243. PyFunction calls that do not create an argument tuple\n\
625 and bypass PyEval_EvalCodeEx()\n\
6264. PyMethod calls\n\
6275. PyMethod calls on bound methods\n\
6286. PyType calls\n\
6297. PyCFunction calls\n\
6308. generator calls\n\
6319. All other calls\n\
63210. Number of stack pops performed by call_function()"
633);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000634
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000635#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000636/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000637extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000638#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000639
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000640#ifdef DYNAMIC_EXECUTION_PROFILE
641/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000642extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000643#endif
644
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000645static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000646 /* Might as well keep this in alphabetic order */
Jeremy Hylton985eba52003-02-05 23:13:00 +0000647 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
648 callstats_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000649 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000650 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
651 {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000652 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000653 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000654#ifdef Py_USING_UNICODE
Jeremy Hylton985eba52003-02-05 23:13:00 +0000655 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
656 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000657#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000658#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000659 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
660 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000661#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000662#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000663 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000664#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000665#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000666 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000667#endif
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000668#ifdef Py_USING_UNICODE
669 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
670 METH_NOARGS, getfilesystemencoding_doc},
671#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000672#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000673 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000674#endif
675#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000676 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000677#endif
Fred Drakea7688822001-10-24 20:47:48 +0000678 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000679 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000680 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000681 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000682#ifdef MS_WINDOWS
683 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
684 getwindowsversion_doc},
685#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000686#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000687 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000688#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000689#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000690 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000692#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000693 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000694 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000695#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000696 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
697 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000698#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000699 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000700 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000701 setrecursionlimit_doc},
Neal Norwitz290d31e2002-03-03 15:12:58 +0000702 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000703 {NULL, NULL} /* sentinel */
704};
705
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000706static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000707list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000708{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000709 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000710 int i;
711 if (list == NULL)
712 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000713 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000714 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000715 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000716 if (name == NULL)
717 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000718 PyList_Append(list, name);
719 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000720 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000721 if (PyList_Sort(list) != 0) {
722 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000723 list = NULL;
724 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000725 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000726 PyObject *v = PyList_AsTuple(list);
727 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000728 list = v;
729 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000730 return list;
731}
732
Guido van Rossum23fff912000-12-15 22:02:05 +0000733static PyObject *warnoptions = NULL;
734
735void
736PySys_ResetWarnOptions(void)
737{
738 if (warnoptions == NULL || !PyList_Check(warnoptions))
739 return;
740 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
741}
742
743void
744PySys_AddWarnOption(char *s)
745{
746 PyObject *str;
747
748 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
749 Py_XDECREF(warnoptions);
750 warnoptions = PyList_New(0);
751 if (warnoptions == NULL)
752 return;
753 }
754 str = PyString_FromString(s);
755 if (str != NULL) {
756 PyList_Append(warnoptions, str);
757 Py_DECREF(str);
758 }
759}
760
Guido van Rossum40552d01998-08-06 03:34:39 +0000761/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
762 Two literals concatenated works just fine. If you have a K&R compiler
763 or other abomination that however *does* understand longer strings,
764 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000765PyDoc_VAR(sys_doc) =
766PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000767"This module provides access to some objects used or maintained by the\n\
768interpreter and to functions that interact strongly with the interpreter.\n\
769\n\
770Dynamic objects:\n\
771\n\
772argv -- command line arguments; argv[0] is the script pathname if known\n\
773path -- module search path; path[0] is the script directory, else ''\n\
774modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000775\n\
776displayhook -- called to show results in an interactive session\n\
777excepthook -- called to handle any uncaught exception other than SystemExit\n\
778 To customize printing in an interactive session or to install a custom\n\
779 top-level exception handler, assign other functions to replace these.\n\
780\n\
781exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
782 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000783\n\
784stdin -- standard input file object; used by raw_input() and input()\n\
785stdout -- standard output file object; used by the print statement\n\
786stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000787 By assigning other file objects (or objects that behave like files)\n\
788 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000789\n\
790last_type -- type of last uncaught exception\n\
791last_value -- value of last uncaught exception\n\
792last_traceback -- traceback of last uncaught exception\n\
793 These three are only available in an interactive session after a\n\
794 traceback has been printed.\n\
795\n\
796exc_type -- type of exception currently being handled\n\
797exc_value -- value of exception currently being handled\n\
798exc_traceback -- traceback of exception currently being handled\n\
799 The function exc_info() should be used instead of these three,\n\
800 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000801"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000802)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000803/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000804PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000805"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000806Static objects:\n\
807\n\
808maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000809maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000810builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000811version -- the version of this interpreter as a string\n\
812version_info -- version information as a tuple\n\
813hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000814copyright -- copyright notice pertaining to this interpreter\n\
815platform -- platform identifier\n\
816executable -- pathname of this Python interpreter\n\
817prefix -- prefix used to find the Python library\n\
818exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000819"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000820)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000821#ifdef MS_WINDOWS
822/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000823PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000824"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000825winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000826"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000827)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000828#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000829PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000830"__stdin__ -- the original stdin; don't touch!\n\
831__stdout__ -- the original stdout; don't touch!\n\
832__stderr__ -- the original stderr; don't touch!\n\
833__displayhook__ -- the original displayhook; don't touch!\n\
834__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000835\n\
836Functions:\n\
837\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000838displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000839excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000840exc_info() -- return thread-safe information about the current exception\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000841exc_clear() -- clear the exception state for the current thread\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000842exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000843getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000844getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000845getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000846setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000847setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000848setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000849setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000850settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000851"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000852)
Fred Drakeccede592000-08-14 20:59:57 +0000853/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000854
Guido van Rossum25ce5661997-08-02 03:10:38 +0000855PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000856_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000858 PyObject *m, *v, *sysdict;
859 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000860 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000861
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000862 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000863 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000864
865 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
866 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
867 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000868 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000869 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000870 PyDict_SetItemString(sysdict, "stdin", sysin);
871 PyDict_SetItemString(sysdict, "stdout", sysout);
872 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000873 /* Make backup copies for cleanup */
874 PyDict_SetItemString(sysdict, "__stdin__", sysin);
875 PyDict_SetItemString(sysdict, "__stdout__", sysout);
876 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000877 PyDict_SetItemString(sysdict, "__displayhook__",
878 PyDict_GetItemString(sysdict, "displayhook"));
879 PyDict_SetItemString(sysdict, "__excepthook__",
880 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000881 Py_XDECREF(sysin);
882 Py_XDECREF(sysout);
883 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000884 PyDict_SetItemString(sysdict, "version",
885 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000886 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000887 PyDict_SetItemString(sysdict, "hexversion",
888 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000889 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000890 /*
891 * These release level checks are mutually exclusive and cover
892 * the field, so don't get too fancy with the pre-processor!
893 */
894#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000895 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000896#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000897 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000898#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000899 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000900#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000901 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000902#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000903 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000904 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000905 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000906 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000907 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000908 Py_XDECREF(v);
Skip Montanaro8e790e72002-09-03 13:25:17 +0000909 PyDict_SetItemString(sysdict, "api_version",
910 v = PyInt_FromLong(PYTHON_API_VERSION));
911 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000912 PyDict_SetItemString(sysdict, "copyright",
913 v = PyString_FromString(Py_GetCopyright()));
914 Py_XDECREF(v);
915 PyDict_SetItemString(sysdict, "platform",
916 v = PyString_FromString(Py_GetPlatform()));
917 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000918 PyDict_SetItemString(sysdict, "executable",
919 v = PyString_FromString(Py_GetProgramFullPath()));
920 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000921 PyDict_SetItemString(sysdict, "prefix",
922 v = PyString_FromString(Py_GetPrefix()));
923 Py_XDECREF(v);
924 PyDict_SetItemString(sysdict, "exec_prefix",
925 v = PyString_FromString(Py_GetExecPrefix()));
926 Py_XDECREF(v);
927 PyDict_SetItemString(sysdict, "maxint",
928 v = PyInt_FromLong(PyInt_GetMax()));
929 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000930#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000931 PyDict_SetItemString(sysdict, "maxunicode",
932 v = PyInt_FromLong(PyUnicode_GetMax()));
933 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000934#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000935 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000937 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000938 {
939 /* Assumes that longs are at least 2 bytes long.
940 Should be safe! */
941 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000942 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000943
944 s = (char *) &number;
945 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000946 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000947 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000948 value = "little";
949 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000950 v = PyString_FromString(value));
951 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000952 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000953#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000954 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000955 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000956 Py_XDECREF(v);
957 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000958 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000959 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000960#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000961 if (warnoptions == NULL) {
962 warnoptions = PyList_New(0);
963 }
964 else {
965 Py_INCREF(warnoptions);
966 }
967 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000968 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000969 }
970
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000971 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000972 return NULL;
973 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000974}
975
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000976static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000977makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000978{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000979 int i, n;
980 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000981 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000982
983 n = 1;
984 p = path;
985 while ((p = strchr(p, delim)) != NULL) {
986 n++;
987 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000988 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000989 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000990 if (v == NULL)
991 return NULL;
992 for (i = 0; ; i++) {
993 p = strchr(path, delim);
994 if (p == NULL)
995 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000996 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000998 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000999 return NULL;
1000 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001001 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001002 if (*p == '\0')
1003 break;
1004 path = p+1;
1005 }
1006 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001007}
1008
1009void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001010PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001011{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001012 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001013 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001014 Py_FatalError("can't create sys.path");
1015 if (PySys_SetObject("path", v) != 0)
1016 Py_FatalError("can't assign sys.path");
1017 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001018}
1019
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001020static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001021makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001022{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001023 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +00001024 if (argc <= 0 || argv == NULL) {
1025 /* Ensure at least one (empty) argument is seen */
1026 static char *empty_argv[1] = {""};
1027 argv = empty_argv;
1028 argc = 1;
1029 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001030 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001031 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001032 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001033 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001034 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001035 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001036 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001037 av = NULL;
1038 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001039 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001040 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001041 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001042 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001043 return av;
1044}
1045
1046void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001047PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001048{
Guido van Rossum162e38c2003-02-19 15:25:10 +00001049#if defined(HAVE_REALPATH)
1050 char fullpath[MAXPATHLEN];
1051#elif defined(MS_WINDOWS)
Thomas Heller27bb71e2003-01-08 14:33:48 +00001052 char fullpath[MAX_PATH];
1053#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001054 PyObject *av = makeargvobject(argc, argv);
1055 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001056 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001057 Py_FatalError("no mem for sys.argv");
1058 if (PySys_SetObject("argv", av) != 0)
1059 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001060 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001061 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001062 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +00001063 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001064 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001065#ifdef HAVE_READLINK
1066 char link[MAXPATHLEN+1];
1067 char argv0copy[2*MAXPATHLEN+1];
1068 int nr = 0;
1069 if (argc > 0 && argv0 != NULL)
1070 nr = readlink(argv0, link, MAXPATHLEN);
1071 if (nr > 0) {
1072 /* It's a symlink */
1073 link[nr] = '\0';
1074 if (link[0] == SEP)
1075 argv0 = link; /* Link to absolute path */
1076 else if (strchr(link, SEP) == NULL)
1077 ; /* Link without path */
1078 else {
1079 /* Must join(dirname(argv0), link) */
1080 char *q = strrchr(argv0, SEP);
1081 if (q == NULL)
1082 argv0 = link; /* argv0 without path */
1083 else {
1084 /* Must make a copy */
1085 strcpy(argv0copy, argv0);
1086 q = strrchr(argv0copy, SEP);
1087 strcpy(q+1, link);
1088 argv0 = argv0copy;
1089 }
1090 }
1091 }
1092#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001093#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001094 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001095 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001096#ifdef MS_WINDOWS
1097 char *ptemp;
1098 if (GetFullPathName(argv0,
1099 sizeof(fullpath),
1100 fullpath,
1101 &ptemp)) {
1102 argv0 = fullpath;
1103 }
1104#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001105 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001106 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001107 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001108 if (q != NULL)
1109 p = q;
1110 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001111 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001112 if (n > 1 && p[-1] != ':')
1113 n--; /* Drop trailing separator */
1114 }
1115 }
1116#else /* All other filename syntaxes */
Guido van Rossum162e38c2003-02-19 15:25:10 +00001117 if (argc > 0 && argv0 != NULL) {
1118#if defined(HAVE_REALPATH)
1119 if (realpath(argv0, fullpath)) {
1120 argv0 = fullpath;
1121 }
1122#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001123 p = strrchr(argv0, SEP);
Guido van Rossum162e38c2003-02-19 15:25:10 +00001124 }
Guido van Rossumcc883411996-09-10 14:44:21 +00001125 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001126#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001127 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001128#else /* don't include trailing separator */
1129 n = p - argv0;
1130#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001131#if SEP == '/' /* Special case for Unix filename syntax */
1132 if (n > 1)
1133 n--; /* Drop trailing separator */
1134#endif /* Unix */
1135 }
1136#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001137 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001138 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001139 Py_FatalError("no mem for sys.path insertion");
1140 if (PyList_Insert(path, 0, a) < 0)
1141 Py_FatalError("sys.path.insert(0) failed");
1142 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001143 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001144 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001145}
Guido van Rossuma890e681998-05-12 14:59:24 +00001146
1147
1148/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1149 Adapted from code submitted by Just van Rossum.
1150
1151 PySys_WriteStdout(format, ...)
1152 PySys_WriteStderr(format, ...)
1153
1154 The first function writes to sys.stdout; the second to sys.stderr. When
1155 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001156 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001157
1158 Both take a printf-style format string as their first argument followed
1159 by a variable length argument list determined by the format string.
1160
1161 *** WARNING ***
1162
1163 The format should limit the total size of the formatted output string to
1164 1000 bytes. In particular, this means that no unrestricted "%s" formats
1165 should occur; these should be limited using "%.<N>s where <N> is a
1166 decimal number calculated so that <N> plus the maximum size of other
1167 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1168 which can print hundreds of digits for very large numbers.
1169
1170 */
1171
1172static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001173mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001174{
1175 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001176 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001177
Guido van Rossum8442af31998-10-12 18:22:10 +00001178 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001179 file = PySys_GetObject(name);
1180 if (file == NULL || PyFile_AsFile(file) == fp)
1181 vfprintf(fp, format, va);
1182 else {
1183 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001184 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1185 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001186 if (PyFile_WriteString(buffer, file) != 0) {
1187 PyErr_Clear();
1188 fputs(buffer, fp);
1189 }
Tim Petersfaad5ad2001-12-03 00:43:33 +00001190 if (written < 0 || written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001191 const char *truncated = "... truncated";
1192 if (PyFile_WriteString(truncated, file) != 0) {
1193 PyErr_Clear();
1194 fputs(truncated, fp);
1195 }
1196 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001197 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001198 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001199}
1200
1201void
Guido van Rossuma890e681998-05-12 14:59:24 +00001202PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001203{
1204 va_list va;
1205
Guido van Rossuma890e681998-05-12 14:59:24 +00001206 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001207 mywrite("stdout", stdout, format, va);
1208 va_end(va);
1209}
1210
1211void
Guido van Rossuma890e681998-05-12 14:59:24 +00001212PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001213{
1214 va_list va;
1215
Guido van Rossuma890e681998-05-12 14:59:24 +00001216 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001217 mywrite("stderr", stderr, format, va);
1218 va_end(va);
1219}