blob: 12ad828bbbc233dff5bd28d3681ab88eb4c4f20e [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
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000757#ifdef __cplusplus
758extern "C" {
759#endif
760
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000761#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000762/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000763extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000764#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000765
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000766#ifdef DYNAMIC_EXECUTION_PROFILE
767/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000768extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000769#endif
770
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000771#ifdef __cplusplus
772}
773#endif
774
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000775static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000776 /* Might as well keep this in alphabetic order */
Tim Peters216b78b2006-01-06 02:40:53 +0000777 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
Jeremy Hylton985eba52003-02-05 23:13:00 +0000778 callstats_doc},
Tim Peters32a83612006-07-10 21:08:24 +0000779 {"_current_frames", sys_current_frames, METH_NOARGS,
780 current_frames_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000781 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000782 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
783 {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000784 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000785 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000786#ifdef Py_USING_UNICODE
Tim Peters216b78b2006-01-06 02:40:53 +0000787 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
788 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000789#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000790#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000791 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000792 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000793#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000794#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000795 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000796#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000797#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000798 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000799#endif
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000800#ifdef Py_USING_UNICODE
801 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
Tim Peters216b78b2006-01-06 02:40:53 +0000802 METH_NOARGS, getfilesystemencoding_doc},
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000803#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000804#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000805 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000806#endif
807#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000808 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000809#endif
Fred Drakea7688822001-10-24 20:47:48 +0000810 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000811 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000812 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000813 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000814#ifdef MS_WINDOWS
815 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
816 getwindowsversion_doc},
817#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000818#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000819 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000820#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000821#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000822 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000823 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000824#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000825 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000826 setcheckinterval_doc},
Tim Peterse5e065b2003-07-06 18:36:54 +0000827 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000828 getcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000829#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000830 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000831 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000832#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000833 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Georg Brandl56112892008-01-20 13:59:46 +0000834 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000835 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000836 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000837#ifdef WITH_TSC
838 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
839#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000840 {"settrace", sys_settrace, METH_O, settrace_doc},
Georg Brandl56112892008-01-20 13:59:46 +0000841 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000842 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000843 {NULL, NULL} /* sentinel */
844};
845
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000846static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000847list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000848{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000849 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000850 int i;
851 if (list == NULL)
852 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000853 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000854 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000855 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000856 if (name == NULL)
857 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000858 PyList_Append(list, name);
859 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000860 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000861 if (PyList_Sort(list) != 0) {
862 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000863 list = NULL;
864 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000865 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000866 PyObject *v = PyList_AsTuple(list);
867 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000868 list = v;
869 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000870 return list;
871}
872
Guido van Rossum23fff912000-12-15 22:02:05 +0000873static PyObject *warnoptions = NULL;
874
875void
876PySys_ResetWarnOptions(void)
877{
878 if (warnoptions == NULL || !PyList_Check(warnoptions))
879 return;
880 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
881}
882
883void
884PySys_AddWarnOption(char *s)
885{
886 PyObject *str;
887
888 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
889 Py_XDECREF(warnoptions);
890 warnoptions = PyList_New(0);
891 if (warnoptions == NULL)
892 return;
893 }
894 str = PyString_FromString(s);
895 if (str != NULL) {
896 PyList_Append(warnoptions, str);
897 Py_DECREF(str);
898 }
899}
900
Guido van Rossum40552d01998-08-06 03:34:39 +0000901/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
902 Two literals concatenated works just fine. If you have a K&R compiler
903 or other abomination that however *does* understand longer strings,
904 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000905PyDoc_VAR(sys_doc) =
906PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000907"This module provides access to some objects used or maintained by the\n\
908interpreter and to functions that interact strongly with the interpreter.\n\
909\n\
910Dynamic objects:\n\
911\n\
912argv -- command line arguments; argv[0] is the script pathname if known\n\
913path -- module search path; path[0] is the script directory, else ''\n\
914modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000915\n\
916displayhook -- called to show results in an interactive session\n\
917excepthook -- called to handle any uncaught exception other than SystemExit\n\
918 To customize printing in an interactive session or to install a custom\n\
919 top-level exception handler, assign other functions to replace these.\n\
920\n\
921exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
922 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000923\n\
924stdin -- standard input file object; used by raw_input() and input()\n\
925stdout -- standard output file object; used by the print statement\n\
926stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000927 By assigning other file objects (or objects that behave like files)\n\
928 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000929\n\
930last_type -- type of last uncaught exception\n\
931last_value -- value of last uncaught exception\n\
932last_traceback -- traceback of last uncaught exception\n\
933 These three are only available in an interactive session after a\n\
934 traceback has been printed.\n\
935\n\
936exc_type -- type of exception currently being handled\n\
937exc_value -- value of exception currently being handled\n\
938exc_traceback -- traceback of exception currently being handled\n\
939 The function exc_info() should be used instead of these three,\n\
940 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000941"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000942)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000943/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000944PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000945"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000946Static objects:\n\
947\n\
948maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000949maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000950builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000951version -- the version of this interpreter as a string\n\
952version_info -- version information as a tuple\n\
953hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000954copyright -- copyright notice pertaining to this interpreter\n\
955platform -- platform identifier\n\
956executable -- pathname of this Python interpreter\n\
957prefix -- prefix used to find the Python library\n\
958exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000959"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000960)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000961#ifdef MS_WINDOWS
962/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000963PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000964"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000965winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000966"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000967)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000968#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000969PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000970"__stdin__ -- the original stdin; don't touch!\n\
971__stdout__ -- the original stdout; don't touch!\n\
972__stderr__ -- the original stderr; don't touch!\n\
973__displayhook__ -- the original displayhook; don't touch!\n\
974__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000975\n\
976Functions:\n\
977\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000978displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000979excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000980exc_info() -- return thread-safe information about the current exception\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000981exc_clear() -- clear the exception state for the current thread\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000982exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000983getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Georg Brandl56112892008-01-20 13:59:46 +0000984getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000985getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000986getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Georg Brandl56112892008-01-20 13:59:46 +0000987gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000988setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000989setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000990setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000991setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000992settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000993"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000994)
Fred Drakeccede592000-08-14 20:59:57 +0000995/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000996
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +0000997static int
998_check_and_flush (FILE *stream)
999{
1000 int prev_fail = ferror (stream);
1001 return fflush (stream) || prev_fail ? EOF : 0;
1002}
1003
Martin v. Löwis43b57802006-01-05 23:38:54 +00001004/* Subversion branch and revision management */
1005static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
1006static const char headurl[] = "$HeadURL$";
1007static int svn_initialized;
1008static char patchlevel_revision[50]; /* Just the number */
1009static char branch[50];
1010static char shortbranch[50];
1011static const char *svn_revision;
1012
Tim Peterse86e7a52006-01-06 02:42:46 +00001013static void
1014svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +00001015{
1016 const char *python, *br_start, *br_end, *br_end2, *svnversion;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001017 Py_ssize_t len;
1018 int istag;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001019
1020 if (svn_initialized)
1021 return;
1022
1023 python = strstr(headurl, "/python/");
1024 if (!python)
1025 Py_FatalError("subversion keywords missing");
1026
1027 br_start = python + 8;
1028 br_end = strchr(br_start, '/');
Neal Norwitz837ce932006-10-28 21:15:30 +00001029 assert(br_end);
1030
Tim Peters216b78b2006-01-06 02:40:53 +00001031 /* Works even for trunk,
Martin v. Löwis43b57802006-01-05 23:38:54 +00001032 as we are in trunk/Python/sysmodule.c */
1033 br_end2 = strchr(br_end+1, '/');
1034
1035 istag = strncmp(br_start, "tags", 4) == 0;
1036 if (strncmp(br_start, "trunk", 5) == 0) {
1037 strcpy(branch, "trunk");
1038 strcpy(shortbranch, "trunk");
1039
Tim Peters216b78b2006-01-06 02:40:53 +00001040 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001041 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1042 len = br_end2 - br_start;
1043 strncpy(branch, br_start, len);
1044 branch[len] = '\0';
1045
1046 len = br_end2 - (br_end + 1);
1047 strncpy(shortbranch, br_end + 1, len);
1048 shortbranch[len] = '\0';
Tim Peters216b78b2006-01-06 02:40:53 +00001049 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001050 else {
1051 Py_FatalError("bad HeadURL");
1052 return;
1053 }
1054
1055
1056 svnversion = _Py_svnversion();
1057 if (strcmp(svnversion, "exported") != 0)
1058 svn_revision = svnversion;
1059 else if (istag) {
1060 len = strlen(_patchlevel_revision);
Neal Norwitz68cdf8a2007-04-16 07:37:55 +00001061 assert(len >= 13);
1062 assert(len < (sizeof(patchlevel_revision) + 13));
Martin v. Löwis43b57802006-01-05 23:38:54 +00001063 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1064 len - 13);
1065 patchlevel_revision[len - 13] = '\0';
1066 svn_revision = patchlevel_revision;
1067 }
1068 else
1069 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001070
Martin v. Löwis43b57802006-01-05 23:38:54 +00001071 svn_initialized = 1;
1072}
1073
1074/* Return svnversion output if available.
1075 Else return Revision of patchlevel.h if on branch.
1076 Else return empty string */
1077const char*
1078Py_SubversionRevision()
1079{
1080 svnversion_init();
1081 return svn_revision;
1082}
1083
1084const char*
1085Py_SubversionShortBranch()
1086{
1087 svnversion_init();
1088 return shortbranch;
1089}
1090
Christian Heimesf31b69f2008-01-14 03:42:48 +00001091
1092PyDoc_STRVAR(flags__doc__,
1093"sys.flags\n\
1094\n\
1095Flags provided through command line arguments or environment vars.");
1096
1097static PyTypeObject FlagsType;
1098
1099static PyStructSequence_Field flags_fields[] = {
1100 {"debug", "-d"},
1101 {"py3k_warning", "-3"},
1102 {"division_warning", "-Q"},
1103 {"division_new", "-Qnew"},
1104 {"inspect", "-i"},
1105 {"interactive", "-i"},
1106 {"optimize", "-O or -OO"},
1107 {"dont_write_bytecode", "-B"},
1108 /* {"no_user_site", "-s"}, */
1109 {"no_site", "-S"},
Andrew M. Kuchling7ce9b182008-01-15 01:29:16 +00001110 {"ignore_environment", "-E"},
Christian Heimesf31b69f2008-01-14 03:42:48 +00001111 {"tabcheck", "-t or -tt"},
1112 {"verbose", "-v"},
1113#ifdef RISCOS
Andrew M. Kuchling7ce9b182008-01-15 01:29:16 +00001114 {"riscos_wimp", "???"},
Christian Heimesf31b69f2008-01-14 03:42:48 +00001115#endif
1116 /* {"unbuffered", "-u"}, */
1117 {"unicode", "-U"},
1118 /* {"skip_first", "-x"}, */
1119 {0}
1120};
1121
1122static PyStructSequence_Desc flags_desc = {
1123 "sys.flags", /* name */
1124 flags__doc__, /* doc */
1125 flags_fields, /* fields */
1126#ifdef RISCOS
1127 14
1128#else
1129 13
1130#endif
1131};
1132
1133static PyObject*
1134make_flags(void)
1135{
1136 int pos = 0;
1137 PyObject *seq;
1138
1139 seq = PyStructSequence_New(&FlagsType);
1140 if (seq == NULL)
1141 return NULL;
1142
1143#define SetFlag(flag) \
1144 PyStructSequence_SET_ITEM(seq, pos++, PyInt_FromLong(flag))
1145
1146 SetFlag(Py_DebugFlag);
1147 SetFlag(Py_Py3kWarningFlag);
1148 SetFlag(Py_DivisionWarningFlag);
1149 SetFlag(_Py_QnewFlag);
1150 SetFlag(Py_InspectFlag);
1151 SetFlag(Py_InteractiveFlag);
1152 SetFlag(Py_OptimizeFlag);
1153 SetFlag(Py_DontWriteBytecodeFlag);
1154 /* SetFlag(Py_NoUserSiteDirectory); */
1155 SetFlag(Py_NoSiteFlag);
1156 SetFlag(Py_IgnoreEnvironmentFlag);
1157 SetFlag(Py_TabcheckFlag);
1158 SetFlag(Py_VerboseFlag);
1159#ifdef RISCOS
1160 SetFlag(Py_RISCOSWimpFlag);
1161#endif
1162 /* SetFlag(saw_unbuffered_flag); */
1163 SetFlag(Py_UnicodeFlag);
1164 /* SetFlag(skipfirstline); */
1165#undef SetFlag
1166
1167 if (PyErr_Occurred()) {
1168 return NULL;
1169 }
1170
1171 Py_INCREF(seq);
1172 return seq;
1173}
1174
Guido van Rossum25ce5661997-08-02 03:10:38 +00001175PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001176_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001178 PyObject *m, *v, *sysdict;
1179 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +00001180 char *s;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001181#ifdef MS_WINDOWS
Tim Peters02f1d0d2006-06-06 00:25:07 +00001182 char buf[128];
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001183#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +00001184
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001185 m = Py_InitModule3("sys", sys_methods, sys_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001186 if (m == NULL)
1187 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001188 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001189
Neal Norwitz11bd1192005-10-03 00:54:56 +00001190 {
1191 /* XXX: does this work on Win/Win64? (see posix_fstat) */
1192 struct stat sb;
1193 if (fstat(fileno(stdin), &sb) == 0 &&
1194 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +00001195 /* There's nothing more we can do. */
1196 /* Py_FatalError() will core dump, so just exit. */
1197 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1198 exit(EXIT_FAILURE);
Neal Norwitz11bd1192005-10-03 00:54:56 +00001199 }
1200 }
1201
Martin v. Löwis13a1fde2005-01-27 18:56:16 +00001202 /* Closing the standard FILE* if sys.std* goes aways causes problems
1203 * for embedded Python usages. Closing them when somebody explicitly
1204 * invokes .close() might be possible, but the FAQ promises they get
1205 * never closed. However, we still need to get write errors when
1206 * writing fails (e.g. because stdout is redirected), so we flush the
1207 * streams and check for errors before the file objects are deleted.
1208 * On OS X, fflush()ing stdin causes an error, so we exempt stdin
1209 * from that procedure.
1210 */
1211 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +00001212 sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
1213 syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001214 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001215 return NULL;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001216#ifdef MS_WINDOWS
Thomas Woutersafea5292007-01-23 13:42:00 +00001217 if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001218 sprintf(buf, "cp%d", GetConsoleCP());
1219 if (!PyFile_SetEncoding(sysin, buf))
1220 return NULL;
1221 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001222 if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001223 sprintf(buf, "cp%d", GetConsoleOutputCP());
1224 if (!PyFile_SetEncoding(sysout, buf))
1225 return NULL;
1226 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001227 if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) {
Martin v. Löwisea62d252006-04-03 10:56:49 +00001228 sprintf(buf, "cp%d", GetConsoleOutputCP());
1229 if (!PyFile_SetEncoding(syserr, buf))
1230 return NULL;
1231 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001232#endif
1233
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001234 PyDict_SetItemString(sysdict, "stdin", sysin);
1235 PyDict_SetItemString(sysdict, "stdout", sysout);
1236 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +00001237 /* Make backup copies for cleanup */
1238 PyDict_SetItemString(sysdict, "__stdin__", sysin);
1239 PyDict_SetItemString(sysdict, "__stdout__", sysout);
1240 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001241 PyDict_SetItemString(sysdict, "__displayhook__",
1242 PyDict_GetItemString(sysdict, "displayhook"));
1243 PyDict_SetItemString(sysdict, "__excepthook__",
1244 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +00001245 Py_XDECREF(sysin);
1246 Py_XDECREF(sysout);
1247 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001248 PyDict_SetItemString(sysdict, "version",
1249 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +00001250 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +00001251 PyDict_SetItemString(sysdict, "hexversion",
1252 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001253 Py_XDECREF(v);
Martin v. Löwis43b57802006-01-05 23:38:54 +00001254 svnversion_init();
1255 v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision);
1256 PyDict_SetItemString(sysdict, "subversion", v);
Barry Warsaw2a38a862005-12-18 01:27:35 +00001257 Py_XDECREF(v);
Christian Heimesf31b69f2008-01-14 03:42:48 +00001258 PyDict_SetItemString(sysdict, "dont_write_bytecode",
1259 v = PyBool_FromLong(Py_DontWriteBytecodeFlag));
1260 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +00001261 /*
1262 * These release level checks are mutually exclusive and cover
1263 * the field, so don't get too fancy with the pre-processor!
1264 */
1265#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001266 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +00001267#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001268 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +00001269#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001270 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +00001271#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +00001272 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +00001273#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001274
1275#define SET_SYS_FROM_STRING(key, value) \
1276 v = value; \
1277 if (v != NULL) \
1278 PyDict_SetItemString(sysdict, key, v); \
1279 Py_XDECREF(v)
1280
1281 SET_SYS_FROM_STRING("version_info",
1282 Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +00001283 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +00001284 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +00001285 PY_RELEASE_SERIAL));
Neal Norwitze1fdb322006-07-21 05:32:28 +00001286 SET_SYS_FROM_STRING("api_version",
1287 PyInt_FromLong(PYTHON_API_VERSION));
1288 SET_SYS_FROM_STRING("copyright",
1289 PyString_FromString(Py_GetCopyright()));
1290 SET_SYS_FROM_STRING("platform",
1291 PyString_FromString(Py_GetPlatform()));
1292 SET_SYS_FROM_STRING("executable",
1293 PyString_FromString(Py_GetProgramFullPath()));
1294 SET_SYS_FROM_STRING("prefix",
1295 PyString_FromString(Py_GetPrefix()));
1296 SET_SYS_FROM_STRING("exec_prefix",
1297 PyString_FromString(Py_GetExecPrefix()));
1298 SET_SYS_FROM_STRING("maxint",
1299 PyInt_FromLong(PyInt_GetMax()));
Christian Heimes28104c52007-11-27 23:16:44 +00001300 SET_SYS_FROM_STRING("py3kwarning",
1301 PyBool_FromLong(Py_Py3kWarningFlag));
Christian Heimesdfdfaab2007-12-01 11:20:10 +00001302 SET_SYS_FROM_STRING("float_info",
1303 PyFloat_GetInfo());
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001304#ifdef Py_USING_UNICODE
Neal Norwitze1fdb322006-07-21 05:32:28 +00001305 SET_SYS_FROM_STRING("maxunicode",
1306 PyInt_FromLong(PyUnicode_GetMax()));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001307#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001308 SET_SYS_FROM_STRING("builtin_module_names",
1309 list_builtin_module_names());
Fred Drake099325e2000-08-14 15:47:03 +00001310 {
1311 /* Assumes that longs are at least 2 bytes long.
1312 Should be safe! */
1313 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +00001314 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001315
1316 s = (char *) &number;
1317 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +00001318 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +00001319 else
Fred Drakea2b6ad62000-08-15 04:24:43 +00001320 value = "little";
Neal Norwitze1fdb322006-07-21 05:32:28 +00001321 SET_SYS_FROM_STRING("byteorder",
1322 PyString_FromString(value));
Fred Drake099325e2000-08-14 15:47:03 +00001323 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001324#ifdef MS_COREDLL
Neal Norwitze1fdb322006-07-21 05:32:28 +00001325 SET_SYS_FROM_STRING("dllhandle",
1326 PyLong_FromVoidPtr(PyWin_DLLhModule));
1327 SET_SYS_FROM_STRING("winver",
1328 PyString_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001329#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001330#undef SET_SYS_FROM_STRING
Guido van Rossum23fff912000-12-15 22:02:05 +00001331 if (warnoptions == NULL) {
1332 warnoptions = PyList_New(0);
1333 }
1334 else {
1335 Py_INCREF(warnoptions);
1336 }
1337 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +00001338 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +00001339 }
Tim Peters216b78b2006-01-06 02:40:53 +00001340
Christian Heimesf31b69f2008-01-14 03:42:48 +00001341 PyStructSequence_InitType(&FlagsType, &flags_desc);
1342 PyDict_SetItemString(sysdict, "flags", make_flags());
1343 /* prevent user from creating new instances */
1344 FlagsType.tp_init = NULL;
1345 FlagsType.tp_new = NULL;
1346
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001347 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001348 return NULL;
1349 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001350}
1351
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001352static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001353makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001354{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001355 int i, n;
1356 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001357 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001358
Guido van Rossum3f5da241990-12-20 15:06:42 +00001359 n = 1;
1360 p = path;
1361 while ((p = strchr(p, delim)) != NULL) {
1362 n++;
1363 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001364 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001365 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366 if (v == NULL)
1367 return NULL;
1368 for (i = 0; ; i++) {
1369 p = strchr(path, delim);
1370 if (p == NULL)
1371 p = strchr(path, '\0'); /* End of string */
Armin Rigo7ccbca92006-10-04 12:17:45 +00001372 w = PyString_FromStringAndSize(path, (Py_ssize_t) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001373 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001374 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001375 return NULL;
1376 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001377 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001378 if (*p == '\0')
1379 break;
1380 path = p+1;
1381 }
1382 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383}
1384
1385void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001386PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001387{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001388 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001389 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001390 Py_FatalError("can't create sys.path");
1391 if (PySys_SetObject("path", v) != 0)
1392 Py_FatalError("can't assign sys.path");
1393 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001394}
1395
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001396static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001397makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001398{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001399 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +00001400 if (argc <= 0 || argv == NULL) {
1401 /* Ensure at least one (empty) argument is seen */
1402 static char *empty_argv[1] = {""};
1403 argv = empty_argv;
1404 argc = 1;
1405 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001406 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001407 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001408 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001409 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001410#ifdef __VMS
1411 PyObject *v;
1412
1413 /* argv[0] is the script pathname if known */
1414 if (i == 0) {
1415 char* fn = decc$translate_vms(argv[0]);
1416 if ((fn == (char *)0) || fn == (char *)-1)
1417 v = PyString_FromString(argv[0]);
1418 else
1419 v = PyString_FromString(
1420 decc$translate_vms(argv[0]));
1421 } else
1422 v = PyString_FromString(argv[i]);
1423#else
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001424 PyObject *v = PyString_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001425#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001426 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001427 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001428 av = NULL;
1429 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001430 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001431 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001432 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001433 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001434 return av;
1435}
1436
1437void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001438PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001439{
Guido van Rossum162e38c2003-02-19 15:25:10 +00001440#if defined(HAVE_REALPATH)
1441 char fullpath[MAXPATHLEN];
1442#elif defined(MS_WINDOWS)
Thomas Heller27bb71e2003-01-08 14:33:48 +00001443 char fullpath[MAX_PATH];
1444#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001445 PyObject *av = makeargvobject(argc, argv);
1446 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001447 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001448 Py_FatalError("no mem for sys.argv");
1449 if (PySys_SetObject("argv", av) != 0)
1450 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001451 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001452 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001453 char *p = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001454 Py_ssize_t n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001455 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001456#ifdef HAVE_READLINK
1457 char link[MAXPATHLEN+1];
1458 char argv0copy[2*MAXPATHLEN+1];
1459 int nr = 0;
Georg Brandl69537722005-09-15 13:00:34 +00001460 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0)
Guido van Rossumc474dea1997-04-25 15:38:31 +00001461 nr = readlink(argv0, link, MAXPATHLEN);
1462 if (nr > 0) {
1463 /* It's a symlink */
1464 link[nr] = '\0';
1465 if (link[0] == SEP)
1466 argv0 = link; /* Link to absolute path */
1467 else if (strchr(link, SEP) == NULL)
1468 ; /* Link without path */
1469 else {
1470 /* Must join(dirname(argv0), link) */
1471 char *q = strrchr(argv0, SEP);
1472 if (q == NULL)
1473 argv0 = link; /* argv0 without path */
1474 else {
1475 /* Must make a copy */
1476 strcpy(argv0copy, argv0);
1477 q = strrchr(argv0copy, SEP);
1478 strcpy(q+1, link);
1479 argv0 = argv0copy;
1480 }
1481 }
1482 }
1483#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001484#if SEP == '\\' /* Special case for MS filename syntax */
Georg Brandl69537722005-09-15 13:00:34 +00001485 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001486 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001487#ifdef MS_WINDOWS
1488 char *ptemp;
1489 if (GetFullPathName(argv0,
1490 sizeof(fullpath),
1491 fullpath,
1492 &ptemp)) {
1493 argv0 = fullpath;
1494 }
1495#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001496 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001497 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001498 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001499 if (q != NULL)
1500 p = q;
1501 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001502 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001503 if (n > 1 && p[-1] != ':')
1504 n--; /* Drop trailing separator */
1505 }
1506 }
1507#else /* All other filename syntaxes */
Georg Brandl69537722005-09-15 13:00:34 +00001508 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossum162e38c2003-02-19 15:25:10 +00001509#if defined(HAVE_REALPATH)
1510 if (realpath(argv0, fullpath)) {
1511 argv0 = fullpath;
1512 }
1513#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001514 p = strrchr(argv0, SEP);
Guido van Rossum162e38c2003-02-19 15:25:10 +00001515 }
Guido van Rossumcc883411996-09-10 14:44:21 +00001516 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001517#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001518 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001519#else /* don't include trailing separator */
1520 n = p - argv0;
1521#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001522#if SEP == '/' /* Special case for Unix filename syntax */
1523 if (n > 1)
1524 n--; /* Drop trailing separator */
1525#endif /* Unix */
1526 }
1527#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001528 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001529 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001530 Py_FatalError("no mem for sys.path insertion");
1531 if (PyList_Insert(path, 0, a) < 0)
1532 Py_FatalError("sys.path.insert(0) failed");
1533 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001534 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001535 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001536}
Guido van Rossuma890e681998-05-12 14:59:24 +00001537
1538
1539/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1540 Adapted from code submitted by Just van Rossum.
1541
1542 PySys_WriteStdout(format, ...)
1543 PySys_WriteStderr(format, ...)
1544
1545 The first function writes to sys.stdout; the second to sys.stderr. When
1546 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001547 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001548
1549 Both take a printf-style format string as their first argument followed
1550 by a variable length argument list determined by the format string.
1551
1552 *** WARNING ***
1553
1554 The format should limit the total size of the formatted output string to
1555 1000 bytes. In particular, this means that no unrestricted "%s" formats
1556 should occur; these should be limited using "%.<N>s where <N> is a
1557 decimal number calculated so that <N> plus the maximum size of other
1558 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1559 which can print hundreds of digits for very large numbers.
1560
1561 */
1562
1563static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001564mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001565{
1566 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001567 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001568
Guido van Rossum8442af31998-10-12 18:22:10 +00001569 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001570 file = PySys_GetObject(name);
1571 if (file == NULL || PyFile_AsFile(file) == fp)
1572 vfprintf(fp, format, va);
1573 else {
1574 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001575 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1576 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001577 if (PyFile_WriteString(buffer, file) != 0) {
1578 PyErr_Clear();
1579 fputs(buffer, fp);
1580 }
Skip Montanaro53a6d1d2006-04-18 00:55:46 +00001581 if (written < 0 || (size_t)written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001582 const char *truncated = "... truncated";
1583 if (PyFile_WriteString(truncated, file) != 0) {
1584 PyErr_Clear();
1585 fputs(truncated, fp);
1586 }
1587 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001588 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001589 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001590}
1591
1592void
Guido van Rossuma890e681998-05-12 14:59:24 +00001593PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001594{
1595 va_list va;
1596
Guido van Rossuma890e681998-05-12 14:59:24 +00001597 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001598 mywrite("stdout", stdout, format, va);
1599 va_end(va);
1600}
1601
1602void
Guido van Rossuma890e681998-05-12 14:59:24 +00001603PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001604{
1605 va_list va;
1606
Guido van Rossuma890e681998-05-12 14:59:24 +00001607 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001608 mywrite("stderr", stderr, format, va);
1609 va_end(va);
1610}