blob: 4a0bf37622bb842e1f88d44f3ea4b29ec6cc2b0f [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
Guido van Rossum9b38a141996-09-11 23:12:24 +000023#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000024extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000025/* A string loaded from the DLL at startup: */
26extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000027#endif
28
Guido van Rossum65bf9f21997-04-29 18:33:38 +000029PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
Guido van Rossum25ce5661997-08-02 03:10:38 +000032 PyThreadState *tstate = PyThreadState_Get();
33 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000034 if (sd == NULL)
35 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000036 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037}
38
39FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000040PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041{
42 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000043 PyObject *v = PySys_GetObject(name);
44 if (v != NULL && PyFile_Check(v))
45 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 if (fp == NULL)
47 fp = def;
48 return fp;
49}
50
51int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000052PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053{
Guido van Rossum25ce5661997-08-02 03:10:38 +000054 PyThreadState *tstate = PyThreadState_Get();
55 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000056 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000057 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000058 return 0;
59 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000060 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000061 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000063 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064}
65
Guido van Rossum65bf9f21997-04-29 18:33:38 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000068{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000069 PyObject *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000070 PyInterpreterState *interp = PyThreadState_Get()->interp;
71 PyObject *modules = interp->modules;
72 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
73
Moshe Zadka03897ea2001-07-23 13:32:43 +000074 if (builtins == NULL) {
75 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
76 return NULL;
77 }
78
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000079 /* Print value except if None */
80 /* After printing, also assign to '_' */
81 /* Before, set '_' to None to avoid recursion */
82 if (o == Py_None) {
83 Py_INCREF(Py_None);
84 return Py_None;
85 }
86 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
87 return NULL;
88 if (Py_FlushLine() != 0)
89 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000090 outf = PySys_GetObject("stdout");
91 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000092 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
93 return NULL;
94 }
Greg Steinceb9b7c2001-01-11 09:27:34 +000095 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000096 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000097 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000098 if (Py_FlushLine() != 0)
99 return NULL;
100 if (PyObject_SetAttrString(builtins, "_", o) != 0)
101 return NULL;
102 Py_INCREF(Py_None);
103 return Py_None;
104}
105
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000106PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000107"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000108"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000109"Print an object to sys.stdout and also save it in __builtin__._\n"
110);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000111
112static PyObject *
113sys_excepthook(PyObject* self, PyObject* args)
114{
115 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000116 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000117 return NULL;
118 PyErr_Display(exc, value, tb);
119 Py_INCREF(Py_None);
120 return Py_None;
121}
122
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000123PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000124"excepthook(exctype, value, traceback) -> None\n"
125"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000126"Handle an exception by displaying it with a traceback on sys.stderr.\n"
127);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000128
129static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000130sys_exc_info(PyObject *self)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131{
132 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000133 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000134 return Py_BuildValue(
135 "(OOO)",
136 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
137 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
138 tstate->exc_traceback != NULL ?
139 tstate->exc_traceback : Py_None);
140}
141
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000142PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000143"exc_info() -> (type, value, traceback)\n\
144\n\
145Return information about the exception that is currently being handled.\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000146This should be called from inside an except clause only."
147);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000148
Guido van Rossuma027efa1997-05-05 20:56:21 +0000149static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000150sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000152 PyObject *exit_code = 0;
153 if (!PyArg_ParseTuple(args, "|O:exit", &exit_code))
154 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000155 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000156 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000157 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}
159
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000160PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000161"exit([status])\n\
162\n\
163Exit the interpreter by raising SystemExit(status).\n\
164If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000165If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000166If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000167exit status will be one (i.e., failure)."
168);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000169
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000170#ifdef Py_USING_UNICODE
171
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000172static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000173sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000174{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000175 return PyString_FromString(PyUnicode_GetDefaultEncoding());
176}
177
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000178PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000179"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000180\n\
181Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000182implementation."
183);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000184
185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000186sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000187{
188 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000189 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000190 return NULL;
191 if (PyUnicode_SetDefaultEncoding(encoding))
192 return NULL;
193 Py_INCREF(Py_None);
194 return Py_None;
195}
196
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000197PyDoc_STRVAR(setdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000198"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000199\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000200Set the current default string encoding used by the Unicode implementation."
201);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000202
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000203#endif
204
Fred Drake5755ce62001-06-27 19:19:46 +0000205/*
206 * Cached interned string objects used for calling the profile and
207 * trace functions. Initialized by trace_init().
208 */
209static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
210
211static int
212trace_init(void)
213{
214 static char *whatnames[4] = {"call", "exception", "line", "return"};
215 PyObject *name;
216 int i;
217 for (i = 0; i < 4; ++i) {
218 if (whatstrings[i] == NULL) {
219 name = PyString_InternFromString(whatnames[i]);
220 if (name == NULL)
221 return -1;
222 whatstrings[i] = name;
223 }
224 }
225 return 0;
226}
227
228
229static PyObject *
230call_trampoline(PyThreadState *tstate, PyObject* callback,
231 PyFrameObject *frame, int what, PyObject *arg)
232{
233 PyObject *args = PyTuple_New(3);
234 PyObject *whatstr;
235 PyObject *result;
236
237 if (args == NULL)
238 return NULL;
239 Py_INCREF(frame);
240 whatstr = whatstrings[what];
241 Py_INCREF(whatstr);
242 if (arg == NULL)
243 arg = Py_None;
244 Py_INCREF(arg);
245 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
246 PyTuple_SET_ITEM(args, 1, whatstr);
247 PyTuple_SET_ITEM(args, 2, arg);
248
249 /* call the Python-level function */
250 PyFrame_FastToLocals(frame);
251 result = PyEval_CallObject(callback, args);
252 PyFrame_LocalsToFast(frame, 1);
253 if (result == NULL)
254 PyTraceBack_Here(frame);
255
256 /* cleanup */
257 Py_DECREF(args);
258 return result;
259}
260
261static int
262profile_trampoline(PyObject *self, PyFrameObject *frame,
263 int what, PyObject *arg)
264{
265 PyThreadState *tstate = frame->f_tstate;
266 PyObject *result;
267
Fred Drake8f51f542001-10-04 14:48:42 +0000268 if (arg == NULL)
269 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000270 result = call_trampoline(tstate, self, frame, what, arg);
271 if (result == NULL) {
272 PyEval_SetProfile(NULL, NULL);
273 return -1;
274 }
275 Py_DECREF(result);
276 return 0;
277}
278
279static int
280trace_trampoline(PyObject *self, PyFrameObject *frame,
281 int what, PyObject *arg)
282{
283 PyThreadState *tstate = frame->f_tstate;
284 PyObject *callback;
285 PyObject *result;
286
287 if (what == PyTrace_CALL)
288 callback = self;
289 else
290 callback = frame->f_trace;
291 if (callback == NULL)
292 return 0;
293 result = call_trampoline(tstate, callback, frame, what, arg);
294 if (result == NULL) {
295 PyEval_SetTrace(NULL, NULL);
296 Py_XDECREF(frame->f_trace);
297 frame->f_trace = NULL;
298 return -1;
299 }
300 if (result != Py_None) {
301 PyObject *temp = frame->f_trace;
302 frame->f_trace = NULL;
303 Py_XDECREF(temp);
304 frame->f_trace = result;
305 }
306 else {
307 Py_DECREF(result);
308 }
309 return 0;
310}
Fred Draked0838392001-06-16 21:02:31 +0000311
Fred Drake8b4d01d2000-05-09 19:57:01 +0000312static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000313sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000314{
Fred Drake5755ce62001-06-27 19:19:46 +0000315 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000316 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000317 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000318 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000319 else
Fred Drake5755ce62001-06-27 19:19:46 +0000320 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000321 Py_INCREF(Py_None);
322 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000323}
324
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000325PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000326"settrace(function)\n\
327\n\
328Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000329function call. See the debugger chapter in the library manual."
330);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000331
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000332static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000334{
Fred Drake5755ce62001-06-27 19:19:46 +0000335 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000336 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000337 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000338 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000339 else
Fred Drake5755ce62001-06-27 19:19:46 +0000340 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000341 Py_INCREF(Py_None);
342 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000343}
344
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000345PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000346"setprofile(function)\n\
347\n\
348Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000349and return. See the profiler chapter in the library manual."
350);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000351
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000352static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000353sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000354{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000355 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000356 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000357 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000358 Py_INCREF(Py_None);
359 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000360}
361
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000362PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000363"setcheckinterval(n)\n\
364\n\
365Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000366n instructions. This also affects how often thread switches occur."
367);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000368
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000369static PyObject *
370sys_setrecursionlimit(PyObject *self, PyObject *args)
371{
372 int new_limit;
373 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
374 return NULL;
375 if (new_limit <= 0) {
376 PyErr_SetString(PyExc_ValueError,
377 "recursion limit must be positive");
378 return NULL;
379 }
380 Py_SetRecursionLimit(new_limit);
381 Py_INCREF(Py_None);
382 return Py_None;
383}
384
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000385PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000386"setrecursionlimit(n)\n\
387\n\
388Set the maximum depth of the Python interpreter stack to n. This\n\
389limit prevents infinite recursion from causing an overflow of the C\n\
390stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000391dependent."
392);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000393
394static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000395sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000396{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000397 return PyInt_FromLong(Py_GetRecursionLimit());
398}
399
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000400PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000401"getrecursionlimit()\n\
402\n\
403Return the current value of the recursion limit, the maximum depth\n\
404of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000405recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000406);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000407
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000408#ifdef HAVE_DLOPEN
409static PyObject *
410sys_setdlopenflags(PyObject *self, PyObject *args)
411{
412 int new_val;
413 PyThreadState *tstate = PyThreadState_Get();
414 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
415 return NULL;
416 if (!tstate)
417 return NULL;
418 tstate->interp->dlopenflags = new_val;
419 Py_INCREF(Py_None);
420 return Py_None;
421}
422
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000423PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000424"setdlopenflags(n) -> None\n\
425\n\
426Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000427things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000428a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000429To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000430sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
431);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000432
433static PyObject *
434sys_getdlopenflags(PyObject *self, PyObject *args)
435{
436 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000437 if (!tstate)
438 return NULL;
439 return PyInt_FromLong(tstate->interp->dlopenflags);
440}
441
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000442PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000443"getdlopenflags() -> int\n\
444\n\
445Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000446calls. The flag constants are defined in the dl module."
447);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000448#endif
449
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000450#ifdef USE_MALLOPT
451/* Link with -lmalloc (or -lmpc) on an SGI */
452#include <malloc.h>
453
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000454static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000455sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000456{
457 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000458 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000459 return NULL;
460 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000461 Py_INCREF(Py_None);
462 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000463}
464#endif /* USE_MALLOPT */
465
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000466static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000467sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000468{
Mark Hammond440d8982000-06-20 08:12:48 +0000469 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000470}
471
Tim Peters4be93d02002-07-07 19:59:50 +0000472#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000473static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000474sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000475{
Mark Hammond440d8982000-06-20 08:12:48 +0000476 return PyInt_FromLong(_Py_RefTotal);
477}
478
479#endif /* Py_TRACE_REFS */
480
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000481PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000482"getrefcount(object) -> integer\n\
483\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000484Return the reference count of object. The count returned is generally\n\
485one higher than you might expect, because it includes the (temporary)\n\
486reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000487);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000488
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000489#ifdef COUNT_ALLOCS
490static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000491sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000492{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000493 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000494
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000495 return get_counts();
496}
497#endif
498
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000499PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000500"_getframe([depth]) -> frameobject\n\
501\n\
502Return a frame object from the call stack. If optional integer depth is\n\
503given, return the frame object that many calls below the top of the stack.\n\
504If that is deeper than the call stack, ValueError is raised. The default\n\
505for depth is zero, returning the frame at the top of the call stack.\n\
506\n\
507This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000508purposes only."
509);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000510
511static PyObject *
512sys_getframe(PyObject *self, PyObject *args)
513{
514 PyFrameObject *f = PyThreadState_Get()->frame;
515 int depth = -1;
516
517 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
518 return NULL;
519
520 while (depth > 0 && f != NULL) {
521 f = f->f_back;
522 --depth;
523 }
524 if (f == NULL) {
525 PyErr_SetString(PyExc_ValueError,
526 "call stack is not deep enough");
527 return NULL;
528 }
529 Py_INCREF(f);
530 return (PyObject*)f;
531}
532
533
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000534#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000535/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000536extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000537#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000538
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000539#ifdef DYNAMIC_EXECUTION_PROFILE
540/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000541extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000542#endif
543
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000544static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000545 /* Might as well keep this in alphabetic order */
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000546 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
547 {"exc_info", (PyCFunction)sys_exc_info, METH_NOARGS, exc_info_doc},
548 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000549 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000550#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000551 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000552 getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000553#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000554#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000555 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
556 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000557#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000558#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000559 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000560#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000561#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000562 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000563#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000564#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000565 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000566#endif
567#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000568 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000569#endif
Fred Drakea7688822001-10-24 20:47:48 +0000570 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000571 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000572 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000573 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000574#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000575 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000576#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000577#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000578 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000579 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000580#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000581 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000582 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000583#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000584 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
585 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000586#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000587 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000588 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000589 setrecursionlimit_doc},
Neal Norwitz290d31e2002-03-03 15:12:58 +0000590 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000591 {NULL, NULL} /* sentinel */
592};
593
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000594static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000595list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000596{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000597 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000598 int i;
599 if (list == NULL)
600 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000601 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000602 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000603 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000604 if (name == NULL)
605 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000606 PyList_Append(list, name);
607 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000608 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000609 if (PyList_Sort(list) != 0) {
610 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000611 list = NULL;
612 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000613 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000614 PyObject *v = PyList_AsTuple(list);
615 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000616 list = v;
617 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000618 return list;
619}
620
Guido van Rossum23fff912000-12-15 22:02:05 +0000621static PyObject *warnoptions = NULL;
622
623void
624PySys_ResetWarnOptions(void)
625{
626 if (warnoptions == NULL || !PyList_Check(warnoptions))
627 return;
628 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
629}
630
631void
632PySys_AddWarnOption(char *s)
633{
634 PyObject *str;
635
636 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
637 Py_XDECREF(warnoptions);
638 warnoptions = PyList_New(0);
639 if (warnoptions == NULL)
640 return;
641 }
642 str = PyString_FromString(s);
643 if (str != NULL) {
644 PyList_Append(warnoptions, str);
645 Py_DECREF(str);
646 }
647}
648
Guido van Rossum40552d01998-08-06 03:34:39 +0000649/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
650 Two literals concatenated works just fine. If you have a K&R compiler
651 or other abomination that however *does* understand longer strings,
652 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000653PyDoc_VAR(sys_doc) =
654PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000655"This module provides access to some objects used or maintained by the\n\
656interpreter and to functions that interact strongly with the interpreter.\n\
657\n\
658Dynamic objects:\n\
659\n\
660argv -- command line arguments; argv[0] is the script pathname if known\n\
661path -- module search path; path[0] is the script directory, else ''\n\
662modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000663\n\
664displayhook -- called to show results in an interactive session\n\
665excepthook -- called to handle any uncaught exception other than SystemExit\n\
666 To customize printing in an interactive session or to install a custom\n\
667 top-level exception handler, assign other functions to replace these.\n\
668\n\
669exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
670 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000671\n\
672stdin -- standard input file object; used by raw_input() and input()\n\
673stdout -- standard output file object; used by the print statement\n\
674stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000675 By assigning other file objects (or objects that behave like files)\n\
676 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000677\n\
678last_type -- type of last uncaught exception\n\
679last_value -- value of last uncaught exception\n\
680last_traceback -- traceback of last uncaught exception\n\
681 These three are only available in an interactive session after a\n\
682 traceback has been printed.\n\
683\n\
684exc_type -- type of exception currently being handled\n\
685exc_value -- value of exception currently being handled\n\
686exc_traceback -- traceback of exception currently being handled\n\
687 The function exc_info() should be used instead of these three,\n\
688 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000689"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000690)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000691/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000692PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000693"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000694Static objects:\n\
695\n\
696maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000697maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000698builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000699version -- the version of this interpreter as a string\n\
700version_info -- version information as a tuple\n\
701hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000702copyright -- copyright notice pertaining to this interpreter\n\
703platform -- platform identifier\n\
704executable -- pathname of this Python interpreter\n\
705prefix -- prefix used to find the Python library\n\
706exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000707"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000708)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000709#ifdef MS_WINDOWS
710/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000711PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000712"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000713winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000714"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000715)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000716#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000717PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000718"__stdin__ -- the original stdin; don't touch!\n\
719__stdout__ -- the original stdout; don't touch!\n\
720__stderr__ -- the original stderr; don't touch!\n\
721__displayhook__ -- the original displayhook; don't touch!\n\
722__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000723\n\
724Functions:\n\
725\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000726displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000727excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000728exc_info() -- return thread-safe information about the current exception\n\
729exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000730getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000731getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000732getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000733setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000734setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000735setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000736setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000737settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000738"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000739)
Fred Drakeccede592000-08-14 20:59:57 +0000740/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000741
Guido van Rossum25ce5661997-08-02 03:10:38 +0000742PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000743_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000745 PyObject *m, *v, *sysdict;
746 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000747 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000748
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000749 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000750 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000751
752 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
753 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
754 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000755 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000756 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000757 PyDict_SetItemString(sysdict, "stdin", sysin);
758 PyDict_SetItemString(sysdict, "stdout", sysout);
759 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000760 /* Make backup copies for cleanup */
761 PyDict_SetItemString(sysdict, "__stdin__", sysin);
762 PyDict_SetItemString(sysdict, "__stdout__", sysout);
763 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000764 PyDict_SetItemString(sysdict, "__displayhook__",
765 PyDict_GetItemString(sysdict, "displayhook"));
766 PyDict_SetItemString(sysdict, "__excepthook__",
767 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000768 Py_XDECREF(sysin);
769 Py_XDECREF(sysout);
770 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000771 PyDict_SetItemString(sysdict, "version",
772 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000773 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000774 PyDict_SetItemString(sysdict, "hexversion",
775 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000776 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000777 /*
778 * These release level checks are mutually exclusive and cover
779 * the field, so don't get too fancy with the pre-processor!
780 */
781#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000782 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000783#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000784 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000785#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000786 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000787#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000788 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000789#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000790 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000791 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000792 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000793 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000794 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000795 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000796 PyDict_SetItemString(sysdict, "copyright",
797 v = PyString_FromString(Py_GetCopyright()));
798 Py_XDECREF(v);
799 PyDict_SetItemString(sysdict, "platform",
800 v = PyString_FromString(Py_GetPlatform()));
801 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000802 PyDict_SetItemString(sysdict, "executable",
803 v = PyString_FromString(Py_GetProgramFullPath()));
804 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000805 PyDict_SetItemString(sysdict, "prefix",
806 v = PyString_FromString(Py_GetPrefix()));
807 Py_XDECREF(v);
808 PyDict_SetItemString(sysdict, "exec_prefix",
809 v = PyString_FromString(Py_GetExecPrefix()));
810 Py_XDECREF(v);
811 PyDict_SetItemString(sysdict, "maxint",
812 v = PyInt_FromLong(PyInt_GetMax()));
813 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000814#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000815 PyDict_SetItemString(sysdict, "maxunicode",
816 v = PyInt_FromLong(PyUnicode_GetMax()));
817 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000818#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000819 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000821 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000822 {
823 /* Assumes that longs are at least 2 bytes long.
824 Should be safe! */
825 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000826 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000827
828 s = (char *) &number;
829 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000830 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000831 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000832 value = "little";
833 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000834 v = PyString_FromString(value));
835 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000836 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000837#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000838 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000839 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000840 Py_XDECREF(v);
841 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000842 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000843 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000844#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000845 if (warnoptions == NULL) {
846 warnoptions = PyList_New(0);
847 }
848 else {
849 Py_INCREF(warnoptions);
850 }
851 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000852 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000853 }
854
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000855 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000856 return NULL;
857 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000858}
859
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000860static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000861makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000862{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000863 int i, n;
864 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000865 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000866
867 n = 1;
868 p = path;
869 while ((p = strchr(p, delim)) != NULL) {
870 n++;
871 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000872 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000873 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000874 if (v == NULL)
875 return NULL;
876 for (i = 0; ; i++) {
877 p = strchr(path, delim);
878 if (p == NULL)
879 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000880 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000881 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000882 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000883 return NULL;
884 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000885 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000886 if (*p == '\0')
887 break;
888 path = p+1;
889 }
890 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891}
892
893void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000894PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000896 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000897 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000898 Py_FatalError("can't create sys.path");
899 if (PySys_SetObject("path", v) != 0)
900 Py_FatalError("can't assign sys.path");
901 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000902}
903
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000904static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000905makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000906{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000907 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000908 if (argc <= 0 || argv == NULL) {
909 /* Ensure at least one (empty) argument is seen */
910 static char *empty_argv[1] = {""};
911 argv = empty_argv;
912 argc = 1;
913 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000914 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000915 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000916 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000917 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000918 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000919 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000920 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000921 av = NULL;
922 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000923 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000924 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000925 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000926 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000927 return av;
928}
929
930void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000931PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000932{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000933 PyObject *av = makeargvobject(argc, argv);
934 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000935 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000936 Py_FatalError("no mem for sys.argv");
937 if (PySys_SetObject("argv", av) != 0)
938 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000939 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000940 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000941 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000942 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000943 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000944#ifdef HAVE_READLINK
945 char link[MAXPATHLEN+1];
946 char argv0copy[2*MAXPATHLEN+1];
947 int nr = 0;
948 if (argc > 0 && argv0 != NULL)
949 nr = readlink(argv0, link, MAXPATHLEN);
950 if (nr > 0) {
951 /* It's a symlink */
952 link[nr] = '\0';
953 if (link[0] == SEP)
954 argv0 = link; /* Link to absolute path */
955 else if (strchr(link, SEP) == NULL)
956 ; /* Link without path */
957 else {
958 /* Must join(dirname(argv0), link) */
959 char *q = strrchr(argv0, SEP);
960 if (q == NULL)
961 argv0 = link; /* argv0 without path */
962 else {
963 /* Must make a copy */
964 strcpy(argv0copy, argv0);
965 q = strrchr(argv0copy, SEP);
966 strcpy(q+1, link);
967 argv0 = argv0copy;
968 }
969 }
970 }
971#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000972#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000973 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000974 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000975 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000976 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000977 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000978 if (q != NULL)
979 p = q;
980 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000981 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000982 if (n > 1 && p[-1] != ':')
983 n--; /* Drop trailing separator */
984 }
985 }
986#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000987 if (argc > 0 && argv0 != NULL)
988 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000989 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +0000990#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +0000991 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +0000992#else /* don't include trailing separator */
993 n = p - argv0;
994#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +0000995#if SEP == '/' /* Special case for Unix filename syntax */
996 if (n > 1)
997 n--; /* Drop trailing separator */
998#endif /* Unix */
999 }
1000#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001001 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001002 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001003 Py_FatalError("no mem for sys.path insertion");
1004 if (PyList_Insert(path, 0, a) < 0)
1005 Py_FatalError("sys.path.insert(0) failed");
1006 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001007 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001008 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001009}
Guido van Rossuma890e681998-05-12 14:59:24 +00001010
1011
1012/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1013 Adapted from code submitted by Just van Rossum.
1014
1015 PySys_WriteStdout(format, ...)
1016 PySys_WriteStderr(format, ...)
1017
1018 The first function writes to sys.stdout; the second to sys.stderr. When
1019 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001020 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001021
1022 Both take a printf-style format string as their first argument followed
1023 by a variable length argument list determined by the format string.
1024
1025 *** WARNING ***
1026
1027 The format should limit the total size of the formatted output string to
1028 1000 bytes. In particular, this means that no unrestricted "%s" formats
1029 should occur; these should be limited using "%.<N>s where <N> is a
1030 decimal number calculated so that <N> plus the maximum size of other
1031 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1032 which can print hundreds of digits for very large numbers.
1033
1034 */
1035
1036static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001037mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001038{
1039 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001040 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001041
Guido van Rossum8442af31998-10-12 18:22:10 +00001042 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001043 file = PySys_GetObject(name);
1044 if (file == NULL || PyFile_AsFile(file) == fp)
1045 vfprintf(fp, format, va);
1046 else {
1047 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001048 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1049 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001050 if (PyFile_WriteString(buffer, file) != 0) {
1051 PyErr_Clear();
1052 fputs(buffer, fp);
1053 }
Tim Petersfaad5ad2001-12-03 00:43:33 +00001054 if (written < 0 || written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001055 const char *truncated = "... truncated";
1056 if (PyFile_WriteString(truncated, file) != 0) {
1057 PyErr_Clear();
1058 fputs(truncated, fp);
1059 }
1060 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001061 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001062 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001063}
1064
1065void
Guido van Rossuma890e681998-05-12 14:59:24 +00001066PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001067{
1068 va_list va;
1069
Guido van Rossuma890e681998-05-12 14:59:24 +00001070 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001071 mywrite("stdout", stdout, format, va);
1072 va_end(va);
1073}
1074
1075void
Guido van Rossuma890e681998-05-12 14:59:24 +00001076PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001077{
1078 va_list va;
1079
Guido van Rossuma890e681998-05-12 14:59:24 +00001080 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001081 mywrite("stderr", stderr, format, va);
1082 va_end(va);
1083}