blob: 0469c7f654f60b2483d547e6449459444b6e648d [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{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000149 PyObject *exit_code = 0;
150 if (!PyArg_ParseTuple(args, "|O:exit", &exit_code))
151 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000152 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000153 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000154 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155}
156
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000157static char exit_doc[] =
158"exit([status])\n\
159\n\
160Exit the interpreter by raising SystemExit(status).\n\
161If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000162If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000163If it is another kind of object, it will be printed and the system\n\
164exit status will be one (i.e., failure).";
165
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000166#ifdef Py_USING_UNICODE
167
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000168static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000169sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000170{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000171 return PyString_FromString(PyUnicode_GetDefaultEncoding());
172}
173
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000174static char getdefaultencoding_doc[] =
175"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000176\n\
177Return the current default string encoding used by the Unicode \n\
178implementation.";
179
180static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000181sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000182{
183 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000184 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000185 return NULL;
186 if (PyUnicode_SetDefaultEncoding(encoding))
187 return NULL;
188 Py_INCREF(Py_None);
189 return Py_None;
190}
191
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000192static char setdefaultencoding_doc[] =
193"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000194\n\
195Set the current default string encoding used by the Unicode implementation.";
196
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000197#endif
198
Fred Drake5755ce62001-06-27 19:19:46 +0000199/*
200 * Cached interned string objects used for calling the profile and
201 * trace functions. Initialized by trace_init().
202 */
203static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL};
204
205static int
206trace_init(void)
207{
208 static char *whatnames[4] = {"call", "exception", "line", "return"};
209 PyObject *name;
210 int i;
211 for (i = 0; i < 4; ++i) {
212 if (whatstrings[i] == NULL) {
213 name = PyString_InternFromString(whatnames[i]);
214 if (name == NULL)
215 return -1;
216 whatstrings[i] = name;
217 }
218 }
219 return 0;
220}
221
222
223static PyObject *
224call_trampoline(PyThreadState *tstate, PyObject* callback,
225 PyFrameObject *frame, int what, PyObject *arg)
226{
227 PyObject *args = PyTuple_New(3);
228 PyObject *whatstr;
229 PyObject *result;
230
231 if (args == NULL)
232 return NULL;
233 Py_INCREF(frame);
234 whatstr = whatstrings[what];
235 Py_INCREF(whatstr);
236 if (arg == NULL)
237 arg = Py_None;
238 Py_INCREF(arg);
239 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
240 PyTuple_SET_ITEM(args, 1, whatstr);
241 PyTuple_SET_ITEM(args, 2, arg);
242
243 /* call the Python-level function */
244 PyFrame_FastToLocals(frame);
245 result = PyEval_CallObject(callback, args);
246 PyFrame_LocalsToFast(frame, 1);
247 if (result == NULL)
248 PyTraceBack_Here(frame);
249
250 /* cleanup */
251 Py_DECREF(args);
252 return result;
253}
254
255static int
256profile_trampoline(PyObject *self, PyFrameObject *frame,
257 int what, PyObject *arg)
258{
259 PyThreadState *tstate = frame->f_tstate;
260 PyObject *result;
261
Fred Drake8f51f542001-10-04 14:48:42 +0000262 if (arg == NULL)
263 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000264 result = call_trampoline(tstate, self, frame, what, arg);
265 if (result == NULL) {
266 PyEval_SetProfile(NULL, NULL);
267 return -1;
268 }
269 Py_DECREF(result);
270 return 0;
271}
272
273static int
274trace_trampoline(PyObject *self, PyFrameObject *frame,
275 int what, PyObject *arg)
276{
277 PyThreadState *tstate = frame->f_tstate;
278 PyObject *callback;
279 PyObject *result;
280
281 if (what == PyTrace_CALL)
282 callback = self;
283 else
284 callback = frame->f_trace;
285 if (callback == NULL)
286 return 0;
287 result = call_trampoline(tstate, callback, frame, what, arg);
288 if (result == NULL) {
289 PyEval_SetTrace(NULL, NULL);
290 Py_XDECREF(frame->f_trace);
291 frame->f_trace = NULL;
292 return -1;
293 }
294 if (result != Py_None) {
295 PyObject *temp = frame->f_trace;
296 frame->f_trace = NULL;
297 Py_XDECREF(temp);
298 frame->f_trace = result;
299 }
300 else {
301 Py_DECREF(result);
302 }
303 return 0;
304}
Fred Draked0838392001-06-16 21:02:31 +0000305
Fred Drake8b4d01d2000-05-09 19:57:01 +0000306static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000308{
Fred Drake5755ce62001-06-27 19:19:46 +0000309 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000310 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000311 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000312 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000313 else
Fred Drake5755ce62001-06-27 19:19:46 +0000314 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000315 Py_INCREF(Py_None);
316 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000317}
318
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000319static char settrace_doc[] =
320"settrace(function)\n\
321\n\
322Set the global debug tracing function. It will be called on each\n\
323function call. See the debugger chapter in the library manual.";
324
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000325static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000327{
Fred Drake5755ce62001-06-27 19:19:46 +0000328 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000329 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000330 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000331 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000332 else
Fred Drake5755ce62001-06-27 19:19:46 +0000333 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000334 Py_INCREF(Py_None);
335 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000336}
337
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000338static char setprofile_doc[] =
339"setprofile(function)\n\
340\n\
341Set the profiling function. It will be called on each function call\n\
342and return. See the profiler chapter in the library manual.";
343
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000344static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000346{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000347 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum43713e52000-02-29 13:59:29 +0000348 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000349 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000350 Py_INCREF(Py_None);
351 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000352}
353
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000354static char setcheckinterval_doc[] =
355"setcheckinterval(n)\n\
356\n\
357Tell the Python interpreter to check for asynchronous events every\n\
358n instructions. This also affects how often thread switches occur.";
359
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000360static PyObject *
361sys_setrecursionlimit(PyObject *self, PyObject *args)
362{
363 int new_limit;
364 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
365 return NULL;
366 if (new_limit <= 0) {
367 PyErr_SetString(PyExc_ValueError,
368 "recursion limit must be positive");
369 return NULL;
370 }
371 Py_SetRecursionLimit(new_limit);
372 Py_INCREF(Py_None);
373 return Py_None;
374}
375
376static char setrecursionlimit_doc[] =
377"setrecursionlimit(n)\n\
378\n\
379Set the maximum depth of the Python interpreter stack to n. This\n\
380limit prevents infinite recursion from causing an overflow of the C\n\
381stack and crashing Python. The highest possible limit is platform-\n\
382dependent.";
383
384static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000385sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000386{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000387 return PyInt_FromLong(Py_GetRecursionLimit());
388}
389
390static char getrecursionlimit_doc[] =
391"getrecursionlimit()\n\
392\n\
393Return the current value of the recursion limit, the maximum depth\n\
394of the Python interpreter stack. This limit prevents infinite\n\
395recursion from causing an overflow of the C stack and crashing Python.";
396
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000397#ifdef HAVE_DLOPEN
398static PyObject *
399sys_setdlopenflags(PyObject *self, PyObject *args)
400{
401 int new_val;
402 PyThreadState *tstate = PyThreadState_Get();
403 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
404 return NULL;
405 if (!tstate)
406 return NULL;
407 tstate->interp->dlopenflags = new_val;
408 Py_INCREF(Py_None);
409 return Py_None;
410}
411
412static char setdlopenflags_doc[] =
413"setdlopenflags(n) -> None\n\
414\n\
415Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000416things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000417a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000418To share symbols across extension modules, call as\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000419sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)";
420
421static PyObject *
422sys_getdlopenflags(PyObject *self, PyObject *args)
423{
424 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000425 if (!tstate)
426 return NULL;
427 return PyInt_FromLong(tstate->interp->dlopenflags);
428}
429
430static char getdlopenflags_doc[] =
431"getdlopenflags() -> int\n\
432\n\
433Return the current value of the flags that are used for dlopen()\n\
434calls. The flag constants are defined in the dl module.";
435#endif
436
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000437#ifdef USE_MALLOPT
438/* Link with -lmalloc (or -lmpc) on an SGI */
439#include <malloc.h>
440
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000441static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000442sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000443{
444 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000445 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000446 return NULL;
447 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000448 Py_INCREF(Py_None);
449 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000450}
451#endif /* USE_MALLOPT */
452
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000453static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000454sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000455{
Mark Hammond440d8982000-06-20 08:12:48 +0000456 return PyInt_FromLong(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000457}
458
Mark Hammond440d8982000-06-20 08:12:48 +0000459#ifdef Py_TRACE_REFS
460static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000461sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000462{
463 extern long _Py_RefTotal;
Mark Hammond440d8982000-06-20 08:12:48 +0000464 return PyInt_FromLong(_Py_RefTotal);
465}
466
467#endif /* Py_TRACE_REFS */
468
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000469static char getrefcount_doc[] =
470"getrefcount(object) -> integer\n\
471\n\
472Return the current reference count for the object. This includes the\n\
473temporary reference in the argument list, so it is at least 2.";
474
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000475#ifdef COUNT_ALLOCS
476static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000477sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000478{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000479 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000480
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000481 return get_counts();
482}
483#endif
484
Barry Warsawb6a54d22000-12-06 21:47:46 +0000485static char getframe_doc[] =
486"_getframe([depth]) -> frameobject\n\
487\n\
488Return a frame object from the call stack. If optional integer depth is\n\
489given, return the frame object that many calls below the top of the stack.\n\
490If that is deeper than the call stack, ValueError is raised. The default\n\
491for depth is zero, returning the frame at the top of the call stack.\n\
492\n\
493This function should be used for internal and specialized\n\
494purposes only.";
495
496static PyObject *
497sys_getframe(PyObject *self, PyObject *args)
498{
499 PyFrameObject *f = PyThreadState_Get()->frame;
500 int depth = -1;
501
502 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
503 return NULL;
504
505 while (depth > 0 && f != NULL) {
506 f = f->f_back;
507 --depth;
508 }
509 if (f == NULL) {
510 PyErr_SetString(PyExc_ValueError,
511 "call stack is not deep enough");
512 return NULL;
513 }
514 Py_INCREF(f);
515 return (PyObject*)f;
516}
517
518
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000519#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000520/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000521extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000522#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000523
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000524#ifdef DYNAMIC_EXECUTION_PROFILE
525/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000526extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000527#endif
528
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000529static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000530 /* Might as well keep this in alphabetic order */
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000531 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
532 {"exc_info", (PyCFunction)sys_exc_info, METH_NOARGS, exc_info_doc},
533 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000534 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000535#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000536 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000537 getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000538#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000539#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000540 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
541 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000542#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000543#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000544 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000545#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000546#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000547 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000548#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000549#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000550 {"getobjects", _Py_GetObjects, METH_VARARGS},
551 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000552#endif
Fred Drakea7688822001-10-24 20:47:48 +0000553 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000554 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000555 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000556 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000557#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000558 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000559#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000560#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000561 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000562 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000563#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000564 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000565 setcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000566#ifdef HAVE_DLOPEN
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000567 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
568 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000569#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000570 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000571 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000572 setrecursionlimit_doc},
Neal Norwitz290d31e2002-03-03 15:12:58 +0000573 {"settrace", sys_settrace, METH_O, settrace_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000574 {NULL, NULL} /* sentinel */
575};
576
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000577static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000578list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000579{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000580 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000581 int i;
582 if (list == NULL)
583 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000584 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000585 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000586 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000587 if (name == NULL)
588 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000589 PyList_Append(list, name);
590 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000591 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000592 if (PyList_Sort(list) != 0) {
593 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000594 list = NULL;
595 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000596 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000597 PyObject *v = PyList_AsTuple(list);
598 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000599 list = v;
600 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000601 return list;
602}
603
Guido van Rossum23fff912000-12-15 22:02:05 +0000604static PyObject *warnoptions = NULL;
605
606void
607PySys_ResetWarnOptions(void)
608{
609 if (warnoptions == NULL || !PyList_Check(warnoptions))
610 return;
611 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
612}
613
614void
615PySys_AddWarnOption(char *s)
616{
617 PyObject *str;
618
619 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
620 Py_XDECREF(warnoptions);
621 warnoptions = PyList_New(0);
622 if (warnoptions == NULL)
623 return;
624 }
625 str = PyString_FromString(s);
626 if (str != NULL) {
627 PyList_Append(warnoptions, str);
628 Py_DECREF(str);
629 }
630}
631
Guido van Rossum40552d01998-08-06 03:34:39 +0000632/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
633 Two literals concatenated works just fine. If you have a K&R compiler
634 or other abomination that however *does* understand longer strings,
635 get rid of the !!! comment in the middle and the quotes that surround it. */
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000636static char sys_doc[] =
637"This module provides access to some objects used or maintained by the\n\
638interpreter and to functions that interact strongly with the interpreter.\n\
639\n\
640Dynamic objects:\n\
641\n\
642argv -- command line arguments; argv[0] is the script pathname if known\n\
643path -- module search path; path[0] is the script directory, else ''\n\
644modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000645\n\
646displayhook -- called to show results in an interactive session\n\
647excepthook -- called to handle any uncaught exception other than SystemExit\n\
648 To customize printing in an interactive session or to install a custom\n\
649 top-level exception handler, assign other functions to replace these.\n\
650\n\
651exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
652 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000653\n\
654stdin -- standard input file object; used by raw_input() and input()\n\
655stdout -- standard output file object; used by the print statement\n\
656stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000657 By assigning other file objects (or objects that behave like files)\n\
658 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000659\n\
660last_type -- type of last uncaught exception\n\
661last_value -- value of last uncaught exception\n\
662last_traceback -- traceback of last uncaught exception\n\
663 These three are only available in an interactive session after a\n\
664 traceback has been printed.\n\
665\n\
666exc_type -- type of exception currently being handled\n\
667exc_value -- value of exception currently being handled\n\
668exc_traceback -- traceback of exception currently being handled\n\
669 The function exc_info() should be used instead of these three,\n\
670 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000671"
672#ifndef MS_WIN16
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000673/* concatenating string here */
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000674"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000675Static objects:\n\
676\n\
677maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000678maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000679builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000680version -- the version of this interpreter as a string\n\
681version_info -- version information as a tuple\n\
682hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000683copyright -- copyright notice pertaining to this interpreter\n\
684platform -- platform identifier\n\
685executable -- pathname of this Python interpreter\n\
686prefix -- prefix used to find the Python library\n\
687exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000688"
689#ifdef MS_WINDOWS
690/* concatenating string here */
691"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000692winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000693"
694#endif /* MS_WINDOWS */
695"__stdin__ -- the original stdin; don't touch!\n\
696__stdout__ -- the original stdout; don't touch!\n\
697__stderr__ -- the original stderr; don't touch!\n\
698__displayhook__ -- the original displayhook; don't touch!\n\
699__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000700\n\
701Functions:\n\
702\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000703displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000704excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000705exc_info() -- return thread-safe information about the current exception\n\
706exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000707getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000708getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000709getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000710setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000711setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000712setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000713setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000714settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +0000715"
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000716#endif /* MS_WIN16 */
Fred Drakeccede592000-08-14 20:59:57 +0000717/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000718
Guido van Rossum25ce5661997-08-02 03:10:38 +0000719PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000720_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000722 PyObject *m, *v, *sysdict;
723 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +0000724 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000725
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000726 m = Py_InitModule3("sys", sys_methods, sys_doc);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000727 sysdict = PyModule_GetDict(m);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000728
729 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
730 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
731 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000732 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000733 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000734 PyDict_SetItemString(sysdict, "stdin", sysin);
735 PyDict_SetItemString(sysdict, "stdout", sysout);
736 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +0000737 /* Make backup copies for cleanup */
738 PyDict_SetItemString(sysdict, "__stdin__", sysin);
739 PyDict_SetItemString(sysdict, "__stdout__", sysout);
740 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000741 PyDict_SetItemString(sysdict, "__displayhook__",
742 PyDict_GetItemString(sysdict, "displayhook"));
743 PyDict_SetItemString(sysdict, "__excepthook__",
744 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000745 Py_XDECREF(sysin);
746 Py_XDECREF(sysout);
747 Py_XDECREF(syserr);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000748 PyDict_SetItemString(sysdict, "version",
749 v = PyString_FromString(Py_GetVersion()));
Barry Warsaw54892c41999-01-27 16:33:19 +0000750 Py_XDECREF(v);
Guido van Rossume0d7dae1999-01-03 12:55:39 +0000751 PyDict_SetItemString(sysdict, "hexversion",
752 v = PyInt_FromLong(PY_VERSION_HEX));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000753 Py_XDECREF(v);
Fred Drake93a20bf2000-04-13 17:44:51 +0000754 /*
755 * These release level checks are mutually exclusive and cover
756 * the field, so don't get too fancy with the pre-processor!
757 */
758#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000759 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +0000760#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000761 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +0000762#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +0000763 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +0000764#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +0000765 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +0000766#endif
Fred Drake801c08d2000-04-13 15:29:10 +0000767 PyDict_SetItemString(sysdict, "version_info",
Fred Drake6d27c1e2000-04-13 20:03:20 +0000768 v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +0000769 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +0000770 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +0000771 PY_RELEASE_SERIAL));
Fred Drake801c08d2000-04-13 15:29:10 +0000772 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000773 PyDict_SetItemString(sysdict, "copyright",
774 v = PyString_FromString(Py_GetCopyright()));
775 Py_XDECREF(v);
776 PyDict_SetItemString(sysdict, "platform",
777 v = PyString_FromString(Py_GetPlatform()));
778 Py_XDECREF(v);
Guido van Rossumb2c8ec41997-05-22 20:41:20 +0000779 PyDict_SetItemString(sysdict, "executable",
780 v = PyString_FromString(Py_GetProgramFullPath()));
781 Py_XDECREF(v);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000782 PyDict_SetItemString(sysdict, "prefix",
783 v = PyString_FromString(Py_GetPrefix()));
784 Py_XDECREF(v);
785 PyDict_SetItemString(sysdict, "exec_prefix",
786 v = PyString_FromString(Py_GetExecPrefix()));
787 Py_XDECREF(v);
788 PyDict_SetItemString(sysdict, "maxint",
789 v = PyInt_FromLong(PyInt_GetMax()));
790 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000791#ifdef Py_USING_UNICODE
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000792 PyDict_SetItemString(sysdict, "maxunicode",
793 v = PyInt_FromLong(PyUnicode_GetMax()));
794 Py_XDECREF(v);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000796 PyDict_SetItemString(sysdict, "builtin_module_names",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 v = list_builtin_module_names());
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000798 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000799 {
800 /* Assumes that longs are at least 2 bytes long.
801 Should be safe! */
802 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +0000803 char *value;
Fred Drake099325e2000-08-14 15:47:03 +0000804
805 s = (char *) &number;
806 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +0000807 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +0000808 else
Fred Drakea2b6ad62000-08-15 04:24:43 +0000809 value = "little";
810 PyDict_SetItemString(sysdict, "byteorder",
Barry Warsawf2581c92000-08-16 23:03:57 +0000811 v = PyString_FromString(value));
812 Py_XDECREF(v);
Fred Drake099325e2000-08-14 15:47:03 +0000813 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +0000814#ifdef MS_COREDLL
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000815 PyDict_SetItemString(sysdict, "dllhandle",
Guido van Rossum582acec2000-06-28 22:07:35 +0000816 v = PyLong_FromVoidPtr(PyWin_DLLhModule));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000817 Py_XDECREF(v);
818 PyDict_SetItemString(sysdict, "winver",
Guido van Rossum6c1e5f21997-09-29 23:34:23 +0000819 v = PyString_FromString(PyWin_DLLVersionString));
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000820 Py_XDECREF(v);
Guido van Rossumc606fe11996-04-09 02:37:57 +0000821#endif
Guido van Rossum23fff912000-12-15 22:02:05 +0000822 if (warnoptions == NULL) {
823 warnoptions = PyList_New(0);
824 }
825 else {
826 Py_INCREF(warnoptions);
827 }
828 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +0000829 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +0000830 }
831
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000832 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000833 return NULL;
834 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000835}
836
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000837static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000838makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000839{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000840 int i, n;
841 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000842 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000843
844 n = 1;
845 p = path;
846 while ((p = strchr(p, delim)) != NULL) {
847 n++;
848 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000849 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000850 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000851 if (v == NULL)
852 return NULL;
853 for (i = 0; ; i++) {
854 p = strchr(path, delim);
855 if (p == NULL)
856 p = strchr(path, '\0'); /* End of string */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000857 w = PyString_FromStringAndSize(path, (int) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000858 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000859 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000860 return NULL;
861 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000862 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000863 if (*p == '\0')
864 break;
865 path = p+1;
866 }
867 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868}
869
870void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000871PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000873 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000874 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000875 Py_FatalError("can't create sys.path");
876 if (PySys_SetObject("path", v) != 0)
877 Py_FatalError("can't assign sys.path");
878 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000879}
880
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000881static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000882makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000883{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000884 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000885 if (argc <= 0 || argv == NULL) {
886 /* Ensure at least one (empty) argument is seen */
887 static char *empty_argv[1] = {""};
888 argv = empty_argv;
889 argc = 1;
890 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000891 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000892 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000893 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000894 for (i = 0; i < argc; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000895 PyObject *v = PyString_FromString(argv[i]);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000896 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000897 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000898 av = NULL;
899 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000900 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000901 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000902 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000903 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000904 return av;
905}
906
907void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000908PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000909{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000910 PyObject *av = makeargvobject(argc, argv);
911 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000912 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000913 Py_FatalError("no mem for sys.argv");
914 if (PySys_SetObject("argv", av) != 0)
915 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +0000916 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000917 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +0000918 char *p = NULL;
Guido van Rossumcc883411996-09-10 14:44:21 +0000919 int n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000920 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000921#ifdef HAVE_READLINK
922 char link[MAXPATHLEN+1];
923 char argv0copy[2*MAXPATHLEN+1];
924 int nr = 0;
925 if (argc > 0 && argv0 != NULL)
926 nr = readlink(argv0, link, MAXPATHLEN);
927 if (nr > 0) {
928 /* It's a symlink */
929 link[nr] = '\0';
930 if (link[0] == SEP)
931 argv0 = link; /* Link to absolute path */
932 else if (strchr(link, SEP) == NULL)
933 ; /* Link without path */
934 else {
935 /* Must join(dirname(argv0), link) */
936 char *q = strrchr(argv0, SEP);
937 if (q == NULL)
938 argv0 = link; /* argv0 without path */
939 else {
940 /* Must make a copy */
941 strcpy(argv0copy, argv0);
942 q = strrchr(argv0copy, SEP);
943 strcpy(q+1, link);
944 argv0 = argv0copy;
945 }
946 }
947 }
948#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +0000949#if SEP == '\\' /* Special case for MS filename syntax */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000950 if (argc > 0 && argv0 != NULL) {
Guido van Rossumcc883411996-09-10 14:44:21 +0000951 char *q;
Guido van Rossumc474dea1997-04-25 15:38:31 +0000952 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000953 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000954 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +0000955 if (q != NULL)
956 p = q;
957 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +0000958 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +0000959 if (n > 1 && p[-1] != ':')
960 n--; /* Drop trailing separator */
961 }
962 }
963#else /* All other filename syntaxes */
Guido van Rossumc474dea1997-04-25 15:38:31 +0000964 if (argc > 0 && argv0 != NULL)
965 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +0000966 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +0000967#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +0000968 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +0000969#else /* don't include trailing separator */
970 n = p - argv0;
971#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +0000972#if SEP == '/' /* Special case for Unix filename syntax */
973 if (n > 1)
974 n--; /* Drop trailing separator */
975#endif /* Unix */
976 }
977#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000978 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +0000979 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000980 Py_FatalError("no mem for sys.path insertion");
981 if (PyList_Insert(path, 0, a) < 0)
982 Py_FatalError("sys.path.insert(0) failed");
983 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +0000984 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000985 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986}
Guido van Rossuma890e681998-05-12 14:59:24 +0000987
988
989/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
990 Adapted from code submitted by Just van Rossum.
991
992 PySys_WriteStdout(format, ...)
993 PySys_WriteStderr(format, ...)
994
995 The first function writes to sys.stdout; the second to sys.stderr. When
996 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +0000997 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +0000998
999 Both take a printf-style format string as their first argument followed
1000 by a variable length argument list determined by the format string.
1001
1002 *** WARNING ***
1003
1004 The format should limit the total size of the formatted output string to
1005 1000 bytes. In particular, this means that no unrestricted "%s" formats
1006 should occur; these should be limited using "%.<N>s where <N> is a
1007 decimal number calculated so that <N> plus the maximum size of other
1008 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1009 which can print hundreds of digits for very large numbers.
1010
1011 */
1012
1013static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001014mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001015{
1016 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001017 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001018
Guido van Rossum8442af31998-10-12 18:22:10 +00001019 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001020 file = PySys_GetObject(name);
1021 if (file == NULL || PyFile_AsFile(file) == fp)
1022 vfprintf(fp, format, va);
1023 else {
1024 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001025 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1026 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001027 if (PyFile_WriteString(buffer, file) != 0) {
1028 PyErr_Clear();
1029 fputs(buffer, fp);
1030 }
Tim Petersfaad5ad2001-12-03 00:43:33 +00001031 if (written < 0 || written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001032 const char *truncated = "... truncated";
1033 if (PyFile_WriteString(truncated, file) != 0) {
1034 PyErr_Clear();
1035 fputs(truncated, fp);
1036 }
1037 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001038 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001039 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001040}
1041
1042void
Guido van Rossuma890e681998-05-12 14:59:24 +00001043PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001044{
1045 va_list va;
1046
Guido van Rossuma890e681998-05-12 14:59:24 +00001047 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001048 mywrite("stdout", stdout, format, va);
1049 va_end(va);
1050}
1051
1052void
Guido van Rossuma890e681998-05-12 14:59:24 +00001053PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001054{
1055 va_list va;
1056
Guido van Rossuma890e681998-05-12 14:59:24 +00001057 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001058 mywrite("stderr", stderr, format, va);
1059 va_end(va);
1060}