blob: 8e27844eaa2571ef54234544024a8ab16d1b498e [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"
Christian Heimesf31b69f2008-01-14 03:42:48 +000018#include "structseq.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019#include "code.h"
Barry Warsawb6a54d22000-12-06 21:47:46 +000020#include "frameobject.h"
Guido van Rossuma12fe4e2003-04-09 19:06:21 +000021#include "eval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Guido van Rossume2437a11992-03-23 18:20:18 +000023#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000024
Mark Hammond8696ebc2002-10-08 02:44:31 +000025#ifdef MS_WINDOWS
26#define WIN32_LEAN_AND_MEAN
27#include "windows.h"
28#endif /* MS_WINDOWS */
29
Guido van Rossum9b38a141996-09-11 23:12:24 +000030#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000031extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000032/* A string loaded from the DLL at startup: */
33extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000034#endif
35
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000036#ifdef __VMS
37#include <unixlib.h>
38#endif
39
Martin v. Löwis5467d4c2003-05-10 07:10:12 +000040#ifdef MS_WINDOWS
41#include <windows.h>
42#endif
43
44#ifdef HAVE_LANGINFO_H
45#include <locale.h>
46#include <langinfo.h>
47#endif
48
Guido van Rossum65bf9f21997-04-29 18:33:38 +000049PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000050PySys_GetObject(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Nicholas Bastine5662ae2004-03-24 22:22:12 +000052 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +000053 PyObject *sd = tstate->interp->sysdict;
Guido van Rossumbe203361999-10-05 22:17:41 +000054 if (sd == NULL)
55 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000056 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
59FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000060PySys_GetFile(char *name, FILE *def)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
62 FILE *fp = NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000063 PyObject *v = PySys_GetObject(name);
64 if (v != NULL && PyFile_Check(v))
65 fp = PyFile_AsFile(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 if (fp == NULL)
67 fp = def;
68 return fp;
69}
70
71int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000072PySys_SetObject(char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073{
Nicholas Bastine5662ae2004-03-24 22:22:12 +000074 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +000075 PyObject *sd = tstate->interp->sysdict;
Guido van Rossum5ad58c61992-01-26 18:15:48 +000076 if (v == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000077 if (PyDict_GetItemString(sd, name) == NULL)
Guido van Rossum5ad58c61992-01-26 18:15:48 +000078 return 0;
79 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000080 return PyDict_DelItemString(sd, name);
Guido van Rossum5ad58c61992-01-26 18:15:48 +000081 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 else
Guido van Rossum25ce5661997-08-02 03:10:38 +000083 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084}
85
Guido van Rossum65bf9f21997-04-29 18:33:38 +000086static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000087sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000088{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000089 PyObject *outf;
Nicholas Bastine5662ae2004-03-24 22:22:12 +000090 PyInterpreterState *interp = PyThreadState_GET()->interp;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000091 PyObject *modules = interp->modules;
92 PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
93
Moshe Zadka03897ea2001-07-23 13:32:43 +000094 if (builtins == NULL) {
95 PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
96 return NULL;
97 }
98
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000099 /* Print value except if None */
100 /* After printing, also assign to '_' */
101 /* Before, set '_' to None to avoid recursion */
102 if (o == Py_None) {
103 Py_INCREF(Py_None);
104 return Py_None;
105 }
106 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
107 return NULL;
108 if (Py_FlushLine() != 0)
109 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000110 outf = PySys_GetObject("stdout");
111 if (outf == NULL) {
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000112 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
113 return NULL;
114 }
Greg Steinceb9b7c2001-01-11 09:27:34 +0000115 if (PyFile_WriteObject(o, outf, 0) != 0)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000116 return NULL;
Greg Steinceb9b7c2001-01-11 09:27:34 +0000117 PyFile_SoftSpace(outf, 1);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000118 if (Py_FlushLine() != 0)
119 return NULL;
120 if (PyObject_SetAttrString(builtins, "_", o) != 0)
121 return NULL;
122 Py_INCREF(Py_None);
123 return Py_None;
124}
125
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000126PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000127"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000128"\n"
Brett Cannon51185172006-03-02 22:07:40 +0000129"Print an object to sys.stdout and also save it in __builtin__.\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000130);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000131
132static PyObject *
133sys_excepthook(PyObject* self, PyObject* args)
134{
135 PyObject *exc, *value, *tb;
Fred Drakea7688822001-10-24 20:47:48 +0000136 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000137 return NULL;
138 PyErr_Display(exc, value, tb);
139 Py_INCREF(Py_None);
140 return Py_None;
141}
142
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000143PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000144"excepthook(exctype, value, traceback) -> None\n"
145"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000146"Handle an exception by displaying it with a traceback on sys.stderr.\n"
147);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000148
149static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000150sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000151{
152 PyThreadState *tstate;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000153 tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154 return Py_BuildValue(
155 "(OOO)",
156 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
157 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
158 tstate->exc_traceback != NULL ?
159 tstate->exc_traceback : Py_None);
160}
161
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000162PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000163"exc_info() -> (type, value, traceback)\n\
164\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000165Return information about the most recent exception caught by an except\n\
166clause in the current stack frame or in an older stack frame."
167);
168
169static PyObject *
170sys_exc_clear(PyObject *self, PyObject *noargs)
171{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000172 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000173 PyObject *tmp_type, *tmp_value, *tmp_tb;
174 tmp_type = tstate->exc_type;
175 tmp_value = tstate->exc_value;
176 tmp_tb = tstate->exc_traceback;
177 tstate->exc_type = NULL;
178 tstate->exc_value = NULL;
179 tstate->exc_traceback = NULL;
180 Py_XDECREF(tmp_type);
181 Py_XDECREF(tmp_value);
182 Py_XDECREF(tmp_tb);
183 /* For b/w compatibility */
184 PySys_SetObject("exc_type", Py_None);
185 PySys_SetObject("exc_value", Py_None);
186 PySys_SetObject("exc_traceback", Py_None);
187 Py_INCREF(Py_None);
188 return Py_None;
189}
190
191PyDoc_STRVAR(exc_clear_doc,
192"exc_clear() -> None\n\
193\n\
194Clear global information on the current exception. Subsequent calls to\n\
195exc_info() will return (None,None,None) until another exception is raised\n\
196in the current thread or the execution stack returns to a frame where\n\
197another exception is being handled."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000198);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000199
Guido van Rossuma027efa1997-05-05 20:56:21 +0000200static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000201sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Neal Norwitz0c766a02002-03-27 13:03:09 +0000203 PyObject *exit_code = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +0000204 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
Neal Norwitz0c766a02002-03-27 13:03:09 +0000205 return NULL;
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000206 /* Raise SystemExit so callers may catch it or clean up. */
Neal Norwitz0c766a02002-03-27 13:03:09 +0000207 PyErr_SetObject(PyExc_SystemExit, exit_code);
Guido van Rossum6a468bf1991-12-31 13:15:35 +0000208 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000211PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000212"exit([status])\n\
213\n\
214Exit the interpreter by raising SystemExit(status).\n\
215If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000216If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000217If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000218exit status will be one (i.e., failure)."
219);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000220
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000221#ifdef Py_USING_UNICODE
222
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000223static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000224sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000225{
Fred Drake8b4d01d2000-05-09 19:57:01 +0000226 return PyString_FromString(PyUnicode_GetDefaultEncoding());
227}
228
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000229PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000230"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000231\n\
232Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000233implementation."
234);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000235
236static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237sys_setdefaultencoding(PyObject *self, PyObject *args)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000238{
239 char *encoding;
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000240 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
Fred Drake8b4d01d2000-05-09 19:57:01 +0000241 return NULL;
242 if (PyUnicode_SetDefaultEncoding(encoding))
243 return NULL;
244 Py_INCREF(Py_None);
245 return Py_None;
246}
247
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000248PyDoc_STRVAR(setdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000249"setdefaultencoding(encoding)\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000250\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000251Set the current default string encoding used by the Unicode implementation."
252);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000253
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000254static PyObject *
255sys_getfilesystemencoding(PyObject *self)
256{
257 if (Py_FileSystemDefaultEncoding)
258 return PyString_FromString(Py_FileSystemDefaultEncoding);
259 Py_INCREF(Py_None);
260 return Py_None;
261}
262
263PyDoc_STRVAR(getfilesystemencoding_doc,
264"getfilesystemencoding() -> string\n\
265\n\
266Return the encoding used to convert Unicode filenames in\n\
267operating system filenames."
268);
269
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000270#endif
271
Fred Drake5755ce62001-06-27 19:19:46 +0000272/*
273 * Cached interned string objects used for calling the profile and
274 * trace functions. Initialized by trace_init().
275 */
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000276static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Fred Drake5755ce62001-06-27 19:19:46 +0000277
278static int
279trace_init(void)
280{
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000281 static char *whatnames[7] = {"call", "exception", "line", "return",
282 "c_call", "c_exception", "c_return"};
Fred Drake5755ce62001-06-27 19:19:46 +0000283 PyObject *name;
284 int i;
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000285 for (i = 0; i < 7; ++i) {
Fred Drake5755ce62001-06-27 19:19:46 +0000286 if (whatstrings[i] == NULL) {
287 name = PyString_InternFromString(whatnames[i]);
288 if (name == NULL)
289 return -1;
290 whatstrings[i] = name;
291 }
292 }
293 return 0;
294}
295
296
297static PyObject *
298call_trampoline(PyThreadState *tstate, PyObject* callback,
299 PyFrameObject *frame, int what, PyObject *arg)
300{
301 PyObject *args = PyTuple_New(3);
302 PyObject *whatstr;
303 PyObject *result;
304
305 if (args == NULL)
306 return NULL;
307 Py_INCREF(frame);
308 whatstr = whatstrings[what];
309 Py_INCREF(whatstr);
310 if (arg == NULL)
311 arg = Py_None;
312 Py_INCREF(arg);
313 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
314 PyTuple_SET_ITEM(args, 1, whatstr);
315 PyTuple_SET_ITEM(args, 2, arg);
316
317 /* call the Python-level function */
318 PyFrame_FastToLocals(frame);
319 result = PyEval_CallObject(callback, args);
320 PyFrame_LocalsToFast(frame, 1);
321 if (result == NULL)
322 PyTraceBack_Here(frame);
323
324 /* cleanup */
325 Py_DECREF(args);
326 return result;
327}
328
329static int
330profile_trampoline(PyObject *self, PyFrameObject *frame,
331 int what, PyObject *arg)
332{
333 PyThreadState *tstate = frame->f_tstate;
334 PyObject *result;
335
Fred Drake8f51f542001-10-04 14:48:42 +0000336 if (arg == NULL)
337 arg = Py_None;
Fred Drake5755ce62001-06-27 19:19:46 +0000338 result = call_trampoline(tstate, self, frame, what, arg);
339 if (result == NULL) {
340 PyEval_SetProfile(NULL, NULL);
341 return -1;
342 }
343 Py_DECREF(result);
344 return 0;
345}
346
347static int
348trace_trampoline(PyObject *self, PyFrameObject *frame,
349 int what, PyObject *arg)
350{
351 PyThreadState *tstate = frame->f_tstate;
352 PyObject *callback;
353 PyObject *result;
354
355 if (what == PyTrace_CALL)
356 callback = self;
357 else
358 callback = frame->f_trace;
359 if (callback == NULL)
360 return 0;
361 result = call_trampoline(tstate, callback, frame, what, arg);
362 if (result == NULL) {
363 PyEval_SetTrace(NULL, NULL);
364 Py_XDECREF(frame->f_trace);
365 frame->f_trace = NULL;
366 return -1;
367 }
368 if (result != Py_None) {
369 PyObject *temp = frame->f_trace;
370 frame->f_trace = NULL;
371 Py_XDECREF(temp);
372 frame->f_trace = result;
373 }
374 else {
375 Py_DECREF(result);
376 }
377 return 0;
378}
Fred Draked0838392001-06-16 21:02:31 +0000379
Fred Drake8b4d01d2000-05-09 19:57:01 +0000380static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000381sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000382{
Fred Drake5755ce62001-06-27 19:19:46 +0000383 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000384 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000385 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000386 PyEval_SetTrace(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000387 else
Fred Drake5755ce62001-06-27 19:19:46 +0000388 PyEval_SetTrace(trace_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000389 Py_INCREF(Py_None);
390 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000391}
392
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000393PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000394"settrace(function)\n\
395\n\
396Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000397function call. See the debugger chapter in the library manual."
398);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000399
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000400static PyObject *
Georg Brandl56112892008-01-20 13:59:46 +0000401sys_gettrace(PyObject *self, PyObject *args)
402{
403 PyThreadState *tstate = PyThreadState_GET();
404 PyObject *temp = tstate->c_traceobj;
405
406 if (temp == NULL)
407 temp = Py_None;
408 Py_INCREF(temp);
409 return temp;
410}
411
412PyDoc_STRVAR(gettrace_doc,
413"gettrace()\n\
414\n\
415Return the global debug tracing function set with sys.settrace.\n\
416See the debugger chapter in the library manual."
417);
418
419static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000421{
Fred Drake5755ce62001-06-27 19:19:46 +0000422 if (trace_init() == -1)
Fred Draked0838392001-06-16 21:02:31 +0000423 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000424 if (args == Py_None)
Fred Drake5755ce62001-06-27 19:19:46 +0000425 PyEval_SetProfile(NULL, NULL);
Guido van Rossume765f7d1992-04-05 14:17:55 +0000426 else
Fred Drake5755ce62001-06-27 19:19:46 +0000427 PyEval_SetProfile(profile_trampoline, args);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000428 Py_INCREF(Py_None);
429 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000430}
431
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000432PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000433"setprofile(function)\n\
434\n\
435Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000436and return. See the profiler chapter in the library manual."
437);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000438
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000439static PyObject *
Georg Brandl56112892008-01-20 13:59:46 +0000440sys_getprofile(PyObject *self, PyObject *args)
441{
442 PyThreadState *tstate = PyThreadState_GET();
443 PyObject *temp = tstate->c_profileobj;
444
445 if (temp == NULL)
446 temp = Py_None;
447 Py_INCREF(temp);
448 return temp;
449}
450
451PyDoc_STRVAR(getprofile_doc,
452"getprofile()\n\
453\n\
454Return the profiling function set with sys.setprofile.\n\
455See the profiler chapter in the library manual."
456);
457
458static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000459sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000460{
Skip Montanarod581d772002-09-03 20:10:45 +0000461 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000462 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000463 Py_INCREF(Py_None);
464 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000465}
466
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000467PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000468"setcheckinterval(n)\n\
469\n\
470Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000471n instructions. This also affects how often thread switches occur."
472);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000473
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000474static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000475sys_getcheckinterval(PyObject *self, PyObject *args)
476{
477 return PyInt_FromLong(_Py_CheckInterval);
478}
479
480PyDoc_STRVAR(getcheckinterval_doc,
481"getcheckinterval() -> current check interval; see setcheckinterval()."
482);
483
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000484#ifdef WITH_TSC
485static PyObject *
486sys_settscdump(PyObject *self, PyObject *args)
487{
488 int bool;
489 PyThreadState *tstate = PyThreadState_Get();
490
491 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
492 return NULL;
493 if (bool)
494 tstate->interp->tscdump = 1;
495 else
496 tstate->interp->tscdump = 0;
497 Py_INCREF(Py_None);
498 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000499
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000500}
501
Tim Peters216b78b2006-01-06 02:40:53 +0000502PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000503"settscdump(bool)\n\
504\n\
505If true, tell the Python interpreter to dump VM measurements to\n\
506stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000507processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000508);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000509#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000510
Tim Peterse5e065b2003-07-06 18:36:54 +0000511static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000512sys_setrecursionlimit(PyObject *self, PyObject *args)
513{
514 int new_limit;
515 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
516 return NULL;
517 if (new_limit <= 0) {
Tim Peters216b78b2006-01-06 02:40:53 +0000518 PyErr_SetString(PyExc_ValueError,
519 "recursion limit must be positive");
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000520 return NULL;
521 }
522 Py_SetRecursionLimit(new_limit);
523 Py_INCREF(Py_None);
524 return Py_None;
525}
526
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000527PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000528"setrecursionlimit(n)\n\
529\n\
530Set the maximum depth of the Python interpreter stack to n. This\n\
531limit prevents infinite recursion from causing an overflow of the C\n\
532stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000533dependent."
534);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000535
536static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000537sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000538{
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000539 return PyInt_FromLong(Py_GetRecursionLimit());
540}
541
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000542PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000543"getrecursionlimit()\n\
544\n\
545Return the current value of the recursion limit, the maximum depth\n\
546of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000547recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000548);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000549
Mark Hammond8696ebc2002-10-08 02:44:31 +0000550#ifdef MS_WINDOWS
551PyDoc_STRVAR(getwindowsversion_doc,
552"getwindowsversion()\n\
553\n\
554Return information about the running version of Windows.\n\
555The result is a tuple of (major, minor, build, platform, text)\n\
556All elements are numbers, except text which is a string.\n\
557Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
558"
559);
560
561static PyObject *
562sys_getwindowsversion(PyObject *self)
563{
564 OSVERSIONINFO ver;
565 ver.dwOSVersionInfoSize = sizeof(ver);
566 if (!GetVersionEx(&ver))
567 return PyErr_SetFromWindowsErr(0);
568 return Py_BuildValue("HHHHs",
569 ver.dwMajorVersion,
570 ver.dwMinorVersion,
571 ver.dwBuildNumber,
572 ver.dwPlatformId,
573 ver.szCSDVersion);
574}
575
576#endif /* MS_WINDOWS */
577
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000578#ifdef HAVE_DLOPEN
579static PyObject *
580sys_setdlopenflags(PyObject *self, PyObject *args)
581{
582 int new_val;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000583 PyThreadState *tstate = PyThreadState_GET();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000584 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
585 return NULL;
586 if (!tstate)
587 return NULL;
588 tstate->interp->dlopenflags = new_val;
589 Py_INCREF(Py_None);
590 return Py_None;
591}
592
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000593PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000594"setdlopenflags(n) -> None\n\
595\n\
596Set the flags that will be used for dlopen() calls. Among other\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000597things, this will enable a lazy resolving of symbols when importing\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000598a module, if called as sys.setdlopenflags(0)\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000599To share symbols across extension modules, call as\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000600sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
601);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000602
603static PyObject *
604sys_getdlopenflags(PyObject *self, PyObject *args)
605{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000606 PyThreadState *tstate = PyThreadState_GET();
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000607 if (!tstate)
608 return NULL;
609 return PyInt_FromLong(tstate->interp->dlopenflags);
610}
611
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000612PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000613"getdlopenflags() -> int\n\
614\n\
615Return the current value of the flags that are used for dlopen()\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000616calls. The flag constants are defined in the dl module."
617);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000618#endif
619
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000620#ifdef USE_MALLOPT
621/* Link with -lmalloc (or -lmpc) on an SGI */
622#include <malloc.h>
623
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000624static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000625sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000626{
627 int flag;
Guido van Rossumffc0f4f2000-03-31 00:38:29 +0000628 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000629 return NULL;
630 mallopt(M_DEBUG, flag);
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000631 Py_INCREF(Py_None);
632 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000633}
634#endif /* USE_MALLOPT */
635
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000636static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000637sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000638{
Martin v. Löwis725507b2006-03-07 12:08:51 +0000639 return PyInt_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000640}
641
Tim Peters4be93d02002-07-07 19:59:50 +0000642#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000643static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000644sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000645{
Armin Rigoe1709372006-04-12 17:06:05 +0000646 return PyInt_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000647}
Armin Rigoe1709372006-04-12 17:06:05 +0000648#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000649
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000650PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000651"getrefcount(object) -> integer\n\
652\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000653Return the reference count of object. The count returned is generally\n\
654one higher than you might expect, because it includes the (temporary)\n\
655reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000656);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000657
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000658#ifdef COUNT_ALLOCS
659static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000660sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000661{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000662 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000663
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000664 return get_counts();
665}
666#endif
667
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000668PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000669"_getframe([depth]) -> frameobject\n\
670\n\
671Return a frame object from the call stack. If optional integer depth is\n\
672given, return the frame object that many calls below the top of the stack.\n\
673If that is deeper than the call stack, ValueError is raised. The default\n\
674for depth is zero, returning the frame at the top of the call stack.\n\
675\n\
676This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000677purposes only."
678);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000679
680static PyObject *
681sys_getframe(PyObject *self, PyObject *args)
682{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000683 PyFrameObject *f = PyThreadState_GET()->frame;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000684 int depth = -1;
685
686 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
687 return NULL;
688
689 while (depth > 0 && f != NULL) {
690 f = f->f_back;
691 --depth;
692 }
693 if (f == NULL) {
694 PyErr_SetString(PyExc_ValueError,
695 "call stack is not deep enough");
696 return NULL;
697 }
698 Py_INCREF(f);
699 return (PyObject*)f;
700}
701
Tim Peters32a83612006-07-10 21:08:24 +0000702PyDoc_STRVAR(current_frames_doc,
703"_current_frames() -> dictionary\n\
704\n\
705Return a dictionary mapping each current thread T's thread id to T's\n\
706current stack frame.\n\
707\n\
708This function should be used for specialized purposes only."
709);
710
711static PyObject *
712sys_current_frames(PyObject *self, PyObject *noargs)
713{
714 return _PyThread_CurrentFrames();
715}
716
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000717PyDoc_STRVAR(call_tracing_doc,
718"call_tracing(func, args) -> object\n\
719\n\
720Call func(*args), while tracing is enabled. The tracing state is\n\
721saved, and restored afterwards. This is intended to be called from\n\
722a debugger from a checkpoint, to recursively debug some other code."
723);
724
725static PyObject *
726sys_call_tracing(PyObject *self, PyObject *args)
727{
728 PyObject *func, *funcargs;
Georg Brandl96a8c392006-05-29 21:04:52 +0000729 if (!PyArg_UnpackTuple(args, "call_tracing", 2, 2, &func, &funcargs))
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000730 return NULL;
731 return _PyEval_CallTracing(func, funcargs);
732}
733
Jeremy Hylton985eba52003-02-05 23:13:00 +0000734PyDoc_STRVAR(callstats_doc,
735"callstats() -> tuple of integers\n\
736\n\
737Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
738when Python was built. Otherwise, return None.\n\
739\n\
740When enabled, this function returns detailed, implementation-specific\n\
741details about the number of function calls executed. The return value is\n\
742a 11-tuple where the entries in the tuple are counts of:\n\
7430. all function calls\n\
7441. calls to PyFunction_Type objects\n\
7452. PyFunction calls that do not create an argument tuple\n\
7463. PyFunction calls that do not create an argument tuple\n\
747 and bypass PyEval_EvalCodeEx()\n\
7484. PyMethod calls\n\
7495. PyMethod calls on bound methods\n\
7506. PyType calls\n\
7517. PyCFunction calls\n\
7528. generator calls\n\
7539. All other calls\n\
75410. Number of stack pops performed by call_function()"
755);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000756
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000757#ifdef __cplusplus
758extern "C" {
759#endif
760
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000761#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000762/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000763extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000764#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000765
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000766#ifdef DYNAMIC_EXECUTION_PROFILE
767/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000768extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000769#endif
770
Skip Montanaro53a6d1d2006-04-18 00:55:46 +0000771#ifdef __cplusplus
772}
773#endif
774
Christian Heimes422051a2008-02-04 18:00:12 +0000775static PyObject *
776sys_clear_type_cache(PyObject* self, PyObject* args)
777{
778 PyType_ClearCache();
779 Py_RETURN_NONE;
780}
781
782PyDoc_STRVAR(sys_clear_type_cache__doc__,
783"_clear_type_cache() -> None\n\
784Clear the internal type lookup cache.");
785
786
787static PyObject *
788sys_compact_freelists(PyObject* self, PyObject* args)
789{
790 size_t isum, ibc, ibf;
791 size_t fsum, fbc, fbf;
792
793 PyInt_CompactFreeList(&ibc, &ibf, &isum);
794 PyFloat_CompactFreeList(&fbc, &fbf, &fsum);
795
796 return Py_BuildValue("(kkk)(kkk)", isum, ibc, ibf,
797 fsum, fbc, fbf);
798
799}
800
801PyDoc_STRVAR(sys_compact_freelists__doc__,
802"_compact_freelists() -> ((remaing_objects, total_blocks, freed_blocks), ...)\n\
803Compact the free lists of ints and floats.");
804
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000805static PyMethodDef sys_methods[] = {
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000806 /* Might as well keep this in alphabetic order */
Tim Peters216b78b2006-01-06 02:40:53 +0000807 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
Jeremy Hylton985eba52003-02-05 23:13:00 +0000808 callstats_doc},
Christian Heimes422051a2008-02-04 18:00:12 +0000809 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
810 sys_clear_type_cache__doc__},
811 {"_compact_freelists", sys_compact_freelists, METH_NOARGS,
812 sys_compact_freelists__doc__},
Tim Peters32a83612006-07-10 21:08:24 +0000813 {"_current_frames", sys_current_frames, METH_NOARGS,
814 current_frames_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000815 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000816 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
817 {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000818 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
Neal Norwitz0c766a02002-03-27 13:03:09 +0000819 {"exit", sys_exit, METH_VARARGS, exit_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000820#ifdef Py_USING_UNICODE
Tim Peters216b78b2006-01-06 02:40:53 +0000821 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
822 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000823#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000824#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000825 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000826 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000827#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000828#ifdef COUNT_ALLOCS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000829 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000830#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000831#ifdef DYNAMIC_EXECUTION_PROFILE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000832 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000833#endif
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000834#ifdef Py_USING_UNICODE
835 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
Tim Peters216b78b2006-01-06 02:40:53 +0000836 METH_NOARGS, getfilesystemencoding_doc},
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000837#endif
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000838#ifdef Py_TRACE_REFS
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000839 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000840#endif
841#ifdef Py_REF_DEBUG
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000842 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000843#endif
Fred Drakea7688822001-10-24 20:47:48 +0000844 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000845 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000846 getrecursionlimit_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000847 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000848#ifdef MS_WINDOWS
849 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
850 getwindowsversion_doc},
851#endif /* MS_WINDOWS */
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000852#ifdef USE_MALLOPT
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000853 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000854#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000855#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000856 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000857 setdefaultencoding_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000858#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000859 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000860 setcheckinterval_doc},
Tim Peterse5e065b2003-07-06 18:36:54 +0000861 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
Tim Peters216b78b2006-01-06 02:40:53 +0000862 getcheckinterval_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000863#ifdef HAVE_DLOPEN
Tim Peters216b78b2006-01-06 02:40:53 +0000864 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000865 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000866#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000867 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
Georg Brandl56112892008-01-20 13:59:46 +0000868 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000869 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000870 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000871#ifdef WITH_TSC
872 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
873#endif
Neal Norwitz290d31e2002-03-03 15:12:58 +0000874 {"settrace", sys_settrace, METH_O, settrace_doc},
Georg Brandl56112892008-01-20 13:59:46 +0000875 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000876 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000877 {NULL, NULL} /* sentinel */
878};
879
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000880static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000881list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +0000882{
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000883 PyObject *list = PyList_New(0);
Guido van Rossum34679b71993-01-26 13:33:44 +0000884 int i;
885 if (list == NULL)
886 return NULL;
Guido van Rossum25c649f1997-11-04 17:04:34 +0000887 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000888 PyObject *name = PyString_FromString(
Guido van Rossum25c649f1997-11-04 17:04:34 +0000889 PyImport_Inittab[i].name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000890 if (name == NULL)
891 break;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000892 PyList_Append(list, name);
893 Py_DECREF(name);
Guido van Rossum34679b71993-01-26 13:33:44 +0000894 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000895 if (PyList_Sort(list) != 0) {
896 Py_DECREF(list);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000897 list = NULL;
898 }
Guido van Rossum8f49e121997-01-06 22:55:54 +0000899 if (list) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000900 PyObject *v = PyList_AsTuple(list);
901 Py_DECREF(list);
Guido van Rossum8f49e121997-01-06 22:55:54 +0000902 list = v;
903 }
Guido van Rossum34679b71993-01-26 13:33:44 +0000904 return list;
905}
906
Guido van Rossum23fff912000-12-15 22:02:05 +0000907static PyObject *warnoptions = NULL;
908
909void
910PySys_ResetWarnOptions(void)
911{
912 if (warnoptions == NULL || !PyList_Check(warnoptions))
913 return;
914 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
915}
916
917void
918PySys_AddWarnOption(char *s)
919{
920 PyObject *str;
921
922 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
923 Py_XDECREF(warnoptions);
924 warnoptions = PyList_New(0);
925 if (warnoptions == NULL)
926 return;
927 }
928 str = PyString_FromString(s);
929 if (str != NULL) {
930 PyList_Append(warnoptions, str);
931 Py_DECREF(str);
932 }
933}
934
Guido van Rossum40552d01998-08-06 03:34:39 +0000935/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
936 Two literals concatenated works just fine. If you have a K&R compiler
937 or other abomination that however *does* understand longer strings,
938 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000939PyDoc_VAR(sys_doc) =
940PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000941"This module provides access to some objects used or maintained by the\n\
942interpreter and to functions that interact strongly with the interpreter.\n\
943\n\
944Dynamic objects:\n\
945\n\
946argv -- command line arguments; argv[0] is the script pathname if known\n\
947path -- module search path; path[0] is the script directory, else ''\n\
948modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000949\n\
950displayhook -- called to show results in an interactive session\n\
951excepthook -- called to handle any uncaught exception other than SystemExit\n\
952 To customize printing in an interactive session or to install a custom\n\
953 top-level exception handler, assign other functions to replace these.\n\
954\n\
955exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
956 Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000957\n\
958stdin -- standard input file object; used by raw_input() and input()\n\
959stdout -- standard output file object; used by the print statement\n\
960stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000961 By assigning other file objects (or objects that behave like files)\n\
962 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000963\n\
964last_type -- type of last uncaught exception\n\
965last_value -- value of last uncaught exception\n\
966last_traceback -- traceback of last uncaught exception\n\
967 These three are only available in an interactive session after a\n\
968 traceback has been printed.\n\
969\n\
970exc_type -- type of exception currently being handled\n\
971exc_value -- value of exception currently being handled\n\
972exc_traceback -- traceback of exception currently being handled\n\
973 The function exc_info() should be used instead of these three,\n\
974 because it is thread-safe.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000975"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000976)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000977/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000978PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +0000979"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000980Static objects:\n\
981\n\
982maxint -- the largest supported integer (the smallest is -maxint-1)\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000983maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000984builtin_module_names -- tuple of module names built into this interpreter\n\
Fred Drake801c08d2000-04-13 15:29:10 +0000985version -- the version of this interpreter as a string\n\
986version_info -- version information as a tuple\n\
987hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000988copyright -- copyright notice pertaining to this interpreter\n\
989platform -- platform identifier\n\
990executable -- pathname of this Python interpreter\n\
991prefix -- prefix used to find the Python library\n\
992exec_prefix -- prefix used to find the machine-specific Python library\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000993"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000994)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000995#ifdef MS_WINDOWS
996/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000997PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000998"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000999winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001000"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001001)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001002#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001003PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001004"__stdin__ -- the original stdin; don't touch!\n\
1005__stdout__ -- the original stdout; don't touch!\n\
1006__stderr__ -- the original stderr; don't touch!\n\
1007__displayhook__ -- the original displayhook; don't touch!\n\
1008__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001009\n\
1010Functions:\n\
1011\n\
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001012displayhook() -- print an object to the screen, and save it in __builtin__._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001013excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001014exc_info() -- return thread-safe information about the current exception\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +00001015exc_clear() -- clear the exception state for the current thread\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001016exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001017getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Georg Brandl56112892008-01-20 13:59:46 +00001018getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001019getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001020getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Georg Brandl56112892008-01-20 13:59:46 +00001021gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001022setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001023setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001024setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001025setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001026settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001027"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001028)
Fred Drakeccede592000-08-14 20:59:57 +00001029/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001030
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +00001031static int
1032_check_and_flush (FILE *stream)
1033{
1034 int prev_fail = ferror (stream);
1035 return fflush (stream) || prev_fail ? EOF : 0;
1036}
1037
Martin v. Löwis43b57802006-01-05 23:38:54 +00001038/* Subversion branch and revision management */
1039static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
1040static const char headurl[] = "$HeadURL$";
1041static int svn_initialized;
1042static char patchlevel_revision[50]; /* Just the number */
1043static char branch[50];
1044static char shortbranch[50];
1045static const char *svn_revision;
1046
Tim Peterse86e7a52006-01-06 02:42:46 +00001047static void
1048svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +00001049{
1050 const char *python, *br_start, *br_end, *br_end2, *svnversion;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001051 Py_ssize_t len;
1052 int istag;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001053
1054 if (svn_initialized)
1055 return;
1056
1057 python = strstr(headurl, "/python/");
1058 if (!python)
1059 Py_FatalError("subversion keywords missing");
1060
1061 br_start = python + 8;
1062 br_end = strchr(br_start, '/');
Neal Norwitz837ce932006-10-28 21:15:30 +00001063 assert(br_end);
1064
Tim Peters216b78b2006-01-06 02:40:53 +00001065 /* Works even for trunk,
Martin v. Löwis43b57802006-01-05 23:38:54 +00001066 as we are in trunk/Python/sysmodule.c */
1067 br_end2 = strchr(br_end+1, '/');
1068
1069 istag = strncmp(br_start, "tags", 4) == 0;
1070 if (strncmp(br_start, "trunk", 5) == 0) {
1071 strcpy(branch, "trunk");
1072 strcpy(shortbranch, "trunk");
1073
Tim Peters216b78b2006-01-06 02:40:53 +00001074 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001075 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1076 len = br_end2 - br_start;
1077 strncpy(branch, br_start, len);
1078 branch[len] = '\0';
1079
1080 len = br_end2 - (br_end + 1);
1081 strncpy(shortbranch, br_end + 1, len);
1082 shortbranch[len] = '\0';
Tim Peters216b78b2006-01-06 02:40:53 +00001083 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001084 else {
1085 Py_FatalError("bad HeadURL");
1086 return;
1087 }
1088
1089
1090 svnversion = _Py_svnversion();
1091 if (strcmp(svnversion, "exported") != 0)
1092 svn_revision = svnversion;
1093 else if (istag) {
1094 len = strlen(_patchlevel_revision);
Neal Norwitz68cdf8a2007-04-16 07:37:55 +00001095 assert(len >= 13);
1096 assert(len < (sizeof(patchlevel_revision) + 13));
Martin v. Löwis43b57802006-01-05 23:38:54 +00001097 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1098 len - 13);
1099 patchlevel_revision[len - 13] = '\0';
1100 svn_revision = patchlevel_revision;
1101 }
1102 else
1103 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001104
Martin v. Löwis43b57802006-01-05 23:38:54 +00001105 svn_initialized = 1;
1106}
1107
1108/* Return svnversion output if available.
1109 Else return Revision of patchlevel.h if on branch.
1110 Else return empty string */
1111const char*
1112Py_SubversionRevision()
1113{
1114 svnversion_init();
1115 return svn_revision;
1116}
1117
1118const char*
1119Py_SubversionShortBranch()
1120{
1121 svnversion_init();
1122 return shortbranch;
1123}
1124
Christian Heimesf31b69f2008-01-14 03:42:48 +00001125
1126PyDoc_STRVAR(flags__doc__,
1127"sys.flags\n\
1128\n\
1129Flags provided through command line arguments or environment vars.");
1130
Christian Heimes796fc312008-01-30 18:58:29 +00001131static PyTypeObject FlagsType = {0, 0, 0, 0, 0, 0};
Christian Heimesf31b69f2008-01-14 03:42:48 +00001132
1133static PyStructSequence_Field flags_fields[] = {
1134 {"debug", "-d"},
1135 {"py3k_warning", "-3"},
1136 {"division_warning", "-Q"},
1137 {"division_new", "-Qnew"},
1138 {"inspect", "-i"},
1139 {"interactive", "-i"},
1140 {"optimize", "-O or -OO"},
1141 {"dont_write_bytecode", "-B"},
1142 /* {"no_user_site", "-s"}, */
1143 {"no_site", "-S"},
Andrew M. Kuchling7ce9b182008-01-15 01:29:16 +00001144 {"ignore_environment", "-E"},
Christian Heimesf31b69f2008-01-14 03:42:48 +00001145 {"tabcheck", "-t or -tt"},
1146 {"verbose", "-v"},
1147#ifdef RISCOS
Andrew M. Kuchling7ce9b182008-01-15 01:29:16 +00001148 {"riscos_wimp", "???"},
Christian Heimesf31b69f2008-01-14 03:42:48 +00001149#endif
1150 /* {"unbuffered", "-u"}, */
1151 {"unicode", "-U"},
1152 /* {"skip_first", "-x"}, */
1153 {0}
1154};
1155
1156static PyStructSequence_Desc flags_desc = {
1157 "sys.flags", /* name */
1158 flags__doc__, /* doc */
1159 flags_fields, /* fields */
1160#ifdef RISCOS
1161 14
1162#else
1163 13
1164#endif
1165};
1166
1167static PyObject*
1168make_flags(void)
1169{
1170 int pos = 0;
1171 PyObject *seq;
1172
1173 seq = PyStructSequence_New(&FlagsType);
1174 if (seq == NULL)
1175 return NULL;
1176
1177#define SetFlag(flag) \
1178 PyStructSequence_SET_ITEM(seq, pos++, PyInt_FromLong(flag))
1179
1180 SetFlag(Py_DebugFlag);
1181 SetFlag(Py_Py3kWarningFlag);
1182 SetFlag(Py_DivisionWarningFlag);
1183 SetFlag(_Py_QnewFlag);
1184 SetFlag(Py_InspectFlag);
1185 SetFlag(Py_InteractiveFlag);
1186 SetFlag(Py_OptimizeFlag);
1187 SetFlag(Py_DontWriteBytecodeFlag);
1188 /* SetFlag(Py_NoUserSiteDirectory); */
1189 SetFlag(Py_NoSiteFlag);
1190 SetFlag(Py_IgnoreEnvironmentFlag);
1191 SetFlag(Py_TabcheckFlag);
1192 SetFlag(Py_VerboseFlag);
1193#ifdef RISCOS
1194 SetFlag(Py_RISCOSWimpFlag);
1195#endif
1196 /* SetFlag(saw_unbuffered_flag); */
1197 SetFlag(Py_UnicodeFlag);
1198 /* SetFlag(skipfirstline); */
1199#undef SetFlag
1200
1201 if (PyErr_Occurred()) {
1202 return NULL;
1203 }
Christian Heimesf31b69f2008-01-14 03:42:48 +00001204 return seq;
1205}
1206
Guido van Rossum25ce5661997-08-02 03:10:38 +00001207PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001208_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001209{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001210 PyObject *m, *v, *sysdict;
1211 PyObject *sysin, *sysout, *syserr;
Fred Drake6d27c1e2000-04-13 20:03:20 +00001212 char *s;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001213#ifdef MS_WINDOWS
Tim Peters02f1d0d2006-06-06 00:25:07 +00001214 char buf[128];
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001215#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +00001216
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001217 m = Py_InitModule3("sys", sys_methods, sys_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001218 if (m == NULL)
1219 return NULL;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001220 sysdict = PyModule_GetDict(m);
Christian Heimes0d924432008-01-30 17:21:22 +00001221#define SET_SYS_FROM_STRING(key, value) \
1222 v = value; \
1223 if (v != NULL) \
1224 PyDict_SetItemString(sysdict, key, v); \
1225 Py_XDECREF(v)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001226
Neal Norwitz11bd1192005-10-03 00:54:56 +00001227 {
1228 /* XXX: does this work on Win/Win64? (see posix_fstat) */
1229 struct stat sb;
1230 if (fstat(fileno(stdin), &sb) == 0 &&
1231 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +00001232 /* There's nothing more we can do. */
1233 /* Py_FatalError() will core dump, so just exit. */
1234 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1235 exit(EXIT_FAILURE);
Neal Norwitz11bd1192005-10-03 00:54:56 +00001236 }
1237 }
1238
Martin v. Löwis13a1fde2005-01-27 18:56:16 +00001239 /* Closing the standard FILE* if sys.std* goes aways causes problems
1240 * for embedded Python usages. Closing them when somebody explicitly
1241 * invokes .close() might be possible, but the FAQ promises they get
1242 * never closed. However, we still need to get write errors when
1243 * writing fails (e.g. because stdout is redirected), so we flush the
1244 * streams and check for errors before the file objects are deleted.
1245 * On OS X, fflush()ing stdin causes an error, so we exempt stdin
1246 * from that procedure.
1247 */
1248 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
Martin v. Löwis8e3ca8a2005-01-23 09:41:49 +00001249 sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
1250 syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001251 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001252 return NULL;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001253#ifdef MS_WINDOWS
Thomas Woutersafea5292007-01-23 13:42:00 +00001254 if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001255 sprintf(buf, "cp%d", GetConsoleCP());
1256 if (!PyFile_SetEncoding(sysin, buf))
1257 return NULL;
1258 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001259 if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001260 sprintf(buf, "cp%d", GetConsoleOutputCP());
1261 if (!PyFile_SetEncoding(sysout, buf))
1262 return NULL;
1263 }
Thomas Woutersafea5292007-01-23 13:42:00 +00001264 if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) {
Martin v. Löwisea62d252006-04-03 10:56:49 +00001265 sprintf(buf, "cp%d", GetConsoleOutputCP());
1266 if (!PyFile_SetEncoding(syserr, buf))
1267 return NULL;
1268 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001269#endif
1270
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001271 PyDict_SetItemString(sysdict, "stdin", sysin);
1272 PyDict_SetItemString(sysdict, "stdout", sysout);
1273 PyDict_SetItemString(sysdict, "stderr", syserr);
Guido van Rossumbd36dba1998-02-19 20:53:06 +00001274 /* Make backup copies for cleanup */
1275 PyDict_SetItemString(sysdict, "__stdin__", sysin);
1276 PyDict_SetItemString(sysdict, "__stdout__", sysout);
1277 PyDict_SetItemString(sysdict, "__stderr__", syserr);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001278 PyDict_SetItemString(sysdict, "__displayhook__",
1279 PyDict_GetItemString(sysdict, "displayhook"));
1280 PyDict_SetItemString(sysdict, "__excepthook__",
1281 PyDict_GetItemString(sysdict, "excepthook"));
Guido van Rossum25ce5661997-08-02 03:10:38 +00001282 Py_XDECREF(sysin);
1283 Py_XDECREF(sysout);
1284 Py_XDECREF(syserr);
Christian Heimes0d924432008-01-30 17:21:22 +00001285
1286 SET_SYS_FROM_STRING("version",
1287 PyString_FromString(Py_GetVersion()));
1288 SET_SYS_FROM_STRING("hexversion",
1289 PyInt_FromLong(PY_VERSION_HEX));
Martin v. Löwis43b57802006-01-05 23:38:54 +00001290 svnversion_init();
Christian Heimes0d924432008-01-30 17:21:22 +00001291 SET_SYS_FROM_STRING("subversion",
1292 Py_BuildValue("(ssz)", "CPython", branch,
1293 svn_revision));
1294 SET_SYS_FROM_STRING("dont_write_bytecode",
1295 PyBool_FromLong(Py_DontWriteBytecodeFlag));
Fred Drake93a20bf2000-04-13 17:44:51 +00001296 /*
1297 * These release level checks are mutually exclusive and cover
1298 * the field, so don't get too fancy with the pre-processor!
1299 */
1300#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001301 s = "alpha";
Fred Drake592f2d62000-08-31 15:21:11 +00001302#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001303 s = "beta";
Fred Drake592f2d62000-08-31 15:21:11 +00001304#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Fred Drake6d27c1e2000-04-13 20:03:20 +00001305 s = "candidate";
Fred Drake592f2d62000-08-31 15:21:11 +00001306#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Fred Drake6d27c1e2000-04-13 20:03:20 +00001307 s = "final";
Fred Drake93a20bf2000-04-13 17:44:51 +00001308#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001309
Neal Norwitze1fdb322006-07-21 05:32:28 +00001310 SET_SYS_FROM_STRING("version_info",
1311 Py_BuildValue("iiisi", PY_MAJOR_VERSION,
Fred Drake801c08d2000-04-13 15:29:10 +00001312 PY_MINOR_VERSION,
Fred Drake6d27c1e2000-04-13 20:03:20 +00001313 PY_MICRO_VERSION, s,
Fred Drake93a20bf2000-04-13 17:44:51 +00001314 PY_RELEASE_SERIAL));
Neal Norwitze1fdb322006-07-21 05:32:28 +00001315 SET_SYS_FROM_STRING("api_version",
1316 PyInt_FromLong(PYTHON_API_VERSION));
1317 SET_SYS_FROM_STRING("copyright",
1318 PyString_FromString(Py_GetCopyright()));
1319 SET_SYS_FROM_STRING("platform",
1320 PyString_FromString(Py_GetPlatform()));
1321 SET_SYS_FROM_STRING("executable",
1322 PyString_FromString(Py_GetProgramFullPath()));
1323 SET_SYS_FROM_STRING("prefix",
1324 PyString_FromString(Py_GetPrefix()));
1325 SET_SYS_FROM_STRING("exec_prefix",
1326 PyString_FromString(Py_GetExecPrefix()));
1327 SET_SYS_FROM_STRING("maxint",
1328 PyInt_FromLong(PyInt_GetMax()));
Christian Heimes28104c52007-11-27 23:16:44 +00001329 SET_SYS_FROM_STRING("py3kwarning",
1330 PyBool_FromLong(Py_Py3kWarningFlag));
Christian Heimesdfdfaab2007-12-01 11:20:10 +00001331 SET_SYS_FROM_STRING("float_info",
1332 PyFloat_GetInfo());
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001333#ifdef Py_USING_UNICODE
Neal Norwitze1fdb322006-07-21 05:32:28 +00001334 SET_SYS_FROM_STRING("maxunicode",
1335 PyInt_FromLong(PyUnicode_GetMax()));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001336#endif
Neal Norwitze1fdb322006-07-21 05:32:28 +00001337 SET_SYS_FROM_STRING("builtin_module_names",
1338 list_builtin_module_names());
Fred Drake099325e2000-08-14 15:47:03 +00001339 {
1340 /* Assumes that longs are at least 2 bytes long.
1341 Should be safe! */
1342 unsigned long number = 1;
Fred Drakea2b6ad62000-08-15 04:24:43 +00001343 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001344
1345 s = (char *) &number;
1346 if (s[0] == 0)
Fred Drakea2b6ad62000-08-15 04:24:43 +00001347 value = "big";
Fred Drake099325e2000-08-14 15:47:03 +00001348 else
Fred Drakea2b6ad62000-08-15 04:24:43 +00001349 value = "little";
Neal Norwitze1fdb322006-07-21 05:32:28 +00001350 SET_SYS_FROM_STRING("byteorder",
1351 PyString_FromString(value));
Fred Drake099325e2000-08-14 15:47:03 +00001352 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001353#ifdef MS_COREDLL
Neal Norwitze1fdb322006-07-21 05:32:28 +00001354 SET_SYS_FROM_STRING("dllhandle",
1355 PyLong_FromVoidPtr(PyWin_DLLhModule));
1356 SET_SYS_FROM_STRING("winver",
1357 PyString_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001358#endif
Guido van Rossum23fff912000-12-15 22:02:05 +00001359 if (warnoptions == NULL) {
1360 warnoptions = PyList_New(0);
1361 }
1362 else {
1363 Py_INCREF(warnoptions);
1364 }
1365 if (warnoptions != NULL) {
Guido van Rossum03df3b32001-01-13 22:06:05 +00001366 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
Guido van Rossum23fff912000-12-15 22:02:05 +00001367 }
Tim Peters216b78b2006-01-06 02:40:53 +00001368
Christian Heimes796fc312008-01-30 18:58:29 +00001369 if (FlagsType.tp_name == 0)
1370 PyStructSequence_InitType(&FlagsType, &flags_desc);
Christian Heimes0d924432008-01-30 17:21:22 +00001371 SET_SYS_FROM_STRING("flags", make_flags());
Christian Heimesf31b69f2008-01-14 03:42:48 +00001372 /* prevent user from creating new instances */
1373 FlagsType.tp_init = NULL;
1374 FlagsType.tp_new = NULL;
1375
Christian Heimes0d924432008-01-30 17:21:22 +00001376#undef SET_SYS_FROM_STRING
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001377 if (PyErr_Occurred())
Guido van Rossum25ce5661997-08-02 03:10:38 +00001378 return NULL;
1379 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001380}
1381
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001382static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001383makepathobject(char *path, int delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001384{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001385 int i, n;
1386 char *p;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001387 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001388
Guido van Rossum3f5da241990-12-20 15:06:42 +00001389 n = 1;
1390 p = path;
1391 while ((p = strchr(p, delim)) != NULL) {
1392 n++;
1393 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001394 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001395 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001396 if (v == NULL)
1397 return NULL;
1398 for (i = 0; ; i++) {
1399 p = strchr(path, delim);
1400 if (p == NULL)
1401 p = strchr(path, '\0'); /* End of string */
Armin Rigo7ccbca92006-10-04 12:17:45 +00001402 w = PyString_FromStringAndSize(path, (Py_ssize_t) (p - path));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001403 if (w == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001404 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001405 return NULL;
1406 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001407 PyList_SetItem(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001408 if (*p == '\0')
1409 break;
1410 path = p+1;
1411 }
1412 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001413}
1414
1415void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001416PySys_SetPath(char *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001417{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001418 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001419 if ((v = makepathobject(path, DELIM)) == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001420 Py_FatalError("can't create sys.path");
1421 if (PySys_SetObject("path", v) != 0)
1422 Py_FatalError("can't assign sys.path");
1423 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001424}
1425
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001426static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001427makeargvobject(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001428{
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001429 PyObject *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +00001430 if (argc <= 0 || argv == NULL) {
1431 /* Ensure at least one (empty) argument is seen */
1432 static char *empty_argv[1] = {""};
1433 argv = empty_argv;
1434 argc = 1;
1435 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001436 av = PyList_New(argc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001437 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001438 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001439 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001440#ifdef __VMS
1441 PyObject *v;
1442
1443 /* argv[0] is the script pathname if known */
1444 if (i == 0) {
1445 char* fn = decc$translate_vms(argv[0]);
1446 if ((fn == (char *)0) || fn == (char *)-1)
1447 v = PyString_FromString(argv[0]);
1448 else
1449 v = PyString_FromString(
1450 decc$translate_vms(argv[0]));
1451 } else
1452 v = PyString_FromString(argv[i]);
1453#else
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001454 PyObject *v = PyString_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001455#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001456 if (v == NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001457 Py_DECREF(av);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001458 av = NULL;
1459 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001460 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001461 PyList_SetItem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001462 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001463 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001464 return av;
1465}
1466
1467void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001468PySys_SetArgv(int argc, char **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001469{
Guido van Rossum162e38c2003-02-19 15:25:10 +00001470#if defined(HAVE_REALPATH)
1471 char fullpath[MAXPATHLEN];
1472#elif defined(MS_WINDOWS)
Thomas Heller27bb71e2003-01-08 14:33:48 +00001473 char fullpath[MAX_PATH];
1474#endif
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001475 PyObject *av = makeargvobject(argc, argv);
1476 PyObject *path = PySys_GetObject("path");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001477 if (av == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001478 Py_FatalError("no mem for sys.argv");
1479 if (PySys_SetObject("argv", av) != 0)
1480 Py_FatalError("can't assign sys.argv");
Guido van Rossum94a96671996-07-30 20:35:50 +00001481 if (path != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001482 char *argv0 = argv[0];
Guido van Rossum94a96671996-07-30 20:35:50 +00001483 char *p = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001484 Py_ssize_t n = 0;
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001485 PyObject *a;
Guido van Rossumc474dea1997-04-25 15:38:31 +00001486#ifdef HAVE_READLINK
1487 char link[MAXPATHLEN+1];
1488 char argv0copy[2*MAXPATHLEN+1];
1489 int nr = 0;
Georg Brandl69537722005-09-15 13:00:34 +00001490 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0)
Guido van Rossumc474dea1997-04-25 15:38:31 +00001491 nr = readlink(argv0, link, MAXPATHLEN);
1492 if (nr > 0) {
1493 /* It's a symlink */
1494 link[nr] = '\0';
1495 if (link[0] == SEP)
1496 argv0 = link; /* Link to absolute path */
1497 else if (strchr(link, SEP) == NULL)
1498 ; /* Link without path */
1499 else {
1500 /* Must join(dirname(argv0), link) */
1501 char *q = strrchr(argv0, SEP);
1502 if (q == NULL)
1503 argv0 = link; /* argv0 without path */
1504 else {
1505 /* Must make a copy */
1506 strcpy(argv0copy, argv0);
1507 q = strrchr(argv0copy, SEP);
1508 strcpy(q+1, link);
1509 argv0 = argv0copy;
1510 }
1511 }
1512 }
1513#endif /* HAVE_READLINK */
Guido van Rossumcc883411996-09-10 14:44:21 +00001514#if SEP == '\\' /* Special case for MS filename syntax */
Georg Brandl69537722005-09-15 13:00:34 +00001515 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossumcc883411996-09-10 14:44:21 +00001516 char *q;
Thomas Heller27bb71e2003-01-08 14:33:48 +00001517#ifdef MS_WINDOWS
1518 char *ptemp;
1519 if (GetFullPathName(argv0,
1520 sizeof(fullpath),
1521 fullpath,
1522 &ptemp)) {
1523 argv0 = fullpath;
1524 }
1525#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001526 p = strrchr(argv0, SEP);
Guido van Rossumcc883411996-09-10 14:44:21 +00001527 /* Test for alternate separator */
Guido van Rossumc474dea1997-04-25 15:38:31 +00001528 q = strrchr(p ? p : argv0, '/');
Guido van Rossumcc883411996-09-10 14:44:21 +00001529 if (q != NULL)
1530 p = q;
1531 if (p != NULL) {
Guido van Rossumc474dea1997-04-25 15:38:31 +00001532 n = p + 1 - argv0;
Guido van Rossumcc883411996-09-10 14:44:21 +00001533 if (n > 1 && p[-1] != ':')
1534 n--; /* Drop trailing separator */
1535 }
1536 }
1537#else /* All other filename syntaxes */
Georg Brandl69537722005-09-15 13:00:34 +00001538 if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
Guido van Rossum162e38c2003-02-19 15:25:10 +00001539#if defined(HAVE_REALPATH)
1540 if (realpath(argv0, fullpath)) {
1541 argv0 = fullpath;
1542 }
1543#endif
Guido van Rossumc474dea1997-04-25 15:38:31 +00001544 p = strrchr(argv0, SEP);
Guido van Rossum162e38c2003-02-19 15:25:10 +00001545 }
Guido van Rossumcc883411996-09-10 14:44:21 +00001546 if (p != NULL) {
Guido van Rossumbceccf52001-04-10 22:07:43 +00001547#ifndef RISCOS
Guido van Rossumc474dea1997-04-25 15:38:31 +00001548 n = p + 1 - argv0;
Guido van Rossumbceccf52001-04-10 22:07:43 +00001549#else /* don't include trailing separator */
1550 n = p - argv0;
1551#endif /* RISCOS */
Guido van Rossumcc883411996-09-10 14:44:21 +00001552#if SEP == '/' /* Special case for Unix filename syntax */
1553 if (n > 1)
1554 n--; /* Drop trailing separator */
1555#endif /* Unix */
1556 }
1557#endif /* All others */
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001558 a = PyString_FromStringAndSize(argv0, n);
Guido van Rossum94a96671996-07-30 20:35:50 +00001559 if (a == NULL)
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001560 Py_FatalError("no mem for sys.path insertion");
1561 if (PyList_Insert(path, 0, a) < 0)
1562 Py_FatalError("sys.path.insert(0) failed");
1563 Py_DECREF(a);
Guido van Rossuma63d9f41996-07-24 01:31:37 +00001564 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001565 Py_DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001566}
Guido van Rossuma890e681998-05-12 14:59:24 +00001567
1568
1569/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1570 Adapted from code submitted by Just van Rossum.
1571
1572 PySys_WriteStdout(format, ...)
1573 PySys_WriteStderr(format, ...)
1574
1575 The first function writes to sys.stdout; the second to sys.stderr. When
1576 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001577 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001578
1579 Both take a printf-style format string as their first argument followed
1580 by a variable length argument list determined by the format string.
1581
1582 *** WARNING ***
1583
1584 The format should limit the total size of the formatted output string to
1585 1000 bytes. In particular, this means that no unrestricted "%s" formats
1586 should occur; these should be limited using "%.<N>s where <N> is a
1587 decimal number calculated so that <N> plus the maximum size of other
1588 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1589 which can print hundreds of digits for very large numbers.
1590
1591 */
1592
1593static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001594mywrite(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001595{
1596 PyObject *file;
Guido van Rossum8442af31998-10-12 18:22:10 +00001597 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossuma890e681998-05-12 14:59:24 +00001598
Guido van Rossum8442af31998-10-12 18:22:10 +00001599 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001600 file = PySys_GetObject(name);
1601 if (file == NULL || PyFile_AsFile(file) == fp)
1602 vfprintf(fp, format, va);
1603 else {
1604 char buffer[1001];
Tim Peters080d5b32001-12-02 08:29:16 +00001605 const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1606 format, va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001607 if (PyFile_WriteString(buffer, file) != 0) {
1608 PyErr_Clear();
1609 fputs(buffer, fp);
1610 }
Skip Montanaro53a6d1d2006-04-18 00:55:46 +00001611 if (written < 0 || (size_t)written >= sizeof(buffer)) {
Jeremy Hylton5d3d1342001-11-28 21:44:53 +00001612 const char *truncated = "... truncated";
1613 if (PyFile_WriteString(truncated, file) != 0) {
1614 PyErr_Clear();
1615 fputs(truncated, fp);
1616 }
1617 }
Guido van Rossuma890e681998-05-12 14:59:24 +00001618 }
Guido van Rossum8442af31998-10-12 18:22:10 +00001619 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001620}
1621
1622void
Guido van Rossuma890e681998-05-12 14:59:24 +00001623PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001624{
1625 va_list va;
1626
Guido van Rossuma890e681998-05-12 14:59:24 +00001627 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001628 mywrite("stdout", stdout, format, va);
1629 va_end(va);
1630}
1631
1632void
Guido van Rossuma890e681998-05-12 14:59:24 +00001633PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001634{
1635 va_list va;
1636
Guido van Rossuma890e681998-05-12 14:59:24 +00001637 va_start(va, format);
Guido van Rossuma890e681998-05-12 14:59:24 +00001638 mywrite("stderr", stderr, format, va);
1639 va_end(va);
1640}