blob: cbdda18cf9760a46a7212d760a0d6b5d04db31f2 [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 *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000401sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000402{
Fred Drake5755ce62001-06-27 19:19:46 +0000403 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000404 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000405 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000406 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000407 else
Fred Drake5755ce62001-06-27 19:19:46 +0000408 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000409 Py_INCREF(Py_None);
410 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000411}
412
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000413PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000414"setprofile(function)\n\
415\n\
416Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000417and return. See the profiler chapter in the library manual."
418);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000419
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000420static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000422{
Skip Montanarod581d772002-09-03 20:10:45 +0000423 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000424 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000425 Py_INCREF(Py_None);
426 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000427}
428
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000429PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000430"setcheckinterval(n)\n\
431\n\
432Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000433n instructions. This also affects how often thread switches occur."
434);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000435
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000436static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000437sys_getcheckinterval(PyObject *self, PyObject *args)
438{
439 return PyInt_FromLong(_Py_CheckInterval);
440}
441
442PyDoc_STRVAR(getcheckinterval_doc,
443"getcheckinterval() -> current check interval; see setcheckinterval()."
444);
445
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000446#ifdef WITH_TSC
447static PyObject *
448sys_settscdump(PyObject *self, PyObject *args)
449{
450 int bool;
451 PyThreadState *tstate = PyThreadState_Get();
452
453 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
454 return NULL;
455 if (bool)
456 tstate->interp->tscdump = 1;
457 else
458 tstate->interp->tscdump = 0;
459 Py_INCREF(Py_None);
460 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000461
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000462}
463
Tim Peters216b78b2006-01-06 02:40:53 +0000464PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000465"settscdump(bool)\n\
466\n\
467If true, tell the Python interpreter to dump VM measurements to\n\
468stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000469processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000470);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000471#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000472
Tim Peterse5e065b2003-07-06 18:36:54 +0000473static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000474sys_setrecursionlimit(PyObject *self, PyObject *args)
475{
476 int new_limit;
477 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
478 return NULL;
479 if (new_limit <= 0) {
Tim Peters216b78b2006-01-06 02:40:53 +0000480 PyErr_SetString(PyExc_ValueError,
481 "recursion limit must be positive");
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000482 return NULL;
483 }
484 Py_SetRecursionLimit(new_limit);
485 Py_INCREF(Py_None);
486 return Py_None;
487}
488
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000489PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000490"setrecursionlimit(n)\n\
491\n\
492Set the maximum depth of the Python interpreter stack to n. This\n\
493limit prevents infinite recursion from causing an overflow of the C\n\
494stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000495dependent."
496);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497
498static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000499sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000500{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000501 return PyInt_FromLong(Py_GetRecursionLimit());
502}
503
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000504PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000505"getrecursionlimit()\n\
506\n\
507Return the current value of the recursion limit, the maximum depth\n\
508of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000509recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000510);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000511
Mark Hammond8696ebc2002-10-08 02:44:31 +0000512#ifdef MS_WINDOWS
513PyDoc_STRVAR(getwindowsversion_doc,
514"getwindowsversion()\n\
515\n\
516Return information about the running version of Windows.\n\
517The result is a tuple of (major, minor, build, platform, text)\n\
518All elements are numbers, except text which is a string.\n\
519Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
520"
521);
522
523static PyObject *
524sys_getwindowsversion(PyObject *self)
525{
526 OSVERSIONINFO ver;
527 ver.dwOSVersionInfoSize = sizeof(ver);
528 if (!GetVersionEx(&ver))
529 return PyErr_SetFromWindowsErr(0);
530 return Py_BuildValue("HHHHs",
531 ver.dwMajorVersion,
532 ver.dwMinorVersion,
533 ver.dwBuildNumber,
534 ver.dwPlatformId,
535 ver.szCSDVersion);
536}
537
538#endif /* MS_WINDOWS */
539
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000540#ifdef HAVE_DLOPEN
541static PyObject *
542sys_setdlopenflags(PyObject *self, PyObject *args)
543{
544 int new_val;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000545 PyThreadState *tstate = PyThreadState_GET();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000546 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
547 return NULL;
548 if (!tstate)
549 return NULL;
550 tstate->interp->dlopenflags = new_val;
551 Py_INCREF(Py_None);
552 return Py_None;
553}
554
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000555PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000556"setdlopenflags(n) -> None\n\
557\n\
558Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000559things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000560a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000561To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000562sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
563);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000564
565static PyObject *
566sys_getdlopenflags(PyObject *self, PyObject *args)
567{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000568 PyThreadState *tstate = PyThreadState_GET();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000569 if (!tstate)
570 return NULL;
571 return PyInt_FromLong(tstate->interp->dlopenflags);
572}
573
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000574PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000575"getdlopenflags() -> int\n\
576\n\
577Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000578calls. The flag constants are defined in the dl module."
579);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000580#endif
581
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000582#ifdef USE_MALLOPT
583/* Link with -lmalloc (or -lmpc) on an SGI */
584#include <malloc.h>
585
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000586static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000587sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000588{
589 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000590 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000591 return NULL;
592 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000593 Py_INCREF(Py_None);
594 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000595}
596#endif /* USE_MALLOPT */
597
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000598static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000599sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000600{
Martin v. Löwis725507b2006-03-07 12:08:51 +0000601 return PyInt_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000602}
603
Tim Peters4be93d02002-07-07 19:59:50 +0000604#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000605static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000606sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000607{
Armin Rigoe1709372006-04-12 17:06:05 +0000608 return PyInt_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000609}
Armin Rigoe1709372006-04-12 17:06:05 +0000610#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000611
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000612PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000613"getrefcount(object) -> integer\n\
614\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000615Return the reference count of object. The count returned is generally\n\
616one higher than you might expect, because it includes the (temporary)\n\
617reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000618);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000619
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000620#ifdef COUNT_ALLOCS
621static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000622sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000623{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000624 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000625
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000626 return get_counts();
627}
628#endif
629
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000630PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000631"_getframe([depth]) -> frameobject\n\
632\n\
633Return a frame object from the call stack. If optional integer depth is\n\
634given, return the frame object that many calls below the top of the stack.\n\
635If that is deeper than the call stack, ValueError is raised. The default\n\
636for depth is zero, returning the frame at the top of the call stack.\n\
637\n\
638This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000639purposes only."
640);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000641
642static PyObject *
643sys_getframe(PyObject *self, PyObject *args)
644{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000645 PyFrameObject *f = PyThreadState_GET()->frame;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000646 int depth = -1;
647
648 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
649 return NULL;
650
651 while (depth > 0 && f != NULL) {
652 f = f->f_back;
653 --depth;
654 }
655 if (f == NULL) {
656 PyErr_SetString(PyExc_ValueError,
657 "call stack is not deep enough");
658 return NULL;
659 }
660 Py_INCREF(f);
661 return (PyObject*)f;
662}
663
Tim Peters32a83612006-07-10 21:08:24 +0000664PyDoc_STRVAR(current_frames_doc,
665"_current_frames() -> dictionary\n\
666\n\
667Return a dictionary mapping each current thread T's thread id to T's\n\
668current stack frame.\n\
669\n\
670This function should be used for specialized purposes only."
671);
672
673static PyObject *
674sys_current_frames(PyObject *self, PyObject *noargs)
675{
676 return _PyThread_CurrentFrames();
677}
678
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000679PyDoc_STRVAR(call_tracing_doc,
680"call_tracing(func, args) -> object\n\
681\n\
682Call func(*args), while tracing is enabled. The tracing state is\n\
683saved, and restored afterwards. This is intended to be called from\n\
684a debugger from a checkpoint, to recursively debug some other code."
685);
686
687static PyObject *
688sys_call_tracing(PyObject *self, PyObject *args)
689{
690 PyObject *func, *funcargs;
Georg Brandl96a8c392006-05-29 21:04:52 +0000691 if (!PyArg_UnpackTuple(args, "call_tracing", 2, 2, &func, &funcargs))
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000692 return NULL;
693 return _PyEval_CallTracing(func, funcargs);
694}
695
Jeremy Hylton985eba52003-02-05 23:13:00 +0000696PyDoc_STRVAR(callstats_doc,
697"callstats() -> tuple of integers\n\
698\n\
699Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
700when Python was built. Otherwise, return None.\n\
701\n\
702When enabled, this function returns detailed, implementation-specific\n\
703details about the number of function calls executed. The return value is\n\
704a 11-tuple where the entries in the tuple are counts of:\n\
7050. all function calls\n\
7061. calls to PyFunction_Type objects\n\
7072. PyFunction calls that do not create an argument tuple\n\
7083. PyFunction calls that do not create an argument tuple\n\
709 and bypass PyEval_EvalCodeEx()\n\
7104. PyMethod calls\n\
7115. PyMethod calls on bound methods\n\
7126. PyType calls\n\
7137. PyCFunction calls\n\
7148. generator calls\n\
7159. All other calls\n\
71610. Number of stack pops performed by call_function()"
717);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000718
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000719#ifdef __cplusplus
720extern "C" {
721#endif
722
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000723#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000724/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000725extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000726#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000727
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000728#ifdef DYNAMIC_EXECUTION_PROFILE
729/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000730extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000731#endif
732
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000733#ifdef __cplusplus
734}
735#endif
736
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000737static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000738 /* Might as well keep this in alphabetic order */
Tim Peters216b78b2006-01-06 02:40:53 +0000739 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
Jeremy Hylton985eba52003-02-05 23:13:00 +0000740 callstats_doc},
Tim Peters32a83612006-07-10 21:08:24 +0000741 {"_current_frames", sys_current_frames, METH_NOARGS,
742 current_frames_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000743 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000744 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
745 {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000746 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000747 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000748#ifdef Py_USING_UNICODE
Tim Peters216b78b2006-01-06 02:40:53 +0000749 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
750 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000751#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000752#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000753 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000754 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000755#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000756#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000757 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000758#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000759#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000760 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000761#endif
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000762#ifdef Py_USING_UNICODE
763 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
Tim Peters216b78b2006-01-06 02:40:53 +0000764 METH_NOARGS, getfilesystemencoding_doc},
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000765#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000766#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000767 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000768#endif
769#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000770 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000771#endif
Fred Drakea7688822001-10-24 20:47:48 +0000772 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000773 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000774 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000775 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000776#ifdef MS_WINDOWS
777 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
778 getwindowsversion_doc},
779#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000780#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000781 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000782#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000783#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000784 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000785 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000786#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000787 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000788 setcheckinterval_doc},
Tim Peterse5e065b2003-07-06 18:36:54 +0000789 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000790 getcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000791#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000792 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000793 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000794#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000795 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000796 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000797 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000798#ifdef WITH_TSC
799 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
800#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000801 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000802 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000803 {NULL, NULL} /* sentinel */
804};
805
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000806static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000807list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000808{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000809 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000810 int i;
811 if (list == NULL)
812 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000813 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000814 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000815 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000816 if (name == NULL)
817 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000818 PyList_Append(list, name);
819 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000820 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000821 if (PyList_Sort(list) != 0) {
822 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000823 list = NULL;
824 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000825 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000826 PyObject *v = PyList_AsTuple(list);
827 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000828 list = v;
829 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000830 return list;
831}
832
Guido van Rossum23fff912000-12-15 22:02:05 +0000833static PyObject *warnoptions = NULL;
834
835void
836PySys_ResetWarnOptions(void)
837{
838 if (warnoptions == NULL || !PyList_Check(warnoptions))
839 return;
840 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
841}
842
843void
844PySys_AddWarnOption(char *s)
845{
846 PyObject *str;
847
848 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
849 Py_XDECREF(warnoptions);
850 warnoptions = PyList_New(0);
851 if (warnoptions == NULL)
852 return;
853 }
854 str = PyString_FromString(s);
855 if (str != NULL) {
856 PyList_Append(warnoptions, str);
857 Py_DECREF(str);
858 }
859}
860
Guido van Rossum40552d01998-08-06 03:34:39 +0000861/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
862 Two literals concatenated works just fine. If you have a K&R compiler
863 or other abomination that however *does* understand longer strings,
864 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000865PyDoc_VAR(sys_doc) =
866PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000867"This module provides access to some objects used or maintained by the\n\
868interpreter and to functions that interact strongly with the interpreter.\n\
869\n\
870Dynamic objects:\n\
871\n\
872argv -- command line arguments; argv[0] is the script pathname if known\n\
873path -- module search path; path[0] is the script directory, else ''\n\
874modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000875\n\
876displayhook -- called to show results in an interactive session\n\
877excepthook -- called to handle any uncaught exception other than SystemExit\n\
878 To customize printing in an interactive session or to install a custom\n\
879 top-level exception handler, assign other functions to replace these.\n\
880\n\
881exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
882 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000883\n\
884stdin -- standard input file object; used by raw_input() and input()\n\
885stdout -- standard output file object; used by the print statement\n\
886stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000887 By assigning other file objects (or objects that behave like files)\n\
888 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000889\n\
890last_type -- type of last uncaught exception\n\
891last_value -- value of last uncaught exception\n\
892last_traceback -- traceback of last uncaught exception\n\
893 These three are only available in an interactive session after a\n\
894 traceback has been printed.\n\
895\n\
896exc_type -- type of exception currently being handled\n\
897exc_value -- value of exception currently being handled\n\
898exc_traceback -- traceback of exception currently being handled\n\
899 The function exc_info() should be used instead of these three,\n\
900 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000901"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000902)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000903/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000904PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000905"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000906Static objects:\n\
907\n\
908maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000909maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000910builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000911version -- the version of this interpreter as a string\n\
912version_info -- version information as a tuple\n\
913hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000914copyright -- copyright notice pertaining to this interpreter\n\
915platform -- platform identifier\n\
916executable -- pathname of this Python interpreter\n\
917prefix -- prefix used to find the Python library\n\
918exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000919"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000920)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000921#ifdef MS_WINDOWS
922/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000923PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000924"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000925winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000926"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000927)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000928#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000929PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000930"__stdin__ -- the original stdin; don't touch!\n\
931__stdout__ -- the original stdout; don't touch!\n\
932__stderr__ -- the original stderr; don't touch!\n\
933__displayhook__ -- the original displayhook; don't touch!\n\
934__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000935\n\
936Functions:\n\
937\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000938displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000939excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000940exc_info() -- return thread-safe information about the current exception\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000941exc_clear() -- clear the exception state for the current thread\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000942exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000943getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000944getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000945getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000946setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000947setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000948setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000949setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000950settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000951"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000952)
Fred Drakeccede592000-08-14 20:59:57 +0000953/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000954
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +0000955static int
956_check_and_flush (FILE *stream)
957{
958 int prev_fail = ferror (stream);
959 return fflush (stream) || prev_fail ? EOF : 0;
960}
961
Martin v. Löwis43b57802006-01-05 23:38:54 +0000962/* Subversion branch and revision management */
963static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
964static const char headurl[] = "$HeadURL$";
965static int svn_initialized;
966static char patchlevel_revision[50]; /* Just the number */
967static char branch[50];
968static char shortbranch[50];
969static const char *svn_revision;
970
Tim Peterse86e7a52006-01-06 02:42:46 +0000971static void
972svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +0000973{
974 const char *python, *br_start, *br_end, *br_end2, *svnversion;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000975 Py_ssize_t len;
976 int istag;
Martin v. Löwis43b57802006-01-05 23:38:54 +0000977
978 if (svn_initialized)
979 return;
980
981 python = strstr(headurl, "/python/");
982 if (!python)
983 Py_FatalError("subversion keywords missing");
984
985 br_start = python + 8;
986 br_end = strchr(br_start, '/');
Neal Norwitz837ce932006-10-28 21:15:30 +0000987 assert(br_end);
988
Tim Peters216b78b2006-01-06 02:40:53 +0000989 /* Works even for trunk,
Martin v. Löwis43b57802006-01-05 23:38:54 +0000990 as we are in trunk/Python/sysmodule.c */
991 br_end2 = strchr(br_end+1, '/');
992
993 istag = strncmp(br_start, "tags", 4) == 0;
994 if (strncmp(br_start, "trunk", 5) == 0) {
995 strcpy(branch, "trunk");
996 strcpy(shortbranch, "trunk");
997
Tim Peters216b78b2006-01-06 02:40:53 +0000998 }
Martin v. Löwis43b57802006-01-05 23:38:54 +0000999 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1000 len = br_end2 - br_start;
1001 strncpy(branch, br_start, len);
1002 branch[len] = '\0';
1003
1004 len = br_end2 - (br_end + 1);
1005 strncpy(shortbranch, br_end + 1, len);
1006 shortbranch[len] = '\0';
Tim Peters216b78b2006-01-06 02:40:53 +00001007 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001008 else {
1009 Py_FatalError("bad HeadURL");
1010 return;
1011 }
1012
1013
1014 svnversion = _Py_svnversion();
1015 if (strcmp(svnversion, "exported") != 0)
1016 svn_revision = svnversion;
1017 else if (istag) {
1018 len = strlen(_patchlevel_revision);
Neal Norwitz68cdf8a2007-04-16 07:37:55 +00001019 assert(len >= 13);
1020 assert(len < (sizeof(patchlevel_revision) + 13));
Martin v. Löwis43b57802006-01-05 23:38:54 +00001021 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1022 len - 13);
1023 patchlevel_revision[len - 13] = '\0';
1024 svn_revision = patchlevel_revision;
1025 }
1026 else
1027 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001028
Martin v. Löwis43b57802006-01-05 23:38:54 +00001029 svn_initialized = 1;
1030}
1031
1032/* Return svnversion output if available.
1033 Else return Revision of patchlevel.h if on branch.
1034 Else return empty string */
1035const char*
1036Py_SubversionRevision()
1037{
1038 svnversion_init();
1039 return svn_revision;
1040}
1041
1042const char*
1043Py_SubversionShortBranch()
1044{
1045 svnversion_init();
1046 return shortbranch;
1047}
1048
Christian Heimesf31b69f2008-01-14 03:42:48 +00001049
1050PyDoc_STRVAR(flags__doc__,
1051"sys.flags\n\
1052\n\
1053Flags provided through command line arguments or environment vars.");
1054
1055static PyTypeObject FlagsType;
1056
1057static PyStructSequence_Field flags_fields[] = {
1058 {"debug", "-d"},
1059 {"py3k_warning", "-3"},
1060 {"division_warning", "-Q"},
1061 {"division_new", "-Qnew"},
1062 {"inspect", "-i"},
1063 {"interactive", "-i"},
1064 {"optimize", "-O or -OO"},
1065 {"dont_write_bytecode", "-B"},
1066 /* {"no_user_site", "-s"}, */
1067 {"no_site", "-S"},
1068 {"ingnore_environment", "-E"},
1069 {"tabcheck", "-t or -tt"},
1070 {"verbose", "-v"},
1071#ifdef RISCOS
1072 {"ricos_wimp", "???"},
1073#endif
1074 /* {"unbuffered", "-u"}, */
1075 {"unicode", "-U"},
1076 /* {"skip_first", "-x"}, */
1077 {0}
1078};
1079
1080static PyStructSequence_Desc flags_desc = {
1081 "sys.flags", /* name */
1082 flags__doc__, /* doc */
1083 flags_fields, /* fields */
1084#ifdef RISCOS
1085 14
1086#else
1087 13
1088#endif
1089};
1090
1091static PyObject*
1092make_flags(void)
1093{
1094 int pos = 0;
1095 PyObject *seq;
1096
1097 seq = PyStructSequence_New(&FlagsType);
1098 if (seq == NULL)
1099 return NULL;
1100
1101#define SetFlag(flag) \
1102 PyStructSequence_SET_ITEM(seq, pos++, PyInt_FromLong(flag))
1103
1104 SetFlag(Py_DebugFlag);
1105 SetFlag(Py_Py3kWarningFlag);
1106 SetFlag(Py_DivisionWarningFlag);
1107 SetFlag(_Py_QnewFlag);
1108 SetFlag(Py_InspectFlag);
1109 SetFlag(Py_InteractiveFlag);
1110 SetFlag(Py_OptimizeFlag);
1111 SetFlag(Py_DontWriteBytecodeFlag);
1112 /* SetFlag(Py_NoUserSiteDirectory); */
1113 SetFlag(Py_NoSiteFlag);
1114 SetFlag(Py_IgnoreEnvironmentFlag);
1115 SetFlag(Py_TabcheckFlag);
1116 SetFlag(Py_VerboseFlag);
1117#ifdef RISCOS
1118 SetFlag(Py_RISCOSWimpFlag);
1119#endif
1120 /* SetFlag(saw_unbuffered_flag); */
1121 SetFlag(Py_UnicodeFlag);
1122 /* SetFlag(skipfirstline); */
1123#undef SetFlag
1124
1125 if (PyErr_Occurred()) {
1126 return NULL;
1127 }
1128
1129 Py_INCREF(seq);
1130 return seq;
1131}
1132
Guido van Rossum25ce5661997-08-02 03:10:38 +00001133PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001134_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001136 PyObject *m, *v, *sysdict;
1137 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +00001138 char *s;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001139#ifdef MS_WINDOWS
Tim Peters02f1d0d2006-06-06 00:25:07 +00001140 char buf[128];
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001141#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +00001142
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001143 m = Py_InitModule3("sys", sys_methods, sys_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001144 if (m == NULL)
1145 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001146 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001147
Neal Norwitz11bd1192005-10-03 00:54:56 +00001148 {
1149 /* XXX: does this work on Win/Win64? (see posix_fstat) */
1150 struct stat sb;
1151 if (fstat(fileno(stdin), &sb) == 0 &&
1152 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +00001153 /* There's nothing more we can do. */
1154 /* Py_FatalError() will core dump, so just exit. */
1155 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1156 exit(EXIT_FAILURE);
Neal Norwitz11bd1192005-10-03 00:54:56 +00001157 }
1158 }
1159
Martin v. Löwis13a1fde2005-01-27 18:56:16 +00001160 /* Closing the standard FILE* if sys.std* goes aways causes problems
1161 * for embedded Python usages. Closing them when somebody explicitly
1162 * invokes .close() might be possible, but the FAQ promises they get
1163 * never closed. However, we still need to get write errors when
1164 * writing fails (e.g. because stdout is redirected), so we flush the
1165 * streams and check for errors before the file objects are deleted.
1166 * On OS X, fflush()ing stdin causes an error, so we exempt stdin
1167 * from that procedure.
1168 */
1169 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +00001170 sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
1171 syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001172 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001173 return NULL;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001174#ifdef MS_WINDOWS
Thomas Woutersafea5292007-01-23 13:42:00 +00001175 if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001176 sprintf(buf, "cp%d", GetConsoleCP());
1177 if (!PyFile_SetEncoding(sysin, buf))
1178 return NULL;
1179 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001180 if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001181 sprintf(buf, "cp%d", GetConsoleOutputCP());
1182 if (!PyFile_SetEncoding(sysout, buf))
1183 return NULL;
1184 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001185 if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) {
Martin v. Löwisea62d252006-04-03 10:56:49 +00001186 sprintf(buf, "cp%d", GetConsoleOutputCP());
1187 if (!PyFile_SetEncoding(syserr, buf))
1188 return NULL;
1189 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001190#endif
1191
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001192 PyDict_SetItemString(sysdict, "stdin", sysin);
1193 PyDict_SetItemString(sysdict, "stdout", sysout);
1194 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +00001195 /* Make backup copies for cleanup */
1196 PyDict_SetItemString(sysdict, "__stdin__", sysin);
1197 PyDict_SetItemString(sysdict, "__stdout__", sysout);
1198 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001199 PyDict_SetItemString(sysdict, "__displayhook__",
1200 PyDict_GetItemString(sysdict, "displayhook"));
1201 PyDict_SetItemString(sysdict, "__excepthook__",
1202 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +00001203 Py_XDECREF(sysin);
1204 Py_XDECREF(sysout);
1205 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001206 PyDict_SetItemString(sysdict, "version",
1207 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +00001208 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +00001209 PyDict_SetItemString(sysdict, "hexversion",
1210 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001211 Py_XDECREF(v);
Martin v. Löwis43b57802006-01-05 23:38:54 +00001212 svnversion_init();
1213 v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision);
1214 PyDict_SetItemString(sysdict, "subversion", v);
Barry Warsaw2a38a862005-12-18 01:27:35 +00001215 Py_XDECREF(v);
Christian Heimesf31b69f2008-01-14 03:42:48 +00001216 PyDict_SetItemString(sysdict, "dont_write_bytecode",
1217 v = PyBool_FromLong(Py_DontWriteBytecodeFlag));
1218 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +00001219 /*
1220 * These release level checks are mutually exclusive and cover
1221 * the field, so don't get too fancy with the pre-processor!
1222 */
1223#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001224 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +00001225#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001226 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +00001227#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001228 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +00001229#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +00001230 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +00001231#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001232
1233#define SET_SYS_FROM_STRING(key, value) \
1234 v = value; \
1235 if (v != NULL) \
1236 PyDict_SetItemString(sysdict, key, v); \
1237 Py_XDECREF(v)
1238
1239 SET_SYS_FROM_STRING("version_info",
1240 Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +00001241 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +00001242 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +00001243 PY_RELEASE_SERIAL));
Neal Norwitze1fdb322006-07-21 05:32:28 +00001244 SET_SYS_FROM_STRING("api_version",
1245 PyInt_FromLong(PYTHON_API_VERSION));
1246 SET_SYS_FROM_STRING("copyright",
1247 PyString_FromString(Py_GetCopyright()));
1248 SET_SYS_FROM_STRING("platform",
1249 PyString_FromString(Py_GetPlatform()));
1250 SET_SYS_FROM_STRING("executable",
1251 PyString_FromString(Py_GetProgramFullPath()));
1252 SET_SYS_FROM_STRING("prefix",
1253 PyString_FromString(Py_GetPrefix()));
1254 SET_SYS_FROM_STRING("exec_prefix",
1255 PyString_FromString(Py_GetExecPrefix()));
1256 SET_SYS_FROM_STRING("maxint",
1257 PyInt_FromLong(PyInt_GetMax()));
Christian Heimes28104c52007-11-27 23:16:44 +00001258 SET_SYS_FROM_STRING("py3kwarning",
1259 PyBool_FromLong(Py_Py3kWarningFlag));
Christian Heimesdfdfaab2007-12-01 11:20:10 +00001260 SET_SYS_FROM_STRING("float_info",
1261 PyFloat_GetInfo());
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001262#ifdef Py_USING_UNICODE
Neal Norwitze1fdb322006-07-21 05:32:28 +00001263 SET_SYS_FROM_STRING("maxunicode",
1264 PyInt_FromLong(PyUnicode_GetMax()));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001265#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001266 SET_SYS_FROM_STRING("builtin_module_names",
1267 list_builtin_module_names());
Fred Drake099325e2000-08-14 15:47:03 +00001268 {
1269 /* Assumes that longs are at least 2 bytes long.
1270 Should be safe! */
1271 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +00001272 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001273
1274 s = (char *) &number;
1275 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +00001276 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +00001277 else
Fred Drakea2b6ad62000-08-15 04:24:43 +00001278 value = "little";
Neal Norwitze1fdb322006-07-21 05:32:28 +00001279 SET_SYS_FROM_STRING("byteorder",
1280 PyString_FromString(value));
Fred Drake099325e2000-08-14 15:47:03 +00001281 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001282#ifdef MS_COREDLL
Neal Norwitze1fdb322006-07-21 05:32:28 +00001283 SET_SYS_FROM_STRING("dllhandle",
1284 PyLong_FromVoidPtr(PyWin_DLLhModule));
1285 SET_SYS_FROM_STRING("winver",
1286 PyString_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001287#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001288#undef SET_SYS_FROM_STRING
Guido van Rossum23fff912000-12-15 22:02:05 +00001289 if (warnoptions == NULL) {
1290 warnoptions = PyList_New(0);
1291 }
1292 else {
1293 Py_INCREF(warnoptions);
1294 }
1295 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +00001296 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +00001297 }
Tim Peters216b78b2006-01-06 02:40:53 +00001298
Christian Heimesf31b69f2008-01-14 03:42:48 +00001299 PyStructSequence_InitType(&FlagsType, &flags_desc);
1300 PyDict_SetItemString(sysdict, "flags", make_flags());
1301 /* prevent user from creating new instances */
1302 FlagsType.tp_init = NULL;
1303 FlagsType.tp_new = NULL;
1304
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001305 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001306 return NULL;
1307 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001308}
1309
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001310static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001311makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001312{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001313 int i, n;
1314 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001315 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001316
Guido van Rossum3f5da241990-12-20 15:06:42 +00001317 n = 1;
1318 p = path;
1319 while ((p = strchr(p, delim)) != NULL) {
1320 n++;
1321 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001322 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001323 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001324 if (v == NULL)
1325 return NULL;
1326 for (i = 0; ; i++) {
1327 p = strchr(path, delim);
1328 if (p == NULL)
1329 p = strchr(path, '\0'); /* End of string */
Armin Rigo7ccbca92006-10-04 12:17:45 +00001330 w = PyString_FromStringAndSize(path, (Py_ssize_t) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001331 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001332 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001333 return NULL;
1334 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001335 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001336 if (*p == '\0')
1337 break;
1338 path = p+1;
1339 }
1340 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001341}
1342
1343void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001344PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001346 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001348 Py_FatalError("can't create sys.path");
1349 if (PySys_SetObject("path", v) != 0)
1350 Py_FatalError("can't assign sys.path");
1351 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352}
1353
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001354static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001355makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001356{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001357 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +00001358 if (argc <= 0 || argv == NULL) {
1359 /* Ensure at least one (empty) argument is seen */
1360 static char *empty_argv[1] = {""};
1361 argv = empty_argv;
1362 argc = 1;
1363 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001364 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001365 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001366 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001367 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001368#ifdef __VMS
1369 PyObject *v;
1370
1371 /* argv[0] is the script pathname if known */
1372 if (i == 0) {
1373 char* fn = decc$translate_vms(argv[0]);
1374 if ((fn == (char *)0) || fn == (char *)-1)
1375 v = PyString_FromString(argv[0]);
1376 else
1377 v = PyString_FromString(
1378 decc$translate_vms(argv[0]));
1379 } else
1380 v = PyString_FromString(argv[i]);
1381#else
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001382 PyObject *v = PyString_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001383#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001384 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001385 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001386 av = NULL;
1387 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001388 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001389 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001390 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001391 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001392 return av;
1393}
1394
1395void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001396PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001397{
Guido van Rossum162e38c2003-02-19 15:25:10 +00001398#if defined(HAVE_REALPATH)
1399 char fullpath[MAXPATHLEN];
1400#elif defined(MS_WINDOWS)
Thomas Heller27bb71e2003-01-08 14:33:48 +00001401 char fullpath[MAX_PATH];
1402#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001403 PyObject *av = makeargvobject(argc, argv);
1404 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001405 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001406 Py_FatalError("no mem for sys.argv");
1407 if (PySys_SetObject("argv", av) != 0)
1408 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001409 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001410 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001411 char *p = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001412 Py_ssize_t n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001413 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001414#ifdef HAVE_READLINK
1415 char link[MAXPATHLEN+1];
1416 char argv0copy[2*MAXPATHLEN+1];
1417 int nr = 0;
Georg Brandl69537722005-09-15 13:00:34 +00001418 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0)
Guido van Rossumc474dea1997-04-25 15:38:31 +00001419 nr = readlink(argv0, link, MAXPATHLEN);
1420 if (nr > 0) {
1421 /* It's a symlink */
1422 link[nr] = '\0';
1423 if (link[0] == SEP)
1424 argv0 = link; /* Link to absolute path */
1425 else if (strchr(link, SEP) == NULL)
1426 ; /* Link without path */
1427 else {
1428 /* Must join(dirname(argv0), link) */
1429 char *q = strrchr(argv0, SEP);
1430 if (q == NULL)
1431 argv0 = link; /* argv0 without path */
1432 else {
1433 /* Must make a copy */
1434 strcpy(argv0copy, argv0);
1435 q = strrchr(argv0copy, SEP);
1436 strcpy(q+1, link);
1437 argv0 = argv0copy;
1438 }
1439 }
1440 }
1441#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001442#if SEP == '\\' /* Special case for MS filename syntax */
Georg Brandl69537722005-09-15 13:00:34 +00001443 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001444 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001445#ifdef MS_WINDOWS
1446 char *ptemp;
1447 if (GetFullPathName(argv0,
1448 sizeof(fullpath),
1449 fullpath,
1450 &ptemp)) {
1451 argv0 = fullpath;
1452 }
1453#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001454 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001455 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001456 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001457 if (q != NULL)
1458 p = q;
1459 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001460 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001461 if (n > 1 && p[-1] != ':')
1462 n--; /* Drop trailing separator */
1463 }
1464 }
1465#else /* All other filename syntaxes */
Georg Brandl69537722005-09-15 13:00:34 +00001466 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossum162e38c2003-02-19 15:25:10 +00001467#if defined(HAVE_REALPATH)
1468 if (realpath(argv0, fullpath)) {
1469 argv0 = fullpath;
1470 }
1471#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001472 p = strrchr(argv0, SEP);
Guido van Rossum162e38c2003-02-19 15:25:10 +00001473 }
Guido van Rossumcc883411996-09-10 14:44:21 +00001474 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001475#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001476 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001477#else /* don't include trailing separator */
1478 n = p - argv0;
1479#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001480#if SEP == '/' /* Special case for Unix filename syntax */
1481 if (n > 1)
1482 n--; /* Drop trailing separator */
1483#endif /* Unix */
1484 }
1485#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001486 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001487 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001488 Py_FatalError("no mem for sys.path insertion");
1489 if (PyList_Insert(path, 0, a) < 0)
1490 Py_FatalError("sys.path.insert(0) failed");
1491 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001492 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001493 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494}
Guido van Rossuma890e681998-05-12 14:59:24 +00001495
1496
1497/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1498 Adapted from code submitted by Just van Rossum.
1499
1500 PySys_WriteStdout(format, ...)
1501 PySys_WriteStderr(format, ...)
1502
1503 The first function writes to sys.stdout; the second to sys.stderr. When
1504 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001505 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001506
1507 Both take a printf-style format string as their first argument followed
1508 by a variable length argument list determined by the format string.
1509
1510 *** WARNING ***
1511
1512 The format should limit the total size of the formatted output string to
1513 1000 bytes. In particular, this means that no unrestricted "%s" formats
1514 should occur; these should be limited using "%.<N>s where <N> is a
1515 decimal number calculated so that <N> plus the maximum size of other
1516 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1517 which can print hundreds of digits for very large numbers.
1518
1519 */
1520
1521static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001522mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001523{
1524 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001525 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001526
Guido van Rossum8442af31998-10-12 18:22:10 +00001527 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001528 file = PySys_GetObject(name);
1529 if (file == NULL || PyFile_AsFile(file) == fp)
1530 vfprintf(fp, format, va);
1531 else {
1532 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001533 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1534 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001535 if (PyFile_WriteString(buffer, file) != 0) {
1536 PyErr_Clear();
1537 fputs(buffer, fp);
1538 }
Skip Montanaro53a6d1d2006-04-18 00:55:46 +00001539 if (written < 0 || (size_t)written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001540 const char *truncated = "... truncated";
1541 if (PyFile_WriteString(truncated, file) != 0) {
1542 PyErr_Clear();
1543 fputs(truncated, fp);
1544 }
1545 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001546 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001547 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001548}
1549
1550void
Guido van Rossuma890e681998-05-12 14:59:24 +00001551PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001552{
1553 va_list va;
1554
Guido van Rossuma890e681998-05-12 14:59:24 +00001555 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001556 mywrite("stdout", stdout, format, va);
1557 va_end(va);
1558}
1559
1560void
Guido van Rossuma890e681998-05-12 14:59:24 +00001561PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001562{
1563 va_list va;
1564
Guido van Rossuma890e681998-05-12 14:59:24 +00001565 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001566 mywrite("stderr", stderr, format, va);
1567 va_end(va);
1568}