blob: b8cf31d435134f0ed39850407b2ecd6859d4e4b9 [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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018#include "code.h"
Barry Warsawb6a54d22000-12-06 21:47:46 +000019#include "frameobject.h"
Victor Stinnerd5c355c2011-04-30 14:53:09 +020020#include "pythread.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossume2437a11992-03-23 18:20:18 +000022#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000023
Mark Hammond8696ebc2002-10-08 02:44:31 +000024#ifdef MS_WINDOWS
25#define WIN32_LEAN_AND_MEAN
Amaury Forgeot d'Arc06cfe952007-11-10 13:55:44 +000026#include <windows.h>
Mark Hammond8696ebc2002-10-08 02:44:31 +000027#endif /* MS_WINDOWS */
28
Guido van Rossum9b38a141996-09-11 23:12:24 +000029#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000030extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000031/* A string loaded from the DLL at startup: */
32extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000033#endif
34
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000035#ifdef __VMS
36#include <unixlib.h>
37#endif
38
Martin v. Löwis5467d4c2003-05-10 07:10:12 +000039#ifdef HAVE_LANGINFO_H
40#include <locale.h>
41#include <langinfo.h>
42#endif
43
Guido van Rossum65bf9f21997-04-29 18:33:38 +000044PyObject *
Neal Norwitzf3081322007-08-25 00:32:45 +000045PySys_GetObject(const char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 PyThreadState *tstate = PyThreadState_GET();
48 PyObject *sd = tstate->interp->sysdict;
49 if (sd == NULL)
50 return NULL;
51 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052}
53
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054int
Neal Norwitzf3081322007-08-25 00:32:45 +000055PySys_SetObject(const char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 PyThreadState *tstate = PyThreadState_GET();
58 PyObject *sd = tstate->interp->sysdict;
59 if (v == NULL) {
60 if (PyDict_GetItemString(sd, name) == NULL)
61 return 0;
62 else
63 return PyDict_DelItemString(sd, name);
64 }
65 else
66 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Victor Stinner13d49ee2010-12-04 17:24:33 +000069/* Write repr(o) to sys.stdout using sys.stdout.encoding and 'backslashreplace'
70 error handler. If sys.stdout has a buffer attribute, use
71 sys.stdout.buffer.write(encoded), otherwise redecode the string and use
72 sys.stdout.write(redecoded).
73
74 Helper function for sys_displayhook(). */
75static int
76sys_displayhook_unencodable(PyObject *outf, PyObject *o)
77{
78 PyObject *stdout_encoding = NULL;
79 PyObject *encoded, *escaped_str, *repr_str, *buffer, *result;
80 char *stdout_encoding_str;
81 int ret;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020082 _Py_IDENTIFIER(encoding);
83 _Py_IDENTIFIER(buffer);
Victor Stinner13d49ee2010-12-04 17:24:33 +000084
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020085 stdout_encoding = _PyObject_GetAttrId(outf, &PyId_encoding);
Victor Stinner13d49ee2010-12-04 17:24:33 +000086 if (stdout_encoding == NULL)
87 goto error;
88 stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
89 if (stdout_encoding_str == NULL)
90 goto error;
91
92 repr_str = PyObject_Repr(o);
93 if (repr_str == NULL)
94 goto error;
95 encoded = PyUnicode_AsEncodedString(repr_str,
96 stdout_encoding_str,
97 "backslashreplace");
98 Py_DECREF(repr_str);
99 if (encoded == NULL)
100 goto error;
101
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200102 buffer = _PyObject_GetAttrId(outf, &PyId_buffer);
Victor Stinner13d49ee2010-12-04 17:24:33 +0000103 if (buffer) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200104 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200105 result = _PyObject_CallMethodId(buffer, &PyId_write, "(O)", encoded);
Victor Stinner13d49ee2010-12-04 17:24:33 +0000106 Py_DECREF(buffer);
107 Py_DECREF(encoded);
108 if (result == NULL)
109 goto error;
110 Py_DECREF(result);
111 }
112 else {
113 PyErr_Clear();
114 escaped_str = PyUnicode_FromEncodedObject(encoded,
115 stdout_encoding_str,
116 "strict");
117 Py_DECREF(encoded);
118 if (PyFile_WriteObject(escaped_str, outf, Py_PRINT_RAW) != 0) {
119 Py_DECREF(escaped_str);
120 goto error;
121 }
122 Py_DECREF(escaped_str);
123 }
124 ret = 0;
125 goto finally;
126
127error:
128 ret = -1;
129finally:
130 Py_XDECREF(stdout_encoding);
131 return ret;
132}
133
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000134static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000135sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 PyObject *outf;
138 PyInterpreterState *interp = PyThreadState_GET()->interp;
139 PyObject *modules = interp->modules;
Victor Stinnerd02fbb82013-11-06 18:27:13 +0100140 PyObject *builtins;
141 static PyObject *newline = NULL;
Victor Stinner13d49ee2010-12-04 17:24:33 +0000142 int err;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200143 _Py_IDENTIFIER(_);
Victor Stinnerd02fbb82013-11-06 18:27:13 +0100144 _Py_IDENTIFIER(builtins);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000145
Victor Stinnerd02fbb82013-11-06 18:27:13 +0100146 builtins = _PyDict_GetItemId(modules, &PyId_builtins);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (builtins == NULL) {
148 PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
149 return NULL;
150 }
Moshe Zadka03897ea2001-07-23 13:32:43 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 /* Print value except if None */
153 /* After printing, also assign to '_' */
154 /* Before, set '_' to None to avoid recursion */
155 if (o == Py_None) {
156 Py_INCREF(Py_None);
157 return Py_None;
158 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200159 if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return NULL;
161 outf = PySys_GetObject("stdout");
162 if (outf == NULL || outf == Py_None) {
163 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
164 return NULL;
165 }
Victor Stinner13d49ee2010-12-04 17:24:33 +0000166 if (PyFile_WriteObject(o, outf, 0) != 0) {
167 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
168 /* repr(o) is not encodable to sys.stdout.encoding with
169 * sys.stdout.errors error handler (which is probably 'strict') */
170 PyErr_Clear();
171 err = sys_displayhook_unencodable(outf, o);
172 if (err)
173 return NULL;
174 }
175 else {
176 return NULL;
177 }
178 }
Victor Stinnerd02fbb82013-11-06 18:27:13 +0100179 if (newline == NULL) {
180 newline = PyUnicode_FromString("\n");
181 if (newline == NULL)
182 return NULL;
183 }
184 if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200186 if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 return NULL;
188 Py_INCREF(Py_None);
189 return Py_None;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000190}
191
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000192PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000193"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000194"\n"
Florent Xicluna5749e852010-03-03 11:54:54 +0000195"Print an object to sys.stdout and also save it in builtins._\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000196);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000197
198static PyObject *
199sys_excepthook(PyObject* self, PyObject* args)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 PyObject *exc, *value, *tb;
202 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
203 return NULL;
204 PyErr_Display(exc, value, tb);
205 Py_INCREF(Py_None);
206 return Py_None;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000207}
208
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000209PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000210"excepthook(exctype, value, traceback) -> None\n"
211"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000212"Handle an exception by displaying it with a traceback on sys.stderr.\n"
213);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000214
215static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000216sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 PyThreadState *tstate;
219 tstate = PyThreadState_GET();
220 return Py_BuildValue(
221 "(OOO)",
222 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
223 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
224 tstate->exc_traceback != NULL ?
225 tstate->exc_traceback : Py_None);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000226}
227
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000228PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000229"exc_info() -> (type, value, traceback)\n\
230\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000231Return information about the most recent exception caught by an except\n\
232clause in the current stack frame or in an older stack frame."
233);
234
235static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 PyObject *exit_code = 0;
239 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
240 return NULL;
241 /* Raise SystemExit so callers may catch it or clean up. */
242 PyErr_SetObject(PyExc_SystemExit, exit_code);
243 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000246PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000247"exit([status])\n\
248\n\
249Exit the interpreter by raising SystemExit(status).\n\
250If the status is omitted or None, it defaults to zero (i.e., success).\n\
Ezio Melotti4af4d272013-08-26 14:00:39 +0300251If the status is an integer, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000252If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000253exit status will be one (i.e., failure)."
254);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000255
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000256
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000257static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000258sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
Fred Drake8b4d01d2000-05-09 19:57:01 +0000261}
262
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000263PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000264"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000265\n\
266Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000267implementation."
268);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000269
270static PyObject *
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000271sys_getfilesystemencoding(PyObject *self)
272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (Py_FileSystemDefaultEncoding)
274 return PyUnicode_FromString(Py_FileSystemDefaultEncoding);
Victor Stinner27181ac2011-03-31 13:39:03 +0200275 PyErr_SetString(PyExc_RuntimeError,
276 "filesystem encoding is not initialized");
277 return NULL;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000278}
279
280PyDoc_STRVAR(getfilesystemencoding_doc,
281"getfilesystemencoding() -> string\n\
282\n\
283Return the encoding used to convert Unicode filenames in\n\
284operating system filenames."
285);
286
Martin v. Löwis04dc25c2008-10-03 16:09:28 +0000287static PyObject *
Georg Brandl66a796e2006-12-19 20:50:34 +0000288sys_intern(PyObject *self, PyObject *args)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyObject *s;
291 if (!PyArg_ParseTuple(args, "U:intern", &s))
292 return NULL;
293 if (PyUnicode_CheckExact(s)) {
294 Py_INCREF(s);
295 PyUnicode_InternInPlace(&s);
296 return s;
297 }
298 else {
299 PyErr_Format(PyExc_TypeError,
300 "can't intern %.400s", s->ob_type->tp_name);
301 return NULL;
302 }
Georg Brandl66a796e2006-12-19 20:50:34 +0000303}
304
305PyDoc_STRVAR(intern_doc,
306"intern(string) -> string\n\
307\n\
308``Intern'' the given string. This enters the string in the (global)\n\
309table of interned strings whose purpose is to speed up dictionary lookups.\n\
310Return the string itself or the previously interned string object with the\n\
311same value.");
312
313
Fred Drake5755ce62001-06-27 19:19:46 +0000314/*
315 * Cached interned string objects used for calling the profile and
316 * trace functions. Initialized by trace_init().
317 */
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000318static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Fred Drake5755ce62001-06-27 19:19:46 +0000319
320static int
321trace_init(void)
322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 static char *whatnames[7] = {"call", "exception", "line", "return",
324 "c_call", "c_exception", "c_return"};
325 PyObject *name;
326 int i;
327 for (i = 0; i < 7; ++i) {
328 if (whatstrings[i] == NULL) {
329 name = PyUnicode_InternFromString(whatnames[i]);
330 if (name == NULL)
331 return -1;
332 whatstrings[i] = name;
333 }
334 }
335 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000336}
337
338
339static PyObject *
340call_trampoline(PyThreadState *tstate, PyObject* callback,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyFrameObject *frame, int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000342{
Victor Stinner41bb43a2013-10-29 01:19:37 +0100343 PyObject *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 PyObject *whatstr;
345 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000346
Victor Stinner41bb43a2013-10-29 01:19:37 +0100347 args = PyTuple_New(3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (args == NULL)
349 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100350 if (PyFrame_FastToLocalsWithError(frame) < 0)
351 return NULL;
352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_INCREF(frame);
354 whatstr = whatstrings[what];
355 Py_INCREF(whatstr);
356 if (arg == NULL)
357 arg = Py_None;
358 Py_INCREF(arg);
359 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
360 PyTuple_SET_ITEM(args, 1, whatstr);
361 PyTuple_SET_ITEM(args, 2, arg);
Fred Drake5755ce62001-06-27 19:19:46 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* call the Python-level function */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 result = PyEval_CallObject(callback, args);
365 PyFrame_LocalsToFast(frame, 1);
366 if (result == NULL)
367 PyTraceBack_Here(frame);
Fred Drake5755ce62001-06-27 19:19:46 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* cleanup */
370 Py_DECREF(args);
371 return result;
Fred Drake5755ce62001-06-27 19:19:46 +0000372}
373
374static int
375profile_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 PyThreadState *tstate = frame->f_tstate;
379 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 if (arg == NULL)
382 arg = Py_None;
383 result = call_trampoline(tstate, self, frame, what, arg);
384 if (result == NULL) {
385 PyEval_SetProfile(NULL, NULL);
386 return -1;
387 }
388 Py_DECREF(result);
389 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000390}
391
392static int
393trace_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyThreadState *tstate = frame->f_tstate;
397 PyObject *callback;
398 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (what == PyTrace_CALL)
401 callback = self;
402 else
403 callback = frame->f_trace;
404 if (callback == NULL)
405 return 0;
406 result = call_trampoline(tstate, callback, frame, what, arg);
407 if (result == NULL) {
408 PyEval_SetTrace(NULL, NULL);
409 Py_XDECREF(frame->f_trace);
410 frame->f_trace = NULL;
411 return -1;
412 }
413 if (result != Py_None) {
414 PyObject *temp = frame->f_trace;
415 frame->f_trace = NULL;
416 Py_XDECREF(temp);
417 frame->f_trace = result;
418 }
419 else {
420 Py_DECREF(result);
421 }
422 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000423}
Fred Draked0838392001-06-16 21:02:31 +0000424
Fred Drake8b4d01d2000-05-09 19:57:01 +0000425static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000426sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (trace_init() == -1)
429 return NULL;
430 if (args == Py_None)
431 PyEval_SetTrace(NULL, NULL);
432 else
433 PyEval_SetTrace(trace_trampoline, args);
434 Py_INCREF(Py_None);
435 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000436}
437
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000438PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000439"settrace(function)\n\
440\n\
441Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000442function call. See the debugger chapter in the library manual."
443);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000444
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000445static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000446sys_gettrace(PyObject *self, PyObject *args)
447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyThreadState *tstate = PyThreadState_GET();
449 PyObject *temp = tstate->c_traceobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (temp == NULL)
452 temp = Py_None;
453 Py_INCREF(temp);
454 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000455}
456
457PyDoc_STRVAR(gettrace_doc,
458"gettrace()\n\
459\n\
460Return the global debug tracing function set with sys.settrace.\n\
461See the debugger chapter in the library manual."
462);
463
464static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000465sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (trace_init() == -1)
468 return NULL;
469 if (args == Py_None)
470 PyEval_SetProfile(NULL, NULL);
471 else
472 PyEval_SetProfile(profile_trampoline, args);
473 Py_INCREF(Py_None);
474 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000475}
476
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000477PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000478"setprofile(function)\n\
479\n\
480Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000481and return. See the profiler chapter in the library manual."
482);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000483
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000484static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000485sys_getprofile(PyObject *self, PyObject *args)
486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 PyThreadState *tstate = PyThreadState_GET();
488 PyObject *temp = tstate->c_profileobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if (temp == NULL)
491 temp = Py_None;
492 Py_INCREF(temp);
493 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000494}
495
496PyDoc_STRVAR(getprofile_doc,
497"getprofile()\n\
498\n\
499Return the profiling function set with sys.setprofile.\n\
500See the profiler chapter in the library manual."
501);
502
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000503static int _check_interval = 100;
504
Christian Heimes9bd667a2008-01-20 15:14:11 +0000505static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000506sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (PyErr_WarnEx(PyExc_DeprecationWarning,
509 "sys.getcheckinterval() and sys.setcheckinterval() "
510 "are deprecated. Use sys.setswitchinterval() "
511 "instead.", 1) < 0)
512 return NULL;
513 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval))
514 return NULL;
515 Py_INCREF(Py_None);
516 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000517}
518
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000519PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000520"setcheckinterval(n)\n\
521\n\
522Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000523n instructions. This also affects how often thread switches occur."
524);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000525
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000526static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000527sys_getcheckinterval(PyObject *self, PyObject *args)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (PyErr_WarnEx(PyExc_DeprecationWarning,
530 "sys.getcheckinterval() and sys.setcheckinterval() "
531 "are deprecated. Use sys.getswitchinterval() "
532 "instead.", 1) < 0)
533 return NULL;
534 return PyLong_FromLong(_check_interval);
Tim Peterse5e065b2003-07-06 18:36:54 +0000535}
536
537PyDoc_STRVAR(getcheckinterval_doc,
538"getcheckinterval() -> current check interval; see setcheckinterval()."
539);
540
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000541#ifdef WITH_THREAD
542static PyObject *
543sys_setswitchinterval(PyObject *self, PyObject *args)
544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 double d;
546 if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d))
547 return NULL;
548 if (d <= 0.0) {
549 PyErr_SetString(PyExc_ValueError,
550 "switch interval must be strictly positive");
551 return NULL;
552 }
553 _PyEval_SetSwitchInterval((unsigned long) (1e6 * d));
554 Py_INCREF(Py_None);
555 return Py_None;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000556}
557
558PyDoc_STRVAR(setswitchinterval_doc,
559"setswitchinterval(n)\n\
560\n\
561Set the ideal thread switching delay inside the Python interpreter\n\
562The actual frequency of switching threads can be lower if the\n\
563interpreter executes long sequences of uninterruptible code\n\
564(this is implementation-specific and workload-dependent).\n\
565\n\
566The parameter must represent the desired switching delay in seconds\n\
567A typical value is 0.005 (5 milliseconds)."
568);
569
570static PyObject *
571sys_getswitchinterval(PyObject *self, PyObject *args)
572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval());
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000574}
575
576PyDoc_STRVAR(getswitchinterval_doc,
577"getswitchinterval() -> current thread switch interval; see setswitchinterval()."
578);
579
580#endif /* WITH_THREAD */
581
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000582#ifdef WITH_TSC
583static PyObject *
584sys_settscdump(PyObject *self, PyObject *args)
585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 int bool;
587 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
590 return NULL;
591 if (bool)
592 tstate->interp->tscdump = 1;
593 else
594 tstate->interp->tscdump = 0;
595 Py_INCREF(Py_None);
596 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000597
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000598}
599
Tim Peters216b78b2006-01-06 02:40:53 +0000600PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000601"settscdump(bool)\n\
602\n\
603If true, tell the Python interpreter to dump VM measurements to\n\
604stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000605processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000606);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000607#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000608
Tim Peterse5e065b2003-07-06 18:36:54 +0000609static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000610sys_setrecursionlimit(PyObject *self, PyObject *args)
611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 int new_limit;
613 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
614 return NULL;
615 if (new_limit <= 0) {
616 PyErr_SetString(PyExc_ValueError,
617 "recursion limit must be positive");
618 return NULL;
619 }
620 Py_SetRecursionLimit(new_limit);
621 Py_INCREF(Py_None);
622 return Py_None;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000623}
624
Mark Dickinsondc787d22010-05-23 13:33:13 +0000625static PyTypeObject Hash_InfoType;
626
627PyDoc_STRVAR(hash_info_doc,
628"hash_info\n\
629\n\
630A struct sequence providing parameters used for computing\n\
631numeric hashes. The attributes are read only.");
632
633static PyStructSequence_Field hash_info_fields[] = {
634 {"width", "width of the type used for hashing, in bits"},
635 {"modulus", "prime number giving the modulus on which the hash "
636 "function is based"},
637 {"inf", "value to be used for hash of a positive infinity"},
638 {"nan", "value to be used for hash of a nan"},
639 {"imag", "multiplier used for the imaginary part of a complex number"},
640 {NULL, NULL}
641};
642
643static PyStructSequence_Desc hash_info_desc = {
644 "sys.hash_info",
645 hash_info_doc,
646 hash_info_fields,
647 5,
648};
649
Matthias Klosed885e952010-07-06 10:53:30 +0000650static PyObject *
Mark Dickinsondc787d22010-05-23 13:33:13 +0000651get_hash_info(void)
652{
653 PyObject *hash_info;
654 int field = 0;
655 hash_info = PyStructSequence_New(&Hash_InfoType);
656 if (hash_info == NULL)
657 return NULL;
658 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000659 PyLong_FromLong(8*sizeof(Py_hash_t)));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000660 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000661 PyLong_FromSsize_t(_PyHASH_MODULUS));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000662 PyStructSequence_SET_ITEM(hash_info, field++,
663 PyLong_FromLong(_PyHASH_INF));
664 PyStructSequence_SET_ITEM(hash_info, field++,
665 PyLong_FromLong(_PyHASH_NAN));
666 PyStructSequence_SET_ITEM(hash_info, field++,
667 PyLong_FromLong(_PyHASH_IMAG));
668 if (PyErr_Occurred()) {
669 Py_CLEAR(hash_info);
670 return NULL;
671 }
672 return hash_info;
673}
674
675
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000676PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000677"setrecursionlimit(n)\n\
678\n\
679Set the maximum depth of the Python interpreter stack to n. This\n\
680limit prevents infinite recursion from causing an overflow of the C\n\
681stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000682dependent."
683);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000684
685static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000686sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 return PyLong_FromLong(Py_GetRecursionLimit());
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000689}
690
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000691PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000692"getrecursionlimit()\n\
693\n\
694Return the current value of the recursion limit, the maximum depth\n\
695of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000696recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000697);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698
Mark Hammond8696ebc2002-10-08 02:44:31 +0000699#ifdef MS_WINDOWS
700PyDoc_STRVAR(getwindowsversion_doc,
701"getwindowsversion()\n\
702\n\
Eric Smithf7bb5782010-01-27 00:44:57 +0000703Return information about the running version of Windows as a named tuple.\n\
704The members are named: major, minor, build, platform, service_pack,\n\
705service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\
Ezio Melotti4969f702011-03-15 05:59:46 +0200706backward compatibility, only the first 5 items are available by indexing.\n\
Eric Smithf7bb5782010-01-27 00:44:57 +0000707All elements are numbers, except service_pack which is a string. Platform\n\
708may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\
7093 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\
710controller, 3 for a server."
Mark Hammond8696ebc2002-10-08 02:44:31 +0000711);
712
Eric Smithf7bb5782010-01-27 00:44:57 +0000713static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};
714
715static PyStructSequence_Field windows_version_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 {"major", "Major version number"},
717 {"minor", "Minor version number"},
718 {"build", "Build number"},
719 {"platform", "Operating system platform"},
720 {"service_pack", "Latest Service Pack installed on the system"},
721 {"service_pack_major", "Service Pack major version number"},
722 {"service_pack_minor", "Service Pack minor version number"},
723 {"suite_mask", "Bit mask identifying available product suites"},
724 {"product_type", "System product type"},
725 {0}
Eric Smithf7bb5782010-01-27 00:44:57 +0000726};
727
728static PyStructSequence_Desc windows_version_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 "sys.getwindowsversion", /* name */
730 getwindowsversion_doc, /* doc */
731 windows_version_fields, /* fields */
732 5 /* For backward compatibility,
733 only the first 5 items are accessible
734 via indexing, the rest are name only */
Eric Smithf7bb5782010-01-27 00:44:57 +0000735};
736
Mark Hammond8696ebc2002-10-08 02:44:31 +0000737static PyObject *
738sys_getwindowsversion(PyObject *self)
739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PyObject *version;
741 int pos = 0;
742 OSVERSIONINFOEX ver;
743 ver.dwOSVersionInfoSize = sizeof(ver);
744 if (!GetVersionEx((OSVERSIONINFO*) &ver))
745 return PyErr_SetFromWindowsErr(0);
Eric Smithf7bb5782010-01-27 00:44:57 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 version = PyStructSequence_New(&WindowsVersionType);
748 if (version == NULL)
749 return NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
752 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
753 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
754 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
755 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
756 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
757 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
758 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
759 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
Eric Smithf7bb5782010-01-27 00:44:57 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 return version;
Mark Hammond8696ebc2002-10-08 02:44:31 +0000762}
763
764#endif /* MS_WINDOWS */
765
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000766#ifdef HAVE_DLOPEN
767static PyObject *
768sys_setdlopenflags(PyObject *self, PyObject *args)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 int new_val;
771 PyThreadState *tstate = PyThreadState_GET();
772 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
773 return NULL;
774 if (!tstate)
775 return NULL;
776 tstate->interp->dlopenflags = new_val;
777 Py_INCREF(Py_None);
778 return Py_None;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000779}
780
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000781PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000782"setdlopenflags(n) -> None\n\
783\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000784Set the flags used by the interpreter for dlopen calls, such as when the\n\
785interpreter loads extension modules. Among other things, this will enable\n\
786a lazy resolving of symbols when importing a module, if called as\n\
787sys.setdlopenflags(0). To share symbols across extension modules, call as\n\
Andrew Kuchlingc61b9132013-06-21 10:58:41 -0400788sys.setdlopenflags(os.RTLD_GLOBAL). Symbolic names for the flag modules\n\
Victor Stinnerf4afa432011-10-31 11:48:09 +0100789can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY).");
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000790
791static PyObject *
792sys_getdlopenflags(PyObject *self, PyObject *args)
793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyThreadState *tstate = PyThreadState_GET();
795 if (!tstate)
796 return NULL;
797 return PyLong_FromLong(tstate->interp->dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000798}
799
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000800PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000801"getdlopenflags() -> int\n\
802\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000803Return the current value of the flags that are used for dlopen calls.\n\
Andrew Kuchlingc61b9132013-06-21 10:58:41 -0400804The flag constants are defined in the os module.");
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806#endif /* HAVE_DLOPEN */
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000807
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000808#ifdef USE_MALLOPT
809/* Link with -lmalloc (or -lmpc) on an SGI */
810#include <malloc.h>
811
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000812static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000813sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 int flag;
816 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
817 return NULL;
818 mallopt(M_DEBUG, flag);
819 Py_INCREF(Py_None);
820 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000821}
822#endif /* USE_MALLOPT */
823
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000824static PyObject *
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000825sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 PyObject *res = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500828 static PyObject *gc_head_size = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 static char *kwlist[] = {"object", "default", 0};
830 PyObject *o, *dflt = NULL;
831 PyObject *method;
Benjamin Petersonce798522012-01-22 11:24:29 -0500832 _Py_IDENTIFIER(__sizeof__);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
835 kwlist, &o, &dflt))
836 return NULL;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 /* Initialize static variable for GC head size */
839 if (gc_head_size == NULL) {
840 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
841 if (gc_head_size == NULL)
842 return NULL;
843 }
Benjamin Petersona5758c02009-05-09 18:15:04 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 /* Make sure the type is initialized. float gets initialized late */
846 if (PyType_Ready(Py_TYPE(o)) < 0)
847 return NULL;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000848
Benjamin Petersonce798522012-01-22 11:24:29 -0500849 method = _PyObject_LookupSpecial(o, &PyId___sizeof__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 if (method == NULL) {
851 if (!PyErr_Occurred())
852 PyErr_Format(PyExc_TypeError,
853 "Type %.100s doesn't define __sizeof__",
854 Py_TYPE(o)->tp_name);
855 }
856 else {
857 res = PyObject_CallFunctionObjArgs(method, NULL);
858 Py_DECREF(method);
859 }
860
861 /* Has a default value been given */
862 if ((res == NULL) && (dflt != NULL) &&
863 PyErr_ExceptionMatches(PyExc_TypeError))
864 {
865 PyErr_Clear();
866 Py_INCREF(dflt);
867 return dflt;
868 }
869 else if (res == NULL)
870 return res;
871
872 /* add gc_head size */
873 if (PyObject_IS_GC(o)) {
874 PyObject *tmp = res;
875 res = PyNumber_Add(tmp, gc_head_size);
876 Py_DECREF(tmp);
877 }
878 return res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000879}
880
881PyDoc_STRVAR(getsizeof_doc,
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000882"getsizeof(object, default) -> int\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000883\n\
884Return the size of object in bytes.");
885
886static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000887sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 return PyLong_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000890}
891
Tim Peters4be93d02002-07-07 19:59:50 +0000892#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000893static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000894sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 return PyLong_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000897}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000898#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000899
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000900PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000901"getrefcount(object) -> integer\n\
902\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000903Return the reference count of object. The count returned is generally\n\
904one higher than you might expect, because it includes the (temporary)\n\
905reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000906);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000907
Antoine Pitrouf9d0b122012-12-09 14:28:26 +0100908static PyObject *
909sys_getallocatedblocks(PyObject *self)
910{
911 return PyLong_FromSsize_t(_Py_GetAllocatedBlocks());
912}
913
914PyDoc_STRVAR(getallocatedblocks_doc,
915"getallocatedblocks() -> integer\n\
916\n\
917Return the number of memory blocks currently allocated, regardless of their\n\
918size."
919);
920
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000921#ifdef COUNT_ALLOCS
922static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000923sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return get_counts();
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000928}
929#endif
930
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000931PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000932"_getframe([depth]) -> frameobject\n\
933\n\
934Return a frame object from the call stack. If optional integer depth is\n\
935given, return the frame object that many calls below the top of the stack.\n\
936If that is deeper than the call stack, ValueError is raised. The default\n\
937for depth is zero, returning the frame at the top of the call stack.\n\
938\n\
939This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000940purposes only."
941);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000942
943static PyObject *
944sys_getframe(PyObject *self, PyObject *args)
945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 PyFrameObject *f = PyThreadState_GET()->frame;
947 int depth = -1;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
950 return NULL;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 while (depth > 0 && f != NULL) {
953 f = f->f_back;
954 --depth;
955 }
956 if (f == NULL) {
957 PyErr_SetString(PyExc_ValueError,
958 "call stack is not deep enough");
959 return NULL;
960 }
961 Py_INCREF(f);
962 return (PyObject*)f;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000963}
964
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000965PyDoc_STRVAR(current_frames_doc,
966"_current_frames() -> dictionary\n\
967\n\
968Return a dictionary mapping each current thread T's thread id to T's\n\
969current stack frame.\n\
970\n\
971This function should be used for specialized purposes only."
972);
973
974static PyObject *
975sys_current_frames(PyObject *self, PyObject *noargs)
976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 return _PyThread_CurrentFrames();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000978}
979
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000980PyDoc_STRVAR(call_tracing_doc,
981"call_tracing(func, args) -> object\n\
982\n\
983Call func(*args), while tracing is enabled. The tracing state is\n\
984saved, and restored afterwards. This is intended to be called from\n\
985a debugger from a checkpoint, to recursively debug some other code."
986);
987
988static PyObject *
989sys_call_tracing(PyObject *self, PyObject *args)
990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyObject *func, *funcargs;
992 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
993 return NULL;
994 return _PyEval_CallTracing(func, funcargs);
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000995}
996
Jeremy Hylton985eba52003-02-05 23:13:00 +0000997PyDoc_STRVAR(callstats_doc,
998"callstats() -> tuple of integers\n\
999\n\
1000Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
1001when Python was built. Otherwise, return None.\n\
1002\n\
1003When enabled, this function returns detailed, implementation-specific\n\
1004details about the number of function calls executed. The return value is\n\
1005a 11-tuple where the entries in the tuple are counts of:\n\
10060. all function calls\n\
10071. calls to PyFunction_Type objects\n\
10082. PyFunction calls that do not create an argument tuple\n\
10093. PyFunction calls that do not create an argument tuple\n\
1010 and bypass PyEval_EvalCodeEx()\n\
10114. PyMethod calls\n\
10125. PyMethod calls on bound methods\n\
10136. PyType calls\n\
10147. PyCFunction calls\n\
10158. generator calls\n\
10169. All other calls\n\
101710. Number of stack pops performed by call_function()"
1018);
Barry Warsawb6a54d22000-12-06 21:47:46 +00001019
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020#ifdef __cplusplus
1021extern "C" {
1022#endif
1023
David Malcolm49526f42012-06-22 14:55:41 -04001024static PyObject *
1025sys_debugmallocstats(PyObject *self, PyObject *args)
1026{
1027#ifdef WITH_PYMALLOC
1028 _PyObject_DebugMallocStats(stderr);
1029 fputc('\n', stderr);
1030#endif
1031 _PyObject_DebugTypeStats(stderr);
1032
1033 Py_RETURN_NONE;
1034}
1035PyDoc_STRVAR(debugmallocstats_doc,
1036"_debugmallocstats()\n\
1037\n\
1038Print summary info to stderr about the state of\n\
1039pymalloc's structures.\n\
1040\n\
1041In Py_DEBUG mode, also perform some expensive internal consistency\n\
1042checks.\n\
1043");
1044
Guido van Rossum7f3f2c11996-05-23 22:45:41 +00001045#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +00001046/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001047extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001048#endif
Guido van Rossumded690f1996-05-24 20:48:31 +00001049
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001050#ifdef DYNAMIC_EXECUTION_PROFILE
1051/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001052extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001053#endif
1054
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055#ifdef __cplusplus
1056}
1057#endif
1058
Christian Heimes15ebc882008-02-04 18:48:49 +00001059static PyObject *
1060sys_clear_type_cache(PyObject* self, PyObject* args)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyType_ClearCache();
1063 Py_RETURN_NONE;
Christian Heimes15ebc882008-02-04 18:48:49 +00001064}
1065
1066PyDoc_STRVAR(sys_clear_type_cache__doc__,
1067"_clear_type_cache() -> None\n\
1068Clear the internal type lookup cache.");
1069
1070
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001071static PyMethodDef sys_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 /* Might as well keep this in alphabetic order */
1073 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
1074 callstats_doc},
1075 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
1076 sys_clear_type_cache__doc__},
1077 {"_current_frames", sys_current_frames, METH_NOARGS,
1078 current_frames_doc},
1079 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
1080 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
1081 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
1082 {"exit", sys_exit, METH_VARARGS, exit_doc},
1083 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
1084 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001085#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
1087 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001088#endif
Antoine Pitrouf9d0b122012-12-09 14:28:26 +01001089 {"getallocatedblocks", (PyCFunction)sys_getallocatedblocks, METH_NOARGS,
1090 getallocatedblocks_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001091#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001093#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001094#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001096#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
1098 METH_NOARGS, getfilesystemencoding_doc},
Guido van Rossum7f3f2c11996-05-23 22:45:41 +00001099#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +00001101#endif
1102#ifdef Py_REF_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001104#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
1106 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
1107 getrecursionlimit_doc},
1108 {"getsizeof", (PyCFunction)sys_getsizeof,
1109 METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
1110 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +00001111#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
1113 getwindowsversion_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +00001114#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 {"intern", sys_intern, METH_VARARGS, intern_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +00001116#ifdef USE_MALLOPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +00001118#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
1120 setcheckinterval_doc},
1121 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
1122 getcheckinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001123#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS,
1125 setswitchinterval_doc},
1126 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS,
1127 getswitchinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001128#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001129#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
1131 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001132#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
1134 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
1135 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
1136 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001137#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001139#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 {"settrace", sys_settrace, METH_O, settrace_doc},
1141 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
1142 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
David Malcolm49526f42012-06-22 14:55:41 -04001143 {"_debugmallocstats", sys_debugmallocstats, METH_VARARGS,
1144 debugmallocstats_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 {NULL, NULL} /* sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001146};
1147
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001148static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001149list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +00001150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 PyObject *list = PyList_New(0);
1152 int i;
1153 if (list == NULL)
1154 return NULL;
1155 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1156 PyObject *name = PyUnicode_FromString(
1157 PyImport_Inittab[i].name);
1158 if (name == NULL)
1159 break;
1160 PyList_Append(list, name);
1161 Py_DECREF(name);
1162 }
1163 if (PyList_Sort(list) != 0) {
1164 Py_DECREF(list);
1165 list = NULL;
1166 }
1167 if (list) {
1168 PyObject *v = PyList_AsTuple(list);
1169 Py_DECREF(list);
1170 list = v;
1171 }
1172 return list;
Guido van Rossum34679b71993-01-26 13:33:44 +00001173}
1174
Guido van Rossum23fff912000-12-15 22:02:05 +00001175static PyObject *warnoptions = NULL;
1176
1177void
1178PySys_ResetWarnOptions(void)
1179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (warnoptions == NULL || !PyList_Check(warnoptions))
1181 return;
1182 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
Guido van Rossum23fff912000-12-15 22:02:05 +00001183}
1184
1185void
Victor Stinner9ca9c252010-05-19 16:53:30 +00001186PySys_AddWarnOptionUnicode(PyObject *unicode)
Guido van Rossum23fff912000-12-15 22:02:05 +00001187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
1189 Py_XDECREF(warnoptions);
1190 warnoptions = PyList_New(0);
1191 if (warnoptions == NULL)
1192 return;
1193 }
Victor Stinner9ca9c252010-05-19 16:53:30 +00001194 PyList_Append(warnoptions, unicode);
1195}
1196
1197void
1198PySys_AddWarnOption(const wchar_t *s)
1199{
1200 PyObject *unicode;
1201 unicode = PyUnicode_FromWideChar(s, -1);
1202 if (unicode == NULL)
1203 return;
1204 PySys_AddWarnOptionUnicode(unicode);
1205 Py_DECREF(unicode);
Guido van Rossum23fff912000-12-15 22:02:05 +00001206}
1207
Christian Heimes33fe8092008-04-13 13:53:33 +00001208int
1209PySys_HasWarnOptions(void)
1210{
1211 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
1212}
1213
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001214static PyObject *xoptions = NULL;
1215
1216static PyObject *
1217get_xoptions(void)
1218{
1219 if (xoptions == NULL || !PyDict_Check(xoptions)) {
1220 Py_XDECREF(xoptions);
1221 xoptions = PyDict_New();
1222 }
1223 return xoptions;
1224}
1225
1226void
1227PySys_AddXOption(const wchar_t *s)
1228{
1229 PyObject *opts;
1230 PyObject *name = NULL, *value = NULL;
1231 const wchar_t *name_end;
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001232
1233 opts = get_xoptions();
1234 if (opts == NULL)
1235 goto error;
1236
1237 name_end = wcschr(s, L'=');
1238 if (!name_end) {
1239 name = PyUnicode_FromWideChar(s, -1);
1240 value = Py_True;
1241 Py_INCREF(value);
1242 }
1243 else {
1244 name = PyUnicode_FromWideChar(s, name_end - s);
1245 value = PyUnicode_FromWideChar(name_end + 1, -1);
1246 }
1247 if (name == NULL || value == NULL)
1248 goto error;
Brett Cannonb94767f2011-02-22 20:15:44 +00001249 PyDict_SetItem(opts, name, value);
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001250 Py_DECREF(name);
1251 Py_DECREF(value);
1252 return;
1253
1254error:
1255 Py_XDECREF(name);
1256 Py_XDECREF(value);
1257 /* No return value, therefore clear error state if possible */
1258 if (_Py_atomic_load_relaxed(&_PyThreadState_Current))
1259 PyErr_Clear();
1260}
1261
1262PyObject *
1263PySys_GetXOptions(void)
1264{
1265 return get_xoptions();
1266}
1267
Guido van Rossum40552d01998-08-06 03:34:39 +00001268/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
1269 Two literals concatenated works just fine. If you have a K&R compiler
1270 or other abomination that however *does* understand longer strings,
1271 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001272PyDoc_VAR(sys_doc) =
1273PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001274"This module provides access to some objects used or maintained by the\n\
1275interpreter and to functions that interact strongly with the interpreter.\n\
1276\n\
1277Dynamic objects:\n\
1278\n\
1279argv -- command line arguments; argv[0] is the script pathname if known\n\
1280path -- module search path; path[0] is the script directory, else ''\n\
1281modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001282\n\
1283displayhook -- called to show results in an interactive session\n\
1284excepthook -- called to handle any uncaught exception other than SystemExit\n\
1285 To customize printing in an interactive session or to install a custom\n\
1286 top-level exception handler, assign other functions to replace these.\n\
1287\n\
Benjamin Peterson06157a42008-07-15 00:28:36 +00001288stdin -- standard input file object; used by input()\n\
Georg Brandl88fc6642007-02-09 21:28:07 +00001289stdout -- standard output file object; used by print()\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001290stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001291 By assigning other file objects (or objects that behave like files)\n\
1292 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001293\n\
1294last_type -- type of last uncaught exception\n\
1295last_value -- value of last uncaught exception\n\
1296last_traceback -- traceback of last uncaught exception\n\
1297 These three are only available in an interactive session after a\n\
1298 traceback has been printed.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001299"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001300)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001301/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001302PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001303"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001304Static objects:\n\
1305\n\
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001306builtin_module_names -- tuple of module names built into this interpreter\n\
1307copyright -- copyright notice pertaining to this interpreter\n\
1308exec_prefix -- prefix used to find the machine-specific Python library\n\
Petri Lehtinen4b0eab62012-02-02 21:23:15 +02001309executable -- absolute path of the executable binary of the Python interpreter\n\
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001310float_info -- a struct sequence with information about the float implementation.\n\
1311float_repr_style -- string indicating the style of repr() output for floats\n\
1312hexversion -- version information encoded as a single integer\n\
Barry Warsaw409da152012-06-03 16:18:47 -04001313implementation -- Python implementation information.\n\
Mark Dickinsonbd792642009-03-18 20:06:12 +00001314int_info -- a struct sequence with information about the int implementation.\n\
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001315maxsize -- the largest supported length of containers.\n\
Ezio Melotti48a2f8f2011-09-29 00:18:19 +03001316maxunicode -- the value of the largest Unicode codepoint\n\
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001317platform -- platform identifier\n\
1318prefix -- prefix used to find the Python library\n\
1319thread_info -- a struct sequence with information about the thread implementation.\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001320version -- the version of this interpreter as a string\n\
Eric Smith0e5b5622009-02-06 01:32:42 +00001321version_info -- version information as a named tuple\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001322"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001323)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001324#ifdef MS_WINDOWS
1325/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001326PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001327"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001328winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001329"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001330)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001331#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001332PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001333"__stdin__ -- the original stdin; don't touch!\n\
1334__stdout__ -- the original stdout; don't touch!\n\
1335__stderr__ -- the original stderr; don't touch!\n\
1336__displayhook__ -- the original displayhook; don't touch!\n\
1337__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001338\n\
1339Functions:\n\
1340\n\
Georg Brandl1a3284e2007-12-02 09:40:06 +00001341displayhook() -- print an object to the screen, and save it in builtins._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001342excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001343exc_info() -- return thread-safe information about the current exception\n\
1344exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001345getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001346getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001347getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001348getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001349getsizeof() -- return the size of an object in bytes\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001350gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001351setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001352setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001353setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001354setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001355settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001356"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001357)
Fred Drakeccede592000-08-14 20:59:57 +00001358/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001359
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001360
1361PyDoc_STRVAR(flags__doc__,
1362"sys.flags\n\
1363\n\
1364Flags provided through command line arguments or environment vars.");
1365
1366static PyTypeObject FlagsType;
1367
1368static PyStructSequence_Field flags_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 {"debug", "-d"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 {"inspect", "-i"},
1371 {"interactive", "-i"},
1372 {"optimize", "-O or -OO"},
1373 {"dont_write_bytecode", "-B"},
1374 {"no_user_site", "-s"},
1375 {"no_site", "-S"},
1376 {"ignore_environment", "-E"},
1377 {"verbose", "-v"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 /* {"unbuffered", "-u"}, */
1379 /* {"skip_first", "-x"}, */
Georg Brandl8aa7e992010-12-28 18:30:18 +00001380 {"bytes_warning", "-b"},
1381 {"quiet", "-q"},
Georg Brandl09a7c722012-02-20 21:31:46 +01001382 {"hash_randomization", "-R"},
Christian Heimesad73a9c2013-08-10 16:36:18 +02001383 {"isolated", "-I"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 {0}
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001385};
1386
1387static PyStructSequence_Desc flags_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 "sys.flags", /* name */
1389 flags__doc__, /* doc */
1390 flags_fields, /* fields */
Christian Heimesad73a9c2013-08-10 16:36:18 +02001391 13
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001392};
1393
1394static PyObject*
1395make_flags(void)
1396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 int pos = 0;
1398 PyObject *seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 seq = PyStructSequence_New(&FlagsType);
1401 if (seq == NULL)
1402 return NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001403
1404#define SetFlag(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 SetFlag(Py_DebugFlag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 SetFlag(Py_InspectFlag);
1409 SetFlag(Py_InteractiveFlag);
1410 SetFlag(Py_OptimizeFlag);
1411 SetFlag(Py_DontWriteBytecodeFlag);
1412 SetFlag(Py_NoUserSiteDirectory);
1413 SetFlag(Py_NoSiteFlag);
1414 SetFlag(Py_IgnoreEnvironmentFlag);
1415 SetFlag(Py_VerboseFlag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 /* SetFlag(saw_unbuffered_flag); */
1417 /* SetFlag(skipfirstline); */
Christian Heimes33fe8092008-04-13 13:53:33 +00001418 SetFlag(Py_BytesWarningFlag);
Georg Brandl8aa7e992010-12-28 18:30:18 +00001419 SetFlag(Py_QuietFlag);
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001420 SetFlag(Py_HashRandomizationFlag);
Christian Heimesad73a9c2013-08-10 16:36:18 +02001421 SetFlag(Py_IsolatedFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001422#undef SetFlag
1423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 if (PyErr_Occurred()) {
1425 return NULL;
1426 }
1427 return seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001428}
1429
Eric Smith0e5b5622009-02-06 01:32:42 +00001430PyDoc_STRVAR(version_info__doc__,
1431"sys.version_info\n\
1432\n\
1433Version information as a named tuple.");
1434
1435static PyTypeObject VersionInfoType;
1436
1437static PyStructSequence_Field version_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 {"major", "Major release number"},
1439 {"minor", "Minor release number"},
1440 {"micro", "Patch release number"},
1441 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1442 {"serial", "Serial release number"},
1443 {0}
Eric Smith0e5b5622009-02-06 01:32:42 +00001444};
1445
1446static PyStructSequence_Desc version_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 "sys.version_info", /* name */
1448 version_info__doc__, /* doc */
1449 version_info_fields, /* fields */
1450 5
Eric Smith0e5b5622009-02-06 01:32:42 +00001451};
1452
1453static PyObject *
1454make_version_info(void)
1455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 PyObject *version_info;
1457 char *s;
1458 int pos = 0;
Eric Smith0e5b5622009-02-06 01:32:42 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 version_info = PyStructSequence_New(&VersionInfoType);
1461 if (version_info == NULL) {
1462 return NULL;
1463 }
Eric Smith0e5b5622009-02-06 01:32:42 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 /*
1466 * These release level checks are mutually exclusive and cover
1467 * the field, so don't get too fancy with the pre-processor!
1468 */
Eric Smith0e5b5622009-02-06 01:32:42 +00001469#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 s = "alpha";
Eric Smith0e5b5622009-02-06 01:32:42 +00001471#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 s = "beta";
Eric Smith0e5b5622009-02-06 01:32:42 +00001473#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 s = "candidate";
Eric Smith0e5b5622009-02-06 01:32:42 +00001475#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 s = "final";
Eric Smith0e5b5622009-02-06 01:32:42 +00001477#endif
1478
1479#define SetIntItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001481#define SetStrItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 SetIntItem(PY_MAJOR_VERSION);
1485 SetIntItem(PY_MINOR_VERSION);
1486 SetIntItem(PY_MICRO_VERSION);
1487 SetStrItem(s);
1488 SetIntItem(PY_RELEASE_SERIAL);
Eric Smith0e5b5622009-02-06 01:32:42 +00001489#undef SetIntItem
1490#undef SetStrItem
1491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (PyErr_Occurred()) {
1493 Py_CLEAR(version_info);
1494 return NULL;
1495 }
1496 return version_info;
Eric Smith0e5b5622009-02-06 01:32:42 +00001497}
1498
Brett Cannon3adc7b72012-07-09 14:22:12 -04001499/* sys.implementation values */
1500#define NAME "cpython"
1501const char *_PySys_ImplName = NAME;
1502#define QUOTE(arg) #arg
1503#define STRIFY(name) QUOTE(name)
1504#define MAJOR STRIFY(PY_MAJOR_VERSION)
1505#define MINOR STRIFY(PY_MINOR_VERSION)
1506#define TAG NAME "-" MAJOR MINOR;
1507const char *_PySys_ImplCacheTag = TAG;
1508#undef NAME
1509#undef QUOTE
1510#undef STRIFY
1511#undef MAJOR
1512#undef MINOR
1513#undef TAG
1514
Barry Warsaw409da152012-06-03 16:18:47 -04001515static PyObject *
1516make_impl_info(PyObject *version_info)
1517{
1518 int res;
1519 PyObject *impl_info, *value, *ns;
1520
1521 impl_info = PyDict_New();
1522 if (impl_info == NULL)
1523 return NULL;
1524
1525 /* populate the dict */
1526
Brett Cannon3adc7b72012-07-09 14:22:12 -04001527 value = PyUnicode_FromString(_PySys_ImplName);
Barry Warsaw409da152012-06-03 16:18:47 -04001528 if (value == NULL)
1529 goto error;
1530 res = PyDict_SetItemString(impl_info, "name", value);
1531 Py_DECREF(value);
1532 if (res < 0)
1533 goto error;
1534
Brett Cannon3adc7b72012-07-09 14:22:12 -04001535 value = PyUnicode_FromString(_PySys_ImplCacheTag);
Barry Warsaw409da152012-06-03 16:18:47 -04001536 if (value == NULL)
1537 goto error;
1538 res = PyDict_SetItemString(impl_info, "cache_tag", value);
1539 Py_DECREF(value);
1540 if (res < 0)
1541 goto error;
Barry Warsaw409da152012-06-03 16:18:47 -04001542
1543 res = PyDict_SetItemString(impl_info, "version", version_info);
1544 if (res < 0)
1545 goto error;
1546
1547 value = PyLong_FromLong(PY_VERSION_HEX);
1548 if (value == NULL)
1549 goto error;
1550 res = PyDict_SetItemString(impl_info, "hexversion", value);
1551 Py_DECREF(value);
1552 if (res < 0)
1553 goto error;
1554
1555 /* dict ready */
1556
1557 ns = _PyNamespace_New(impl_info);
1558 Py_DECREF(impl_info);
1559 return ns;
1560
1561error:
1562 Py_CLEAR(impl_info);
1563 return NULL;
1564}
1565
Martin v. Löwis1a214512008-06-11 05:26:20 +00001566static struct PyModuleDef sysmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 PyModuleDef_HEAD_INIT,
1568 "sys",
1569 sys_doc,
1570 -1, /* multiple "initialization" just copies the module dict. */
1571 sys_methods,
1572 NULL,
1573 NULL,
1574 NULL,
1575 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001576};
1577
Guido van Rossum25ce5661997-08-02 03:10:38 +00001578PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001579_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001580{
Victor Stinner58049602013-07-22 22:40:00 +02001581 PyObject *m, *sysdict, *version_info;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 m = PyModule_Create(&sysmodule);
1584 if (m == NULL)
1585 return NULL;
1586 sysdict = PyModule_GetDict(m);
Victor Stinner8fea2522013-10-27 17:15:42 +01001587#define SET_SYS_FROM_STRING_BORROW(key, value) \
Victor Stinner58049602013-07-22 22:40:00 +02001588 do { \
1589 int res; \
1590 PyObject *v = (value); \
1591 if (v == NULL) \
1592 return NULL; \
1593 res = PyDict_SetItemString(sysdict, key, v); \
1594 if (res < 0) { \
Victor Stinner8fea2522013-10-27 17:15:42 +01001595 return NULL; \
1596 } \
1597 } while (0)
1598#define SET_SYS_FROM_STRING(key, value) \
1599 do { \
1600 int res; \
1601 PyObject *v = (value); \
1602 if (v == NULL) \
1603 return NULL; \
1604 res = PyDict_SetItemString(sysdict, key, v); \
1605 Py_DECREF(v); \
1606 if (res < 0) { \
Victor Stinner58049602013-07-22 22:40:00 +02001607 return NULL; \
1608 } \
1609 } while (0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 /* Check that stdin is not a directory
1612 Using shell redirection, you can redirect stdin to a directory,
1613 crashing the Python interpreter. Catch this common mistake here
1614 and output a useful error message. Note that under MS Windows,
1615 the shell already prevents that. */
Martin v. Löwisec59d042009-01-12 07:59:10 +00001616#if !defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 {
1618 struct stat sb;
1619 if (fstat(fileno(stdin), &sb) == 0 &&
1620 S_ISDIR(sb.st_mode)) {
1621 /* There's nothing more we can do. */
1622 /* Py_FatalError() will core dump, so just exit. */
1623 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1624 exit(EXIT_FAILURE);
1625 }
1626 }
Martin v. Löwisec59d042009-01-12 07:59:10 +00001627#endif
Neal Norwitz11bd1192005-10-03 00:54:56 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 /* stdin/stdout/stderr are now set by pythonrun.c */
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001630
Victor Stinner8fea2522013-10-27 17:15:42 +01001631 SET_SYS_FROM_STRING_BORROW("__displayhook__",
1632 PyDict_GetItemString(sysdict, "displayhook"));
1633 SET_SYS_FROM_STRING_BORROW("__excepthook__",
1634 PyDict_GetItemString(sysdict, "excepthook"));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 SET_SYS_FROM_STRING("version",
1636 PyUnicode_FromString(Py_GetVersion()));
1637 SET_SYS_FROM_STRING("hexversion",
1638 PyLong_FromLong(PY_VERSION_HEX));
Georg Brandl1ca2e792011-03-05 20:51:24 +01001639 SET_SYS_FROM_STRING("_mercurial",
1640 Py_BuildValue("(szz)", "CPython", _Py_hgidentifier(),
1641 _Py_hgversion()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 SET_SYS_FROM_STRING("dont_write_bytecode",
1643 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1644 SET_SYS_FROM_STRING("api_version",
1645 PyLong_FromLong(PYTHON_API_VERSION));
1646 SET_SYS_FROM_STRING("copyright",
1647 PyUnicode_FromString(Py_GetCopyright()));
1648 SET_SYS_FROM_STRING("platform",
1649 PyUnicode_FromString(Py_GetPlatform()));
1650 SET_SYS_FROM_STRING("executable",
1651 PyUnicode_FromWideChar(
1652 Py_GetProgramFullPath(), -1));
1653 SET_SYS_FROM_STRING("prefix",
1654 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1655 SET_SYS_FROM_STRING("exec_prefix",
1656 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
Vinay Sajip7ded1f02012-05-26 03:45:29 +01001657 SET_SYS_FROM_STRING("base_prefix",
1658 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1659 SET_SYS_FROM_STRING("base_exec_prefix",
1660 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 SET_SYS_FROM_STRING("maxsize",
1662 PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1663 SET_SYS_FROM_STRING("float_info",
1664 PyFloat_GetInfo());
1665 SET_SYS_FROM_STRING("int_info",
1666 PyLong_GetInfo());
Mark Dickinsondc787d22010-05-23 13:33:13 +00001667 /* initialize hash_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +02001668 if (Hash_InfoType.tp_name == NULL) {
1669 if (PyStructSequence_InitType2(&Hash_InfoType, &hash_info_desc) < 0)
1670 return NULL;
1671 }
Mark Dickinsondc787d22010-05-23 13:33:13 +00001672 SET_SYS_FROM_STRING("hash_info",
1673 get_hash_info());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 SET_SYS_FROM_STRING("maxunicode",
Ezio Melotti48a2f8f2011-09-29 00:18:19 +03001675 PyLong_FromLong(0x10FFFF));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 SET_SYS_FROM_STRING("builtin_module_names",
1677 list_builtin_module_names());
Christian Heimes743e0cd2012-10-17 23:52:17 +02001678#if PY_BIG_ENDIAN
1679 SET_SYS_FROM_STRING("byteorder",
1680 PyUnicode_FromString("big"));
1681#else
1682 SET_SYS_FROM_STRING("byteorder",
1683 PyUnicode_FromString("little"));
1684#endif
Fred Drake099325e2000-08-14 15:47:03 +00001685
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001686#ifdef MS_COREDLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 SET_SYS_FROM_STRING("dllhandle",
1688 PyLong_FromVoidPtr(PyWin_DLLhModule));
1689 SET_SYS_FROM_STRING("winver",
1690 PyUnicode_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001691#endif
Barry Warsaw8cf4eae2010-10-16 01:04:07 +00001692#ifdef ABIFLAGS
1693 SET_SYS_FROM_STRING("abiflags",
1694 PyUnicode_FromString(ABIFLAGS));
1695#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 if (warnoptions == NULL) {
1697 warnoptions = PyList_New(0);
Victor Stinner58049602013-07-22 22:40:00 +02001698 if (warnoptions == NULL)
1699 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 }
1701 else {
1702 Py_INCREF(warnoptions);
1703 }
Victor Stinner8fea2522013-10-27 17:15:42 +01001704 SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions);
Tim Peters216b78b2006-01-06 02:40:53 +00001705
Victor Stinner8fea2522013-10-27 17:15:42 +01001706 SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions());
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 /* version_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +02001709 if (VersionInfoType.tp_name == NULL) {
1710 if (PyStructSequence_InitType2(&VersionInfoType,
1711 &version_info_desc) < 0)
1712 return NULL;
1713 }
Barry Warsaw409da152012-06-03 16:18:47 -04001714 version_info = make_version_info();
1715 SET_SYS_FROM_STRING("version_info", version_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 /* prevent user from creating new instances */
1717 VersionInfoType.tp_init = NULL;
1718 VersionInfoType.tp_new = NULL;
Eric Smith0e5b5622009-02-06 01:32:42 +00001719
Barry Warsaw409da152012-06-03 16:18:47 -04001720 /* implementation */
1721 SET_SYS_FROM_STRING("implementation", make_impl_info(version_info));
1722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 /* flags */
Victor Stinner1c8f0592013-07-22 22:24:54 +02001724 if (FlagsType.tp_name == 0) {
1725 if (PyStructSequence_InitType2(&FlagsType, &flags_desc) < 0)
1726 return NULL;
1727 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 SET_SYS_FROM_STRING("flags", make_flags());
1729 /* prevent user from creating new instances */
1730 FlagsType.tp_init = NULL;
1731 FlagsType.tp_new = NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001732
Eric Smithf7bb5782010-01-27 00:44:57 +00001733
1734#if defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 /* getwindowsversion */
1736 if (WindowsVersionType.tp_name == 0)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001737 if (PyStructSequence_InitType2(&WindowsVersionType,
1738 &windows_version_desc) < 0)
1739 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 /* prevent user from creating new instances */
1741 WindowsVersionType.tp_init = NULL;
1742 WindowsVersionType.tp_new = NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +00001743#endif
1744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001746#ifndef PY_NO_SHORT_FLOAT_REPR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 SET_SYS_FROM_STRING("float_repr_style",
1748 PyUnicode_FromString("short"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001749#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 SET_SYS_FROM_STRING("float_repr_style",
1751 PyUnicode_FromString("legacy"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001752#endif
1753
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001754#ifdef WITH_THREAD
1755 SET_SYS_FROM_STRING("thread_info", PyThread_GetInfo());
1756#endif
1757
Christian Heimes7b3ce6a2008-01-31 14:31:45 +00001758#undef SET_SYS_FROM_STRING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 if (PyErr_Occurred())
1760 return NULL;
1761 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001762}
1763
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001764static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001765makepathobject(const wchar_t *path, wchar_t delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 int i, n;
1768 const wchar_t *p;
1769 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 n = 1;
1772 p = path;
1773 while ((p = wcschr(p, delim)) != NULL) {
1774 n++;
1775 p++;
1776 }
1777 v = PyList_New(n);
1778 if (v == NULL)
1779 return NULL;
1780 for (i = 0; ; i++) {
1781 p = wcschr(path, delim);
1782 if (p == NULL)
1783 p = path + wcslen(path); /* End of string */
1784 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
1785 if (w == NULL) {
1786 Py_DECREF(v);
1787 return NULL;
1788 }
1789 PyList_SetItem(v, i, w);
1790 if (*p == '\0')
1791 break;
1792 path = p+1;
1793 }
1794 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001795}
1796
1797void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001798PySys_SetPath(const wchar_t *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 PyObject *v;
1801 if ((v = makepathobject(path, DELIM)) == NULL)
1802 Py_FatalError("can't create sys.path");
1803 if (PySys_SetObject("path", v) != 0)
1804 Py_FatalError("can't assign sys.path");
1805 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001806}
1807
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001808static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001809makeargvobject(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 PyObject *av;
1812 if (argc <= 0 || argv == NULL) {
1813 /* Ensure at least one (empty) argument is seen */
1814 static wchar_t *empty_argv[1] = {L""};
1815 argv = empty_argv;
1816 argc = 1;
1817 }
1818 av = PyList_New(argc);
1819 if (av != NULL) {
1820 int i;
1821 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001822#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 PyObject *v;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 /* argv[0] is the script pathname if known */
1826 if (i == 0) {
1827 char* fn = decc$translate_vms(argv[0]);
1828 if ((fn == (char *)0) || fn == (char *)-1)
1829 v = PyUnicode_FromString(argv[0]);
1830 else
1831 v = PyUnicode_FromString(
1832 decc$translate_vms(argv[0]));
1833 } else
1834 v = PyUnicode_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001835#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001837#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 if (v == NULL) {
1839 Py_DECREF(av);
1840 av = NULL;
1841 break;
1842 }
1843 PyList_SetItem(av, i, v);
1844 }
1845 }
1846 return av;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001847}
1848
Nick Coghland26c18a2010-08-17 13:06:11 +00001849#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
1850 (argc > 0 && argv0 != NULL && \
1851 wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001852
1853static void
1854sys_update_path(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001855{
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001856 wchar_t *argv0;
1857 wchar_t *p = NULL;
1858 Py_ssize_t n = 0;
1859 PyObject *a;
1860 PyObject *path;
1861#ifdef HAVE_READLINK
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001862 wchar_t link[MAXPATHLEN+1];
1863 wchar_t argv0copy[2*MAXPATHLEN+1];
1864 int nr = 0;
1865#endif
Guido van Rossum162e38c2003-02-19 15:25:10 +00001866#if defined(HAVE_REALPATH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 wchar_t fullpath[MAXPATHLEN];
Martin v. Löwisec59d042009-01-12 07:59:10 +00001868#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 wchar_t fullpath[MAX_PATH];
Thomas Heller27bb71e2003-01-08 14:33:48 +00001870#endif
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001871
1872 path = PySys_GetObject("path");
1873 if (path == NULL)
1874 return;
1875
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001876 argv0 = argv[0];
1877
1878#ifdef HAVE_READLINK
1879 if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
1880 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
1881 if (nr > 0) {
1882 /* It's a symlink */
1883 link[nr] = '\0';
1884 if (link[0] == SEP)
1885 argv0 = link; /* Link to absolute path */
1886 else if (wcschr(link, SEP) == NULL)
1887 ; /* Link without path */
1888 else {
1889 /* Must join(dirname(argv0), link) */
1890 wchar_t *q = wcsrchr(argv0, SEP);
1891 if (q == NULL)
1892 argv0 = link; /* argv0 without path */
1893 else {
Christian Heimes60a60672013-07-22 12:53:32 +02001894 /* Must make a copy, argv0copy has room for 2 * MAXPATHLEN */
1895 wcsncpy(argv0copy, argv0, MAXPATHLEN);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001896 q = wcsrchr(argv0copy, SEP);
Christian Heimes60a60672013-07-22 12:53:32 +02001897 wcsncpy(q+1, link, MAXPATHLEN);
1898 q[MAXPATHLEN + 1] = L'\0';
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001899 argv0 = argv0copy;
1900 }
1901 }
1902 }
1903#endif /* HAVE_READLINK */
1904#if SEP == '\\' /* Special case for MS filename syntax */
1905 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1906 wchar_t *q;
1907#if defined(MS_WINDOWS) && !defined(MS_WINCE)
1908 /* This code here replaces the first element in argv with the full
1909 path that it represents. Under CE, there are no relative paths so
1910 the argument must be the full path anyway. */
1911 wchar_t *ptemp;
1912 if (GetFullPathNameW(argv0,
Victor Stinner63941882011-09-29 00:42:28 +02001913 Py_ARRAY_LENGTH(fullpath),
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001914 fullpath,
1915 &ptemp)) {
1916 argv0 = fullpath;
1917 }
1918#endif
1919 p = wcsrchr(argv0, SEP);
1920 /* Test for alternate separator */
1921 q = wcsrchr(p ? p : argv0, '/');
1922 if (q != NULL)
1923 p = q;
1924 if (p != NULL) {
1925 n = p + 1 - argv0;
1926 if (n > 1 && p[-1] != ':')
1927 n--; /* Drop trailing separator */
1928 }
1929 }
1930#else /* All other filename syntaxes */
1931 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1932#if defined(HAVE_REALPATH)
Victor Stinner015f4d82010-10-07 22:29:53 +00001933 if (_Py_wrealpath(argv0, fullpath, PATH_MAX)) {
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001934 argv0 = fullpath;
1935 }
1936#endif
1937 p = wcsrchr(argv0, SEP);
1938 }
1939 if (p != NULL) {
1940 n = p + 1 - argv0;
1941#if SEP == '/' /* Special case for Unix filename syntax */
1942 if (n > 1)
1943 n--; /* Drop trailing separator */
1944#endif /* Unix */
1945 }
1946#endif /* All others */
1947 a = PyUnicode_FromWideChar(argv0, n);
1948 if (a == NULL)
1949 Py_FatalError("no mem for sys.path insertion");
1950 if (PyList_Insert(path, 0, a) < 0)
1951 Py_FatalError("sys.path.insert(0) failed");
1952 Py_DECREF(a);
1953}
1954
1955void
1956PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
1957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 PyObject *av = makeargvobject(argc, argv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 if (av == NULL)
1960 Py_FatalError("no mem for sys.argv");
1961 if (PySys_SetObject("argv", av) != 0)
1962 Py_FatalError("can't assign sys.argv");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 Py_DECREF(av);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001964 if (updatepath)
1965 sys_update_path(argc, argv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001966}
Guido van Rossuma890e681998-05-12 14:59:24 +00001967
Antoine Pitrouf978fac2010-05-21 17:25:34 +00001968void
1969PySys_SetArgv(int argc, wchar_t **argv)
1970{
Christian Heimesad73a9c2013-08-10 16:36:18 +02001971 PySys_SetArgvEx(argc, argv, Py_IsolatedFlag == 0);
Antoine Pitrouf978fac2010-05-21 17:25:34 +00001972}
1973
Victor Stinner14284c22010-04-23 12:02:30 +00001974/* Reimplementation of PyFile_WriteString() no calling indirectly
1975 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
1976
1977static int
Victor Stinner79766632010-08-16 17:36:42 +00001978sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
Victor Stinner14284c22010-04-23 12:02:30 +00001979{
Victor Stinner79766632010-08-16 17:36:42 +00001980 PyObject *writer = NULL, *args = NULL, *result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 int err;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001982 _Py_IDENTIFIER(write);
Victor Stinner14284c22010-04-23 12:02:30 +00001983
Victor Stinnerecccc4f2010-06-08 20:46:00 +00001984 if (file == NULL)
1985 return -1;
1986
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001987 writer = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 if (writer == NULL)
1989 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 args = PyTuple_Pack(1, unicode);
1992 if (args == NULL)
1993 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 result = PyEval_CallObject(writer, args);
1996 if (result == NULL) {
1997 goto error;
1998 } else {
1999 err = 0;
2000 goto finally;
2001 }
Victor Stinner14284c22010-04-23 12:02:30 +00002002
2003error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 err = -1;
Victor Stinner14284c22010-04-23 12:02:30 +00002005finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 Py_XDECREF(writer);
2007 Py_XDECREF(args);
2008 Py_XDECREF(result);
2009 return err;
Victor Stinner14284c22010-04-23 12:02:30 +00002010}
2011
Victor Stinner79766632010-08-16 17:36:42 +00002012static int
2013sys_pyfile_write(const char *text, PyObject *file)
2014{
2015 PyObject *unicode = NULL;
2016 int err;
2017
2018 if (file == NULL)
2019 return -1;
2020
2021 unicode = PyUnicode_FromString(text);
2022 if (unicode == NULL)
2023 return -1;
2024
2025 err = sys_pyfile_write_unicode(unicode, file);
2026 Py_DECREF(unicode);
2027 return err;
2028}
Guido van Rossuma890e681998-05-12 14:59:24 +00002029
2030/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
2031 Adapted from code submitted by Just van Rossum.
2032
2033 PySys_WriteStdout(format, ...)
2034 PySys_WriteStderr(format, ...)
2035
2036 The first function writes to sys.stdout; the second to sys.stderr. When
2037 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00002038 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00002039
Victor Stinner14284c22010-04-23 12:02:30 +00002040 PyErr_CheckSignals() is not called to avoid the execution of the Python
Victor Stinner79766632010-08-16 17:36:42 +00002041 signal handlers: they may raise a new exception whereas sys_write()
2042 ignores all exceptions.
Victor Stinner14284c22010-04-23 12:02:30 +00002043
Guido van Rossuma890e681998-05-12 14:59:24 +00002044 Both take a printf-style format string as their first argument followed
2045 by a variable length argument list determined by the format string.
2046
2047 *** WARNING ***
2048
2049 The format should limit the total size of the formatted output string to
2050 1000 bytes. In particular, this means that no unrestricted "%s" formats
2051 should occur; these should be limited using "%.<N>s where <N> is a
2052 decimal number calculated so that <N> plus the maximum size of other
2053 formatted text does not exceed 1000 bytes. Also watch out for "%f",
2054 which can print hundreds of digits for very large numbers.
2055
2056 */
2057
2058static void
Victor Stinner79766632010-08-16 17:36:42 +00002059sys_write(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00002060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 PyObject *file;
2062 PyObject *error_type, *error_value, *error_traceback;
2063 char buffer[1001];
2064 int written;
Guido van Rossuma890e681998-05-12 14:59:24 +00002065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 PyErr_Fetch(&error_type, &error_value, &error_traceback);
2067 file = PySys_GetObject(name);
2068 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
2069 if (sys_pyfile_write(buffer, file) != 0) {
2070 PyErr_Clear();
2071 fputs(buffer, fp);
2072 }
2073 if (written < 0 || (size_t)written >= sizeof(buffer)) {
2074 const char *truncated = "... truncated";
Victor Stinner79766632010-08-16 17:36:42 +00002075 if (sys_pyfile_write(truncated, file) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 fputs(truncated, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 }
2078 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00002079}
2080
2081void
Guido van Rossuma890e681998-05-12 14:59:24 +00002082PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00002083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00002087 sys_write("stdout", stdout, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00002089}
2090
2091void
Guido van Rossuma890e681998-05-12 14:59:24 +00002092PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00002093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00002095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00002097 sys_write("stderr", stderr, format, va);
2098 va_end(va);
2099}
2100
2101static void
2102sys_format(char *name, FILE *fp, const char *format, va_list va)
2103{
2104 PyObject *file, *message;
2105 PyObject *error_type, *error_value, *error_traceback;
2106 char *utf8;
2107
2108 PyErr_Fetch(&error_type, &error_value, &error_traceback);
2109 file = PySys_GetObject(name);
2110 message = PyUnicode_FromFormatV(format, va);
2111 if (message != NULL) {
2112 if (sys_pyfile_write_unicode(message, file) != 0) {
2113 PyErr_Clear();
2114 utf8 = _PyUnicode_AsString(message);
2115 if (utf8 != NULL)
2116 fputs(utf8, fp);
2117 }
2118 Py_DECREF(message);
2119 }
2120 PyErr_Restore(error_type, error_value, error_traceback);
2121}
2122
2123void
2124PySys_FormatStdout(const char *format, ...)
2125{
2126 va_list va;
2127
2128 va_start(va, format);
2129 sys_format("stdout", stdout, format, va);
2130 va_end(va);
2131}
2132
2133void
2134PySys_FormatStderr(const char *format, ...)
2135{
2136 va_list va;
2137
2138 va_start(va, format);
2139 sys_format("stderr", stderr, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00002141}