blob: 765621ecdb515ab1412eb2c942a26dbd74876460 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* System module */
3
4/*
5Various bits of information used by the interpreter are collected in
6module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +00007Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +00008- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009Data members:
10- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000012- path: module search path (list of strings)
13- argv: script arguments (list of strings)
14- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015*/
16
Guido van Rossum65bf9f21997-04-29 18:33:38 +000017#include "Python.h"
Barry Warsawb6a54d22000-12-06 21:47:46 +000018#include "compile.h"
19#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossume2437a11992-03-23 18:20:18 +000021#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
Mark Hammond8696ebc2002-10-08 02:44:31 +000023#ifdef MS_WINDOWS
24#define WIN32_LEAN_AND_MEAN
25#include "windows.h"
26#endif /* MS_WINDOWS */
27
Guido van Rossum9b38a141996-09-11 23:12:24 +000028#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000029extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000030/* A string loaded from the DLL at startup: */
31extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000032#endif
33
Guido van Rossum65bf9f21997-04-29 18:33:38 +000034PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000035PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036{
Guido van Rossum25ce5661997-08-02 03:10:38 +000037 PyThreadState *tstate = PyThreadState_Get();
38 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000039 if (sd == NULL)
40 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000041 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042}
43
44FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000045PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046{
47 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000048 PyObject *v = PySys_GetObject(name);
49 if (v != NULL && PyFile_Check(v))
50 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 if (fp == NULL)
52 fp = def;
53 return fp;
54}
55
56int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000057PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058{
Guido van Rossum25ce5661997-08-02 03:10:38 +000059 PyThreadState *tstate = PyThreadState_Get();
60 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000061 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000062 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000063 return 0;
64 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000065 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000066 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000068 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069}
70
Guido van Rossum65bf9f21997-04-29 18:33:38 +000071static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000072sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000073{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000074 PyObject *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000075 PyInterpreterState *interp = PyThreadState_Get()->interp;
76 PyObject *modules = interp->modules;
77 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
78
Moshe Zadka03897ea2001-07-23 13:32:43 +000079 if (builtins == NULL) {
80 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
81 return NULL;
82 }
83
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000084 /* Print value except if None */
85 /* After printing, also assign to '_' */
86 /* Before, set '_' to None to avoid recursion */
87 if (o == Py_None) {
88 Py_INCREF(Py_None);
89 return Py_None;
90 }
91 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
92 return NULL;
93 if (Py_FlushLine() != 0)
94 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000095 outf = PySys_GetObject("stdout");
96 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000097 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
98 return NULL;
99 }
Greg Steinceb9b7c2001-01-11 09:27:34 +0000100 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000101 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000102 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000103 if (Py_FlushLine() != 0)
104 return NULL;
105 if (PyObject_SetAttrString(builtins, "_", o) != 0)
106 return NULL;
107 Py_INCREF(Py_None);
108 return Py_None;
109}
110
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000111PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000112"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000113"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000114"Print an object to sys.stdout and also save it in __builtin__._\n"
115);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000116
117static PyObject *
118sys_excepthook(PyObject* self, PyObject* args)
119{
120 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000121 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000122 return NULL;
123 PyErr_Display(exc, value, tb);
124 Py_INCREF(Py_None);
125 return Py_None;
126}
127
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000128PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000129"excepthook(exctype, value, traceback) -> None\n"
130"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000131"Handle an exception by displaying it with a traceback on sys.stderr.\n"
132);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000133
134static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000135sys_exc_info(PyObject *self)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000136{
137 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000139 return Py_BuildValue(
140 "(OOO)",
141 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
142 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
143 tstate->exc_traceback != NULL ?
144 tstate->exc_traceback : Py_None);
145}
146
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000147PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000148"exc_info() -> (type, value, traceback)\n\
149\n\
150Return information about the exception that is currently being handled.\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000151This should be called from inside an except clause only."
152);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000153
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000157 PyObject *exit_code = 0;
158 if (!PyArg_ParseTuple(args, "|O:exit", &exit_code))
159 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000160 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000161 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000162 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000165PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000166"exit([status])\n\
167\n\
168Exit the interpreter by raising SystemExit(status).\n\
169If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000170If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000171If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000172exit status will be one (i.e., failure)."
173);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000174
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000175#ifdef Py_USING_UNICODE
176
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000177static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000178sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000179{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000180 return PyString_FromString(PyUnicode_GetDefaultEncoding());
181}
182
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000183PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000184"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000185\n\
186Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000187implementation."
188);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000189
190static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000191sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000192{
193 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000194 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000195 return NULL;
196 if (PyUnicode_SetDefaultEncoding(encoding))
197 return NULL;
198 Py_INCREF(Py_None);
199 return Py_None;
200}
201
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000202PyDoc_STRVAR(setdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000203"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000204\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000205Set the current default string encoding used by the Unicode implementation."
206);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000207
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000208#endif
209
Fred Drake5755ce62001-06-27 19:19:46 +0000210/*
211 * Cached interned string objects used for calling the profile and
212 * trace functions. Initialized by trace_init().
213 */
214static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
215
216static int
217trace_init(void)
218{
219 static char *whatnames[4] = {"call", "exception", "line", "return"};
220 PyObject *name;
221 int i;
222 for (i = 0; i < 4; ++i) {
223 if (whatstrings[i] == NULL) {
224 name = PyString_InternFromString(whatnames[i]);
225 if (name == NULL)
226 return -1;
227 whatstrings[i] = name;
228 }
229 }
230 return 0;
231}
232
233
234static PyObject *
235call_trampoline(PyThreadState *tstate, PyObject* callback,
236 PyFrameObject *frame, int what, PyObject *arg)
237{
238 PyObject *args = PyTuple_New(3);
239 PyObject *whatstr;
240 PyObject *result;
241
242 if (args == NULL)
243 return NULL;
244 Py_INCREF(frame);
245 whatstr = whatstrings[what];
246 Py_INCREF(whatstr);
247 if (arg == NULL)
248 arg = Py_None;
249 Py_INCREF(arg);
250 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
251 PyTuple_SET_ITEM(args, 1, whatstr);
252 PyTuple_SET_ITEM(args, 2, arg);
253
254 /* call the Python-level function */
255 PyFrame_FastToLocals(frame);
256 result = PyEval_CallObject(callback, args);
257 PyFrame_LocalsToFast(frame, 1);
258 if (result == NULL)
259 PyTraceBack_Here(frame);
260
261 /* cleanup */
262 Py_DECREF(args);
263 return result;
264}
265
266static int
267profile_trampoline(PyObject *self, PyFrameObject *frame,
268 int what, PyObject *arg)
269{
270 PyThreadState *tstate = frame->f_tstate;
271 PyObject *result;
272
Fred Drake8f51f542001-10-04 14:48:42 +0000273 if (arg == NULL)
274 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000275 result = call_trampoline(tstate, self, frame, what, arg);
276 if (result == NULL) {
277 PyEval_SetProfile(NULL, NULL);
278 return -1;
279 }
280 Py_DECREF(result);
281 return 0;
282}
283
284static int
285trace_trampoline(PyObject *self, PyFrameObject *frame,
286 int what, PyObject *arg)
287{
288 PyThreadState *tstate = frame->f_tstate;
289 PyObject *callback;
290 PyObject *result;
291
292 if (what == PyTrace_CALL)
293 callback = self;
294 else
295 callback = frame->f_trace;
296 if (callback == NULL)
297 return 0;
298 result = call_trampoline(tstate, callback, frame, what, arg);
299 if (result == NULL) {
300 PyEval_SetTrace(NULL, NULL);
301 Py_XDECREF(frame->f_trace);
302 frame->f_trace = NULL;
303 return -1;
304 }
305 if (result != Py_None) {
306 PyObject *temp = frame->f_trace;
307 frame->f_trace = NULL;
308 Py_XDECREF(temp);
309 frame->f_trace = result;
310 }
311 else {
312 Py_DECREF(result);
313 }
314 return 0;
315}
Fred Draked0838392001-06-16 21:02:31 +0000316
Fred Drake8b4d01d2000-05-09 19:57:01 +0000317static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000318sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000319{
Fred Drake5755ce62001-06-27 19:19:46 +0000320 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000321 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000322 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000323 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000324 else
Fred Drake5755ce62001-06-27 19:19:46 +0000325 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000326 Py_INCREF(Py_None);
327 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000328}
329
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000330PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000331"settrace(function)\n\
332\n\
333Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000334function call. See the debugger chapter in the library manual."
335);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000336
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000337static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000339{
Fred Drake5755ce62001-06-27 19:19:46 +0000340 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000341 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000342 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000343 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000344 else
Fred Drake5755ce62001-06-27 19:19:46 +0000345 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000346 Py_INCREF(Py_None);
347 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000348}
349
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000350PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000351"setprofile(function)\n\
352\n\
353Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000354and return. See the profiler chapter in the library manual."
355);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000356
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000357static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000359{
Skip Montanarod581d772002-09-03 20:10:45 +0000360 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000361 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000362 Py_INCREF(Py_None);
363 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000364}
365
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000366PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000367"setcheckinterval(n)\n\
368\n\
369Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000370n instructions. This also affects how often thread switches occur."
371);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000372
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000373static PyObject *
374sys_setrecursionlimit(PyObject *self, PyObject *args)
375{
376 int new_limit;
377 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
378 return NULL;
379 if (new_limit <= 0) {
380 PyErr_SetString(PyExc_ValueError,
381 "recursion limit must be positive");
382 return NULL;
383 }
384 Py_SetRecursionLimit(new_limit);
385 Py_INCREF(Py_None);
386 return Py_None;
387}
388
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000389PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000390"setrecursionlimit(n)\n\
391\n\
392Set the maximum depth of the Python interpreter stack to n. This\n\
393limit prevents infinite recursion from causing an overflow of the C\n\
394stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000395dependent."
396);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000397
398static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000399sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000400{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000401 return PyInt_FromLong(Py_GetRecursionLimit());
402}
403
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000404PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000405"getrecursionlimit()\n\
406\n\
407Return the current value of the recursion limit, the maximum depth\n\
408of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000409recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000410);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000411
Mark Hammond8696ebc2002-10-08 02:44:31 +0000412#ifdef MS_WINDOWS
413PyDoc_STRVAR(getwindowsversion_doc,
414"getwindowsversion()\n\
415\n\
416Return information about the running version of Windows.\n\
417The result is a tuple of (major, minor, build, platform, text)\n\
418All elements are numbers, except text which is a string.\n\
419Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
420"
421);
422
423static PyObject *
424sys_getwindowsversion(PyObject *self)
425{
426 OSVERSIONINFO ver;
427 ver.dwOSVersionInfoSize = sizeof(ver);
428 if (!GetVersionEx(&ver))
429 return PyErr_SetFromWindowsErr(0);
430 return Py_BuildValue("HHHHs",
431 ver.dwMajorVersion,
432 ver.dwMinorVersion,
433 ver.dwBuildNumber,
434 ver.dwPlatformId,
435 ver.szCSDVersion);
436}
437
438#endif /* MS_WINDOWS */
439
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000440#ifdef HAVE_DLOPEN
441static PyObject *
442sys_setdlopenflags(PyObject *self, PyObject *args)
443{
444 int new_val;
445 PyThreadState *tstate = PyThreadState_Get();
446 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
447 return NULL;
448 if (!tstate)
449 return NULL;
450 tstate->interp->dlopenflags = new_val;
451 Py_INCREF(Py_None);
452 return Py_None;
453}
454
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000455PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000456"setdlopenflags(n) -> None\n\
457\n\
458Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000459things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000460a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000461To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000462sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
463);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000464
465static PyObject *
466sys_getdlopenflags(PyObject *self, PyObject *args)
467{
468 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000469 if (!tstate)
470 return NULL;
471 return PyInt_FromLong(tstate->interp->dlopenflags);
472}
473
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000474PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000475"getdlopenflags() -> int\n\
476\n\
477Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000478calls. The flag constants are defined in the dl module."
479);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000480#endif
481
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000482#ifdef USE_MALLOPT
483/* Link with -lmalloc (or -lmpc) on an SGI */
484#include <malloc.h>
485
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000486static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000487sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000488{
489 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000490 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000491 return NULL;
492 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000493 Py_INCREF(Py_None);
494 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000495}
496#endif /* USE_MALLOPT */
497
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000498static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000499sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000500{
Mark Hammond440d8982000-06-20 08:12:48 +0000501 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000502}
503
Tim Peters4be93d02002-07-07 19:59:50 +0000504#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000505static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000506sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000507{
Mark Hammond440d8982000-06-20 08:12:48 +0000508 return PyInt_FromLong(_Py_RefTotal);
509}
510
511#endif /* Py_TRACE_REFS */
512
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000513PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000514"getrefcount(object) -> integer\n\
515\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000516Return the reference count of object. The count returned is generally\n\
517one higher than you might expect, because it includes the (temporary)\n\
518reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000519);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000520
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000521#ifdef COUNT_ALLOCS
522static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000523sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000524{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000525 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000526
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000527 return get_counts();
528}
529#endif
530
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000531PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000532"_getframe([depth]) -> frameobject\n\
533\n\
534Return a frame object from the call stack. If optional integer depth is\n\
535given, return the frame object that many calls below the top of the stack.\n\
536If that is deeper than the call stack, ValueError is raised. The default\n\
537for depth is zero, returning the frame at the top of the call stack.\n\
538\n\
539This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000540purposes only."
541);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000542
543static PyObject *
544sys_getframe(PyObject *self, PyObject *args)
545{
546 PyFrameObject *f = PyThreadState_Get()->frame;
547 int depth = -1;
548
549 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
550 return NULL;
551
552 while (depth > 0 && f != NULL) {
553 f = f->f_back;
554 --depth;
555 }
556 if (f == NULL) {
557 PyErr_SetString(PyExc_ValueError,
558 "call stack is not deep enough");
559 return NULL;
560 }
561 Py_INCREF(f);
562 return (PyObject*)f;
563}
564
Jeremy Hylton985eba52003-02-05 23:13:00 +0000565PyDoc_STRVAR(callstats_doc,
566"callstats() -> tuple of integers\n\
567\n\
568Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
569when Python was built. Otherwise, return None.\n\
570\n\
571When enabled, this function returns detailed, implementation-specific\n\
572details about the number of function calls executed. The return value is\n\
573a 11-tuple where the entries in the tuple are counts of:\n\
5740. all function calls\n\
5751. calls to PyFunction_Type objects\n\
5762. PyFunction calls that do not create an argument tuple\n\
5773. PyFunction calls that do not create an argument tuple\n\
578 and bypass PyEval_EvalCodeEx()\n\
5794. PyMethod calls\n\
5805. PyMethod calls on bound methods\n\
5816. PyType calls\n\
5827. PyCFunction calls\n\
5838. generator calls\n\
5849. All other calls\n\
58510. Number of stack pops performed by call_function()"
586);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000587
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000588#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000589/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000590extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000591#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000592
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000593#ifdef DYNAMIC_EXECUTION_PROFILE
594/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000595extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000596#endif
597
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000598static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000599 /* Might as well keep this in alphabetic order */
Jeremy Hylton985eba52003-02-05 23:13:00 +0000600 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
601 callstats_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000602 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
603 {"exc_info", (PyCFunction)sys_exc_info, METH_NOARGS, exc_info_doc},
604 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000605 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000606#ifdef Py_USING_UNICODE
Jeremy Hylton985eba52003-02-05 23:13:00 +0000607 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
608 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000609#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000610#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000611 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
612 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000613#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000614#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000615 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000616#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000617#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000618 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000619#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000620#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000621 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000622#endif
623#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000624 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000625#endif
Fred Drakea7688822001-10-24 20:47:48 +0000626 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000627 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000628 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000629 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000630#ifdef MS_WINDOWS
631 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
632 getwindowsversion_doc},
633#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000634#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000635 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000636#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000637#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000638 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000639 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000640#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000641 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000642 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000643#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000644 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
645 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000646#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000647 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000648 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000649 setrecursionlimit_doc},
Neal Norwitz290d31e2002-03-03 15:12:58 +0000650 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000651 {NULL, NULL} /* sentinel */
652};
653
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000654static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000655list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000656{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000657 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000658 int i;
659 if (list == NULL)
660 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000661 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000662 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000663 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000664 if (name == NULL)
665 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000666 PyList_Append(list, name);
667 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000668 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000669 if (PyList_Sort(list) != 0) {
670 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000671 list = NULL;
672 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000673 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000674 PyObject *v = PyList_AsTuple(list);
675 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000676 list = v;
677 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000678 return list;
679}
680
Guido van Rossum23fff912000-12-15 22:02:05 +0000681static PyObject *warnoptions = NULL;
682
683void
684PySys_ResetWarnOptions(void)
685{
686 if (warnoptions == NULL || !PyList_Check(warnoptions))
687 return;
688 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
689}
690
691void
692PySys_AddWarnOption(char *s)
693{
694 PyObject *str;
695
696 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
697 Py_XDECREF(warnoptions);
698 warnoptions = PyList_New(0);
699 if (warnoptions == NULL)
700 return;
701 }
702 str = PyString_FromString(s);
703 if (str != NULL) {
704 PyList_Append(warnoptions, str);
705 Py_DECREF(str);
706 }
707}
708
Guido van Rossum40552d01998-08-06 03:34:39 +0000709/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
710 Two literals concatenated works just fine. If you have a K&R compiler
711 or other abomination that however *does* understand longer strings,
712 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000713PyDoc_VAR(sys_doc) =
714PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000715"This module provides access to some objects used or maintained by the\n\
716interpreter and to functions that interact strongly with the interpreter.\n\
717\n\
718Dynamic objects:\n\
719\n\
720argv -- command line arguments; argv[0] is the script pathname if known\n\
721path -- module search path; path[0] is the script directory, else ''\n\
722modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000723\n\
724displayhook -- called to show results in an interactive session\n\
725excepthook -- called to handle any uncaught exception other than SystemExit\n\
726 To customize printing in an interactive session or to install a custom\n\
727 top-level exception handler, assign other functions to replace these.\n\
728\n\
729exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
730 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000731\n\
732stdin -- standard input file object; used by raw_input() and input()\n\
733stdout -- standard output file object; used by the print statement\n\
734stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000735 By assigning other file objects (or objects that behave like files)\n\
736 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000737\n\
738last_type -- type of last uncaught exception\n\
739last_value -- value of last uncaught exception\n\
740last_traceback -- traceback of last uncaught exception\n\
741 These three are only available in an interactive session after a\n\
742 traceback has been printed.\n\
743\n\
744exc_type -- type of exception currently being handled\n\
745exc_value -- value of exception currently being handled\n\
746exc_traceback -- traceback of exception currently being handled\n\
747 The function exc_info() should be used instead of these three,\n\
748 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000749"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000750)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000751/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000752PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000753"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000754Static objects:\n\
755\n\
756maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000757maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000758builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000759version -- the version of this interpreter as a string\n\
760version_info -- version information as a tuple\n\
761hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000762copyright -- copyright notice pertaining to this interpreter\n\
763platform -- platform identifier\n\
764executable -- pathname of this Python interpreter\n\
765prefix -- prefix used to find the Python library\n\
766exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000767"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000768)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000769#ifdef MS_WINDOWS
770/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000771PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000772"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000773winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000774"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000775)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000776#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000777PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000778"__stdin__ -- the original stdin; don't touch!\n\
779__stdout__ -- the original stdout; don't touch!\n\
780__stderr__ -- the original stderr; don't touch!\n\
781__displayhook__ -- the original displayhook; don't touch!\n\
782__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000783\n\
784Functions:\n\
785\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000786displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000787excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000788exc_info() -- return thread-safe information about the current exception\n\
789exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000790getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000791getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000792getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000793setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000794setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000795setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000796setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000797settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000798"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000799)
Fred Drakeccede592000-08-14 20:59:57 +0000800/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000801
Guido van Rossum25ce5661997-08-02 03:10:38 +0000802PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000803_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000805 PyObject *m, *v, *sysdict;
806 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000807 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000808
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000809 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000810 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000811
812 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
813 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
814 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000815 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000816 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000817 PyDict_SetItemString(sysdict, "stdin", sysin);
818 PyDict_SetItemString(sysdict, "stdout", sysout);
819 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000820 /* Make backup copies for cleanup */
821 PyDict_SetItemString(sysdict, "__stdin__", sysin);
822 PyDict_SetItemString(sysdict, "__stdout__", sysout);
823 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000824 PyDict_SetItemString(sysdict, "__displayhook__",
825 PyDict_GetItemString(sysdict, "displayhook"));
826 PyDict_SetItemString(sysdict, "__excepthook__",
827 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000828 Py_XDECREF(sysin);
829 Py_XDECREF(sysout);
830 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000831 PyDict_SetItemString(sysdict, "version",
832 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000833 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000834 PyDict_SetItemString(sysdict, "hexversion",
835 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000836 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000837 /*
838 * These release level checks are mutually exclusive and cover
839 * the field, so don't get too fancy with the pre-processor!
840 */
841#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000842 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000843#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000844 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000845#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000846 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000847#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000848 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000849#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000850 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000851 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000852 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000853 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000854 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000855 Py_XDECREF(v);
Skip Montanaro8e790e72002-09-03 13:25:17 +0000856 PyDict_SetItemString(sysdict, "api_version",
857 v = PyInt_FromLong(PYTHON_API_VERSION));
858 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000859 PyDict_SetItemString(sysdict, "copyright",
860 v = PyString_FromString(Py_GetCopyright()));
861 Py_XDECREF(v);
862 PyDict_SetItemString(sysdict, "platform",
863 v = PyString_FromString(Py_GetPlatform()));
864 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000865 PyDict_SetItemString(sysdict, "executable",
866 v = PyString_FromString(Py_GetProgramFullPath()));
867 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000868 PyDict_SetItemString(sysdict, "prefix",
869 v = PyString_FromString(Py_GetPrefix()));
870 Py_XDECREF(v);
871 PyDict_SetItemString(sysdict, "exec_prefix",
872 v = PyString_FromString(Py_GetExecPrefix()));
873 Py_XDECREF(v);
874 PyDict_SetItemString(sysdict, "maxint",
875 v = PyInt_FromLong(PyInt_GetMax()));
876 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000877#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000878 PyDict_SetItemString(sysdict, "maxunicode",
879 v = PyInt_FromLong(PyUnicode_GetMax()));
880 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000881#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000882 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000884 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000885 {
886 /* Assumes that longs are at least 2 bytes long.
887 Should be safe! */
888 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000889 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000890
891 s = (char *) &number;
892 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000893 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000894 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000895 value = "little";
896 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000897 v = PyString_FromString(value));
898 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000899 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000900#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000901 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000902 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000903 Py_XDECREF(v);
904 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000905 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000906 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000907#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000908 if (warnoptions == NULL) {
909 warnoptions = PyList_New(0);
910 }
911 else {
912 Py_INCREF(warnoptions);
913 }
914 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000915 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000916 }
917
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000918 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000919 return NULL;
920 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000921}
922
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000923static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000924makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000925{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000926 int i, n;
927 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000928 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000929
930 n = 1;
931 p = path;
932 while ((p = strchr(p, delim)) != NULL) {
933 n++;
934 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000935 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000936 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000937 if (v == NULL)
938 return NULL;
939 for (i = 0; ; i++) {
940 p = strchr(path, delim);
941 if (p == NULL)
942 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000943 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000944 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000945 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000946 return NULL;
947 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000948 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000949 if (*p == '\0')
950 break;
951 path = p+1;
952 }
953 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954}
955
956void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000957PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000958{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000959 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000960 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000961 Py_FatalError("can't create sys.path");
962 if (PySys_SetObject("path", v) != 0)
963 Py_FatalError("can't assign sys.path");
964 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000965}
966
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000967static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000968makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000969{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000970 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000971 if (argc <= 0 || argv == NULL) {
972 /* Ensure at least one (empty) argument is seen */
973 static char *empty_argv[1] = {""};
974 argv = empty_argv;
975 argc = 1;
976 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000977 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000978 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000979 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000980 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000981 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000982 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000983 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000984 av = NULL;
985 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000986 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000987 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000988 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000989 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000990 return av;
991}
992
993void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000994PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000995{
Thomas Heller27bb71e2003-01-08 14:33:48 +0000996#ifdef MS_WINDOWS
997 char fullpath[MAX_PATH];
998#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000999 PyObject *av = makeargvobject(argc, argv);
1000 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001001 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001002 Py_FatalError("no mem for sys.argv");
1003 if (PySys_SetObject("argv", av) != 0)
1004 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001005 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001006 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001007 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +00001008 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001009 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001010#ifdef HAVE_READLINK
1011 char link[MAXPATHLEN+1];
1012 char argv0copy[2*MAXPATHLEN+1];
1013 int nr = 0;
1014 if (argc > 0 && argv0 != NULL)
1015 nr = readlink(argv0, link, MAXPATHLEN);
1016 if (nr > 0) {
1017 /* It's a symlink */
1018 link[nr] = '\0';
1019 if (link[0] == SEP)
1020 argv0 = link; /* Link to absolute path */
1021 else if (strchr(link, SEP) == NULL)
1022 ; /* Link without path */
1023 else {
1024 /* Must join(dirname(argv0), link) */
1025 char *q = strrchr(argv0, SEP);
1026 if (q == NULL)
1027 argv0 = link; /* argv0 without path */
1028 else {
1029 /* Must make a copy */
1030 strcpy(argv0copy, argv0);
1031 q = strrchr(argv0copy, SEP);
1032 strcpy(q+1, link);
1033 argv0 = argv0copy;
1034 }
1035 }
1036 }
1037#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001038#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001039 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001040 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001041#ifdef MS_WINDOWS
1042 char *ptemp;
1043 if (GetFullPathName(argv0,
1044 sizeof(fullpath),
1045 fullpath,
1046 &ptemp)) {
1047 argv0 = fullpath;
1048 }
1049#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001050 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001051 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001052 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001053 if (q != NULL)
1054 p = q;
1055 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001056 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001057 if (n > 1 && p[-1] != ':')
1058 n--; /* Drop trailing separator */
1059 }
1060 }
1061#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001062 if (argc > 0 && argv0 != NULL)
1063 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001064 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001065#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001066 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001067#else /* don't include trailing separator */
1068 n = p - argv0;
1069#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001070#if SEP == '/' /* Special case for Unix filename syntax */
1071 if (n > 1)
1072 n--; /* Drop trailing separator */
1073#endif /* Unix */
1074 }
1075#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001076 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001077 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001078 Py_FatalError("no mem for sys.path insertion");
1079 if (PyList_Insert(path, 0, a) < 0)
1080 Py_FatalError("sys.path.insert(0) failed");
1081 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001082 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001083 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001084}
Guido van Rossuma890e681998-05-12 14:59:24 +00001085
1086
1087/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1088 Adapted from code submitted by Just van Rossum.
1089
1090 PySys_WriteStdout(format, ...)
1091 PySys_WriteStderr(format, ...)
1092
1093 The first function writes to sys.stdout; the second to sys.stderr. When
1094 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001095 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001096
1097 Both take a printf-style format string as their first argument followed
1098 by a variable length argument list determined by the format string.
1099
1100 *** WARNING ***
1101
1102 The format should limit the total size of the formatted output string to
1103 1000 bytes. In particular, this means that no unrestricted "%s" formats
1104 should occur; these should be limited using "%.<N>s where <N> is a
1105 decimal number calculated so that <N> plus the maximum size of other
1106 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1107 which can print hundreds of digits for very large numbers.
1108
1109 */
1110
1111static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001112mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001113{
1114 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001115 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001116
Guido van Rossum8442af31998-10-12 18:22:10 +00001117 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001118 file = PySys_GetObject(name);
1119 if (file == NULL || PyFile_AsFile(file) == fp)
1120 vfprintf(fp, format, va);
1121 else {
1122 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001123 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1124 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001125 if (PyFile_WriteString(buffer, file) != 0) {
1126 PyErr_Clear();
1127 fputs(buffer, fp);
1128 }
Tim Petersfaad5ad2001-12-03 00:43:33 +00001129 if (written < 0 || written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001130 const char *truncated = "... truncated";
1131 if (PyFile_WriteString(truncated, file) != 0) {
1132 PyErr_Clear();
1133 fputs(truncated, fp);
1134 }
1135 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001136 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001137 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001138}
1139
1140void
Guido van Rossuma890e681998-05-12 14:59:24 +00001141PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001142{
1143 va_list va;
1144
Guido van Rossuma890e681998-05-12 14:59:24 +00001145 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001146 mywrite("stdout", stdout, format, va);
1147 va_end(va);
1148}
1149
1150void
Guido van Rossuma890e681998-05-12 14:59:24 +00001151PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001152{
1153 va_list va;
1154
Guido van Rossuma890e681998-05-12 14:59:24 +00001155 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001156 mywrite("stderr", stderr, format, va);
1157 va_end(va);
1158}