blob: 2eae03d0b8c43702432f9bc82b3813d72e7f8515 [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;
119 if (!PyArg_ParseTuple(args, "OOO:excepthook", &exc, &value, &tb))
120 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
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000167static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000168sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000169{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000170 return PyString_FromString(PyUnicode_GetDefaultEncoding());
171}
172
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000173static char getdefaultencoding_doc[] =
174"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000175\n\
176Return the current default string encoding used by the Unicode \n\
177implementation.";
178
179static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000180sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000181{
182 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000183 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000184 return NULL;
185 if (PyUnicode_SetDefaultEncoding(encoding))
186 return NULL;
187 Py_INCREF(Py_None);
188 return Py_None;
189}
190
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000191static char setdefaultencoding_doc[] =
192"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000193\n\
194Set the current default string encoding used by the Unicode implementation.";
195
Fred Drake5755ce62001-06-27 19:19:46 +0000196/*
197 * Cached interned string objects used for calling the profile and
198 * trace functions. Initialized by trace_init().
199 */
200static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
201
202static int
203trace_init(void)
204{
205 static char *whatnames[4] = {"call", "exception", "line", "return"};
206 PyObject *name;
207 int i;
208 for (i = 0; i < 4; ++i) {
209 if (whatstrings[i] == NULL) {
210 name = PyString_InternFromString(whatnames[i]);
211 if (name == NULL)
212 return -1;
213 whatstrings[i] = name;
214 }
215 }
216 return 0;
217}
218
219
220static PyObject *
221call_trampoline(PyThreadState *tstate, PyObject* callback,
222 PyFrameObject *frame, int what, PyObject *arg)
223{
224 PyObject *args = PyTuple_New(3);
225 PyObject *whatstr;
226 PyObject *result;
227
228 if (args == NULL)
229 return NULL;
230 Py_INCREF(frame);
231 whatstr = whatstrings[what];
232 Py_INCREF(whatstr);
233 if (arg == NULL)
234 arg = Py_None;
235 Py_INCREF(arg);
236 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
237 PyTuple_SET_ITEM(args, 1, whatstr);
238 PyTuple_SET_ITEM(args, 2, arg);
239
240 /* call the Python-level function */
241 PyFrame_FastToLocals(frame);
242 result = PyEval_CallObject(callback, args);
243 PyFrame_LocalsToFast(frame, 1);
244 if (result == NULL)
245 PyTraceBack_Here(frame);
246
247 /* cleanup */
248 Py_DECREF(args);
249 return result;
250}
251
252static int
253profile_trampoline(PyObject *self, PyFrameObject *frame,
254 int what, PyObject *arg)
255{
256 PyThreadState *tstate = frame->f_tstate;
257 PyObject *result;
258
259 result = call_trampoline(tstate, self, frame, what, arg);
260 if (result == NULL) {
261 PyEval_SetProfile(NULL, NULL);
262 return -1;
263 }
264 Py_DECREF(result);
265 return 0;
266}
267
268static int
269trace_trampoline(PyObject *self, PyFrameObject *frame,
270 int what, PyObject *arg)
271{
272 PyThreadState *tstate = frame->f_tstate;
273 PyObject *callback;
274 PyObject *result;
275
276 if (what == PyTrace_CALL)
277 callback = self;
278 else
279 callback = frame->f_trace;
280 if (callback == NULL)
281 return 0;
282 result = call_trampoline(tstate, callback, frame, what, arg);
283 if (result == NULL) {
284 PyEval_SetTrace(NULL, NULL);
285 Py_XDECREF(frame->f_trace);
286 frame->f_trace = NULL;
287 return -1;
288 }
289 if (result != Py_None) {
290 PyObject *temp = frame->f_trace;
291 frame->f_trace = NULL;
292 Py_XDECREF(temp);
293 frame->f_trace = result;
294 }
295 else {
296 Py_DECREF(result);
297 }
298 return 0;
299}
Fred Draked0838392001-06-16 21:02:31 +0000300
Fred Drake8b4d01d2000-05-09 19:57:01 +0000301static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000302sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000303{
Fred Drake5755ce62001-06-27 19:19:46 +0000304 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000305 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000306 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000307 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000308 else
Fred Drake5755ce62001-06-27 19:19:46 +0000309 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000310 Py_INCREF(Py_None);
311 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000312}
313
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000314static char settrace_doc[] =
315"settrace(function)\n\
316\n\
317Set the global debug tracing function. It will be called on each\n\
318function call. See the debugger chapter in the library manual.";
319
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000320static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000322{
Fred Drake5755ce62001-06-27 19:19:46 +0000323 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000324 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000325 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000326 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000327 else
Fred Drake5755ce62001-06-27 19:19:46 +0000328 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000329 Py_INCREF(Py_None);
330 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000331}
332
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000333static char setprofile_doc[] =
334"setprofile(function)\n\
335\n\
336Set the profiling function. It will be called on each function call\n\
337and return. See the profiler chapter in the library manual.";
338
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000339static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000341{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000342 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000343 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000344 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000345 Py_INCREF(Py_None);
346 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000347}
348
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000349static char setcheckinterval_doc[] =
350"setcheckinterval(n)\n\
351\n\
352Tell the Python interpreter to check for asynchronous events every\n\
353n instructions. This also affects how often thread switches occur.";
354
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000355static PyObject *
356sys_setrecursionlimit(PyObject *self, PyObject *args)
357{
358 int new_limit;
359 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
360 return NULL;
361 if (new_limit <= 0) {
362 PyErr_SetString(PyExc_ValueError,
363 "recursion limit must be positive");
364 return NULL;
365 }
366 Py_SetRecursionLimit(new_limit);
367 Py_INCREF(Py_None);
368 return Py_None;
369}
370
371static char setrecursionlimit_doc[] =
372"setrecursionlimit(n)\n\
373\n\
374Set the maximum depth of the Python interpreter stack to n. This\n\
375limit prevents infinite recursion from causing an overflow of the C\n\
376stack and crashing Python. The highest possible limit is platform-\n\
377dependent.";
378
379static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000380sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000381{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000382 return PyInt_FromLong(Py_GetRecursionLimit());
383}
384
385static char getrecursionlimit_doc[] =
386"getrecursionlimit()\n\
387\n\
388Return the current value of the recursion limit, the maximum depth\n\
389of the Python interpreter stack. This limit prevents infinite\n\
390recursion from causing an overflow of the C stack and crashing Python.";
391
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000392#ifdef HAVE_DLOPEN
393static PyObject *
394sys_setdlopenflags(PyObject *self, PyObject *args)
395{
396 int new_val;
397 PyThreadState *tstate = PyThreadState_Get();
398 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
399 return NULL;
400 if (!tstate)
401 return NULL;
402 tstate->interp->dlopenflags = new_val;
403 Py_INCREF(Py_None);
404 return Py_None;
405}
406
407static char setdlopenflags_doc[] =
408"setdlopenflags(n) -> None\n\
409\n\
410Set the flags that will be used for dlopen() calls. Among other\n\
411things, this will enable a lazy resolving of symbols when imporing\n\
412a module, if called as sys.setdlopenflags(0)\n\
413To share symols across extension modules, call as\n\
414sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)";
415
416static PyObject *
417sys_getdlopenflags(PyObject *self, PyObject *args)
418{
419 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000420 if (!tstate)
421 return NULL;
422 return PyInt_FromLong(tstate->interp->dlopenflags);
423}
424
425static char getdlopenflags_doc[] =
426"getdlopenflags() -> int\n\
427\n\
428Return the current value of the flags that are used for dlopen()\n\
429calls. The flag constants are defined in the dl module.";
430#endif
431
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000432#ifdef USE_MALLOPT
433/* Link with -lmalloc (or -lmpc) on an SGI */
434#include <malloc.h>
435
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000436static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000437sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000438{
439 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000440 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000441 return NULL;
442 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000443 Py_INCREF(Py_None);
444 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000445}
446#endif /* USE_MALLOPT */
447
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000448static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000449sys_getrefcount(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000450{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000451 PyObject *arg;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000452 if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000453 return NULL;
Mark Hammond440d8982000-06-20 08:12:48 +0000454 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000455}
456
Mark Hammond440d8982000-06-20 08:12:48 +0000457#ifdef Py_TRACE_REFS
458static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000459sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000460{
461 extern long _Py_RefTotal;
Mark Hammond440d8982000-06-20 08:12:48 +0000462 return PyInt_FromLong(_Py_RefTotal);
463}
464
465#endif /* Py_TRACE_REFS */
466
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000467static char getrefcount_doc[] =
468"getrefcount(object) -> integer\n\
469\n\
470Return the current reference count for the object. This includes the\n\
471temporary reference in the argument list, so it is at least 2.";
472
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000473#ifdef COUNT_ALLOCS
474static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000475sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000476{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000477 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000478
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000479 return get_counts();
480}
481#endif
482
Barry Warsawb6a54d22000-12-06 21:47:46 +0000483static char getframe_doc[] =
484"_getframe([depth]) -> frameobject\n\
485\n\
486Return a frame object from the call stack. If optional integer depth is\n\
487given, return the frame object that many calls below the top of the stack.\n\
488If that is deeper than the call stack, ValueError is raised. The default\n\
489for depth is zero, returning the frame at the top of the call stack.\n\
490\n\
491This function should be used for internal and specialized\n\
492purposes only.";
493
494static PyObject *
495sys_getframe(PyObject *self, PyObject *args)
496{
497 PyFrameObject *f = PyThreadState_Get()->frame;
498 int depth = -1;
499
500 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
501 return NULL;
502
503 while (depth > 0 && f != NULL) {
504 f = f->f_back;
505 --depth;
506 }
507 if (f == NULL) {
508 PyErr_SetString(PyExc_ValueError,
509 "call stack is not deep enough");
510 return NULL;
511 }
512 Py_INCREF(f);
513 return (PyObject*)f;
514}
515
516
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000517#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000518/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000519extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000520#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000521
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000522#ifdef DYNAMIC_EXECUTION_PROFILE
523/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000524extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000525#endif
526
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000527static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000528 /* Might as well keep this in alphabetic order */
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000529 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
530 {"exc_info", (PyCFunction)sys_exc_info, METH_NOARGS, exc_info_doc},
531 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
532 {"exit", sys_exit, METH_OLDARGS, exit_doc},
533 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000534 getdefaultencoding_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000535#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000536 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
537 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000538#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000539#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000540 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000541#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000542#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000543 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000544#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000545#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000546 {"getobjects", _Py_GetObjects, METH_VARARGS},
547 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000548#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000549 {"getrefcount", sys_getrefcount, METH_VARARGS, getrefcount_doc},
550 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000551 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000552 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000553#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000554 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000555#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000556 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000557 setdefaultencoding_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000558 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000559 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000560#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000561 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
562 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000563#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000564 {"setprofile", sys_setprofile, METH_OLDARGS, setprofile_doc},
565 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000566 setrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000567 {"settrace", sys_settrace, METH_OLDARGS, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000568 {NULL, NULL} /* sentinel */
569};
570
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000571static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000572list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000573{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000574 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000575 int i;
576 if (list == NULL)
577 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000578 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000579 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000580 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000581 if (name == NULL)
582 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000583 PyList_Append(list, name);
584 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000585 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000586 if (PyList_Sort(list) != 0) {
587 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000588 list = NULL;
589 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000590 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000591 PyObject *v = PyList_AsTuple(list);
592 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000593 list = v;
594 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000595 return list;
596}
597
Guido van Rossum23fff912000-12-15 22:02:05 +0000598static PyObject *warnoptions = NULL;
599
600void
601PySys_ResetWarnOptions(void)
602{
603 if (warnoptions == NULL || !PyList_Check(warnoptions))
604 return;
605 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
606}
607
608void
609PySys_AddWarnOption(char *s)
610{
611 PyObject *str;
612
613 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
614 Py_XDECREF(warnoptions);
615 warnoptions = PyList_New(0);
616 if (warnoptions == NULL)
617 return;
618 }
619 str = PyString_FromString(s);
620 if (str != NULL) {
621 PyList_Append(warnoptions, str);
622 Py_DECREF(str);
623 }
624}
625
Guido van Rossum40552d01998-08-06 03:34:39 +0000626/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
627 Two literals concatenated works just fine. If you have a K&R compiler
628 or other abomination that however *does* understand longer strings,
629 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000630static char sys_doc[] =
631"This module provides access to some objects used or maintained by the\n\
632interpreter and to functions that interact strongly with the interpreter.\n\
633\n\
634Dynamic objects:\n\
635\n\
636argv -- command line arguments; argv[0] is the script pathname if known\n\
637path -- module search path; path[0] is the script directory, else ''\n\
638modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000639\n\
640displayhook -- called to show results in an interactive session\n\
641excepthook -- called to handle any uncaught exception other than SystemExit\n\
642 To customize printing in an interactive session or to install a custom\n\
643 top-level exception handler, assign other functions to replace these.\n\
644\n\
645exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
646 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000647\n\
648stdin -- standard input file object; used by raw_input() and input()\n\
649stdout -- standard output file object; used by the print statement\n\
650stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000651 By assigning other file objects (or objects that behave like files)\n\
652 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000653\n\
654last_type -- type of last uncaught exception\n\
655last_value -- value of last uncaught exception\n\
656last_traceback -- traceback of last uncaught exception\n\
657 These three are only available in an interactive session after a\n\
658 traceback has been printed.\n\
659\n\
660exc_type -- type of exception currently being handled\n\
661exc_value -- value of exception currently being handled\n\
662exc_traceback -- traceback of exception currently being handled\n\
663 The function exc_info() should be used instead of these three,\n\
664 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000665"
666#ifndef MS_WIN16
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000667/* concatenating string here */
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000668"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000669Static objects:\n\
670\n\
671maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000672maxunicode -- the largest supported character\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000673builtin_module_names -- tuple of module names built into this intepreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000674version -- the version of this interpreter as a string\n\
675version_info -- version information as a tuple\n\
676hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000677copyright -- copyright notice pertaining to this interpreter\n\
678platform -- platform identifier\n\
679executable -- pathname of this Python interpreter\n\
680prefix -- prefix used to find the Python library\n\
681exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000682"
683#ifdef MS_WINDOWS
684/* concatenating string here */
685"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000686winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000687"
688#endif /* MS_WINDOWS */
689"__stdin__ -- the original stdin; don't touch!\n\
690__stdout__ -- the original stdout; don't touch!\n\
691__stderr__ -- the original stderr; don't touch!\n\
692__displayhook__ -- the original displayhook; don't touch!\n\
693__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000694\n\
695Functions:\n\
696\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000697displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000698excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000699exc_info() -- return thread-safe information about the current exception\n\
700exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000701getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000702getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000703getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000704setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000705setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000706setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000707setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000708settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000709"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000710#endif /* MS_WIN16 */
Fred Drakeccede592000-08-14 20:59:57 +0000711/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000712
Guido van Rossum25ce5661997-08-02 03:10:38 +0000713PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000714_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000716 PyObject *m, *v, *sysdict;
717 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000718 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000719
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000720 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000721 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000722
723 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
724 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
725 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000726 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000727 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000728 PyDict_SetItemString(sysdict, "stdin", sysin);
729 PyDict_SetItemString(sysdict, "stdout", sysout);
730 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000731 /* Make backup copies for cleanup */
732 PyDict_SetItemString(sysdict, "__stdin__", sysin);
733 PyDict_SetItemString(sysdict, "__stdout__", sysout);
734 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000735 PyDict_SetItemString(sysdict, "__displayhook__",
736 PyDict_GetItemString(sysdict, "displayhook"));
737 PyDict_SetItemString(sysdict, "__excepthook__",
738 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000739 Py_XDECREF(sysin);
740 Py_XDECREF(sysout);
741 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000742 PyDict_SetItemString(sysdict, "version",
743 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000744 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000745 PyDict_SetItemString(sysdict, "hexversion",
746 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000747 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000748 /*
749 * These release level checks are mutually exclusive and cover
750 * the field, so don't get too fancy with the pre-processor!
751 */
752#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000753 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000754#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000755 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000756#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000757 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000758#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000759 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000760#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000761 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000762 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000763 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000764 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000765 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000766 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000767 PyDict_SetItemString(sysdict, "copyright",
768 v = PyString_FromString(Py_GetCopyright()));
769 Py_XDECREF(v);
770 PyDict_SetItemString(sysdict, "platform",
771 v = PyString_FromString(Py_GetPlatform()));
772 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000773 PyDict_SetItemString(sysdict, "executable",
774 v = PyString_FromString(Py_GetProgramFullPath()));
775 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000776 PyDict_SetItemString(sysdict, "prefix",
777 v = PyString_FromString(Py_GetPrefix()));
778 Py_XDECREF(v);
779 PyDict_SetItemString(sysdict, "exec_prefix",
780 v = PyString_FromString(Py_GetExecPrefix()));
781 Py_XDECREF(v);
782 PyDict_SetItemString(sysdict, "maxint",
783 v = PyInt_FromLong(PyInt_GetMax()));
784 Py_XDECREF(v);
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000785 PyDict_SetItemString(sysdict, "maxunicode",
786 v = PyInt_FromLong(PyUnicode_GetMax()));
787 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000788 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000789 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000790 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000791 {
792 /* Assumes that longs are at least 2 bytes long.
793 Should be safe! */
794 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000795 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000796
797 s = (char *) &number;
798 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000799 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000800 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000801 value = "little";
802 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000803 v = PyString_FromString(value));
804 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000805 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000806#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000807 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000808 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000809 Py_XDECREF(v);
810 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000811 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000812 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000813#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000814 if (warnoptions == NULL) {
815 warnoptions = PyList_New(0);
816 }
817 else {
818 Py_INCREF(warnoptions);
819 }
820 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000821 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000822 }
823
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000824 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000825 return NULL;
826 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000827}
828
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000829static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000830makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000831{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000832 int i, n;
833 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000834 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000835
836 n = 1;
837 p = path;
838 while ((p = strchr(p, delim)) != NULL) {
839 n++;
840 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000841 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000842 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000843 if (v == NULL)
844 return NULL;
845 for (i = 0; ; i++) {
846 p = strchr(path, delim);
847 if (p == NULL)
848 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000849 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000850 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000851 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000852 return NULL;
853 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000854 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000855 if (*p == '\0')
856 break;
857 path = p+1;
858 }
859 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
862void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000863PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000864{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000865 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000866 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000867 Py_FatalError("can't create sys.path");
868 if (PySys_SetObject("path", v) != 0)
869 Py_FatalError("can't assign sys.path");
870 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000871}
872
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000873static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000874makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000875{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000876 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000877 if (argc <= 0 || argv == NULL) {
878 /* Ensure at least one (empty) argument is seen */
879 static char *empty_argv[1] = {""};
880 argv = empty_argv;
881 argc = 1;
882 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000883 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000884 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000885 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000886 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000887 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000888 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000889 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000890 av = NULL;
891 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000892 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000893 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000894 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000895 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000896 return av;
897}
898
899void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000900PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000901{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000902 PyObject *av = makeargvobject(argc, argv);
903 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000904 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000905 Py_FatalError("no mem for sys.argv");
906 if (PySys_SetObject("argv", av) != 0)
907 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000908 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000909 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000910 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000911 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000912 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000913#ifdef HAVE_READLINK
914 char link[MAXPATHLEN+1];
915 char argv0copy[2*MAXPATHLEN+1];
916 int nr = 0;
917 if (argc > 0 && argv0 != NULL)
918 nr = readlink(argv0, link, MAXPATHLEN);
919 if (nr > 0) {
920 /* It's a symlink */
921 link[nr] = '\0';
922 if (link[0] == SEP)
923 argv0 = link; /* Link to absolute path */
924 else if (strchr(link, SEP) == NULL)
925 ; /* Link without path */
926 else {
927 /* Must join(dirname(argv0), link) */
928 char *q = strrchr(argv0, SEP);
929 if (q == NULL)
930 argv0 = link; /* argv0 without path */
931 else {
932 /* Must make a copy */
933 strcpy(argv0copy, argv0);
934 q = strrchr(argv0copy, SEP);
935 strcpy(q+1, link);
936 argv0 = argv0copy;
937 }
938 }
939 }
940#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000941#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000942 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000943 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000944 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000945 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000946 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000947 if (q != NULL)
948 p = q;
949 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000950 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000951 if (n > 1 && p[-1] != ':')
952 n--; /* Drop trailing separator */
953 }
954 }
955#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000956 if (argc > 0 && argv0 != NULL)
957 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000958 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +0000959#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +0000960 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +0000961#else /* don't include trailing separator */
962 n = p - argv0;
963#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +0000964#if SEP == '/' /* Special case for Unix filename syntax */
965 if (n > 1)
966 n--; /* Drop trailing separator */
967#endif /* Unix */
968 }
969#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000970 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000971 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000972 Py_FatalError("no mem for sys.path insertion");
973 if (PyList_Insert(path, 0, a) < 0)
974 Py_FatalError("sys.path.insert(0) failed");
975 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000976 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000977 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000978}
Guido van Rossuma890e681998-05-12 14:59:24 +0000979
980
981/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
982 Adapted from code submitted by Just van Rossum.
983
984 PySys_WriteStdout(format, ...)
985 PySys_WriteStderr(format, ...)
986
987 The first function writes to sys.stdout; the second to sys.stderr. When
988 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000989 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000990
991 Both take a printf-style format string as their first argument followed
992 by a variable length argument list determined by the format string.
993
994 *** WARNING ***
995
996 The format should limit the total size of the formatted output string to
997 1000 bytes. In particular, this means that no unrestricted "%s" formats
998 should occur; these should be limited using "%.<N>s where <N> is a
999 decimal number calculated so that <N> plus the maximum size of other
1000 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1001 which can print hundreds of digits for very large numbers.
1002
1003 */
1004
1005static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001006mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001007{
1008 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001009 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001010
Guido van Rossum8442af31998-10-12 18:22:10 +00001011 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001012 file = PySys_GetObject(name);
1013 if (file == NULL || PyFile_AsFile(file) == fp)
1014 vfprintf(fp, format, va);
1015 else {
1016 char buffer[1001];
Guido van Rossum8442af31998-10-12 18:22:10 +00001017 if (vsprintf(buffer, format, va) >= sizeof(buffer))
1018 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
Guido van Rossuma890e681998-05-12 14:59:24 +00001019 if (PyFile_WriteString(buffer, file) != 0) {
1020 PyErr_Clear();
1021 fputs(buffer, fp);
1022 }
1023 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001024 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001025}
1026
1027void
Guido van Rossuma890e681998-05-12 14:59:24 +00001028PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001029{
1030 va_list va;
1031
Guido van Rossuma890e681998-05-12 14:59:24 +00001032 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001033 mywrite("stdout", stdout, format, va);
1034 va_end(va);
1035}
1036
1037void
Guido van Rossuma890e681998-05-12 14:59:24 +00001038PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001039{
1040 va_list va;
1041
Guido van Rossuma890e681998-05-12 14:59:24 +00001042 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001043 mywrite("stderr", stderr, format, va);
1044 va_end(va);
1045}