blob: 581a19baae0512618d06a1d7f944d5e3cf5487af [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* System module */
3
4/*
5Various bits of information used by the interpreter are collected in
6module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +00007Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +00008- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009Data members:
10- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000012- path: module search path (list of strings)
13- argv: script arguments (list of strings)
14- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015*/
16
Guido van Rossum65bf9f21997-04-29 18:33:38 +000017#include "Python.h"
Barry Warsawb6a54d22000-12-06 21:47:46 +000018#include "compile.h"
19#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossume2437a11992-03-23 18:20:18 +000021#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
Guido van Rossum9b38a141996-09-11 23:12:24 +000023#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000024extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000025/* A string loaded from the DLL at startup: */
26extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000027#endif
28
Guido van Rossum65bf9f21997-04-29 18:33:38 +000029PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
Guido van Rossum25ce5661997-08-02 03:10:38 +000032 PyThreadState *tstate = PyThreadState_Get();
33 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000034 if (sd == NULL)
35 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000036 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037}
38
39FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000040PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041{
42 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000043 PyObject *v = PySys_GetObject(name);
44 if (v != NULL && PyFile_Check(v))
45 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 if (fp == NULL)
47 fp = def;
48 return fp;
49}
50
51int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000052PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053{
Guido van Rossum25ce5661997-08-02 03:10:38 +000054 PyThreadState *tstate = PyThreadState_Get();
55 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000056 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000057 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000058 return 0;
59 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000060 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000061 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000063 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064}
65
Guido van Rossum65bf9f21997-04-29 18:33:38 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000068{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000069 PyObject *outf;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000070 PyInterpreterState *interp = PyThreadState_Get()->interp;
71 PyObject *modules = interp->modules;
72 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
73
Moshe Zadka03897ea2001-07-23 13:32:43 +000074 if (builtins == NULL) {
75 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
76 return NULL;
77 }
78
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000079 /* Print value except if None */
80 /* After printing, also assign to '_' */
81 /* Before, set '_' to None to avoid recursion */
82 if (o == Py_None) {
83 Py_INCREF(Py_None);
84 return Py_None;
85 }
86 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
87 return NULL;
88 if (Py_FlushLine() != 0)
89 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000090 outf = PySys_GetObject("stdout");
91 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000092 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
93 return NULL;
94 }
Greg Steinceb9b7c2001-01-11 09:27:34 +000095 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000096 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +000097 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000098 if (Py_FlushLine() != 0)
99 return NULL;
100 if (PyObject_SetAttrString(builtins, "_", o) != 0)
101 return NULL;
102 Py_INCREF(Py_None);
103 return Py_None;
104}
105
106static char displayhook_doc[] =
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000107"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000108"\n"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000109"Print an object to sys.stdout and also save it in __builtin__._\n";
110
111static PyObject *
112sys_excepthook(PyObject* self, PyObject* args)
113{
114 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000115 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000116 return NULL;
117 PyErr_Display(exc, value, tb);
118 Py_INCREF(Py_None);
119 return Py_None;
120}
121
122static char excepthook_doc[] =
123"excepthook(exctype, value, traceback) -> None\n"
124"\n"
125"Handle an exception by displaying it with a traceback on sys.stderr.\n";
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000126
127static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000128sys_exc_info(PyObject *self)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000129{
130 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131 tstate = PyThreadState_Get();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132 return Py_BuildValue(
133 "(OOO)",
134 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
135 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
136 tstate->exc_traceback != NULL ?
137 tstate->exc_traceback : Py_None);
138}
139
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000140static char exc_info_doc[] =
141"exc_info() -> (type, value, traceback)\n\
142\n\
143Return information about the exception that is currently being handled.\n\
144This should be called from inside an except clause only.";
145
Guido van Rossuma027efa1997-05-05 20:56:21 +0000146static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000147sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000149 /* Raise SystemExit so callers may catch it or clean up. */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000150 PyErr_SetObject(PyExc_SystemExit, args);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000151 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152}
153
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000154static char exit_doc[] =
155"exit([status])\n\
156\n\
157Exit the interpreter by raising SystemExit(status).\n\
158If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000159If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000160If it is another kind of object, it will be printed and the system\n\
161exit status will be one (i.e., failure).";
162
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000163#ifdef Py_USING_UNICODE
164
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000165static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000166sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000167{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000168 return PyString_FromString(PyUnicode_GetDefaultEncoding());
169}
170
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000171static char getdefaultencoding_doc[] =
172"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000173\n\
174Return the current default string encoding used by the Unicode \n\
175implementation.";
176
177static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000178sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000179{
180 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000181 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000182 return NULL;
183 if (PyUnicode_SetDefaultEncoding(encoding))
184 return NULL;
185 Py_INCREF(Py_None);
186 return Py_None;
187}
188
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000189static char setdefaultencoding_doc[] =
190"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000191\n\
192Set the current default string encoding used by the Unicode implementation.";
193
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000194#endif
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
Fred Drake8f51f542001-10-04 14:48:42 +0000259 if (arg == NULL)
260 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000261 result = call_trampoline(tstate, self, frame, what, arg);
262 if (result == NULL) {
263 PyEval_SetProfile(NULL, NULL);
264 return -1;
265 }
266 Py_DECREF(result);
267 return 0;
268}
269
270static int
271trace_trampoline(PyObject *self, PyFrameObject *frame,
272 int what, PyObject *arg)
273{
274 PyThreadState *tstate = frame->f_tstate;
275 PyObject *callback;
276 PyObject *result;
277
278 if (what == PyTrace_CALL)
279 callback = self;
280 else
281 callback = frame->f_trace;
282 if (callback == NULL)
283 return 0;
284 result = call_trampoline(tstate, callback, frame, what, arg);
285 if (result == NULL) {
286 PyEval_SetTrace(NULL, NULL);
287 Py_XDECREF(frame->f_trace);
288 frame->f_trace = NULL;
289 return -1;
290 }
291 if (result != Py_None) {
292 PyObject *temp = frame->f_trace;
293 frame->f_trace = NULL;
294 Py_XDECREF(temp);
295 frame->f_trace = result;
296 }
297 else {
298 Py_DECREF(result);
299 }
300 return 0;
301}
Fred Draked0838392001-06-16 21:02:31 +0000302
Fred Drake8b4d01d2000-05-09 19:57:01 +0000303static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000305{
Fred Drake5755ce62001-06-27 19:19:46 +0000306 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000307 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000308 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000309 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000310 else
Fred Drake5755ce62001-06-27 19:19:46 +0000311 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000312 Py_INCREF(Py_None);
313 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000314}
315
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000316static char settrace_doc[] =
317"settrace(function)\n\
318\n\
319Set the global debug tracing function. It will be called on each\n\
320function call. See the debugger chapter in the library manual.";
321
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000322static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000323sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000324{
Fred Drake5755ce62001-06-27 19:19:46 +0000325 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000326 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000327 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000328 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000329 else
Fred Drake5755ce62001-06-27 19:19:46 +0000330 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000331 Py_INCREF(Py_None);
332 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000333}
334
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000335static char setprofile_doc[] =
336"setprofile(function)\n\
337\n\
338Set the profiling function. It will be called on each function call\n\
339and return. See the profiler chapter in the library manual.";
340
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000341static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000342sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000343{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000344 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000345 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000346 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000347 Py_INCREF(Py_None);
348 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000349}
350
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000351static char setcheckinterval_doc[] =
352"setcheckinterval(n)\n\
353\n\
354Tell the Python interpreter to check for asynchronous events every\n\
355n instructions. This also affects how often thread switches occur.";
356
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000357static PyObject *
358sys_setrecursionlimit(PyObject *self, PyObject *args)
359{
360 int new_limit;
361 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
362 return NULL;
363 if (new_limit <= 0) {
364 PyErr_SetString(PyExc_ValueError,
365 "recursion limit must be positive");
366 return NULL;
367 }
368 Py_SetRecursionLimit(new_limit);
369 Py_INCREF(Py_None);
370 return Py_None;
371}
372
373static char setrecursionlimit_doc[] =
374"setrecursionlimit(n)\n\
375\n\
376Set the maximum depth of the Python interpreter stack to n. This\n\
377limit prevents infinite recursion from causing an overflow of the C\n\
378stack and crashing Python. The highest possible limit is platform-\n\
379dependent.";
380
381static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000382sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000383{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000384 return PyInt_FromLong(Py_GetRecursionLimit());
385}
386
387static char getrecursionlimit_doc[] =
388"getrecursionlimit()\n\
389\n\
390Return the current value of the recursion limit, the maximum depth\n\
391of the Python interpreter stack. This limit prevents infinite\n\
392recursion from causing an overflow of the C stack and crashing Python.";
393
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000394#ifdef HAVE_DLOPEN
395static PyObject *
396sys_setdlopenflags(PyObject *self, PyObject *args)
397{
398 int new_val;
399 PyThreadState *tstate = PyThreadState_Get();
400 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
401 return NULL;
402 if (!tstate)
403 return NULL;
404 tstate->interp->dlopenflags = new_val;
405 Py_INCREF(Py_None);
406 return Py_None;
407}
408
409static char setdlopenflags_doc[] =
410"setdlopenflags(n) -> None\n\
411\n\
412Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000413things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000414a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000415To share symbols across extension modules, call as\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000416sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)";
417
418static PyObject *
419sys_getdlopenflags(PyObject *self, PyObject *args)
420{
421 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000422 if (!tstate)
423 return NULL;
424 return PyInt_FromLong(tstate->interp->dlopenflags);
425}
426
427static char getdlopenflags_doc[] =
428"getdlopenflags() -> int\n\
429\n\
430Return the current value of the flags that are used for dlopen()\n\
431calls. The flag constants are defined in the dl module.";
432#endif
433
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000434#ifdef USE_MALLOPT
435/* Link with -lmalloc (or -lmpc) on an SGI */
436#include <malloc.h>
437
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000438static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000439sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000440{
441 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000442 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000443 return NULL;
444 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000445 Py_INCREF(Py_None);
446 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000447}
448#endif /* USE_MALLOPT */
449
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000450static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000451sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000452{
Mark Hammond440d8982000-06-20 08:12:48 +0000453 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000454}
455
Mark Hammond440d8982000-06-20 08:12:48 +0000456#ifdef Py_TRACE_REFS
457static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000458sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000459{
460 extern long _Py_RefTotal;
Mark Hammond440d8982000-06-20 08:12:48 +0000461 return PyInt_FromLong(_Py_RefTotal);
462}
463
464#endif /* Py_TRACE_REFS */
465
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000466static char getrefcount_doc[] =
467"getrefcount(object) -> integer\n\
468\n\
469Return the current reference count for the object. This includes the\n\
470temporary reference in the argument list, so it is at least 2.";
471
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000472#ifdef COUNT_ALLOCS
473static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000474sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000475{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000476 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000477
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000478 return get_counts();
479}
480#endif
481
Barry Warsawb6a54d22000-12-06 21:47:46 +0000482static char getframe_doc[] =
483"_getframe([depth]) -> frameobject\n\
484\n\
485Return a frame object from the call stack. If optional integer depth is\n\
486given, return the frame object that many calls below the top of the stack.\n\
487If that is deeper than the call stack, ValueError is raised. The default\n\
488for depth is zero, returning the frame at the top of the call stack.\n\
489\n\
490This function should be used for internal and specialized\n\
491purposes only.";
492
493static PyObject *
494sys_getframe(PyObject *self, PyObject *args)
495{
496 PyFrameObject *f = PyThreadState_Get()->frame;
497 int depth = -1;
498
499 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
500 return NULL;
501
502 while (depth > 0 && f != NULL) {
503 f = f->f_back;
504 --depth;
505 }
506 if (f == NULL) {
507 PyErr_SetString(PyExc_ValueError,
508 "call stack is not deep enough");
509 return NULL;
510 }
511 Py_INCREF(f);
512 return (PyObject*)f;
513}
514
515
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000516#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000517/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000518extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000519#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000520
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000521#ifdef DYNAMIC_EXECUTION_PROFILE
522/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000523extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000524#endif
525
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000526static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000527 /* Might as well keep this in alphabetic order */
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000528 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
529 {"exc_info", (PyCFunction)sys_exc_info, METH_NOARGS, exc_info_doc},
530 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
531 {"exit", sys_exit, METH_OLDARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000532#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000533 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000534 getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000535#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000536#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000537 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
538 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000539#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000540#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000541 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000542#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000543#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000544 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000545#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000546#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000547 {"getobjects", _Py_GetObjects, METH_VARARGS},
548 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000549#endif
Fred Drakea7688822001-10-24 20:47:48 +0000550 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000551 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000552 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000553 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000554#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000555 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000556#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000557#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000558 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000559 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000560#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000561 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000562 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000563#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000564 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
565 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000566#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000567 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000568 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000569 setrecursionlimit_doc},
Neal Norwitz290d31e2002-03-03 15:12:58 +0000570 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000571 {NULL, NULL} /* sentinel */
572};
573
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000574static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000575list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000576{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000577 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000578 int i;
579 if (list == NULL)
580 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000581 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000582 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000583 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000584 if (name == NULL)
585 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000586 PyList_Append(list, name);
587 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000588 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000589 if (PyList_Sort(list) != 0) {
590 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000591 list = NULL;
592 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000593 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000594 PyObject *v = PyList_AsTuple(list);
595 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000596 list = v;
597 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000598 return list;
599}
600
Guido van Rossum23fff912000-12-15 22:02:05 +0000601static PyObject *warnoptions = NULL;
602
603void
604PySys_ResetWarnOptions(void)
605{
606 if (warnoptions == NULL || !PyList_Check(warnoptions))
607 return;
608 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
609}
610
611void
612PySys_AddWarnOption(char *s)
613{
614 PyObject *str;
615
616 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
617 Py_XDECREF(warnoptions);
618 warnoptions = PyList_New(0);
619 if (warnoptions == NULL)
620 return;
621 }
622 str = PyString_FromString(s);
623 if (str != NULL) {
624 PyList_Append(warnoptions, str);
625 Py_DECREF(str);
626 }
627}
628
Guido van Rossum40552d01998-08-06 03:34:39 +0000629/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
630 Two literals concatenated works just fine. If you have a K&R compiler
631 or other abomination that however *does* understand longer strings,
632 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000633static char sys_doc[] =
634"This module provides access to some objects used or maintained by the\n\
635interpreter and to functions that interact strongly with the interpreter.\n\
636\n\
637Dynamic objects:\n\
638\n\
639argv -- command line arguments; argv[0] is the script pathname if known\n\
640path -- module search path; path[0] is the script directory, else ''\n\
641modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000642\n\
643displayhook -- called to show results in an interactive session\n\
644excepthook -- called to handle any uncaught exception other than SystemExit\n\
645 To customize printing in an interactive session or to install a custom\n\
646 top-level exception handler, assign other functions to replace these.\n\
647\n\
648exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
649 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000650\n\
651stdin -- standard input file object; used by raw_input() and input()\n\
652stdout -- standard output file object; used by the print statement\n\
653stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000654 By assigning other file objects (or objects that behave like files)\n\
655 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000656\n\
657last_type -- type of last uncaught exception\n\
658last_value -- value of last uncaught exception\n\
659last_traceback -- traceback of last uncaught exception\n\
660 These three are only available in an interactive session after a\n\
661 traceback has been printed.\n\
662\n\
663exc_type -- type of exception currently being handled\n\
664exc_value -- value of exception currently being handled\n\
665exc_traceback -- traceback of exception currently being handled\n\
666 The function exc_info() should be used instead of these three,\n\
667 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000668"
669#ifndef MS_WIN16
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000670/* concatenating string here */
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000671"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000672Static objects:\n\
673\n\
674maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000675maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000676builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000677version -- the version of this interpreter as a string\n\
678version_info -- version information as a tuple\n\
679hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000680copyright -- copyright notice pertaining to this interpreter\n\
681platform -- platform identifier\n\
682executable -- pathname of this Python interpreter\n\
683prefix -- prefix used to find the Python library\n\
684exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000685"
686#ifdef MS_WINDOWS
687/* concatenating string here */
688"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000689winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000690"
691#endif /* MS_WINDOWS */
692"__stdin__ -- the original stdin; don't touch!\n\
693__stdout__ -- the original stdout; don't touch!\n\
694__stderr__ -- the original stderr; don't touch!\n\
695__displayhook__ -- the original displayhook; don't touch!\n\
696__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000697\n\
698Functions:\n\
699\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000700displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000701excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000702exc_info() -- return thread-safe information about the current exception\n\
703exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000704getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000705getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000706getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000707setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000708setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000709setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000710setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000711settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000712"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000713#endif /* MS_WIN16 */
Fred Drakeccede592000-08-14 20:59:57 +0000714/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000715
Guido van Rossum25ce5661997-08-02 03:10:38 +0000716PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000717_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000719 PyObject *m, *v, *sysdict;
720 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000721 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000722
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000723 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000724 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000725
726 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
727 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
728 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000729 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000730 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000731 PyDict_SetItemString(sysdict, "stdin", sysin);
732 PyDict_SetItemString(sysdict, "stdout", sysout);
733 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000734 /* Make backup copies for cleanup */
735 PyDict_SetItemString(sysdict, "__stdin__", sysin);
736 PyDict_SetItemString(sysdict, "__stdout__", sysout);
737 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000738 PyDict_SetItemString(sysdict, "__displayhook__",
739 PyDict_GetItemString(sysdict, "displayhook"));
740 PyDict_SetItemString(sysdict, "__excepthook__",
741 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000742 Py_XDECREF(sysin);
743 Py_XDECREF(sysout);
744 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000745 PyDict_SetItemString(sysdict, "version",
746 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000747 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000748 PyDict_SetItemString(sysdict, "hexversion",
749 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000750 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000751 /*
752 * These release level checks are mutually exclusive and cover
753 * the field, so don't get too fancy with the pre-processor!
754 */
755#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000756 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000757#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000758 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000759#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000760 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000761#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000762 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000763#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000764 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000765 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000766 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000767 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000768 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000769 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000770 PyDict_SetItemString(sysdict, "copyright",
771 v = PyString_FromString(Py_GetCopyright()));
772 Py_XDECREF(v);
773 PyDict_SetItemString(sysdict, "platform",
774 v = PyString_FromString(Py_GetPlatform()));
775 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000776 PyDict_SetItemString(sysdict, "executable",
777 v = PyString_FromString(Py_GetProgramFullPath()));
778 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000779 PyDict_SetItemString(sysdict, "prefix",
780 v = PyString_FromString(Py_GetPrefix()));
781 Py_XDECREF(v);
782 PyDict_SetItemString(sysdict, "exec_prefix",
783 v = PyString_FromString(Py_GetExecPrefix()));
784 Py_XDECREF(v);
785 PyDict_SetItemString(sysdict, "maxint",
786 v = PyInt_FromLong(PyInt_GetMax()));
787 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000788#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000789 PyDict_SetItemString(sysdict, "maxunicode",
790 v = PyInt_FromLong(PyUnicode_GetMax()));
791 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000792#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000793 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000794 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000795 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000796 {
797 /* Assumes that longs are at least 2 bytes long.
798 Should be safe! */
799 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000800 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000801
802 s = (char *) &number;
803 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000804 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000805 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000806 value = "little";
807 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000808 v = PyString_FromString(value));
809 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000810 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000811#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000812 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000813 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000814 Py_XDECREF(v);
815 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000816 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000817 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000818#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000819 if (warnoptions == NULL) {
820 warnoptions = PyList_New(0);
821 }
822 else {
823 Py_INCREF(warnoptions);
824 }
825 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000826 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000827 }
828
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000829 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000830 return NULL;
831 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000832}
833
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000834static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000835makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000836{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000837 int i, n;
838 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000839 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000840
841 n = 1;
842 p = path;
843 while ((p = strchr(p, delim)) != NULL) {
844 n++;
845 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000846 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000847 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000848 if (v == NULL)
849 return NULL;
850 for (i = 0; ; i++) {
851 p = strchr(path, delim);
852 if (p == NULL)
853 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000854 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000855 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000856 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000857 return NULL;
858 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000859 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000860 if (*p == '\0')
861 break;
862 path = p+1;
863 }
864 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
867void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000868PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000870 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000871 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000872 Py_FatalError("can't create sys.path");
873 if (PySys_SetObject("path", v) != 0)
874 Py_FatalError("can't assign sys.path");
875 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000876}
877
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000878static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000879makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000880{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000881 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000882 if (argc <= 0 || argv == NULL) {
883 /* Ensure at least one (empty) argument is seen */
884 static char *empty_argv[1] = {""};
885 argv = empty_argv;
886 argc = 1;
887 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000888 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000889 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000890 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000891 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000892 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000893 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000894 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000895 av = NULL;
896 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000897 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000898 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000899 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000900 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000901 return av;
902}
903
904void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000905PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000906{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000907 PyObject *av = makeargvobject(argc, argv);
908 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000909 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000910 Py_FatalError("no mem for sys.argv");
911 if (PySys_SetObject("argv", av) != 0)
912 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000913 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000914 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000915 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000916 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000917 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000918#ifdef HAVE_READLINK
919 char link[MAXPATHLEN+1];
920 char argv0copy[2*MAXPATHLEN+1];
921 int nr = 0;
922 if (argc > 0 && argv0 != NULL)
923 nr = readlink(argv0, link, MAXPATHLEN);
924 if (nr > 0) {
925 /* It's a symlink */
926 link[nr] = '\0';
927 if (link[0] == SEP)
928 argv0 = link; /* Link to absolute path */
929 else if (strchr(link, SEP) == NULL)
930 ; /* Link without path */
931 else {
932 /* Must join(dirname(argv0), link) */
933 char *q = strrchr(argv0, SEP);
934 if (q == NULL)
935 argv0 = link; /* argv0 without path */
936 else {
937 /* Must make a copy */
938 strcpy(argv0copy, argv0);
939 q = strrchr(argv0copy, SEP);
940 strcpy(q+1, link);
941 argv0 = argv0copy;
942 }
943 }
944 }
945#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000946#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000947 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000948 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000949 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000950 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000951 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000952 if (q != NULL)
953 p = q;
954 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000955 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000956 if (n > 1 && p[-1] != ':')
957 n--; /* Drop trailing separator */
958 }
959 }
960#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000961 if (argc > 0 && argv0 != NULL)
962 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000963 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +0000964#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +0000965 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +0000966#else /* don't include trailing separator */
967 n = p - argv0;
968#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +0000969#if SEP == '/' /* Special case for Unix filename syntax */
970 if (n > 1)
971 n--; /* Drop trailing separator */
972#endif /* Unix */
973 }
974#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000975 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000976 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000977 Py_FatalError("no mem for sys.path insertion");
978 if (PyList_Insert(path, 0, a) < 0)
979 Py_FatalError("sys.path.insert(0) failed");
980 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000981 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000982 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000983}
Guido van Rossuma890e681998-05-12 14:59:24 +0000984
985
986/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
987 Adapted from code submitted by Just van Rossum.
988
989 PySys_WriteStdout(format, ...)
990 PySys_WriteStderr(format, ...)
991
992 The first function writes to sys.stdout; the second to sys.stderr. When
993 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000994 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000995
996 Both take a printf-style format string as their first argument followed
997 by a variable length argument list determined by the format string.
998
999 *** WARNING ***
1000
1001 The format should limit the total size of the formatted output string to
1002 1000 bytes. In particular, this means that no unrestricted "%s" formats
1003 should occur; these should be limited using "%.<N>s where <N> is a
1004 decimal number calculated so that <N> plus the maximum size of other
1005 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1006 which can print hundreds of digits for very large numbers.
1007
1008 */
1009
1010static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001011mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001012{
1013 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001014 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001015
Guido van Rossum8442af31998-10-12 18:22:10 +00001016 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001017 file = PySys_GetObject(name);
1018 if (file == NULL || PyFile_AsFile(file) == fp)
1019 vfprintf(fp, format, va);
1020 else {
1021 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001022 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1023 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001024 if (PyFile_WriteString(buffer, file) != 0) {
1025 PyErr_Clear();
1026 fputs(buffer, fp);
1027 }
Tim Petersfaad5ad2001-12-03 00:43:33 +00001028 if (written < 0 || written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001029 const char *truncated = "... truncated";
1030 if (PyFile_WriteString(truncated, file) != 0) {
1031 PyErr_Clear();
1032 fputs(truncated, fp);
1033 }
1034 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001035 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001036 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001037}
1038
1039void
Guido van Rossuma890e681998-05-12 14:59:24 +00001040PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001041{
1042 va_list va;
1043
Guido van Rossuma890e681998-05-12 14:59:24 +00001044 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001045 mywrite("stdout", stdout, format, va);
1046 va_end(va);
1047}
1048
1049void
Guido van Rossuma890e681998-05-12 14:59:24 +00001050PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001051{
1052 va_list va;
1053
Guido van Rossuma890e681998-05-12 14:59:24 +00001054 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001055 mywrite("stderr", stderr, format, va);
1056 va_end(va);
1057}