blob: faa63ab7a3d34f184a031c165ea19c22f7d36b38 [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 Rossum1254d791997-05-20 15:57:25 +000023#ifdef HAVE_UNISTD_H
Guido van Rossumc474dea1997-04-25 15:38:31 +000024#include <unistd.h>
25#endif
26
Guido van Rossum9b38a141996-09-11 23:12:24 +000027#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000028extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000029/* A string loaded from the DLL at startup: */
30extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000031#endif
32
Guido van Rossum65bf9f21997-04-29 18:33:38 +000033PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000034PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035{
Guido van Rossum25ce5661997-08-02 03:10:38 +000036 PyThreadState *tstate = PyThreadState_Get();
37 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000038 if (sd == NULL)
39 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000040 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041}
42
43FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000044PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045{
46 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000047 PyObject *v = PySys_GetObject(name);
48 if (v != NULL && PyFile_Check(v))
49 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050 if (fp == NULL)
51 fp = def;
52 return fp;
53}
54
55int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000056PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Guido van Rossum25ce5661997-08-02 03:10:38 +000058 PyThreadState *tstate = PyThreadState_Get();
59 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000060 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000061 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000062 return 0;
63 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000064 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000065 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000067 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossum65bf9f21997-04-29 18:33:38 +000070static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000071sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000072{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000073 PyObject *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000074 PyInterpreterState *interp = PyThreadState_Get()->interp;
75 PyObject *modules = interp->modules;
76 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
77
Moshe Zadka03897ea2001-07-23 13:32:43 +000078 if (builtins == NULL) {
79 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
80 return NULL;
81 }
82
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000083 /* Print value except if None */
84 /* After printing, also assign to '_' */
85 /* Before, set '_' to None to avoid recursion */
86 if (o == Py_None) {
87 Py_INCREF(Py_None);
88 return Py_None;
89 }
90 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
91 return NULL;
92 if (Py_FlushLine() != 0)
93 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000094 outf = PySys_GetObject("stdout");
95 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000096 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
97 return NULL;
98 }
Greg Steinceb9b7c2001-01-11 09:27:34 +000099 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000100 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000101 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000102 if (Py_FlushLine() != 0)
103 return NULL;
104 if (PyObject_SetAttrString(builtins, "_", o) != 0)
105 return NULL;
106 Py_INCREF(Py_None);
107 return Py_None;
108}
109
110static char displayhook_doc[] =
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000111"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000112"\n"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000113"Print an object to sys.stdout and also save it in __builtin__._\n";
114
115static PyObject *
116sys_excepthook(PyObject* self, PyObject* args)
117{
118 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000119 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000120 return NULL;
121 PyErr_Display(exc, value, tb);
122 Py_INCREF(Py_None);
123 return Py_None;
124}
125
126static char excepthook_doc[] =
127"excepthook(exctype, value, traceback) -> None\n"
128"\n"
129"Handle an exception by displaying it with a traceback on sys.stderr.\n";
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000130
131static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000132sys_exc_info(PyObject *self)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000133{
134 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000136 return Py_BuildValue(
137 "(OOO)",
138 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
139 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
140 tstate->exc_traceback != NULL ?
141 tstate->exc_traceback : Py_None);
142}
143
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000144static char exc_info_doc[] =
145"exc_info() -> (type, value, traceback)\n\
146\n\
147Return information about the exception that is currently being handled.\n\
148This should be called from inside an except clause only.";
149
Guido van Rossuma027efa1997-05-05 20:56:21 +0000150static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000151sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000153 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000154 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000155 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156}
157
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000158static char exit_doc[] =
159"exit([status])\n\
160\n\
161Exit the interpreter by raising SystemExit(status).\n\
162If the status is omitted or None, it defaults to zero (i.e., success).\n\
163If the status numeric, it will be used as the system exit status.\n\
164If it is another kind of object, it will be printed and the system\n\
165exit status will be one (i.e., failure).";
166
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000167#ifdef Py_USING_UNICODE
168
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000169static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000170sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000171{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000172 return PyString_FromString(PyUnicode_GetDefaultEncoding());
173}
174
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000175static char getdefaultencoding_doc[] =
176"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000177\n\
178Return the current default string encoding used by the Unicode \n\
179implementation.";
180
181static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000182sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000183{
184 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000185 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000186 return NULL;
187 if (PyUnicode_SetDefaultEncoding(encoding))
188 return NULL;
189 Py_INCREF(Py_None);
190 return Py_None;
191}
192
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000193static char setdefaultencoding_doc[] =
194"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000195\n\
196Set the current default string encoding used by the Unicode implementation.";
197
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000198#endif
199
Fred Drake5755ce62001-06-27 19:19:46 +0000200/*
201 * Cached interned string objects used for calling the profile and
202 * trace functions. Initialized by trace_init().
203 */
204static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
205
206static int
207trace_init(void)
208{
209 static char *whatnames[4] = {"call", "exception", "line", "return"};
210 PyObject *name;
211 int i;
212 for (i = 0; i < 4; ++i) {
213 if (whatstrings[i] == NULL) {
214 name = PyString_InternFromString(whatnames[i]);
215 if (name == NULL)
216 return -1;
217 whatstrings[i] = name;
218 }
219 }
220 return 0;
221}
222
223
224static PyObject *
225call_trampoline(PyThreadState *tstate, PyObject* callback,
226 PyFrameObject *frame, int what, PyObject *arg)
227{
228 PyObject *args = PyTuple_New(3);
229 PyObject *whatstr;
230 PyObject *result;
231
232 if (args == NULL)
233 return NULL;
234 Py_INCREF(frame);
235 whatstr = whatstrings[what];
236 Py_INCREF(whatstr);
237 if (arg == NULL)
238 arg = Py_None;
239 Py_INCREF(arg);
240 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
241 PyTuple_SET_ITEM(args, 1, whatstr);
242 PyTuple_SET_ITEM(args, 2, arg);
243
244 /* call the Python-level function */
245 PyFrame_FastToLocals(frame);
246 result = PyEval_CallObject(callback, args);
247 PyFrame_LocalsToFast(frame, 1);
248 if (result == NULL)
249 PyTraceBack_Here(frame);
250
251 /* cleanup */
252 Py_DECREF(args);
253 return result;
254}
255
256static int
257profile_trampoline(PyObject *self, PyFrameObject *frame,
258 int what, PyObject *arg)
259{
260 PyThreadState *tstate = frame->f_tstate;
261 PyObject *result;
262
Fred Drake8f51f542001-10-04 14:48:42 +0000263 if (arg == NULL)
264 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000265 result = call_trampoline(tstate, self, frame, what, arg);
266 if (result == NULL) {
267 PyEval_SetProfile(NULL, NULL);
268 return -1;
269 }
270 Py_DECREF(result);
271 return 0;
272}
273
274static int
275trace_trampoline(PyObject *self, PyFrameObject *frame,
276 int what, PyObject *arg)
277{
278 PyThreadState *tstate = frame->f_tstate;
279 PyObject *callback;
280 PyObject *result;
281
282 if (what == PyTrace_CALL)
283 callback = self;
284 else
285 callback = frame->f_trace;
286 if (callback == NULL)
287 return 0;
288 result = call_trampoline(tstate, callback, frame, what, arg);
289 if (result == NULL) {
290 PyEval_SetTrace(NULL, NULL);
291 Py_XDECREF(frame->f_trace);
292 frame->f_trace = NULL;
293 return -1;
294 }
295 if (result != Py_None) {
296 PyObject *temp = frame->f_trace;
297 frame->f_trace = NULL;
298 Py_XDECREF(temp);
299 frame->f_trace = result;
300 }
301 else {
302 Py_DECREF(result);
303 }
304 return 0;
305}
Fred Draked0838392001-06-16 21:02:31 +0000306
Fred Drake8b4d01d2000-05-09 19:57:01 +0000307static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000309{
Fred Drake5755ce62001-06-27 19:19:46 +0000310 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000311 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000312 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000313 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000314 else
Fred Drake5755ce62001-06-27 19:19:46 +0000315 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000316 Py_INCREF(Py_None);
317 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000318}
319
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000320static char settrace_doc[] =
321"settrace(function)\n\
322\n\
323Set the global debug tracing function. It will be called on each\n\
324function call. See the debugger chapter in the library manual.";
325
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000326static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000328{
Fred Drake5755ce62001-06-27 19:19:46 +0000329 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000330 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000331 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000332 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000333 else
Fred Drake5755ce62001-06-27 19:19:46 +0000334 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000335 Py_INCREF(Py_None);
336 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000337}
338
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000339static char setprofile_doc[] =
340"setprofile(function)\n\
341\n\
342Set the profiling function. It will be called on each function call\n\
343and return. See the profiler chapter in the library manual.";
344
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000345static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000347{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000348 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000349 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000350 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000351 Py_INCREF(Py_None);
352 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000353}
354
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000355static char setcheckinterval_doc[] =
356"setcheckinterval(n)\n\
357\n\
358Tell the Python interpreter to check for asynchronous events every\n\
359n instructions. This also affects how often thread switches occur.";
360
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000361static PyObject *
362sys_setrecursionlimit(PyObject *self, PyObject *args)
363{
364 int new_limit;
365 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
366 return NULL;
367 if (new_limit <= 0) {
368 PyErr_SetString(PyExc_ValueError,
369 "recursion limit must be positive");
370 return NULL;
371 }
372 Py_SetRecursionLimit(new_limit);
373 Py_INCREF(Py_None);
374 return Py_None;
375}
376
377static char setrecursionlimit_doc[] =
378"setrecursionlimit(n)\n\
379\n\
380Set the maximum depth of the Python interpreter stack to n. This\n\
381limit prevents infinite recursion from causing an overflow of the C\n\
382stack and crashing Python. The highest possible limit is platform-\n\
383dependent.";
384
385static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000386sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000387{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000388 return PyInt_FromLong(Py_GetRecursionLimit());
389}
390
391static char getrecursionlimit_doc[] =
392"getrecursionlimit()\n\
393\n\
394Return the current value of the recursion limit, the maximum depth\n\
395of the Python interpreter stack. This limit prevents infinite\n\
396recursion from causing an overflow of the C stack and crashing Python.";
397
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000398#ifdef HAVE_DLOPEN
399static PyObject *
400sys_setdlopenflags(PyObject *self, PyObject *args)
401{
402 int new_val;
403 PyThreadState *tstate = PyThreadState_Get();
404 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
405 return NULL;
406 if (!tstate)
407 return NULL;
408 tstate->interp->dlopenflags = new_val;
409 Py_INCREF(Py_None);
410 return Py_None;
411}
412
413static char setdlopenflags_doc[] =
414"setdlopenflags(n) -> None\n\
415\n\
416Set the flags that will be used for dlopen() calls. Among other\n\
417things, this will enable a lazy resolving of symbols when imporing\n\
418a module, if called as sys.setdlopenflags(0)\n\
419To share symols across extension modules, call as\n\
420sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)";
421
422static PyObject *
423sys_getdlopenflags(PyObject *self, PyObject *args)
424{
425 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000426 if (!tstate)
427 return NULL;
428 return PyInt_FromLong(tstate->interp->dlopenflags);
429}
430
431static char getdlopenflags_doc[] =
432"getdlopenflags() -> int\n\
433\n\
434Return the current value of the flags that are used for dlopen()\n\
435calls. The flag constants are defined in the dl module.";
436#endif
437
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000438#ifdef USE_MALLOPT
439/* Link with -lmalloc (or -lmpc) on an SGI */
440#include <malloc.h>
441
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000442static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000444{
445 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000446 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000447 return NULL;
448 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000449 Py_INCREF(Py_None);
450 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000451}
452#endif /* USE_MALLOPT */
453
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000454static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000455sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000456{
Mark Hammond440d8982000-06-20 08:12:48 +0000457 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000458}
459
Mark Hammond440d8982000-06-20 08:12:48 +0000460#ifdef Py_TRACE_REFS
461static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000462sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000463{
464 extern long _Py_RefTotal;
Mark Hammond440d8982000-06-20 08:12:48 +0000465 return PyInt_FromLong(_Py_RefTotal);
466}
467
468#endif /* Py_TRACE_REFS */
469
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000470static char getrefcount_doc[] =
471"getrefcount(object) -> integer\n\
472\n\
473Return the current reference count for the object. This includes the\n\
474temporary reference in the argument list, so it is at least 2.";
475
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000476#ifdef COUNT_ALLOCS
477static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000478sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000479{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000480 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000481
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000482 return get_counts();
483}
484#endif
485
Barry Warsawb6a54d22000-12-06 21:47:46 +0000486static char getframe_doc[] =
487"_getframe([depth]) -> frameobject\n\
488\n\
489Return a frame object from the call stack. If optional integer depth is\n\
490given, return the frame object that many calls below the top of the stack.\n\
491If that is deeper than the call stack, ValueError is raised. The default\n\
492for depth is zero, returning the frame at the top of the call stack.\n\
493\n\
494This function should be used for internal and specialized\n\
495purposes only.";
496
497static PyObject *
498sys_getframe(PyObject *self, PyObject *args)
499{
500 PyFrameObject *f = PyThreadState_Get()->frame;
501 int depth = -1;
502
503 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
504 return NULL;
505
506 while (depth > 0 && f != NULL) {
507 f = f->f_back;
508 --depth;
509 }
510 if (f == NULL) {
511 PyErr_SetString(PyExc_ValueError,
512 "call stack is not deep enough");
513 return NULL;
514 }
515 Py_INCREF(f);
516 return (PyObject*)f;
517}
518
519
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000520#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000521/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000522extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000523#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000524
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000525#ifdef DYNAMIC_EXECUTION_PROFILE
526/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000527extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000528#endif
529
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000530static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000531 /* Might as well keep this in alphabetic order */
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000532 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
533 {"exc_info", (PyCFunction)sys_exc_info, METH_NOARGS, exc_info_doc},
534 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
535 {"exit", sys_exit, METH_OLDARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000536#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000537 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000538 getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000539#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000540#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000541 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
542 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000543#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000544#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000545 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000546#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000547#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000548 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000549#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000550#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000551 {"getobjects", _Py_GetObjects, METH_VARARGS},
552 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000553#endif
Fred Drakea7688822001-10-24 20:47:48 +0000554 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000555 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000556 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000557 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000558#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000559 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000560#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000561#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000562 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000563 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000564#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000565 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000566 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000567#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000568 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
569 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000570#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000571 {"setprofile", sys_setprofile, METH_OLDARGS, setprofile_doc},
572 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000573 setrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000574 {"settrace", sys_settrace, METH_OLDARGS, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000575 {NULL, NULL} /* sentinel */
576};
577
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000578static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000579list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000580{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000581 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000582 int i;
583 if (list == NULL)
584 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000585 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000586 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000587 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000588 if (name == NULL)
589 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000590 PyList_Append(list, name);
591 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000592 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000593 if (PyList_Sort(list) != 0) {
594 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000595 list = NULL;
596 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000597 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000598 PyObject *v = PyList_AsTuple(list);
599 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000600 list = v;
601 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000602 return list;
603}
604
Guido van Rossum23fff912000-12-15 22:02:05 +0000605static PyObject *warnoptions = NULL;
606
607void
608PySys_ResetWarnOptions(void)
609{
610 if (warnoptions == NULL || !PyList_Check(warnoptions))
611 return;
612 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
613}
614
615void
616PySys_AddWarnOption(char *s)
617{
618 PyObject *str;
619
620 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
621 Py_XDECREF(warnoptions);
622 warnoptions = PyList_New(0);
623 if (warnoptions == NULL)
624 return;
625 }
626 str = PyString_FromString(s);
627 if (str != NULL) {
628 PyList_Append(warnoptions, str);
629 Py_DECREF(str);
630 }
631}
632
Guido van Rossum40552d01998-08-06 03:34:39 +0000633/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
634 Two literals concatenated works just fine. If you have a K&R compiler
635 or other abomination that however *does* understand longer strings,
636 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000637static char sys_doc[] =
638"This module provides access to some objects used or maintained by the\n\
639interpreter and to functions that interact strongly with the interpreter.\n\
640\n\
641Dynamic objects:\n\
642\n\
643argv -- command line arguments; argv[0] is the script pathname if known\n\
644path -- module search path; path[0] is the script directory, else ''\n\
645modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000646\n\
647displayhook -- called to show results in an interactive session\n\
648excepthook -- called to handle any uncaught exception other than SystemExit\n\
649 To customize printing in an interactive session or to install a custom\n\
650 top-level exception handler, assign other functions to replace these.\n\
651\n\
652exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
653 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000654\n\
655stdin -- standard input file object; used by raw_input() and input()\n\
656stdout -- standard output file object; used by the print statement\n\
657stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000658 By assigning other file objects (or objects that behave like files)\n\
659 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000660\n\
661last_type -- type of last uncaught exception\n\
662last_value -- value of last uncaught exception\n\
663last_traceback -- traceback of last uncaught exception\n\
664 These three are only available in an interactive session after a\n\
665 traceback has been printed.\n\
666\n\
667exc_type -- type of exception currently being handled\n\
668exc_value -- value of exception currently being handled\n\
669exc_traceback -- traceback of exception currently being handled\n\
670 The function exc_info() should be used instead of these three,\n\
671 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000672"
673#ifndef MS_WIN16
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000674/* concatenating string here */
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000675"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000676Static objects:\n\
677\n\
678maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000679maxunicode -- the largest supported character\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000680builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000681version -- the version of this interpreter as a string\n\
682version_info -- version information as a tuple\n\
683hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000684copyright -- copyright notice pertaining to this interpreter\n\
685platform -- platform identifier\n\
686executable -- pathname of this Python interpreter\n\
687prefix -- prefix used to find the Python library\n\
688exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000689"
690#ifdef MS_WINDOWS
691/* concatenating string here */
692"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000693winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000694"
695#endif /* MS_WINDOWS */
696"__stdin__ -- the original stdin; don't touch!\n\
697__stdout__ -- the original stdout; don't touch!\n\
698__stderr__ -- the original stderr; don't touch!\n\
699__displayhook__ -- the original displayhook; don't touch!\n\
700__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000701\n\
702Functions:\n\
703\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000704displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000705excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000706exc_info() -- return thread-safe information about the current exception\n\
707exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000708getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000709getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000710getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000711setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000712setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000713setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000714setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000715settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000716"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000717#endif /* MS_WIN16 */
Fred Drakeccede592000-08-14 20:59:57 +0000718/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000719
Guido van Rossum25ce5661997-08-02 03:10:38 +0000720PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000721_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000723 PyObject *m, *v, *sysdict;
724 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000725 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000726
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000727 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000728 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000729
730 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
731 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
732 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000733 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000734 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000735 PyDict_SetItemString(sysdict, "stdin", sysin);
736 PyDict_SetItemString(sysdict, "stdout", sysout);
737 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000738 /* Make backup copies for cleanup */
739 PyDict_SetItemString(sysdict, "__stdin__", sysin);
740 PyDict_SetItemString(sysdict, "__stdout__", sysout);
741 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000742 PyDict_SetItemString(sysdict, "__displayhook__",
743 PyDict_GetItemString(sysdict, "displayhook"));
744 PyDict_SetItemString(sysdict, "__excepthook__",
745 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000746 Py_XDECREF(sysin);
747 Py_XDECREF(sysout);
748 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000749 PyDict_SetItemString(sysdict, "version",
750 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000751 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000752 PyDict_SetItemString(sysdict, "hexversion",
753 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000754 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000755 /*
756 * These release level checks are mutually exclusive and cover
757 * the field, so don't get too fancy with the pre-processor!
758 */
759#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000760 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000761#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000762 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000763#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000764 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000765#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000766 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000767#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000768 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000769 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000770 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000771 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000772 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000773 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000774 PyDict_SetItemString(sysdict, "copyright",
775 v = PyString_FromString(Py_GetCopyright()));
776 Py_XDECREF(v);
777 PyDict_SetItemString(sysdict, "platform",
778 v = PyString_FromString(Py_GetPlatform()));
779 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000780 PyDict_SetItemString(sysdict, "executable",
781 v = PyString_FromString(Py_GetProgramFullPath()));
782 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000783 PyDict_SetItemString(sysdict, "prefix",
784 v = PyString_FromString(Py_GetPrefix()));
785 Py_XDECREF(v);
786 PyDict_SetItemString(sysdict, "exec_prefix",
787 v = PyString_FromString(Py_GetExecPrefix()));
788 Py_XDECREF(v);
789 PyDict_SetItemString(sysdict, "maxint",
790 v = PyInt_FromLong(PyInt_GetMax()));
791 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000792#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000793 PyDict_SetItemString(sysdict, "maxunicode",
794 v = PyInt_FromLong(PyUnicode_GetMax()));
795 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000796#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000797 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000799 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000800 {
801 /* Assumes that longs are at least 2 bytes long.
802 Should be safe! */
803 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000804 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000805
806 s = (char *) &number;
807 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000808 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000809 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000810 value = "little";
811 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000812 v = PyString_FromString(value));
813 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000814 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000815#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000816 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000817 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000818 Py_XDECREF(v);
819 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000820 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000821 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000822#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000823 if (warnoptions == NULL) {
824 warnoptions = PyList_New(0);
825 }
826 else {
827 Py_INCREF(warnoptions);
828 }
829 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000830 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000831 }
832
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000833 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000834 return NULL;
835 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000836}
837
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000838static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000839makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000840{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000841 int i, n;
842 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000843 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000844
845 n = 1;
846 p = path;
847 while ((p = strchr(p, delim)) != NULL) {
848 n++;
849 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000850 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000851 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000852 if (v == NULL)
853 return NULL;
854 for (i = 0; ; i++) {
855 p = strchr(path, delim);
856 if (p == NULL)
857 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000858 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000859 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000860 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000861 return NULL;
862 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000863 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000864 if (*p == '\0')
865 break;
866 path = p+1;
867 }
868 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869}
870
871void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000872PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000874 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000875 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000876 Py_FatalError("can't create sys.path");
877 if (PySys_SetObject("path", v) != 0)
878 Py_FatalError("can't assign sys.path");
879 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000880}
881
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000882static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000883makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000884{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000885 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000886 if (argc <= 0 || argv == NULL) {
887 /* Ensure at least one (empty) argument is seen */
888 static char *empty_argv[1] = {""};
889 argv = empty_argv;
890 argc = 1;
891 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000892 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000893 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000894 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000895 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000896 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000897 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000898 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000899 av = NULL;
900 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000901 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000902 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000903 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000904 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000905 return av;
906}
907
908void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000909PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000910{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000911 PyObject *av = makeargvobject(argc, argv);
912 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000913 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000914 Py_FatalError("no mem for sys.argv");
915 if (PySys_SetObject("argv", av) != 0)
916 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000917 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000918 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000919 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000920 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000921 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000922#ifdef HAVE_READLINK
923 char link[MAXPATHLEN+1];
924 char argv0copy[2*MAXPATHLEN+1];
925 int nr = 0;
926 if (argc > 0 && argv0 != NULL)
927 nr = readlink(argv0, link, MAXPATHLEN);
928 if (nr > 0) {
929 /* It's a symlink */
930 link[nr] = '\0';
931 if (link[0] == SEP)
932 argv0 = link; /* Link to absolute path */
933 else if (strchr(link, SEP) == NULL)
934 ; /* Link without path */
935 else {
936 /* Must join(dirname(argv0), link) */
937 char *q = strrchr(argv0, SEP);
938 if (q == NULL)
939 argv0 = link; /* argv0 without path */
940 else {
941 /* Must make a copy */
942 strcpy(argv0copy, argv0);
943 q = strrchr(argv0copy, SEP);
944 strcpy(q+1, link);
945 argv0 = argv0copy;
946 }
947 }
948 }
949#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000950#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000951 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000952 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000953 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000954 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000955 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000956 if (q != NULL)
957 p = q;
958 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000959 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000960 if (n > 1 && p[-1] != ':')
961 n--; /* Drop trailing separator */
962 }
963 }
964#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000965 if (argc > 0 && argv0 != NULL)
966 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000967 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +0000968#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +0000969 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +0000970#else /* don't include trailing separator */
971 n = p - argv0;
972#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +0000973#if SEP == '/' /* Special case for Unix filename syntax */
974 if (n > 1)
975 n--; /* Drop trailing separator */
976#endif /* Unix */
977 }
978#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000979 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000980 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000981 Py_FatalError("no mem for sys.path insertion");
982 if (PyList_Insert(path, 0, a) < 0)
983 Py_FatalError("sys.path.insert(0) failed");
984 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000985 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000986 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000987}
Guido van Rossuma890e681998-05-12 14:59:24 +0000988
989
990/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
991 Adapted from code submitted by Just van Rossum.
992
993 PySys_WriteStdout(format, ...)
994 PySys_WriteStderr(format, ...)
995
996 The first function writes to sys.stdout; the second to sys.stderr. When
997 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000998 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000999
1000 Both take a printf-style format string as their first argument followed
1001 by a variable length argument list determined by the format string.
1002
1003 *** WARNING ***
1004
1005 The format should limit the total size of the formatted output string to
1006 1000 bytes. In particular, this means that no unrestricted "%s" formats
1007 should occur; these should be limited using "%.<N>s where <N> is a
1008 decimal number calculated so that <N> plus the maximum size of other
1009 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1010 which can print hundreds of digits for very large numbers.
1011
1012 */
1013
1014static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001015mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001016{
1017 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001018 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001019
Guido van Rossum8442af31998-10-12 18:22:10 +00001020 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001021 file = PySys_GetObject(name);
1022 if (file == NULL || PyFile_AsFile(file) == fp)
1023 vfprintf(fp, format, va);
1024 else {
1025 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001026 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1027 format, va);
1028 const int trouble = written < 0 || written >= sizeof(buffer);
1029 if (trouble) {
1030 /* Ensure there's a trailing null byte -- MS
1031 vsnprintf fills the buffer to the very end
1032 if it's not big enough. */
1033 buffer[sizeof(buffer) - 1] = '\0';
1034 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001035 if (PyFile_WriteString(buffer, file) != 0) {
1036 PyErr_Clear();
1037 fputs(buffer, fp);
1038 }
Tim Peters080d5b32001-12-02 08:29:16 +00001039 if (trouble) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001040 const char *truncated = "... truncated";
1041 if (PyFile_WriteString(truncated, file) != 0) {
1042 PyErr_Clear();
1043 fputs(truncated, fp);
1044 }
1045 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001046 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001047 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001048}
1049
1050void
Guido van Rossuma890e681998-05-12 14:59:24 +00001051PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001052{
1053 va_list va;
1054
Guido van Rossuma890e681998-05-12 14:59:24 +00001055 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001056 mywrite("stdout", stdout, format, va);
1057 va_end(va);
1058}
1059
1060void
Guido van Rossuma890e681998-05-12 14:59:24 +00001061PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001062{
1063 va_list va;
1064
Guido van Rossuma890e681998-05-12 14:59:24 +00001065 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001066 mywrite("stderr", stderr, format, va);
1067 va_end(va);
1068}