blob: d06d18a98b40fe48409ebe198dacc5536e128679 [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
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000035#ifdef __VMS
36#include <unixlib.h>
37#endif
38
Guido van Rossum65bf9f21997-04-29 18:33:38 +000039PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000040PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041{
Guido van Rossum25ce5661997-08-02 03:10:38 +000042 PyThreadState *tstate = PyThreadState_Get();
43 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000044 if (sd == NULL)
45 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000046 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047}
48
49FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000050PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
52 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000053 PyObject *v = PySys_GetObject(name);
54 if (v != NULL && PyFile_Check(v))
55 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 if (fp == NULL)
57 fp = def;
58 return fp;
59}
60
61int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000062PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
Guido van Rossum25ce5661997-08-02 03:10:38 +000064 PyThreadState *tstate = PyThreadState_Get();
65 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000066 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000067 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000068 return 0;
69 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000070 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000071 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000073 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
Guido van Rossum65bf9f21997-04-29 18:33:38 +000076static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000077sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000078{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000079 PyObject *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000080 PyInterpreterState *interp = PyThreadState_Get()->interp;
81 PyObject *modules = interp->modules;
82 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
83
Moshe Zadka03897ea2001-07-23 13:32:43 +000084 if (builtins == NULL) {
85 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
86 return NULL;
87 }
88
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000089 /* Print value except if None */
90 /* After printing, also assign to '_' */
91 /* Before, set '_' to None to avoid recursion */
92 if (o == Py_None) {
93 Py_INCREF(Py_None);
94 return Py_None;
95 }
96 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
97 return NULL;
98 if (Py_FlushLine() != 0)
99 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000100 outf = PySys_GetObject("stdout");
101 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000102 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
103 return NULL;
104 }
Greg Steinceb9b7c2001-01-11 09:27:34 +0000105 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000106 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000107 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000108 if (Py_FlushLine() != 0)
109 return NULL;
110 if (PyObject_SetAttrString(builtins, "_", o) != 0)
111 return NULL;
112 Py_INCREF(Py_None);
113 return Py_None;
114}
115
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000116PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000117"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000118"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000119"Print an object to sys.stdout and also save it in __builtin__._\n"
120);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000121
122static PyObject *
123sys_excepthook(PyObject* self, PyObject* args)
124{
125 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000126 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000127 return NULL;
128 PyErr_Display(exc, value, tb);
129 Py_INCREF(Py_None);
130 return Py_None;
131}
132
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000133PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000134"excepthook(exctype, value, traceback) -> None\n"
135"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000136"Handle an exception by displaying it with a traceback on sys.stderr.\n"
137);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000138
139static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000140sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000141{
142 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000143 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000144 return Py_BuildValue(
145 "(OOO)",
146 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
147 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
148 tstate->exc_traceback != NULL ?
149 tstate->exc_traceback : Py_None);
150}
151
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000152PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000153"exc_info() -> (type, value, traceback)\n\
154\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000155Return information about the most recent exception caught by an except\n\
156clause in the current stack frame or in an older stack frame."
157);
158
159static PyObject *
160sys_exc_clear(PyObject *self, PyObject *noargs)
161{
162 PyThreadState *tstate = PyThreadState_Get();
163 PyObject *tmp_type, *tmp_value, *tmp_tb;
164 tmp_type = tstate->exc_type;
165 tmp_value = tstate->exc_value;
166 tmp_tb = tstate->exc_traceback;
167 tstate->exc_type = NULL;
168 tstate->exc_value = NULL;
169 tstate->exc_traceback = NULL;
170 Py_XDECREF(tmp_type);
171 Py_XDECREF(tmp_value);
172 Py_XDECREF(tmp_tb);
173 /* For b/w compatibility */
174 PySys_SetObject("exc_type", Py_None);
175 PySys_SetObject("exc_value", Py_None);
176 PySys_SetObject("exc_traceback", Py_None);
177 Py_INCREF(Py_None);
178 return Py_None;
179}
180
181PyDoc_STRVAR(exc_clear_doc,
182"exc_clear() -> None\n\
183\n\
184Clear global information on the current exception. Subsequent calls to\n\
185exc_info() will return (None,None,None) until another exception is raised\n\
186in the current thread or the execution stack returns to a frame where\n\
187another exception is being handled."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000188);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000189
Guido van Rossuma027efa1997-05-05 20:56:21 +0000190static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000191sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000193 PyObject *exit_code = 0;
194 if (!PyArg_ParseTuple(args, "|O:exit", &exit_code))
195 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000196 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000197 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000198 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199}
200
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000201PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000202"exit([status])\n\
203\n\
204Exit the interpreter by raising SystemExit(status).\n\
205If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000206If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000207If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000208exit status will be one (i.e., failure)."
209);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000210
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000211#ifdef Py_USING_UNICODE
212
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000213static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000214sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000215{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000216 return PyString_FromString(PyUnicode_GetDefaultEncoding());
217}
218
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000219PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000220"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000221\n\
222Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000223implementation."
224);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000225
226static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000228{
229 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000230 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000231 return NULL;
232 if (PyUnicode_SetDefaultEncoding(encoding))
233 return NULL;
234 Py_INCREF(Py_None);
235 return Py_None;
236}
237
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000238PyDoc_STRVAR(setdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000239"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000240\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000241Set the current default string encoding used by the Unicode implementation."
242);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000243
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000244static PyObject *
245sys_getfilesystemencoding(PyObject *self)
246{
247 if (Py_FileSystemDefaultEncoding)
248 return PyString_FromString(Py_FileSystemDefaultEncoding);
249 Py_INCREF(Py_None);
250 return Py_None;
251}
252
253PyDoc_STRVAR(getfilesystemencoding_doc,
254"getfilesystemencoding() -> string\n\
255\n\
256Return the encoding used to convert Unicode filenames in\n\
257operating system filenames."
258);
259
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000260#endif
261
Fred Drake5755ce62001-06-27 19:19:46 +0000262/*
263 * Cached interned string objects used for calling the profile and
264 * trace functions. Initialized by trace_init().
265 */
266static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
267
268static int
269trace_init(void)
270{
271 static char *whatnames[4] = {"call", "exception", "line", "return"};
272 PyObject *name;
273 int i;
274 for (i = 0; i < 4; ++i) {
275 if (whatstrings[i] == NULL) {
276 name = PyString_InternFromString(whatnames[i]);
277 if (name == NULL)
278 return -1;
279 whatstrings[i] = name;
280 }
281 }
282 return 0;
283}
284
285
286static PyObject *
287call_trampoline(PyThreadState *tstate, PyObject* callback,
288 PyFrameObject *frame, int what, PyObject *arg)
289{
290 PyObject *args = PyTuple_New(3);
291 PyObject *whatstr;
292 PyObject *result;
293
294 if (args == NULL)
295 return NULL;
296 Py_INCREF(frame);
297 whatstr = whatstrings[what];
298 Py_INCREF(whatstr);
299 if (arg == NULL)
300 arg = Py_None;
301 Py_INCREF(arg);
302 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
303 PyTuple_SET_ITEM(args, 1, whatstr);
304 PyTuple_SET_ITEM(args, 2, arg);
305
306 /* call the Python-level function */
307 PyFrame_FastToLocals(frame);
308 result = PyEval_CallObject(callback, args);
309 PyFrame_LocalsToFast(frame, 1);
310 if (result == NULL)
311 PyTraceBack_Here(frame);
312
313 /* cleanup */
314 Py_DECREF(args);
315 return result;
316}
317
318static int
319profile_trampoline(PyObject *self, PyFrameObject *frame,
320 int what, PyObject *arg)
321{
322 PyThreadState *tstate = frame->f_tstate;
323 PyObject *result;
324
Fred Drake8f51f542001-10-04 14:48:42 +0000325 if (arg == NULL)
326 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000327 result = call_trampoline(tstate, self, frame, what, arg);
328 if (result == NULL) {
329 PyEval_SetProfile(NULL, NULL);
330 return -1;
331 }
332 Py_DECREF(result);
333 return 0;
334}
335
336static int
337trace_trampoline(PyObject *self, PyFrameObject *frame,
338 int what, PyObject *arg)
339{
340 PyThreadState *tstate = frame->f_tstate;
341 PyObject *callback;
342 PyObject *result;
343
344 if (what == PyTrace_CALL)
345 callback = self;
346 else
347 callback = frame->f_trace;
348 if (callback == NULL)
349 return 0;
350 result = call_trampoline(tstate, callback, frame, what, arg);
351 if (result == NULL) {
352 PyEval_SetTrace(NULL, NULL);
353 Py_XDECREF(frame->f_trace);
354 frame->f_trace = NULL;
355 return -1;
356 }
357 if (result != Py_None) {
358 PyObject *temp = frame->f_trace;
359 frame->f_trace = NULL;
360 Py_XDECREF(temp);
361 frame->f_trace = result;
362 }
363 else {
364 Py_DECREF(result);
365 }
366 return 0;
367}
Fred Draked0838392001-06-16 21:02:31 +0000368
Fred Drake8b4d01d2000-05-09 19:57:01 +0000369static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000370sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000371{
Fred Drake5755ce62001-06-27 19:19:46 +0000372 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000373 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000374 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000375 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000376 else
Fred Drake5755ce62001-06-27 19:19:46 +0000377 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000378 Py_INCREF(Py_None);
379 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000380}
381
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000382PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000383"settrace(function)\n\
384\n\
385Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000386function call. See the debugger chapter in the library manual."
387);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000388
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000389static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000391{
Fred Drake5755ce62001-06-27 19:19:46 +0000392 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000393 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000394 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000395 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000396 else
Fred Drake5755ce62001-06-27 19:19:46 +0000397 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000398 Py_INCREF(Py_None);
399 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000400}
401
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000402PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000403"setprofile(function)\n\
404\n\
405Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000406and return. See the profiler chapter in the library manual."
407);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000408
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000409static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000410sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000411{
Skip Montanarod581d772002-09-03 20:10:45 +0000412 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000413 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000414 Py_INCREF(Py_None);
415 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000416}
417
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000418PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000419"setcheckinterval(n)\n\
420\n\
421Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000422n instructions. This also affects how often thread switches occur."
423);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000424
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000425static PyObject *
426sys_setrecursionlimit(PyObject *self, PyObject *args)
427{
428 int new_limit;
429 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
430 return NULL;
431 if (new_limit <= 0) {
432 PyErr_SetString(PyExc_ValueError,
433 "recursion limit must be positive");
434 return NULL;
435 }
436 Py_SetRecursionLimit(new_limit);
437 Py_INCREF(Py_None);
438 return Py_None;
439}
440
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000441PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000442"setrecursionlimit(n)\n\
443\n\
444Set the maximum depth of the Python interpreter stack to n. This\n\
445limit prevents infinite recursion from causing an overflow of the C\n\
446stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000447dependent."
448);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000449
450static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000451sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000452{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000453 return PyInt_FromLong(Py_GetRecursionLimit());
454}
455
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000456PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000457"getrecursionlimit()\n\
458\n\
459Return the current value of the recursion limit, the maximum depth\n\
460of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000461recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000462);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000463
Mark Hammond8696ebc2002-10-08 02:44:31 +0000464#ifdef MS_WINDOWS
465PyDoc_STRVAR(getwindowsversion_doc,
466"getwindowsversion()\n\
467\n\
468Return information about the running version of Windows.\n\
469The result is a tuple of (major, minor, build, platform, text)\n\
470All elements are numbers, except text which is a string.\n\
471Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
472"
473);
474
475static PyObject *
476sys_getwindowsversion(PyObject *self)
477{
478 OSVERSIONINFO ver;
479 ver.dwOSVersionInfoSize = sizeof(ver);
480 if (!GetVersionEx(&ver))
481 return PyErr_SetFromWindowsErr(0);
482 return Py_BuildValue("HHHHs",
483 ver.dwMajorVersion,
484 ver.dwMinorVersion,
485 ver.dwBuildNumber,
486 ver.dwPlatformId,
487 ver.szCSDVersion);
488}
489
490#endif /* MS_WINDOWS */
491
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000492#ifdef HAVE_DLOPEN
493static PyObject *
494sys_setdlopenflags(PyObject *self, PyObject *args)
495{
496 int new_val;
497 PyThreadState *tstate = PyThreadState_Get();
498 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
499 return NULL;
500 if (!tstate)
501 return NULL;
502 tstate->interp->dlopenflags = new_val;
503 Py_INCREF(Py_None);
504 return Py_None;
505}
506
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000507PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000508"setdlopenflags(n) -> None\n\
509\n\
510Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000511things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000512a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000513To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000514sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
515);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000516
517static PyObject *
518sys_getdlopenflags(PyObject *self, PyObject *args)
519{
520 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000521 if (!tstate)
522 return NULL;
523 return PyInt_FromLong(tstate->interp->dlopenflags);
524}
525
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000526PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000527"getdlopenflags() -> int\n\
528\n\
529Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000530calls. The flag constants are defined in the dl module."
531);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000532#endif
533
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000534#ifdef USE_MALLOPT
535/* Link with -lmalloc (or -lmpc) on an SGI */
536#include <malloc.h>
537
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000538static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000539sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000540{
541 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000542 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000543 return NULL;
544 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000545 Py_INCREF(Py_None);
546 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000547}
548#endif /* USE_MALLOPT */
549
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000550static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000551sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000552{
Mark Hammond440d8982000-06-20 08:12:48 +0000553 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000554}
555
Tim Peters4be93d02002-07-07 19:59:50 +0000556#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000557static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000558sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000559{
Mark Hammond440d8982000-06-20 08:12:48 +0000560 return PyInt_FromLong(_Py_RefTotal);
561}
562
563#endif /* Py_TRACE_REFS */
564
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000565PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000566"getrefcount(object) -> integer\n\
567\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000568Return the reference count of object. The count returned is generally\n\
569one higher than you might expect, because it includes the (temporary)\n\
570reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000571);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000572
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000573#ifdef COUNT_ALLOCS
574static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000575sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000576{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000577 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000578
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000579 return get_counts();
580}
581#endif
582
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000583PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000584"_getframe([depth]) -> frameobject\n\
585\n\
586Return a frame object from the call stack. If optional integer depth is\n\
587given, return the frame object that many calls below the top of the stack.\n\
588If that is deeper than the call stack, ValueError is raised. The default\n\
589for depth is zero, returning the frame at the top of the call stack.\n\
590\n\
591This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000592purposes only."
593);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000594
595static PyObject *
596sys_getframe(PyObject *self, PyObject *args)
597{
598 PyFrameObject *f = PyThreadState_Get()->frame;
599 int depth = -1;
600
601 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
602 return NULL;
603
604 while (depth > 0 && f != NULL) {
605 f = f->f_back;
606 --depth;
607 }
608 if (f == NULL) {
609 PyErr_SetString(PyExc_ValueError,
610 "call stack is not deep enough");
611 return NULL;
612 }
613 Py_INCREF(f);
614 return (PyObject*)f;
615}
616
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000617PyDoc_STRVAR(call_tracing_doc,
618"call_tracing(func, args) -> object\n\
619\n\
620Call func(*args), while tracing is enabled. The tracing state is\n\
621saved, and restored afterwards. This is intended to be called from\n\
622a debugger from a checkpoint, to recursively debug some other code."
623);
624
625static PyObject *
626sys_call_tracing(PyObject *self, PyObject *args)
627{
628 PyObject *func, *funcargs;
629 if (!PyArg_ParseTuple(args, "OO:call_tracing", &func, &funcargs))
630 return NULL;
631 return _PyEval_CallTracing(func, funcargs);
632}
633
Jeremy Hylton985eba52003-02-05 23:13:00 +0000634PyDoc_STRVAR(callstats_doc,
635"callstats() -> tuple of integers\n\
636\n\
637Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
638when Python was built. Otherwise, return None.\n\
639\n\
640When enabled, this function returns detailed, implementation-specific\n\
641details about the number of function calls executed. The return value is\n\
642a 11-tuple where the entries in the tuple are counts of:\n\
6430. all function calls\n\
6441. calls to PyFunction_Type objects\n\
6452. PyFunction calls that do not create an argument tuple\n\
6463. PyFunction calls that do not create an argument tuple\n\
647 and bypass PyEval_EvalCodeEx()\n\
6484. PyMethod calls\n\
6495. PyMethod calls on bound methods\n\
6506. PyType calls\n\
6517. PyCFunction calls\n\
6528. generator calls\n\
6539. All other calls\n\
65410. Number of stack pops performed by call_function()"
655);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000656
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000657#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000658/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000659extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000660#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000661
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000662#ifdef DYNAMIC_EXECUTION_PROFILE
663/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000664extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000665#endif
666
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000667static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000668 /* Might as well keep this in alphabetic order */
Jeremy Hylton985eba52003-02-05 23:13:00 +0000669 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
670 callstats_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000671 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000672 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
673 {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000674 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000675 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000676#ifdef Py_USING_UNICODE
Jeremy Hylton985eba52003-02-05 23:13:00 +0000677 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
678 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000679#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000680#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000681 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
682 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000683#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000684#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000685 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000686#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000687#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000688 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000689#endif
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000690#ifdef Py_USING_UNICODE
691 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
692 METH_NOARGS, getfilesystemencoding_doc},
693#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000694#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000695 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000696#endif
697#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000698 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000699#endif
Fred Drakea7688822001-10-24 20:47:48 +0000700 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000701 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000702 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000703 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000704#ifdef MS_WINDOWS
705 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
706 getwindowsversion_doc},
707#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000708#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000709 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000710#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000711#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000712 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000713 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000714#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000715 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000716 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000717#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000718 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
719 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000720#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000721 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000722 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000723 setrecursionlimit_doc},
Neal Norwitz290d31e2002-03-03 15:12:58 +0000724 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000725 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000726 {NULL, NULL} /* sentinel */
727};
728
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000729static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000730list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000731{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000732 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000733 int i;
734 if (list == NULL)
735 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000736 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000737 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000738 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000739 if (name == NULL)
740 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000741 PyList_Append(list, name);
742 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000743 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000744 if (PyList_Sort(list) != 0) {
745 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000746 list = NULL;
747 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000748 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000749 PyObject *v = PyList_AsTuple(list);
750 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000751 list = v;
752 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000753 return list;
754}
755
Guido van Rossum23fff912000-12-15 22:02:05 +0000756static PyObject *warnoptions = NULL;
757
758void
759PySys_ResetWarnOptions(void)
760{
761 if (warnoptions == NULL || !PyList_Check(warnoptions))
762 return;
763 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
764}
765
766void
767PySys_AddWarnOption(char *s)
768{
769 PyObject *str;
770
771 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
772 Py_XDECREF(warnoptions);
773 warnoptions = PyList_New(0);
774 if (warnoptions == NULL)
775 return;
776 }
777 str = PyString_FromString(s);
778 if (str != NULL) {
779 PyList_Append(warnoptions, str);
780 Py_DECREF(str);
781 }
782}
783
Guido van Rossum40552d01998-08-06 03:34:39 +0000784/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
785 Two literals concatenated works just fine. If you have a K&R compiler
786 or other abomination that however *does* understand longer strings,
787 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000788PyDoc_VAR(sys_doc) =
789PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000790"This module provides access to some objects used or maintained by the\n\
791interpreter and to functions that interact strongly with the interpreter.\n\
792\n\
793Dynamic objects:\n\
794\n\
795argv -- command line arguments; argv[0] is the script pathname if known\n\
796path -- module search path; path[0] is the script directory, else ''\n\
797modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000798\n\
799displayhook -- called to show results in an interactive session\n\
800excepthook -- called to handle any uncaught exception other than SystemExit\n\
801 To customize printing in an interactive session or to install a custom\n\
802 top-level exception handler, assign other functions to replace these.\n\
803\n\
804exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
805 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000806\n\
807stdin -- standard input file object; used by raw_input() and input()\n\
808stdout -- standard output file object; used by the print statement\n\
809stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000810 By assigning other file objects (or objects that behave like files)\n\
811 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000812\n\
813last_type -- type of last uncaught exception\n\
814last_value -- value of last uncaught exception\n\
815last_traceback -- traceback of last uncaught exception\n\
816 These three are only available in an interactive session after a\n\
817 traceback has been printed.\n\
818\n\
819exc_type -- type of exception currently being handled\n\
820exc_value -- value of exception currently being handled\n\
821exc_traceback -- traceback of exception currently being handled\n\
822 The function exc_info() should be used instead of these three,\n\
823 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000824"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000825)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000826/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000827PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000828"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000829Static objects:\n\
830\n\
831maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000832maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000833builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000834version -- the version of this interpreter as a string\n\
835version_info -- version information as a tuple\n\
836hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000837copyright -- copyright notice pertaining to this interpreter\n\
838platform -- platform identifier\n\
839executable -- pathname of this Python interpreter\n\
840prefix -- prefix used to find the Python library\n\
841exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000842"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000843)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000844#ifdef MS_WINDOWS
845/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000846PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000847"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000848winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000849"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000850)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000851#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000852PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000853"__stdin__ -- the original stdin; don't touch!\n\
854__stdout__ -- the original stdout; don't touch!\n\
855__stderr__ -- the original stderr; don't touch!\n\
856__displayhook__ -- the original displayhook; don't touch!\n\
857__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000858\n\
859Functions:\n\
860\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000861displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000862excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000863exc_info() -- return thread-safe information about the current exception\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000864exc_clear() -- clear the exception state for the current thread\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000865exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000866getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000867getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000868getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000869setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000870setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000871setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000872setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000873settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000874"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000875)
Fred Drakeccede592000-08-14 20:59:57 +0000876/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000877
Guido van Rossum25ce5661997-08-02 03:10:38 +0000878PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000879_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000880{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000881 PyObject *m, *v, *sysdict;
882 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000883 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000884
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000885 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000886 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000887
888 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
889 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
890 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000891 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000892 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000893 PyDict_SetItemString(sysdict, "stdin", sysin);
894 PyDict_SetItemString(sysdict, "stdout", sysout);
895 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000896 /* Make backup copies for cleanup */
897 PyDict_SetItemString(sysdict, "__stdin__", sysin);
898 PyDict_SetItemString(sysdict, "__stdout__", sysout);
899 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000900 PyDict_SetItemString(sysdict, "__displayhook__",
901 PyDict_GetItemString(sysdict, "displayhook"));
902 PyDict_SetItemString(sysdict, "__excepthook__",
903 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000904 Py_XDECREF(sysin);
905 Py_XDECREF(sysout);
906 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000907 PyDict_SetItemString(sysdict, "version",
908 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000909 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000910 PyDict_SetItemString(sysdict, "hexversion",
911 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000912 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000913 /*
914 * These release level checks are mutually exclusive and cover
915 * the field, so don't get too fancy with the pre-processor!
916 */
917#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000918 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000919#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000920 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000921#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000922 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000923#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000924 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000925#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000926 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000927 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000928 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000929 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000930 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000931 Py_XDECREF(v);
Skip Montanaro8e790e72002-09-03 13:25:17 +0000932 PyDict_SetItemString(sysdict, "api_version",
933 v = PyInt_FromLong(PYTHON_API_VERSION));
934 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000935 PyDict_SetItemString(sysdict, "copyright",
936 v = PyString_FromString(Py_GetCopyright()));
937 Py_XDECREF(v);
938 PyDict_SetItemString(sysdict, "platform",
939 v = PyString_FromString(Py_GetPlatform()));
940 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000941 PyDict_SetItemString(sysdict, "executable",
942 v = PyString_FromString(Py_GetProgramFullPath()));
943 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000944 PyDict_SetItemString(sysdict, "prefix",
945 v = PyString_FromString(Py_GetPrefix()));
946 Py_XDECREF(v);
947 PyDict_SetItemString(sysdict, "exec_prefix",
948 v = PyString_FromString(Py_GetExecPrefix()));
949 Py_XDECREF(v);
950 PyDict_SetItemString(sysdict, "maxint",
951 v = PyInt_FromLong(PyInt_GetMax()));
952 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000953#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000954 PyDict_SetItemString(sysdict, "maxunicode",
955 v = PyInt_FromLong(PyUnicode_GetMax()));
956 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000957#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000958 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000960 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000961 {
962 /* Assumes that longs are at least 2 bytes long.
963 Should be safe! */
964 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000965 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000966
967 s = (char *) &number;
968 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000969 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000970 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000971 value = "little";
972 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000973 v = PyString_FromString(value));
974 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000975 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000976#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000977 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000978 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000979 Py_XDECREF(v);
980 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000981 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000982 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000983#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000984 if (warnoptions == NULL) {
985 warnoptions = PyList_New(0);
986 }
987 else {
988 Py_INCREF(warnoptions);
989 }
990 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000991 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000992 }
993
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000994 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000995 return NULL;
996 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000997}
998
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000999static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001000makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001001{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001002 int i, n;
1003 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001004 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001005
1006 n = 1;
1007 p = path;
1008 while ((p = strchr(p, delim)) != NULL) {
1009 n++;
1010 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001011 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001012 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001013 if (v == NULL)
1014 return NULL;
1015 for (i = 0; ; i++) {
1016 p = strchr(path, delim);
1017 if (p == NULL)
1018 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001019 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001020 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001021 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001022 return NULL;
1023 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001024 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001025 if (*p == '\0')
1026 break;
1027 path = p+1;
1028 }
1029 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001030}
1031
1032void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001033PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001034{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001035 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001036 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001037 Py_FatalError("can't create sys.path");
1038 if (PySys_SetObject("path", v) != 0)
1039 Py_FatalError("can't assign sys.path");
1040 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001041}
1042
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001043static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001044makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001045{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001046 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +00001047 if (argc <= 0 || argv == NULL) {
1048 /* Ensure at least one (empty) argument is seen */
1049 static char *empty_argv[1] = {""};
1050 argv = empty_argv;
1051 argc = 1;
1052 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001053 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001054 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001055 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001056 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001057#ifdef __VMS
1058 PyObject *v;
1059
1060 /* argv[0] is the script pathname if known */
1061 if (i == 0) {
1062 char* fn = decc$translate_vms(argv[0]);
1063 if ((fn == (char *)0) || fn == (char *)-1)
1064 v = PyString_FromString(argv[0]);
1065 else
1066 v = PyString_FromString(
1067 decc$translate_vms(argv[0]));
1068 } else
1069 v = PyString_FromString(argv[i]);
1070#else
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001071 PyObject *v = PyString_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001072#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001073 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001074 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001075 av = NULL;
1076 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001077 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001078 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001079 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001080 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001081 return av;
1082}
1083
1084void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001085PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001086{
Guido van Rossum162e38c2003-02-19 15:25:10 +00001087#if defined(HAVE_REALPATH)
1088 char fullpath[MAXPATHLEN];
1089#elif defined(MS_WINDOWS)
Thomas Heller27bb71e2003-01-08 14:33:48 +00001090 char fullpath[MAX_PATH];
1091#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001092 PyObject *av = makeargvobject(argc, argv);
1093 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001094 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001095 Py_FatalError("no mem for sys.argv");
1096 if (PySys_SetObject("argv", av) != 0)
1097 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001098 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001099 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001100 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +00001101 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001102 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001103#ifdef HAVE_READLINK
1104 char link[MAXPATHLEN+1];
1105 char argv0copy[2*MAXPATHLEN+1];
1106 int nr = 0;
1107 if (argc > 0 && argv0 != NULL)
1108 nr = readlink(argv0, link, MAXPATHLEN);
1109 if (nr > 0) {
1110 /* It's a symlink */
1111 link[nr] = '\0';
1112 if (link[0] == SEP)
1113 argv0 = link; /* Link to absolute path */
1114 else if (strchr(link, SEP) == NULL)
1115 ; /* Link without path */
1116 else {
1117 /* Must join(dirname(argv0), link) */
1118 char *q = strrchr(argv0, SEP);
1119 if (q == NULL)
1120 argv0 = link; /* argv0 without path */
1121 else {
1122 /* Must make a copy */
1123 strcpy(argv0copy, argv0);
1124 q = strrchr(argv0copy, SEP);
1125 strcpy(q+1, link);
1126 argv0 = argv0copy;
1127 }
1128 }
1129 }
1130#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001131#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001132 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001133 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001134#ifdef MS_WINDOWS
1135 char *ptemp;
1136 if (GetFullPathName(argv0,
1137 sizeof(fullpath),
1138 fullpath,
1139 &ptemp)) {
1140 argv0 = fullpath;
1141 }
1142#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001143 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001144 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001145 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001146 if (q != NULL)
1147 p = q;
1148 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001149 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001150 if (n > 1 && p[-1] != ':')
1151 n--; /* Drop trailing separator */
1152 }
1153 }
1154#else /* All other filename syntaxes */
Guido van Rossum162e38c2003-02-19 15:25:10 +00001155 if (argc > 0 && argv0 != NULL) {
1156#if defined(HAVE_REALPATH)
1157 if (realpath(argv0, fullpath)) {
1158 argv0 = fullpath;
1159 }
1160#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001161 p = strrchr(argv0, SEP);
Guido van Rossum162e38c2003-02-19 15:25:10 +00001162 }
Guido van Rossumcc883411996-09-10 14:44:21 +00001163 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001164#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001165 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001166#else /* don't include trailing separator */
1167 n = p - argv0;
1168#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001169#if SEP == '/' /* Special case for Unix filename syntax */
1170 if (n > 1)
1171 n--; /* Drop trailing separator */
1172#endif /* Unix */
1173 }
1174#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001175 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001176 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001177 Py_FatalError("no mem for sys.path insertion");
1178 if (PyList_Insert(path, 0, a) < 0)
1179 Py_FatalError("sys.path.insert(0) failed");
1180 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001181 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001182 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001183}
Guido van Rossuma890e681998-05-12 14:59:24 +00001184
1185
1186/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1187 Adapted from code submitted by Just van Rossum.
1188
1189 PySys_WriteStdout(format, ...)
1190 PySys_WriteStderr(format, ...)
1191
1192 The first function writes to sys.stdout; the second to sys.stderr. When
1193 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001194 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001195
1196 Both take a printf-style format string as their first argument followed
1197 by a variable length argument list determined by the format string.
1198
1199 *** WARNING ***
1200
1201 The format should limit the total size of the formatted output string to
1202 1000 bytes. In particular, this means that no unrestricted "%s" formats
1203 should occur; these should be limited using "%.<N>s where <N> is a
1204 decimal number calculated so that <N> plus the maximum size of other
1205 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1206 which can print hundreds of digits for very large numbers.
1207
1208 */
1209
1210static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001211mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001212{
1213 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001214 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001215
Guido van Rossum8442af31998-10-12 18:22:10 +00001216 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001217 file = PySys_GetObject(name);
1218 if (file == NULL || PyFile_AsFile(file) == fp)
1219 vfprintf(fp, format, va);
1220 else {
1221 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001222 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1223 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001224 if (PyFile_WriteString(buffer, file) != 0) {
1225 PyErr_Clear();
1226 fputs(buffer, fp);
1227 }
Tim Petersfaad5ad2001-12-03 00:43:33 +00001228 if (written < 0 || written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001229 const char *truncated = "... truncated";
1230 if (PyFile_WriteString(truncated, file) != 0) {
1231 PyErr_Clear();
1232 fputs(truncated, fp);
1233 }
1234 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001235 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001236 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001237}
1238
1239void
Guido van Rossuma890e681998-05-12 14:59:24 +00001240PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001241{
1242 va_list va;
1243
Guido van Rossuma890e681998-05-12 14:59:24 +00001244 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001245 mywrite("stdout", stdout, format, va);
1246 va_end(va);
1247}
1248
1249void
Guido van Rossuma890e681998-05-12 14:59:24 +00001250PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001251{
1252 va_list va;
1253
Guido van Rossuma890e681998-05-12 14:59:24 +00001254 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001255 mywrite("stderr", stderr, format, va);
1256 va_end(va);
1257}