blob: 80d24d1911a3087b649ef47200f9880f8cf51c8f [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"
Christian Heimesf31b69f2008-01-14 03:42:48 +000018#include "structseq.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019#include "code.h"
Barry Warsawb6a54d22000-12-06 21:47:46 +000020#include "frameobject.h"
Guido van Rossuma12fe4e2003-04-09 19:06:21 +000021#include "eval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Guido van Rossume2437a11992-03-23 18:20:18 +000023#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000024
Mark Hammond8696ebc2002-10-08 02:44:31 +000025#ifdef MS_WINDOWS
26#define WIN32_LEAN_AND_MEAN
27#include "windows.h"
28#endif /* MS_WINDOWS */
29
Guido van Rossum9b38a141996-09-11 23:12:24 +000030#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000031extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000032/* A string loaded from the DLL at startup: */
33extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000034#endif
35
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000036#ifdef __VMS
37#include <unixlib.h>
38#endif
39
Martin v. Löwis5467d4c2003-05-10 07:10:12 +000040#ifdef MS_WINDOWS
41#include <windows.h>
42#endif
43
44#ifdef HAVE_LANGINFO_H
45#include <locale.h>
46#include <langinfo.h>
47#endif
48
Guido van Rossum65bf9f21997-04-29 18:33:38 +000049PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000050PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Nicholas Bastine5662ae2004-03-24 22:22:12 +000052 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +000053 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000054 if (sd == NULL)
55 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000056 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
59FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000060PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
62 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000063 PyObject *v = PySys_GetObject(name);
64 if (v != NULL && PyFile_Check(v))
65 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 if (fp == NULL)
67 fp = def;
68 return fp;
69}
70
71int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000072PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073{
Nicholas Bastine5662ae2004-03-24 22:22:12 +000074 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +000075 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000076 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000077 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000078 return 0;
79 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000080 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000081 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000083 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084}
85
Guido van Rossum65bf9f21997-04-29 18:33:38 +000086static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000087sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000088{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000089 PyObject *outf;
Nicholas Bastine5662ae2004-03-24 22:22:12 +000090 PyInterpreterState *interp = PyThreadState_GET()->interp;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000091 PyObject *modules = interp->modules;
92 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
93
Moshe Zadka03897ea2001-07-23 13:32:43 +000094 if (builtins == NULL) {
95 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
96 return NULL;
97 }
98
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000099 /* Print value except if None */
100 /* After printing, also assign to '_' */
101 /* Before, set '_' to None to avoid recursion */
102 if (o == Py_None) {
103 Py_INCREF(Py_None);
104 return Py_None;
105 }
106 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
107 return NULL;
108 if (Py_FlushLine() != 0)
109 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000110 outf = PySys_GetObject("stdout");
111 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000112 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
113 return NULL;
114 }
Greg Steinceb9b7c2001-01-11 09:27:34 +0000115 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000116 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000117 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000118 if (Py_FlushLine() != 0)
119 return NULL;
120 if (PyObject_SetAttrString(builtins, "_", o) != 0)
121 return NULL;
122 Py_INCREF(Py_None);
123 return Py_None;
124}
125
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000126PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000127"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000128"\n"
Brett Cannon51185172006-03-02 22:07:40 +0000129"Print an object to sys.stdout and also save it in __builtin__.\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000130);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000131
132static PyObject *
133sys_excepthook(PyObject* self, PyObject* args)
134{
135 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000136 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000137 return NULL;
138 PyErr_Display(exc, value, tb);
139 Py_INCREF(Py_None);
140 return Py_None;
141}
142
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000143PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000144"excepthook(exctype, value, traceback) -> None\n"
145"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000146"Handle an exception by displaying it with a traceback on sys.stderr.\n"
147);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000148
149static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000150sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000151{
152 PyThreadState *tstate;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000153 tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154 return Py_BuildValue(
155 "(OOO)",
156 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
157 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
158 tstate->exc_traceback != NULL ?
159 tstate->exc_traceback : Py_None);
160}
161
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000162PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000163"exc_info() -> (type, value, traceback)\n\
164\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000165Return information about the most recent exception caught by an except\n\
166clause in the current stack frame or in an older stack frame."
167);
168
169static PyObject *
170sys_exc_clear(PyObject *self, PyObject *noargs)
171{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000172 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000173 PyObject *tmp_type, *tmp_value, *tmp_tb;
174 tmp_type = tstate->exc_type;
175 tmp_value = tstate->exc_value;
176 tmp_tb = tstate->exc_traceback;
177 tstate->exc_type = NULL;
178 tstate->exc_value = NULL;
179 tstate->exc_traceback = NULL;
180 Py_XDECREF(tmp_type);
181 Py_XDECREF(tmp_value);
182 Py_XDECREF(tmp_tb);
183 /* For b/w compatibility */
184 PySys_SetObject("exc_type", Py_None);
185 PySys_SetObject("exc_value", Py_None);
186 PySys_SetObject("exc_traceback", Py_None);
187 Py_INCREF(Py_None);
188 return Py_None;
189}
190
191PyDoc_STRVAR(exc_clear_doc,
192"exc_clear() -> None\n\
193\n\
194Clear global information on the current exception. Subsequent calls to\n\
195exc_info() will return (None,None,None) until another exception is raised\n\
196in the current thread or the execution stack returns to a frame where\n\
197another exception is being handled."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000198);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000199
Guido van Rossuma027efa1997-05-05 20:56:21 +0000200static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000201sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000203 PyObject *exit_code = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +0000204 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
Neal Norwitz0c766a02002-03-27 13:03:09 +0000205 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000206 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000207 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000208 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000211PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000212"exit([status])\n\
213\n\
214Exit the interpreter by raising SystemExit(status).\n\
215If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000216If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000217If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000218exit status will be one (i.e., failure)."
219);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000220
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000221#ifdef Py_USING_UNICODE
222
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000223static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000224sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000225{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000226 return PyString_FromString(PyUnicode_GetDefaultEncoding());
227}
228
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000229PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000230"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000231\n\
232Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000233implementation."
234);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000235
236static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000238{
239 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000240 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000241 return NULL;
242 if (PyUnicode_SetDefaultEncoding(encoding))
243 return NULL;
244 Py_INCREF(Py_None);
245 return Py_None;
246}
247
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000248PyDoc_STRVAR(setdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000249"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000250\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000251Set the current default string encoding used by the Unicode implementation."
252);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000253
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000254static PyObject *
255sys_getfilesystemencoding(PyObject *self)
256{
257 if (Py_FileSystemDefaultEncoding)
258 return PyString_FromString(Py_FileSystemDefaultEncoding);
259 Py_INCREF(Py_None);
260 return Py_None;
261}
262
263PyDoc_STRVAR(getfilesystemencoding_doc,
264"getfilesystemencoding() -> string\n\
265\n\
266Return the encoding used to convert Unicode filenames in\n\
267operating system filenames."
268);
269
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000270#endif
271
Fred Drake5755ce62001-06-27 19:19:46 +0000272/*
273 * Cached interned string objects used for calling the profile and
274 * trace functions. Initialized by trace_init().
275 */
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000276static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Fred Drake5755ce62001-06-27 19:19:46 +0000277
278static int
279trace_init(void)
280{
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000281 static char *whatnames[7] = {"call", "exception", "line", "return",
282 "c_call", "c_exception", "c_return"};
Fred Drake5755ce62001-06-27 19:19:46 +0000283 PyObject *name;
284 int i;
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000285 for (i = 0; i < 7; ++i) {
Fred Drake5755ce62001-06-27 19:19:46 +0000286 if (whatstrings[i] == NULL) {
287 name = PyString_InternFromString(whatnames[i]);
288 if (name == NULL)
289 return -1;
290 whatstrings[i] = name;
291 }
292 }
293 return 0;
294}
295
296
297static PyObject *
298call_trampoline(PyThreadState *tstate, PyObject* callback,
299 PyFrameObject *frame, int what, PyObject *arg)
300{
301 PyObject *args = PyTuple_New(3);
302 PyObject *whatstr;
303 PyObject *result;
304
305 if (args == NULL)
306 return NULL;
307 Py_INCREF(frame);
308 whatstr = whatstrings[what];
309 Py_INCREF(whatstr);
310 if (arg == NULL)
311 arg = Py_None;
312 Py_INCREF(arg);
313 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
314 PyTuple_SET_ITEM(args, 1, whatstr);
315 PyTuple_SET_ITEM(args, 2, arg);
316
317 /* call the Python-level function */
318 PyFrame_FastToLocals(frame);
319 result = PyEval_CallObject(callback, args);
320 PyFrame_LocalsToFast(frame, 1);
321 if (result == NULL)
322 PyTraceBack_Here(frame);
323
324 /* cleanup */
325 Py_DECREF(args);
326 return result;
327}
328
329static int
330profile_trampoline(PyObject *self, PyFrameObject *frame,
331 int what, PyObject *arg)
332{
333 PyThreadState *tstate = frame->f_tstate;
334 PyObject *result;
335
Fred Drake8f51f542001-10-04 14:48:42 +0000336 if (arg == NULL)
337 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000338 result = call_trampoline(tstate, self, frame, what, arg);
339 if (result == NULL) {
340 PyEval_SetProfile(NULL, NULL);
341 return -1;
342 }
343 Py_DECREF(result);
344 return 0;
345}
346
347static int
348trace_trampoline(PyObject *self, PyFrameObject *frame,
349 int what, PyObject *arg)
350{
351 PyThreadState *tstate = frame->f_tstate;
352 PyObject *callback;
353 PyObject *result;
354
355 if (what == PyTrace_CALL)
356 callback = self;
357 else
358 callback = frame->f_trace;
359 if (callback == NULL)
360 return 0;
361 result = call_trampoline(tstate, callback, frame, what, arg);
362 if (result == NULL) {
363 PyEval_SetTrace(NULL, NULL);
364 Py_XDECREF(frame->f_trace);
365 frame->f_trace = NULL;
366 return -1;
367 }
368 if (result != Py_None) {
369 PyObject *temp = frame->f_trace;
370 frame->f_trace = NULL;
371 Py_XDECREF(temp);
372 frame->f_trace = result;
373 }
374 else {
375 Py_DECREF(result);
376 }
377 return 0;
378}
Fred Draked0838392001-06-16 21:02:31 +0000379
Fred Drake8b4d01d2000-05-09 19:57:01 +0000380static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000381sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000382{
Fred Drake5755ce62001-06-27 19:19:46 +0000383 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000384 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000385 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000386 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000387 else
Fred Drake5755ce62001-06-27 19:19:46 +0000388 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000389 Py_INCREF(Py_None);
390 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000391}
392
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000393PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000394"settrace(function)\n\
395\n\
396Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000397function call. See the debugger chapter in the library manual."
398);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000399
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000400static PyObject *
Georg Brandl56112892008-01-20 13:59:46 +0000401sys_gettrace(PyObject *self, PyObject *args)
402{
403 PyThreadState *tstate = PyThreadState_GET();
404 PyObject *temp = tstate->c_traceobj;
405
406 if (temp == NULL)
407 temp = Py_None;
408 Py_INCREF(temp);
409 return temp;
410}
411
412PyDoc_STRVAR(gettrace_doc,
413"gettrace()\n\
414\n\
415Return the global debug tracing function set with sys.settrace.\n\
416See the debugger chapter in the library manual."
417);
418
419static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000421{
Fred Drake5755ce62001-06-27 19:19:46 +0000422 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000423 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000424 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000425 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000426 else
Fred Drake5755ce62001-06-27 19:19:46 +0000427 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000428 Py_INCREF(Py_None);
429 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000430}
431
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000432PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000433"setprofile(function)\n\
434\n\
435Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000436and return. See the profiler chapter in the library manual."
437);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000438
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000439static PyObject *
Georg Brandl56112892008-01-20 13:59:46 +0000440sys_getprofile(PyObject *self, PyObject *args)
441{
442 PyThreadState *tstate = PyThreadState_GET();
443 PyObject *temp = tstate->c_profileobj;
444
445 if (temp == NULL)
446 temp = Py_None;
447 Py_INCREF(temp);
448 return temp;
449}
450
451PyDoc_STRVAR(getprofile_doc,
452"getprofile()\n\
453\n\
454Return the profiling function set with sys.setprofile.\n\
455See the profiler chapter in the library manual."
456);
457
458static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000459sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000460{
Skip Montanarod581d772002-09-03 20:10:45 +0000461 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000462 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000463 Py_INCREF(Py_None);
464 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000465}
466
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000467PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000468"setcheckinterval(n)\n\
469\n\
470Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000471n instructions. This also affects how often thread switches occur."
472);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000473
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000474static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000475sys_getcheckinterval(PyObject *self, PyObject *args)
476{
477 return PyInt_FromLong(_Py_CheckInterval);
478}
479
480PyDoc_STRVAR(getcheckinterval_doc,
481"getcheckinterval() -> current check interval; see setcheckinterval()."
482);
483
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000484#ifdef WITH_TSC
485static PyObject *
486sys_settscdump(PyObject *self, PyObject *args)
487{
488 int bool;
489 PyThreadState *tstate = PyThreadState_Get();
490
491 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
492 return NULL;
493 if (bool)
494 tstate->interp->tscdump = 1;
495 else
496 tstate->interp->tscdump = 0;
497 Py_INCREF(Py_None);
498 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000499
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000500}
501
Tim Peters216b78b2006-01-06 02:40:53 +0000502PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000503"settscdump(bool)\n\
504\n\
505If true, tell the Python interpreter to dump VM measurements to\n\
506stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000507processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000508);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000509#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000510
Tim Peterse5e065b2003-07-06 18:36:54 +0000511static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000512sys_setrecursionlimit(PyObject *self, PyObject *args)
513{
514 int new_limit;
515 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
516 return NULL;
517 if (new_limit <= 0) {
Tim Peters216b78b2006-01-06 02:40:53 +0000518 PyErr_SetString(PyExc_ValueError,
519 "recursion limit must be positive");
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000520 return NULL;
521 }
522 Py_SetRecursionLimit(new_limit);
523 Py_INCREF(Py_None);
524 return Py_None;
525}
526
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000527PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000528"setrecursionlimit(n)\n\
529\n\
530Set the maximum depth of the Python interpreter stack to n. This\n\
531limit prevents infinite recursion from causing an overflow of the C\n\
532stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000533dependent."
534);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000535
536static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000537sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000538{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000539 return PyInt_FromLong(Py_GetRecursionLimit());
540}
541
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000542PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000543"getrecursionlimit()\n\
544\n\
545Return the current value of the recursion limit, the maximum depth\n\
546of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000547recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000548);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000549
Mark Hammond8696ebc2002-10-08 02:44:31 +0000550#ifdef MS_WINDOWS
551PyDoc_STRVAR(getwindowsversion_doc,
552"getwindowsversion()\n\
553\n\
554Return information about the running version of Windows.\n\
555The result is a tuple of (major, minor, build, platform, text)\n\
556All elements are numbers, except text which is a string.\n\
557Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
558"
559);
560
561static PyObject *
562sys_getwindowsversion(PyObject *self)
563{
564 OSVERSIONINFO ver;
565 ver.dwOSVersionInfoSize = sizeof(ver);
566 if (!GetVersionEx(&ver))
567 return PyErr_SetFromWindowsErr(0);
568 return Py_BuildValue("HHHHs",
569 ver.dwMajorVersion,
570 ver.dwMinorVersion,
571 ver.dwBuildNumber,
572 ver.dwPlatformId,
573 ver.szCSDVersion);
574}
575
576#endif /* MS_WINDOWS */
577
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000578#ifdef HAVE_DLOPEN
579static PyObject *
580sys_setdlopenflags(PyObject *self, PyObject *args)
581{
582 int new_val;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000583 PyThreadState *tstate = PyThreadState_GET();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000584 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
585 return NULL;
586 if (!tstate)
587 return NULL;
588 tstate->interp->dlopenflags = new_val;
589 Py_INCREF(Py_None);
590 return Py_None;
591}
592
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000593PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000594"setdlopenflags(n) -> None\n\
595\n\
596Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000597things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000598a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000599To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000600sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
601);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000602
603static PyObject *
604sys_getdlopenflags(PyObject *self, PyObject *args)
605{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000606 PyThreadState *tstate = PyThreadState_GET();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000607 if (!tstate)
608 return NULL;
609 return PyInt_FromLong(tstate->interp->dlopenflags);
610}
611
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000612PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000613"getdlopenflags() -> int\n\
614\n\
615Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000616calls. The flag constants are defined in the dl module."
617);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000618#endif
619
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000620#ifdef USE_MALLOPT
621/* Link with -lmalloc (or -lmpc) on an SGI */
622#include <malloc.h>
623
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000624static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000625sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000626{
627 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000628 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000629 return NULL;
630 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000631 Py_INCREF(Py_None);
632 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000633}
634#endif /* USE_MALLOPT */
635
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000636static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000637sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000638{
Martin v. Löwis725507b2006-03-07 12:08:51 +0000639 return PyInt_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000640}
641
Tim Peters4be93d02002-07-07 19:59:50 +0000642#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000643static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000644sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000645{
Armin Rigoe1709372006-04-12 17:06:05 +0000646 return PyInt_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000647}
Armin Rigoe1709372006-04-12 17:06:05 +0000648#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000649
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000650PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000651"getrefcount(object) -> integer\n\
652\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000653Return the reference count of object. The count returned is generally\n\
654one higher than you might expect, because it includes the (temporary)\n\
655reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000656);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000657
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000658#ifdef COUNT_ALLOCS
659static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000660sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000661{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000662 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000663
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000664 return get_counts();
665}
666#endif
667
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000668PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000669"_getframe([depth]) -> frameobject\n\
670\n\
671Return a frame object from the call stack. If optional integer depth is\n\
672given, return the frame object that many calls below the top of the stack.\n\
673If that is deeper than the call stack, ValueError is raised. The default\n\
674for depth is zero, returning the frame at the top of the call stack.\n\
675\n\
676This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000677purposes only."
678);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000679
680static PyObject *
681sys_getframe(PyObject *self, PyObject *args)
682{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000683 PyFrameObject *f = PyThreadState_GET()->frame;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000684 int depth = -1;
685
686 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
687 return NULL;
688
689 while (depth > 0 && f != NULL) {
690 f = f->f_back;
691 --depth;
692 }
693 if (f == NULL) {
694 PyErr_SetString(PyExc_ValueError,
695 "call stack is not deep enough");
696 return NULL;
697 }
698 Py_INCREF(f);
699 return (PyObject*)f;
700}
701
Tim Peters32a83612006-07-10 21:08:24 +0000702PyDoc_STRVAR(current_frames_doc,
703"_current_frames() -> dictionary\n\
704\n\
705Return a dictionary mapping each current thread T's thread id to T's\n\
706current stack frame.\n\
707\n\
708This function should be used for specialized purposes only."
709);
710
711static PyObject *
712sys_current_frames(PyObject *self, PyObject *noargs)
713{
714 return _PyThread_CurrentFrames();
715}
716
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000717PyDoc_STRVAR(call_tracing_doc,
718"call_tracing(func, args) -> object\n\
719\n\
720Call func(*args), while tracing is enabled. The tracing state is\n\
721saved, and restored afterwards. This is intended to be called from\n\
722a debugger from a checkpoint, to recursively debug some other code."
723);
724
725static PyObject *
726sys_call_tracing(PyObject *self, PyObject *args)
727{
728 PyObject *func, *funcargs;
Georg Brandl96a8c392006-05-29 21:04:52 +0000729 if (!PyArg_UnpackTuple(args, "call_tracing", 2, 2, &func, &funcargs))
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000730 return NULL;
731 return _PyEval_CallTracing(func, funcargs);
732}
733
Jeremy Hylton985eba52003-02-05 23:13:00 +0000734PyDoc_STRVAR(callstats_doc,
735"callstats() -> tuple of integers\n\
736\n\
737Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
738when Python was built. Otherwise, return None.\n\
739\n\
740When enabled, this function returns detailed, implementation-specific\n\
741details about the number of function calls executed. The return value is\n\
742a 11-tuple where the entries in the tuple are counts of:\n\
7430. all function calls\n\
7441. calls to PyFunction_Type objects\n\
7452. PyFunction calls that do not create an argument tuple\n\
7463. PyFunction calls that do not create an argument tuple\n\
747 and bypass PyEval_EvalCodeEx()\n\
7484. PyMethod calls\n\
7495. PyMethod calls on bound methods\n\
7506. PyType calls\n\
7517. PyCFunction calls\n\
7528. generator calls\n\
7539. All other calls\n\
75410. Number of stack pops performed by call_function()"
755);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000756
Christian Heimes908caac2008-01-27 23:34:59 +0000757static PyObject *
758sys_cleartypecache(PyObject* self, PyObject* args)
759{
760 PyType_ClearCache();
761 Py_RETURN_NONE;
762}
763
764PyDoc_STRVAR(cleartypecache_doc,
765"_cleartypecache() -> None\n\
766Clear the internal type lookup cache.");
767
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000768#ifdef __cplusplus
769extern "C" {
770#endif
771
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000772#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000773/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000774extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000775#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000776
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000777#ifdef DYNAMIC_EXECUTION_PROFILE
778/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000779extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000780#endif
781
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000782#ifdef __cplusplus
783}
784#endif
785
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000786static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000787 /* Might as well keep this in alphabetic order */
Tim Peters216b78b2006-01-06 02:40:53 +0000788 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
Jeremy Hylton985eba52003-02-05 23:13:00 +0000789 callstats_doc},
Christian Heimes908caac2008-01-27 23:34:59 +0000790 {"_cleartypecache", sys_cleartypecache, METH_NOARGS,
791 cleartypecache_doc},
Tim Peters32a83612006-07-10 21:08:24 +0000792 {"_current_frames", sys_current_frames, METH_NOARGS,
793 current_frames_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000794 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000795 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
796 {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000797 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000798 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000799#ifdef Py_USING_UNICODE
Tim Peters216b78b2006-01-06 02:40:53 +0000800 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
801 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000802#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000803#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000804 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000805 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000806#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000807#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000808 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000809#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000810#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000811 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000812#endif
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000813#ifdef Py_USING_UNICODE
814 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
Tim Peters216b78b2006-01-06 02:40:53 +0000815 METH_NOARGS, getfilesystemencoding_doc},
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000816#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000817#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000818 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000819#endif
820#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000821 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000822#endif
Fred Drakea7688822001-10-24 20:47:48 +0000823 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000824 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000825 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000826 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000827#ifdef MS_WINDOWS
828 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
829 getwindowsversion_doc},
830#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000831#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000832 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000833#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000834#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000835 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000836 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000837#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000838 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000839 setcheckinterval_doc},
Tim Peterse5e065b2003-07-06 18:36:54 +0000840 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000841 getcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000842#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000843 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000844 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000845#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000846 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Georg Brandl56112892008-01-20 13:59:46 +0000847 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000848 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000849 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000850#ifdef WITH_TSC
851 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
852#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000853 {"settrace", sys_settrace, METH_O, settrace_doc},
Georg Brandl56112892008-01-20 13:59:46 +0000854 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000855 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000856 {NULL, NULL} /* sentinel */
857};
858
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000859static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000860list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000861{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000862 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000863 int i;
864 if (list == NULL)
865 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000866 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000867 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000868 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000869 if (name == NULL)
870 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000871 PyList_Append(list, name);
872 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000873 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000874 if (PyList_Sort(list) != 0) {
875 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000876 list = NULL;
877 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000878 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000879 PyObject *v = PyList_AsTuple(list);
880 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000881 list = v;
882 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000883 return list;
884}
885
Guido van Rossum23fff912000-12-15 22:02:05 +0000886static PyObject *warnoptions = NULL;
887
888void
889PySys_ResetWarnOptions(void)
890{
891 if (warnoptions == NULL || !PyList_Check(warnoptions))
892 return;
893 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
894}
895
896void
897PySys_AddWarnOption(char *s)
898{
899 PyObject *str;
900
901 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
902 Py_XDECREF(warnoptions);
903 warnoptions = PyList_New(0);
904 if (warnoptions == NULL)
905 return;
906 }
907 str = PyString_FromString(s);
908 if (str != NULL) {
909 PyList_Append(warnoptions, str);
910 Py_DECREF(str);
911 }
912}
913
Guido van Rossum40552d01998-08-06 03:34:39 +0000914/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
915 Two literals concatenated works just fine. If you have a K&R compiler
916 or other abomination that however *does* understand longer strings,
917 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000918PyDoc_VAR(sys_doc) =
919PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000920"This module provides access to some objects used or maintained by the\n\
921interpreter and to functions that interact strongly with the interpreter.\n\
922\n\
923Dynamic objects:\n\
924\n\
925argv -- command line arguments; argv[0] is the script pathname if known\n\
926path -- module search path; path[0] is the script directory, else ''\n\
927modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000928\n\
929displayhook -- called to show results in an interactive session\n\
930excepthook -- called to handle any uncaught exception other than SystemExit\n\
931 To customize printing in an interactive session or to install a custom\n\
932 top-level exception handler, assign other functions to replace these.\n\
933\n\
934exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
935 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000936\n\
937stdin -- standard input file object; used by raw_input() and input()\n\
938stdout -- standard output file object; used by the print statement\n\
939stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000940 By assigning other file objects (or objects that behave like files)\n\
941 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000942\n\
943last_type -- type of last uncaught exception\n\
944last_value -- value of last uncaught exception\n\
945last_traceback -- traceback of last uncaught exception\n\
946 These three are only available in an interactive session after a\n\
947 traceback has been printed.\n\
948\n\
949exc_type -- type of exception currently being handled\n\
950exc_value -- value of exception currently being handled\n\
951exc_traceback -- traceback of exception currently being handled\n\
952 The function exc_info() should be used instead of these three,\n\
953 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000954"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000955)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000956/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000957PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000958"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000959Static objects:\n\
960\n\
961maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000962maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000963builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000964version -- the version of this interpreter as a string\n\
965version_info -- version information as a tuple\n\
966hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000967copyright -- copyright notice pertaining to this interpreter\n\
968platform -- platform identifier\n\
969executable -- pathname of this Python interpreter\n\
970prefix -- prefix used to find the Python library\n\
971exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000972"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000973)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000974#ifdef MS_WINDOWS
975/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000976PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000977"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000978winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000979"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000980)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000981#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000982PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000983"__stdin__ -- the original stdin; don't touch!\n\
984__stdout__ -- the original stdout; don't touch!\n\
985__stderr__ -- the original stderr; don't touch!\n\
986__displayhook__ -- the original displayhook; don't touch!\n\
987__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000988\n\
989Functions:\n\
990\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000991displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000992excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000993exc_info() -- return thread-safe information about the current exception\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000994exc_clear() -- clear the exception state for the current thread\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000995exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000996getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Georg Brandl56112892008-01-20 13:59:46 +0000997getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000998getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000999getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Georg Brandl56112892008-01-20 13:59:46 +00001000gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001001setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001002setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001003setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001004setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001005settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001006"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001007)
Fred Drakeccede592000-08-14 20:59:57 +00001008/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001009
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +00001010static int
1011_check_and_flush (FILE *stream)
1012{
1013 int prev_fail = ferror (stream);
1014 return fflush (stream) || prev_fail ? EOF : 0;
1015}
1016
Martin v. Löwis43b57802006-01-05 23:38:54 +00001017/* Subversion branch and revision management */
1018static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
1019static const char headurl[] = "$HeadURL$";
1020static int svn_initialized;
1021static char patchlevel_revision[50]; /* Just the number */
1022static char branch[50];
1023static char shortbranch[50];
1024static const char *svn_revision;
1025
Tim Peterse86e7a52006-01-06 02:42:46 +00001026static void
1027svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +00001028{
1029 const char *python, *br_start, *br_end, *br_end2, *svnversion;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001030 Py_ssize_t len;
1031 int istag;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001032
1033 if (svn_initialized)
1034 return;
1035
1036 python = strstr(headurl, "/python/");
1037 if (!python)
1038 Py_FatalError("subversion keywords missing");
1039
1040 br_start = python + 8;
1041 br_end = strchr(br_start, '/');
Neal Norwitz837ce932006-10-28 21:15:30 +00001042 assert(br_end);
1043
Tim Peters216b78b2006-01-06 02:40:53 +00001044 /* Works even for trunk,
Martin v. Löwis43b57802006-01-05 23:38:54 +00001045 as we are in trunk/Python/sysmodule.c */
1046 br_end2 = strchr(br_end+1, '/');
1047
1048 istag = strncmp(br_start, "tags", 4) == 0;
1049 if (strncmp(br_start, "trunk", 5) == 0) {
1050 strcpy(branch, "trunk");
1051 strcpy(shortbranch, "trunk");
1052
Tim Peters216b78b2006-01-06 02:40:53 +00001053 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001054 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1055 len = br_end2 - br_start;
1056 strncpy(branch, br_start, len);
1057 branch[len] = '\0';
1058
1059 len = br_end2 - (br_end + 1);
1060 strncpy(shortbranch, br_end + 1, len);
1061 shortbranch[len] = '\0';
Tim Peters216b78b2006-01-06 02:40:53 +00001062 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001063 else {
1064 Py_FatalError("bad HeadURL");
1065 return;
1066 }
1067
1068
1069 svnversion = _Py_svnversion();
1070 if (strcmp(svnversion, "exported") != 0)
1071 svn_revision = svnversion;
1072 else if (istag) {
1073 len = strlen(_patchlevel_revision);
Neal Norwitz68cdf8a2007-04-16 07:37:55 +00001074 assert(len >= 13);
1075 assert(len < (sizeof(patchlevel_revision) + 13));
Martin v. Löwis43b57802006-01-05 23:38:54 +00001076 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1077 len - 13);
1078 patchlevel_revision[len - 13] = '\0';
1079 svn_revision = patchlevel_revision;
1080 }
1081 else
1082 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001083
Martin v. Löwis43b57802006-01-05 23:38:54 +00001084 svn_initialized = 1;
1085}
1086
1087/* Return svnversion output if available.
1088 Else return Revision of patchlevel.h if on branch.
1089 Else return empty string */
1090const char*
1091Py_SubversionRevision()
1092{
1093 svnversion_init();
1094 return svn_revision;
1095}
1096
1097const char*
1098Py_SubversionShortBranch()
1099{
1100 svnversion_init();
1101 return shortbranch;
1102}
1103
Christian Heimesf31b69f2008-01-14 03:42:48 +00001104
1105PyDoc_STRVAR(flags__doc__,
1106"sys.flags\n\
1107\n\
1108Flags provided through command line arguments or environment vars.");
1109
1110static PyTypeObject FlagsType;
1111
1112static PyStructSequence_Field flags_fields[] = {
1113 {"debug", "-d"},
1114 {"py3k_warning", "-3"},
1115 {"division_warning", "-Q"},
1116 {"division_new", "-Qnew"},
1117 {"inspect", "-i"},
1118 {"interactive", "-i"},
1119 {"optimize", "-O or -OO"},
1120 {"dont_write_bytecode", "-B"},
1121 /* {"no_user_site", "-s"}, */
1122 {"no_site", "-S"},
Andrew M. Kuchling7ce9b182008-01-15 01:29:16 +00001123 {"ignore_environment", "-E"},
Christian Heimesf31b69f2008-01-14 03:42:48 +00001124 {"tabcheck", "-t or -tt"},
1125 {"verbose", "-v"},
1126#ifdef RISCOS
Andrew M. Kuchling7ce9b182008-01-15 01:29:16 +00001127 {"riscos_wimp", "???"},
Christian Heimesf31b69f2008-01-14 03:42:48 +00001128#endif
1129 /* {"unbuffered", "-u"}, */
1130 {"unicode", "-U"},
1131 /* {"skip_first", "-x"}, */
1132 {0}
1133};
1134
1135static PyStructSequence_Desc flags_desc = {
1136 "sys.flags", /* name */
1137 flags__doc__, /* doc */
1138 flags_fields, /* fields */
1139#ifdef RISCOS
1140 14
1141#else
1142 13
1143#endif
1144};
1145
1146static PyObject*
1147make_flags(void)
1148{
1149 int pos = 0;
1150 PyObject *seq;
1151
1152 seq = PyStructSequence_New(&FlagsType);
1153 if (seq == NULL)
1154 return NULL;
1155
1156#define SetFlag(flag) \
1157 PyStructSequence_SET_ITEM(seq, pos++, PyInt_FromLong(flag))
1158
1159 SetFlag(Py_DebugFlag);
1160 SetFlag(Py_Py3kWarningFlag);
1161 SetFlag(Py_DivisionWarningFlag);
1162 SetFlag(_Py_QnewFlag);
1163 SetFlag(Py_InspectFlag);
1164 SetFlag(Py_InteractiveFlag);
1165 SetFlag(Py_OptimizeFlag);
1166 SetFlag(Py_DontWriteBytecodeFlag);
1167 /* SetFlag(Py_NoUserSiteDirectory); */
1168 SetFlag(Py_NoSiteFlag);
1169 SetFlag(Py_IgnoreEnvironmentFlag);
1170 SetFlag(Py_TabcheckFlag);
1171 SetFlag(Py_VerboseFlag);
1172#ifdef RISCOS
1173 SetFlag(Py_RISCOSWimpFlag);
1174#endif
1175 /* SetFlag(saw_unbuffered_flag); */
1176 SetFlag(Py_UnicodeFlag);
1177 /* SetFlag(skipfirstline); */
1178#undef SetFlag
1179
1180 if (PyErr_Occurred()) {
1181 return NULL;
1182 }
1183
Christian Heimesf31b69f2008-01-14 03:42:48 +00001184 return seq;
1185}
1186
Guido van Rossum25ce5661997-08-02 03:10:38 +00001187PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001188_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001190 PyObject *m, *v, *sysdict;
1191 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +00001192 char *s;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001193#ifdef MS_WINDOWS
Tim Peters02f1d0d2006-06-06 00:25:07 +00001194 char buf[128];
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001195#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +00001196
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001197 m = Py_InitModule3("sys", sys_methods, sys_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001198 if (m == NULL)
1199 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001200 sysdict = PyModule_GetDict(m);
Christian Heimes0d924432008-01-30 17:21:22 +00001201#define SET_SYS_FROM_STRING(key, value) \
1202 v = value; \
1203 if (v != NULL) \
1204 PyDict_SetItemString(sysdict, key, v); \
1205 Py_XDECREF(v)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001206
Neal Norwitz11bd1192005-10-03 00:54:56 +00001207 {
1208 /* XXX: does this work on Win/Win64? (see posix_fstat) */
1209 struct stat sb;
1210 if (fstat(fileno(stdin), &sb) == 0 &&
1211 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +00001212 /* There's nothing more we can do. */
1213 /* Py_FatalError() will core dump, so just exit. */
1214 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1215 exit(EXIT_FAILURE);
Neal Norwitz11bd1192005-10-03 00:54:56 +00001216 }
1217 }
1218
Martin v. Löwis13a1fde2005-01-27 18:56:16 +00001219 /* Closing the standard FILE* if sys.std* goes aways causes problems
1220 * for embedded Python usages. Closing them when somebody explicitly
1221 * invokes .close() might be possible, but the FAQ promises they get
1222 * never closed. However, we still need to get write errors when
1223 * writing fails (e.g. because stdout is redirected), so we flush the
1224 * streams and check for errors before the file objects are deleted.
1225 * On OS X, fflush()ing stdin causes an error, so we exempt stdin
1226 * from that procedure.
1227 */
1228 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +00001229 sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
1230 syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001231 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001232 return NULL;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001233#ifdef MS_WINDOWS
Thomas Woutersafea5292007-01-23 13:42:00 +00001234 if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001235 sprintf(buf, "cp%d", GetConsoleCP());
1236 if (!PyFile_SetEncoding(sysin, buf))
1237 return NULL;
1238 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001239 if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001240 sprintf(buf, "cp%d", GetConsoleOutputCP());
1241 if (!PyFile_SetEncoding(sysout, buf))
1242 return NULL;
1243 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001244 if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) {
Martin v. Löwisea62d252006-04-03 10:56:49 +00001245 sprintf(buf, "cp%d", GetConsoleOutputCP());
1246 if (!PyFile_SetEncoding(syserr, buf))
1247 return NULL;
1248 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001249#endif
1250
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001251 PyDict_SetItemString(sysdict, "stdin", sysin);
1252 PyDict_SetItemString(sysdict, "stdout", sysout);
1253 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +00001254 /* Make backup copies for cleanup */
1255 PyDict_SetItemString(sysdict, "__stdin__", sysin);
1256 PyDict_SetItemString(sysdict, "__stdout__", sysout);
1257 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001258 PyDict_SetItemString(sysdict, "__displayhook__",
1259 PyDict_GetItemString(sysdict, "displayhook"));
1260 PyDict_SetItemString(sysdict, "__excepthook__",
1261 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +00001262 Py_XDECREF(sysin);
1263 Py_XDECREF(sysout);
1264 Py_XDECREF(syserr);
Christian Heimes0d924432008-01-30 17:21:22 +00001265
1266 SET_SYS_FROM_STRING("version",
1267 PyString_FromString(Py_GetVersion()));
1268 SET_SYS_FROM_STRING("hexversion",
1269 PyInt_FromLong(PY_VERSION_HEX));
Martin v. Löwis43b57802006-01-05 23:38:54 +00001270 svnversion_init();
Christian Heimes0d924432008-01-30 17:21:22 +00001271 SET_SYS_FROM_STRING("subversion",
1272 Py_BuildValue("(ssz)", "CPython", branch,
1273 svn_revision));
1274 SET_SYS_FROM_STRING("dont_write_bytecode",
1275 PyBool_FromLong(Py_DontWriteBytecodeFlag));
Fred Drake93a20bf2000-04-13 17:44:51 +00001276 /*
1277 * These release level checks are mutually exclusive and cover
1278 * the field, so don't get too fancy with the pre-processor!
1279 */
1280#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001281 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +00001282#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001283 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +00001284#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001285 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +00001286#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +00001287 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +00001288#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001289
Neal Norwitze1fdb322006-07-21 05:32:28 +00001290 SET_SYS_FROM_STRING("version_info",
1291 Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +00001292 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +00001293 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +00001294 PY_RELEASE_SERIAL));
Neal Norwitze1fdb322006-07-21 05:32:28 +00001295 SET_SYS_FROM_STRING("api_version",
1296 PyInt_FromLong(PYTHON_API_VERSION));
1297 SET_SYS_FROM_STRING("copyright",
1298 PyString_FromString(Py_GetCopyright()));
1299 SET_SYS_FROM_STRING("platform",
1300 PyString_FromString(Py_GetPlatform()));
1301 SET_SYS_FROM_STRING("executable",
1302 PyString_FromString(Py_GetProgramFullPath()));
1303 SET_SYS_FROM_STRING("prefix",
1304 PyString_FromString(Py_GetPrefix()));
1305 SET_SYS_FROM_STRING("exec_prefix",
1306 PyString_FromString(Py_GetExecPrefix()));
1307 SET_SYS_FROM_STRING("maxint",
1308 PyInt_FromLong(PyInt_GetMax()));
Christian Heimes28104c52007-11-27 23:16:44 +00001309 SET_SYS_FROM_STRING("py3kwarning",
1310 PyBool_FromLong(Py_Py3kWarningFlag));
Christian Heimesdfdfaab2007-12-01 11:20:10 +00001311 SET_SYS_FROM_STRING("float_info",
1312 PyFloat_GetInfo());
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001313#ifdef Py_USING_UNICODE
Neal Norwitze1fdb322006-07-21 05:32:28 +00001314 SET_SYS_FROM_STRING("maxunicode",
1315 PyInt_FromLong(PyUnicode_GetMax()));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001316#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001317 SET_SYS_FROM_STRING("builtin_module_names",
1318 list_builtin_module_names());
Fred Drake099325e2000-08-14 15:47:03 +00001319 {
1320 /* Assumes that longs are at least 2 bytes long.
1321 Should be safe! */
1322 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +00001323 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001324
1325 s = (char *) &number;
1326 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +00001327 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +00001328 else
Fred Drakea2b6ad62000-08-15 04:24:43 +00001329 value = "little";
Neal Norwitze1fdb322006-07-21 05:32:28 +00001330 SET_SYS_FROM_STRING("byteorder",
1331 PyString_FromString(value));
Fred Drake099325e2000-08-14 15:47:03 +00001332 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001333#ifdef MS_COREDLL
Neal Norwitze1fdb322006-07-21 05:32:28 +00001334 SET_SYS_FROM_STRING("dllhandle",
1335 PyLong_FromVoidPtr(PyWin_DLLhModule));
1336 SET_SYS_FROM_STRING("winver",
1337 PyString_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001338#endif
Guido van Rossum23fff912000-12-15 22:02:05 +00001339 if (warnoptions == NULL) {
1340 warnoptions = PyList_New(0);
1341 }
1342 else {
1343 Py_INCREF(warnoptions);
1344 }
1345 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +00001346 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +00001347 }
Tim Peters216b78b2006-01-06 02:40:53 +00001348
Christian Heimesf31b69f2008-01-14 03:42:48 +00001349 PyStructSequence_InitType(&FlagsType, &flags_desc);
Christian Heimes0d924432008-01-30 17:21:22 +00001350 SET_SYS_FROM_STRING("flags", make_flags());
Christian Heimesf31b69f2008-01-14 03:42:48 +00001351 /* prevent user from creating new instances */
1352 FlagsType.tp_init = NULL;
1353 FlagsType.tp_new = NULL;
1354
Christian Heimes0d924432008-01-30 17:21:22 +00001355#undef SET_SYS_FROM_STRING
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001356 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001357 return NULL;
1358 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001359}
1360
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001361static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001362makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001363{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001364 int i, n;
1365 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001366 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001367
Guido van Rossum3f5da241990-12-20 15:06:42 +00001368 n = 1;
1369 p = path;
1370 while ((p = strchr(p, delim)) != NULL) {
1371 n++;
1372 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001373 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001374 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001375 if (v == NULL)
1376 return NULL;
1377 for (i = 0; ; i++) {
1378 p = strchr(path, delim);
1379 if (p == NULL)
1380 p = strchr(path, '\0'); /* End of string */
Armin Rigo7ccbca92006-10-04 12:17:45 +00001381 w = PyString_FromStringAndSize(path, (Py_ssize_t) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001382 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001383 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001384 return NULL;
1385 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001386 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001387 if (*p == '\0')
1388 break;
1389 path = p+1;
1390 }
1391 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001392}
1393
1394void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001395PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001396{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001397 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001398 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001399 Py_FatalError("can't create sys.path");
1400 if (PySys_SetObject("path", v) != 0)
1401 Py_FatalError("can't assign sys.path");
1402 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001403}
1404
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001405static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001406makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001407{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001408 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +00001409 if (argc <= 0 || argv == NULL) {
1410 /* Ensure at least one (empty) argument is seen */
1411 static char *empty_argv[1] = {""};
1412 argv = empty_argv;
1413 argc = 1;
1414 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001415 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001416 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001417 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001418 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001419#ifdef __VMS
1420 PyObject *v;
1421
1422 /* argv[0] is the script pathname if known */
1423 if (i == 0) {
1424 char* fn = decc$translate_vms(argv[0]);
1425 if ((fn == (char *)0) || fn == (char *)-1)
1426 v = PyString_FromString(argv[0]);
1427 else
1428 v = PyString_FromString(
1429 decc$translate_vms(argv[0]));
1430 } else
1431 v = PyString_FromString(argv[i]);
1432#else
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001433 PyObject *v = PyString_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001434#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001435 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001436 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001437 av = NULL;
1438 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001439 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001440 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001441 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001442 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001443 return av;
1444}
1445
1446void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001447PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001448{
Guido van Rossum162e38c2003-02-19 15:25:10 +00001449#if defined(HAVE_REALPATH)
1450 char fullpath[MAXPATHLEN];
1451#elif defined(MS_WINDOWS)
Thomas Heller27bb71e2003-01-08 14:33:48 +00001452 char fullpath[MAX_PATH];
1453#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001454 PyObject *av = makeargvobject(argc, argv);
1455 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001456 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001457 Py_FatalError("no mem for sys.argv");
1458 if (PySys_SetObject("argv", av) != 0)
1459 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001460 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001461 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001462 char *p = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001463 Py_ssize_t n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001464 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001465#ifdef HAVE_READLINK
1466 char link[MAXPATHLEN+1];
1467 char argv0copy[2*MAXPATHLEN+1];
1468 int nr = 0;
Georg Brandl69537722005-09-15 13:00:34 +00001469 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0)
Guido van Rossumc474dea1997-04-25 15:38:31 +00001470 nr = readlink(argv0, link, MAXPATHLEN);
1471 if (nr > 0) {
1472 /* It's a symlink */
1473 link[nr] = '\0';
1474 if (link[0] == SEP)
1475 argv0 = link; /* Link to absolute path */
1476 else if (strchr(link, SEP) == NULL)
1477 ; /* Link without path */
1478 else {
1479 /* Must join(dirname(argv0), link) */
1480 char *q = strrchr(argv0, SEP);
1481 if (q == NULL)
1482 argv0 = link; /* argv0 without path */
1483 else {
1484 /* Must make a copy */
1485 strcpy(argv0copy, argv0);
1486 q = strrchr(argv0copy, SEP);
1487 strcpy(q+1, link);
1488 argv0 = argv0copy;
1489 }
1490 }
1491 }
1492#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001493#if SEP == '\\' /* Special case for MS filename syntax */
Georg Brandl69537722005-09-15 13:00:34 +00001494 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001495 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001496#ifdef MS_WINDOWS
1497 char *ptemp;
1498 if (GetFullPathName(argv0,
1499 sizeof(fullpath),
1500 fullpath,
1501 &ptemp)) {
1502 argv0 = fullpath;
1503 }
1504#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001505 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001506 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001507 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001508 if (q != NULL)
1509 p = q;
1510 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001511 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001512 if (n > 1 && p[-1] != ':')
1513 n--; /* Drop trailing separator */
1514 }
1515 }
1516#else /* All other filename syntaxes */
Georg Brandl69537722005-09-15 13:00:34 +00001517 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossum162e38c2003-02-19 15:25:10 +00001518#if defined(HAVE_REALPATH)
1519 if (realpath(argv0, fullpath)) {
1520 argv0 = fullpath;
1521 }
1522#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001523 p = strrchr(argv0, SEP);
Guido van Rossum162e38c2003-02-19 15:25:10 +00001524 }
Guido van Rossumcc883411996-09-10 14:44:21 +00001525 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001526#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001527 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001528#else /* don't include trailing separator */
1529 n = p - argv0;
1530#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001531#if SEP == '/' /* Special case for Unix filename syntax */
1532 if (n > 1)
1533 n--; /* Drop trailing separator */
1534#endif /* Unix */
1535 }
1536#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001537 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001538 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001539 Py_FatalError("no mem for sys.path insertion");
1540 if (PyList_Insert(path, 0, a) < 0)
1541 Py_FatalError("sys.path.insert(0) failed");
1542 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001543 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001544 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001545}
Guido van Rossuma890e681998-05-12 14:59:24 +00001546
1547
1548/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1549 Adapted from code submitted by Just van Rossum.
1550
1551 PySys_WriteStdout(format, ...)
1552 PySys_WriteStderr(format, ...)
1553
1554 The first function writes to sys.stdout; the second to sys.stderr. When
1555 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001556 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001557
1558 Both take a printf-style format string as their first argument followed
1559 by a variable length argument list determined by the format string.
1560
1561 *** WARNING ***
1562
1563 The format should limit the total size of the formatted output string to
1564 1000 bytes. In particular, this means that no unrestricted "%s" formats
1565 should occur; these should be limited using "%.<N>s where <N> is a
1566 decimal number calculated so that <N> plus the maximum size of other
1567 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1568 which can print hundreds of digits for very large numbers.
1569
1570 */
1571
1572static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001573mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001574{
1575 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001576 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001577
Guido van Rossum8442af31998-10-12 18:22:10 +00001578 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001579 file = PySys_GetObject(name);
1580 if (file == NULL || PyFile_AsFile(file) == fp)
1581 vfprintf(fp, format, va);
1582 else {
1583 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001584 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1585 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001586 if (PyFile_WriteString(buffer, file) != 0) {
1587 PyErr_Clear();
1588 fputs(buffer, fp);
1589 }
Skip Montanaro53a6d1d2006-04-18 00:55:46 +00001590 if (written < 0 || (size_t)written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001591 const char *truncated = "... truncated";
1592 if (PyFile_WriteString(truncated, file) != 0) {
1593 PyErr_Clear();
1594 fputs(truncated, fp);
1595 }
1596 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001597 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001598 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001599}
1600
1601void
Guido van Rossuma890e681998-05-12 14:59:24 +00001602PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001603{
1604 va_list va;
1605
Guido van Rossuma890e681998-05-12 14:59:24 +00001606 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001607 mywrite("stdout", stdout, format, va);
1608 va_end(va);
1609}
1610
1611void
Guido van Rossuma890e681998-05-12 14:59:24 +00001612PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001613{
1614 va_list va;
1615
Guido van Rossuma890e681998-05-12 14:59:24 +00001616 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001617 mywrite("stderr", stderr, format, va);
1618 va_end(va);
1619}