blob: 3ebb6c9493aa022a73fc02c4016eebdb046cea5c [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
Victor Stinnerbd303c12013-11-07 23:07:29 +010044_Py_IDENTIFIER(_);
45_Py_IDENTIFIER(__sizeof__);
46_Py_IDENTIFIER(buffer);
47_Py_IDENTIFIER(builtins);
48_Py_IDENTIFIER(encoding);
49_Py_IDENTIFIER(path);
50_Py_IDENTIFIER(stdout);
51_Py_IDENTIFIER(stderr);
52_Py_IDENTIFIER(write);
53
Guido van Rossum65bf9f21997-04-29 18:33:38 +000054PyObject *
Victor Stinnerd67bd452013-11-06 22:36:40 +010055_PySys_GetObjectId(_Py_Identifier *key)
56{
57 PyThreadState *tstate = PyThreadState_GET();
58 PyObject *sd = tstate->interp->sysdict;
59 if (sd == NULL)
60 return NULL;
61 return _PyDict_GetItemId(sd, key);
62}
63
64PyObject *
Neal Norwitzf3081322007-08-25 00:32:45 +000065PySys_GetObject(const char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 PyThreadState *tstate = PyThreadState_GET();
68 PyObject *sd = tstate->interp->sysdict;
69 if (sd == NULL)
70 return NULL;
71 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074int
Victor Stinnerd67bd452013-11-06 22:36:40 +010075_PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
76{
77 PyThreadState *tstate = PyThreadState_GET();
78 PyObject *sd = tstate->interp->sysdict;
79 if (v == NULL) {
80 if (_PyDict_GetItemId(sd, key) == NULL)
81 return 0;
82 else
83 return _PyDict_DelItemId(sd, key);
84 }
85 else
86 return _PyDict_SetItemId(sd, key, v);
87}
88
89int
Neal Norwitzf3081322007-08-25 00:32:45 +000090PySys_SetObject(const char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyThreadState *tstate = PyThreadState_GET();
93 PyObject *sd = tstate->interp->sysdict;
94 if (v == NULL) {
95 if (PyDict_GetItemString(sd, name) == NULL)
96 return 0;
97 else
98 return PyDict_DelItemString(sd, name);
99 }
100 else
101 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102}
103
Victor Stinner13d49ee2010-12-04 17:24:33 +0000104/* Write repr(o) to sys.stdout using sys.stdout.encoding and 'backslashreplace'
105 error handler. If sys.stdout has a buffer attribute, use
106 sys.stdout.buffer.write(encoded), otherwise redecode the string and use
107 sys.stdout.write(redecoded).
108
109 Helper function for sys_displayhook(). */
110static int
111sys_displayhook_unencodable(PyObject *outf, PyObject *o)
112{
113 PyObject *stdout_encoding = NULL;
114 PyObject *encoded, *escaped_str, *repr_str, *buffer, *result;
115 char *stdout_encoding_str;
116 int ret;
117
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200118 stdout_encoding = _PyObject_GetAttrId(outf, &PyId_encoding);
Victor Stinner13d49ee2010-12-04 17:24:33 +0000119 if (stdout_encoding == NULL)
120 goto error;
121 stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
122 if (stdout_encoding_str == NULL)
123 goto error;
124
125 repr_str = PyObject_Repr(o);
126 if (repr_str == NULL)
127 goto error;
128 encoded = PyUnicode_AsEncodedString(repr_str,
129 stdout_encoding_str,
130 "backslashreplace");
131 Py_DECREF(repr_str);
132 if (encoded == NULL)
133 goto error;
134
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200135 buffer = _PyObject_GetAttrId(outf, &PyId_buffer);
Victor Stinner13d49ee2010-12-04 17:24:33 +0000136 if (buffer) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200137 result = _PyObject_CallMethodId(buffer, &PyId_write, "(O)", encoded);
Victor Stinner13d49ee2010-12-04 17:24:33 +0000138 Py_DECREF(buffer);
139 Py_DECREF(encoded);
140 if (result == NULL)
141 goto error;
142 Py_DECREF(result);
143 }
144 else {
145 PyErr_Clear();
146 escaped_str = PyUnicode_FromEncodedObject(encoded,
147 stdout_encoding_str,
148 "strict");
149 Py_DECREF(encoded);
150 if (PyFile_WriteObject(escaped_str, outf, Py_PRINT_RAW) != 0) {
151 Py_DECREF(escaped_str);
152 goto error;
153 }
154 Py_DECREF(escaped_str);
155 }
156 ret = 0;
157 goto finally;
158
159error:
160 ret = -1;
161finally:
162 Py_XDECREF(stdout_encoding);
163 return ret;
164}
165
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000166static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000167sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 PyObject *outf;
170 PyInterpreterState *interp = PyThreadState_GET()->interp;
171 PyObject *modules = interp->modules;
Victor Stinnerd02fbb82013-11-06 18:27:13 +0100172 PyObject *builtins;
173 static PyObject *newline = NULL;
Victor Stinner13d49ee2010-12-04 17:24:33 +0000174 int err;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000175
Victor Stinnerd02fbb82013-11-06 18:27:13 +0100176 builtins = _PyDict_GetItemId(modules, &PyId_builtins);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (builtins == NULL) {
178 PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
179 return NULL;
180 }
Moshe Zadka03897ea2001-07-23 13:32:43 +0000181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 /* Print value except if None */
183 /* After printing, also assign to '_' */
184 /* Before, set '_' to None to avoid recursion */
185 if (o == Py_None) {
186 Py_INCREF(Py_None);
187 return Py_None;
188 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200189 if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 return NULL;
Victor Stinnerbd303c12013-11-07 23:07:29 +0100191 outf = _PySys_GetObjectId(&PyId_stdout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (outf == NULL || outf == Py_None) {
193 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
194 return NULL;
195 }
Victor Stinner13d49ee2010-12-04 17:24:33 +0000196 if (PyFile_WriteObject(o, outf, 0) != 0) {
197 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
198 /* repr(o) is not encodable to sys.stdout.encoding with
199 * sys.stdout.errors error handler (which is probably 'strict') */
200 PyErr_Clear();
201 err = sys_displayhook_unencodable(outf, o);
202 if (err)
203 return NULL;
204 }
205 else {
206 return NULL;
207 }
208 }
Victor Stinnerd02fbb82013-11-06 18:27:13 +0100209 if (newline == NULL) {
210 newline = PyUnicode_FromString("\n");
211 if (newline == NULL)
212 return NULL;
213 }
214 if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200216 if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 return NULL;
218 Py_INCREF(Py_None);
219 return Py_None;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000220}
221
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000222PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000223"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000224"\n"
Florent Xicluna5749e852010-03-03 11:54:54 +0000225"Print an object to sys.stdout and also save it in builtins._\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000226);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000227
228static PyObject *
229sys_excepthook(PyObject* self, PyObject* args)
230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 PyObject *exc, *value, *tb;
232 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
233 return NULL;
234 PyErr_Display(exc, value, tb);
235 Py_INCREF(Py_None);
236 return Py_None;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000237}
238
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000239PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000240"excepthook(exctype, value, traceback) -> None\n"
241"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000242"Handle an exception by displaying it with a traceback on sys.stderr.\n"
243);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000244
245static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000246sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 PyThreadState *tstate;
249 tstate = PyThreadState_GET();
250 return Py_BuildValue(
251 "(OOO)",
252 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
253 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
254 tstate->exc_traceback != NULL ?
255 tstate->exc_traceback : Py_None);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000256}
257
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000258PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000259"exc_info() -> (type, value, traceback)\n\
260\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000261Return information about the most recent exception caught by an except\n\
262clause in the current stack frame or in an older stack frame."
263);
264
265static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 PyObject *exit_code = 0;
269 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
270 return NULL;
271 /* Raise SystemExit so callers may catch it or clean up. */
272 PyErr_SetObject(PyExc_SystemExit, exit_code);
273 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274}
275
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000276PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000277"exit([status])\n\
278\n\
279Exit the interpreter by raising SystemExit(status).\n\
280If the status is omitted or None, it defaults to zero (i.e., success).\n\
Ezio Melotti4af4d272013-08-26 14:00:39 +0300281If the status is an integer, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000282If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000283exit status will be one (i.e., failure)."
284);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000285
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000286
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000287static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000288sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
Fred Drake8b4d01d2000-05-09 19:57:01 +0000291}
292
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000293PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000294"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000295\n\
296Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000297implementation."
298);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000299
300static PyObject *
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000301sys_getfilesystemencoding(PyObject *self)
302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (Py_FileSystemDefaultEncoding)
304 return PyUnicode_FromString(Py_FileSystemDefaultEncoding);
Victor Stinner27181ac2011-03-31 13:39:03 +0200305 PyErr_SetString(PyExc_RuntimeError,
306 "filesystem encoding is not initialized");
307 return NULL;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000308}
309
310PyDoc_STRVAR(getfilesystemencoding_doc,
311"getfilesystemencoding() -> string\n\
312\n\
313Return the encoding used to convert Unicode filenames in\n\
314operating system filenames."
315);
316
Martin v. Löwis04dc25c2008-10-03 16:09:28 +0000317static PyObject *
Georg Brandl66a796e2006-12-19 20:50:34 +0000318sys_intern(PyObject *self, PyObject *args)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyObject *s;
321 if (!PyArg_ParseTuple(args, "U:intern", &s))
322 return NULL;
323 if (PyUnicode_CheckExact(s)) {
324 Py_INCREF(s);
325 PyUnicode_InternInPlace(&s);
326 return s;
327 }
328 else {
329 PyErr_Format(PyExc_TypeError,
330 "can't intern %.400s", s->ob_type->tp_name);
331 return NULL;
332 }
Georg Brandl66a796e2006-12-19 20:50:34 +0000333}
334
335PyDoc_STRVAR(intern_doc,
336"intern(string) -> string\n\
337\n\
338``Intern'' the given string. This enters the string in the (global)\n\
339table of interned strings whose purpose is to speed up dictionary lookups.\n\
340Return the string itself or the previously interned string object with the\n\
341same value.");
342
343
Fred Drake5755ce62001-06-27 19:19:46 +0000344/*
345 * Cached interned string objects used for calling the profile and
346 * trace functions. Initialized by trace_init().
347 */
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000348static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Fred Drake5755ce62001-06-27 19:19:46 +0000349
350static int
351trace_init(void)
352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 static char *whatnames[7] = {"call", "exception", "line", "return",
354 "c_call", "c_exception", "c_return"};
355 PyObject *name;
356 int i;
357 for (i = 0; i < 7; ++i) {
358 if (whatstrings[i] == NULL) {
359 name = PyUnicode_InternFromString(whatnames[i]);
360 if (name == NULL)
361 return -1;
362 whatstrings[i] = name;
363 }
364 }
365 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000366}
367
368
369static PyObject *
370call_trampoline(PyThreadState *tstate, PyObject* callback,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 PyFrameObject *frame, int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000372{
Victor Stinner41bb43a2013-10-29 01:19:37 +0100373 PyObject *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyObject *whatstr;
375 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000376
Victor Stinner41bb43a2013-10-29 01:19:37 +0100377 args = PyTuple_New(3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (args == NULL)
379 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100380 if (PyFrame_FastToLocalsWithError(frame) < 0)
381 return NULL;
382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_INCREF(frame);
384 whatstr = whatstrings[what];
385 Py_INCREF(whatstr);
386 if (arg == NULL)
387 arg = Py_None;
388 Py_INCREF(arg);
389 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
390 PyTuple_SET_ITEM(args, 1, whatstr);
391 PyTuple_SET_ITEM(args, 2, arg);
Fred Drake5755ce62001-06-27 19:19:46 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 /* call the Python-level function */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 result = PyEval_CallObject(callback, args);
395 PyFrame_LocalsToFast(frame, 1);
396 if (result == NULL)
397 PyTraceBack_Here(frame);
Fred Drake5755ce62001-06-27 19:19:46 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* cleanup */
400 Py_DECREF(args);
401 return result;
Fred Drake5755ce62001-06-27 19:19:46 +0000402}
403
404static int
405profile_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyThreadState *tstate = frame->f_tstate;
409 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (arg == NULL)
412 arg = Py_None;
413 result = call_trampoline(tstate, self, frame, what, arg);
414 if (result == NULL) {
415 PyEval_SetProfile(NULL, NULL);
416 return -1;
417 }
418 Py_DECREF(result);
419 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000420}
421
422static int
423trace_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyThreadState *tstate = frame->f_tstate;
427 PyObject *callback;
428 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (what == PyTrace_CALL)
431 callback = self;
432 else
433 callback = frame->f_trace;
434 if (callback == NULL)
435 return 0;
436 result = call_trampoline(tstate, callback, frame, what, arg);
437 if (result == NULL) {
438 PyEval_SetTrace(NULL, NULL);
439 Py_XDECREF(frame->f_trace);
440 frame->f_trace = NULL;
441 return -1;
442 }
443 if (result != Py_None) {
444 PyObject *temp = frame->f_trace;
445 frame->f_trace = NULL;
446 Py_XDECREF(temp);
447 frame->f_trace = result;
448 }
449 else {
450 Py_DECREF(result);
451 }
452 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000453}
Fred Draked0838392001-06-16 21:02:31 +0000454
Fred Drake8b4d01d2000-05-09 19:57:01 +0000455static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000456sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (trace_init() == -1)
459 return NULL;
460 if (args == Py_None)
461 PyEval_SetTrace(NULL, NULL);
462 else
463 PyEval_SetTrace(trace_trampoline, args);
464 Py_INCREF(Py_None);
465 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000466}
467
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000468PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000469"settrace(function)\n\
470\n\
471Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000472function call. See the debugger chapter in the library manual."
473);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000474
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000475static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000476sys_gettrace(PyObject *self, PyObject *args)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyThreadState *tstate = PyThreadState_GET();
479 PyObject *temp = tstate->c_traceobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (temp == NULL)
482 temp = Py_None;
483 Py_INCREF(temp);
484 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000485}
486
487PyDoc_STRVAR(gettrace_doc,
488"gettrace()\n\
489\n\
490Return the global debug tracing function set with sys.settrace.\n\
491See the debugger chapter in the library manual."
492);
493
494static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000495sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (trace_init() == -1)
498 return NULL;
499 if (args == Py_None)
500 PyEval_SetProfile(NULL, NULL);
501 else
502 PyEval_SetProfile(profile_trampoline, args);
503 Py_INCREF(Py_None);
504 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000505}
506
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000507PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000508"setprofile(function)\n\
509\n\
510Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000511and return. See the profiler chapter in the library manual."
512);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000513
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000514static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000515sys_getprofile(PyObject *self, PyObject *args)
516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 PyThreadState *tstate = PyThreadState_GET();
518 PyObject *temp = tstate->c_profileobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (temp == NULL)
521 temp = Py_None;
522 Py_INCREF(temp);
523 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000524}
525
526PyDoc_STRVAR(getprofile_doc,
527"getprofile()\n\
528\n\
529Return the profiling function set with sys.setprofile.\n\
530See the profiler chapter in the library manual."
531);
532
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000533static int _check_interval = 100;
534
Christian Heimes9bd667a2008-01-20 15:14:11 +0000535static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000536sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (PyErr_WarnEx(PyExc_DeprecationWarning,
539 "sys.getcheckinterval() and sys.setcheckinterval() "
540 "are deprecated. Use sys.setswitchinterval() "
541 "instead.", 1) < 0)
542 return NULL;
543 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval))
544 return NULL;
545 Py_INCREF(Py_None);
546 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000547}
548
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000549PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000550"setcheckinterval(n)\n\
551\n\
552Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000553n instructions. This also affects how often thread switches occur."
554);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000555
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000556static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000557sys_getcheckinterval(PyObject *self, PyObject *args)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (PyErr_WarnEx(PyExc_DeprecationWarning,
560 "sys.getcheckinterval() and sys.setcheckinterval() "
561 "are deprecated. Use sys.getswitchinterval() "
562 "instead.", 1) < 0)
563 return NULL;
564 return PyLong_FromLong(_check_interval);
Tim Peterse5e065b2003-07-06 18:36:54 +0000565}
566
567PyDoc_STRVAR(getcheckinterval_doc,
568"getcheckinterval() -> current check interval; see setcheckinterval()."
569);
570
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000571#ifdef WITH_THREAD
572static PyObject *
573sys_setswitchinterval(PyObject *self, PyObject *args)
574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 double d;
576 if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d))
577 return NULL;
578 if (d <= 0.0) {
579 PyErr_SetString(PyExc_ValueError,
580 "switch interval must be strictly positive");
581 return NULL;
582 }
583 _PyEval_SetSwitchInterval((unsigned long) (1e6 * d));
584 Py_INCREF(Py_None);
585 return Py_None;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000586}
587
588PyDoc_STRVAR(setswitchinterval_doc,
589"setswitchinterval(n)\n\
590\n\
591Set the ideal thread switching delay inside the Python interpreter\n\
592The actual frequency of switching threads can be lower if the\n\
593interpreter executes long sequences of uninterruptible code\n\
594(this is implementation-specific and workload-dependent).\n\
595\n\
596The parameter must represent the desired switching delay in seconds\n\
597A typical value is 0.005 (5 milliseconds)."
598);
599
600static PyObject *
601sys_getswitchinterval(PyObject *self, PyObject *args)
602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval());
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000604}
605
606PyDoc_STRVAR(getswitchinterval_doc,
607"getswitchinterval() -> current thread switch interval; see setswitchinterval()."
608);
609
610#endif /* WITH_THREAD */
611
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000612#ifdef WITH_TSC
613static PyObject *
614sys_settscdump(PyObject *self, PyObject *args)
615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 int bool;
617 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
620 return NULL;
621 if (bool)
622 tstate->interp->tscdump = 1;
623 else
624 tstate->interp->tscdump = 0;
625 Py_INCREF(Py_None);
626 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000627
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000628}
629
Tim Peters216b78b2006-01-06 02:40:53 +0000630PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000631"settscdump(bool)\n\
632\n\
633If true, tell the Python interpreter to dump VM measurements to\n\
634stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000635processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000636);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000637#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000638
Tim Peterse5e065b2003-07-06 18:36:54 +0000639static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000640sys_setrecursionlimit(PyObject *self, PyObject *args)
641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 int new_limit;
643 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
644 return NULL;
645 if (new_limit <= 0) {
646 PyErr_SetString(PyExc_ValueError,
647 "recursion limit must be positive");
648 return NULL;
649 }
650 Py_SetRecursionLimit(new_limit);
651 Py_INCREF(Py_None);
652 return Py_None;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653}
654
Mark Dickinsondc787d22010-05-23 13:33:13 +0000655static PyTypeObject Hash_InfoType;
656
657PyDoc_STRVAR(hash_info_doc,
658"hash_info\n\
659\n\
660A struct sequence providing parameters used for computing\n\
661numeric hashes. The attributes are read only.");
662
663static PyStructSequence_Field hash_info_fields[] = {
664 {"width", "width of the type used for hashing, in bits"},
665 {"modulus", "prime number giving the modulus on which the hash "
666 "function is based"},
667 {"inf", "value to be used for hash of a positive infinity"},
668 {"nan", "value to be used for hash of a nan"},
669 {"imag", "multiplier used for the imaginary part of a complex number"},
670 {NULL, NULL}
671};
672
673static PyStructSequence_Desc hash_info_desc = {
674 "sys.hash_info",
675 hash_info_doc,
676 hash_info_fields,
677 5,
678};
679
Matthias Klosed885e952010-07-06 10:53:30 +0000680static PyObject *
Mark Dickinsondc787d22010-05-23 13:33:13 +0000681get_hash_info(void)
682{
683 PyObject *hash_info;
684 int field = 0;
685 hash_info = PyStructSequence_New(&Hash_InfoType);
686 if (hash_info == NULL)
687 return NULL;
688 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000689 PyLong_FromLong(8*sizeof(Py_hash_t)));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000690 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000691 PyLong_FromSsize_t(_PyHASH_MODULUS));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000692 PyStructSequence_SET_ITEM(hash_info, field++,
693 PyLong_FromLong(_PyHASH_INF));
694 PyStructSequence_SET_ITEM(hash_info, field++,
695 PyLong_FromLong(_PyHASH_NAN));
696 PyStructSequence_SET_ITEM(hash_info, field++,
697 PyLong_FromLong(_PyHASH_IMAG));
698 if (PyErr_Occurred()) {
699 Py_CLEAR(hash_info);
700 return NULL;
701 }
702 return hash_info;
703}
704
705
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000706PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000707"setrecursionlimit(n)\n\
708\n\
709Set the maximum depth of the Python interpreter stack to n. This\n\
710limit prevents infinite recursion from causing an overflow of the C\n\
711stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000712dependent."
713);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000714
715static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000716sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return PyLong_FromLong(Py_GetRecursionLimit());
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000719}
720
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000721PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000722"getrecursionlimit()\n\
723\n\
724Return the current value of the recursion limit, the maximum depth\n\
725of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000726recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000727);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000728
Mark Hammond8696ebc2002-10-08 02:44:31 +0000729#ifdef MS_WINDOWS
730PyDoc_STRVAR(getwindowsversion_doc,
731"getwindowsversion()\n\
732\n\
Eric Smithf7bb5782010-01-27 00:44:57 +0000733Return information about the running version of Windows as a named tuple.\n\
734The members are named: major, minor, build, platform, service_pack,\n\
735service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\
Ezio Melotti4969f702011-03-15 05:59:46 +0200736backward compatibility, only the first 5 items are available by indexing.\n\
Eric Smithf7bb5782010-01-27 00:44:57 +0000737All elements are numbers, except service_pack which is a string. Platform\n\
738may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\
7393 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\
740controller, 3 for a server."
Mark Hammond8696ebc2002-10-08 02:44:31 +0000741);
742
Eric Smithf7bb5782010-01-27 00:44:57 +0000743static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};
744
745static PyStructSequence_Field windows_version_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 {"major", "Major version number"},
747 {"minor", "Minor version number"},
748 {"build", "Build number"},
749 {"platform", "Operating system platform"},
750 {"service_pack", "Latest Service Pack installed on the system"},
751 {"service_pack_major", "Service Pack major version number"},
752 {"service_pack_minor", "Service Pack minor version number"},
753 {"suite_mask", "Bit mask identifying available product suites"},
754 {"product_type", "System product type"},
755 {0}
Eric Smithf7bb5782010-01-27 00:44:57 +0000756};
757
758static PyStructSequence_Desc windows_version_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 "sys.getwindowsversion", /* name */
760 getwindowsversion_doc, /* doc */
761 windows_version_fields, /* fields */
762 5 /* For backward compatibility,
763 only the first 5 items are accessible
764 via indexing, the rest are name only */
Eric Smithf7bb5782010-01-27 00:44:57 +0000765};
766
Mark Hammond8696ebc2002-10-08 02:44:31 +0000767static PyObject *
768sys_getwindowsversion(PyObject *self)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject *version;
771 int pos = 0;
772 OSVERSIONINFOEX ver;
773 ver.dwOSVersionInfoSize = sizeof(ver);
774 if (!GetVersionEx((OSVERSIONINFO*) &ver))
775 return PyErr_SetFromWindowsErr(0);
Eric Smithf7bb5782010-01-27 00:44:57 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 version = PyStructSequence_New(&WindowsVersionType);
778 if (version == NULL)
779 return NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
782 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
783 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
784 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
785 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
786 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
787 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
788 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
789 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
Eric Smithf7bb5782010-01-27 00:44:57 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 return version;
Mark Hammond8696ebc2002-10-08 02:44:31 +0000792}
793
794#endif /* MS_WINDOWS */
795
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000796#ifdef HAVE_DLOPEN
797static PyObject *
798sys_setdlopenflags(PyObject *self, PyObject *args)
799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 int new_val;
801 PyThreadState *tstate = PyThreadState_GET();
802 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
803 return NULL;
804 if (!tstate)
805 return NULL;
806 tstate->interp->dlopenflags = new_val;
807 Py_INCREF(Py_None);
808 return Py_None;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000809}
810
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000811PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000812"setdlopenflags(n) -> None\n\
813\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000814Set the flags used by the interpreter for dlopen calls, such as when the\n\
815interpreter loads extension modules. Among other things, this will enable\n\
816a lazy resolving of symbols when importing a module, if called as\n\
817sys.setdlopenflags(0). To share symbols across extension modules, call as\n\
Andrew Kuchlingc61b9132013-06-21 10:58:41 -0400818sys.setdlopenflags(os.RTLD_GLOBAL). Symbolic names for the flag modules\n\
Victor Stinnerf4afa432011-10-31 11:48:09 +0100819can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY).");
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000820
821static PyObject *
822sys_getdlopenflags(PyObject *self, PyObject *args)
823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 PyThreadState *tstate = PyThreadState_GET();
825 if (!tstate)
826 return NULL;
827 return PyLong_FromLong(tstate->interp->dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000828}
829
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000830PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000831"getdlopenflags() -> int\n\
832\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000833Return the current value of the flags that are used for dlopen calls.\n\
Andrew Kuchlingc61b9132013-06-21 10:58:41 -0400834The flag constants are defined in the os module.");
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836#endif /* HAVE_DLOPEN */
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000837
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000838#ifdef USE_MALLOPT
839/* Link with -lmalloc (or -lmpc) on an SGI */
840#include <malloc.h>
841
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000842static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000843sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 int flag;
846 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
847 return NULL;
848 mallopt(M_DEBUG, flag);
849 Py_INCREF(Py_None);
850 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000851}
852#endif /* USE_MALLOPT */
853
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000854static PyObject *
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000855sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *res = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500858 static PyObject *gc_head_size = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 static char *kwlist[] = {"object", "default", 0};
860 PyObject *o, *dflt = NULL;
861 PyObject *method;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
864 kwlist, &o, &dflt))
865 return NULL;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 /* Initialize static variable for GC head size */
868 if (gc_head_size == NULL) {
869 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
870 if (gc_head_size == NULL)
871 return NULL;
872 }
Benjamin Petersona5758c02009-05-09 18:15:04 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 /* Make sure the type is initialized. float gets initialized late */
875 if (PyType_Ready(Py_TYPE(o)) < 0)
876 return NULL;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000877
Benjamin Petersonce798522012-01-22 11:24:29 -0500878 method = _PyObject_LookupSpecial(o, &PyId___sizeof__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (method == NULL) {
880 if (!PyErr_Occurred())
881 PyErr_Format(PyExc_TypeError,
882 "Type %.100s doesn't define __sizeof__",
883 Py_TYPE(o)->tp_name);
884 }
885 else {
886 res = PyObject_CallFunctionObjArgs(method, NULL);
887 Py_DECREF(method);
888 }
889
890 /* Has a default value been given */
891 if ((res == NULL) && (dflt != NULL) &&
892 PyErr_ExceptionMatches(PyExc_TypeError))
893 {
894 PyErr_Clear();
895 Py_INCREF(dflt);
896 return dflt;
897 }
898 else if (res == NULL)
899 return res;
900
901 /* add gc_head size */
902 if (PyObject_IS_GC(o)) {
903 PyObject *tmp = res;
904 res = PyNumber_Add(tmp, gc_head_size);
905 Py_DECREF(tmp);
906 }
907 return res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000908}
909
910PyDoc_STRVAR(getsizeof_doc,
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000911"getsizeof(object, default) -> int\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000912\n\
913Return the size of object in bytes.");
914
915static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000916sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 return PyLong_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000919}
920
Tim Peters4be93d02002-07-07 19:59:50 +0000921#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000922static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000923sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return PyLong_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000926}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000927#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000928
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000929PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000930"getrefcount(object) -> integer\n\
931\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000932Return the reference count of object. The count returned is generally\n\
933one higher than you might expect, because it includes the (temporary)\n\
934reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000935);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000936
Antoine Pitrouf9d0b122012-12-09 14:28:26 +0100937static PyObject *
938sys_getallocatedblocks(PyObject *self)
939{
940 return PyLong_FromSsize_t(_Py_GetAllocatedBlocks());
941}
942
943PyDoc_STRVAR(getallocatedblocks_doc,
944"getallocatedblocks() -> integer\n\
945\n\
946Return the number of memory blocks currently allocated, regardless of their\n\
947size."
948);
949
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000950#ifdef COUNT_ALLOCS
951static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000952sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return get_counts();
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000957}
958#endif
959
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000960PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000961"_getframe([depth]) -> frameobject\n\
962\n\
963Return a frame object from the call stack. If optional integer depth is\n\
964given, return the frame object that many calls below the top of the stack.\n\
965If that is deeper than the call stack, ValueError is raised. The default\n\
966for depth is zero, returning the frame at the top of the call stack.\n\
967\n\
968This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000969purposes only."
970);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000971
972static PyObject *
973sys_getframe(PyObject *self, PyObject *args)
974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyFrameObject *f = PyThreadState_GET()->frame;
976 int depth = -1;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
979 return NULL;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 while (depth > 0 && f != NULL) {
982 f = f->f_back;
983 --depth;
984 }
985 if (f == NULL) {
986 PyErr_SetString(PyExc_ValueError,
987 "call stack is not deep enough");
988 return NULL;
989 }
990 Py_INCREF(f);
991 return (PyObject*)f;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000992}
993
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000994PyDoc_STRVAR(current_frames_doc,
995"_current_frames() -> dictionary\n\
996\n\
997Return a dictionary mapping each current thread T's thread id to T's\n\
998current stack frame.\n\
999\n\
1000This function should be used for specialized purposes only."
1001);
1002
1003static PyObject *
1004sys_current_frames(PyObject *self, PyObject *noargs)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 return _PyThread_CurrentFrames();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001007}
1008
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00001009PyDoc_STRVAR(call_tracing_doc,
1010"call_tracing(func, args) -> object\n\
1011\n\
1012Call func(*args), while tracing is enabled. The tracing state is\n\
1013saved, and restored afterwards. This is intended to be called from\n\
1014a debugger from a checkpoint, to recursively debug some other code."
1015);
1016
1017static PyObject *
1018sys_call_tracing(PyObject *self, PyObject *args)
1019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 PyObject *func, *funcargs;
1021 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
1022 return NULL;
1023 return _PyEval_CallTracing(func, funcargs);
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00001024}
1025
Jeremy Hylton985eba52003-02-05 23:13:00 +00001026PyDoc_STRVAR(callstats_doc,
1027"callstats() -> tuple of integers\n\
1028\n\
1029Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
1030when Python was built. Otherwise, return None.\n\
1031\n\
1032When enabled, this function returns detailed, implementation-specific\n\
1033details about the number of function calls executed. The return value is\n\
1034a 11-tuple where the entries in the tuple are counts of:\n\
10350. all function calls\n\
10361. calls to PyFunction_Type objects\n\
10372. PyFunction calls that do not create an argument tuple\n\
10383. PyFunction calls that do not create an argument tuple\n\
1039 and bypass PyEval_EvalCodeEx()\n\
10404. PyMethod calls\n\
10415. PyMethod calls on bound methods\n\
10426. PyType calls\n\
10437. PyCFunction calls\n\
10448. generator calls\n\
10459. All other calls\n\
104610. Number of stack pops performed by call_function()"
1047);
Barry Warsawb6a54d22000-12-06 21:47:46 +00001048
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001049#ifdef __cplusplus
1050extern "C" {
1051#endif
1052
David Malcolm49526f42012-06-22 14:55:41 -04001053static PyObject *
1054sys_debugmallocstats(PyObject *self, PyObject *args)
1055{
1056#ifdef WITH_PYMALLOC
1057 _PyObject_DebugMallocStats(stderr);
1058 fputc('\n', stderr);
1059#endif
1060 _PyObject_DebugTypeStats(stderr);
1061
1062 Py_RETURN_NONE;
1063}
1064PyDoc_STRVAR(debugmallocstats_doc,
1065"_debugmallocstats()\n\
1066\n\
1067Print summary info to stderr about the state of\n\
1068pymalloc's structures.\n\
1069\n\
1070In Py_DEBUG mode, also perform some expensive internal consistency\n\
1071checks.\n\
1072");
1073
Guido van Rossum7f3f2c11996-05-23 22:45:41 +00001074#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +00001075/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001076extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001077#endif
Guido van Rossumded690f1996-05-24 20:48:31 +00001078
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001079#ifdef DYNAMIC_EXECUTION_PROFILE
1080/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001081extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001082#endif
1083
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084#ifdef __cplusplus
1085}
1086#endif
1087
Christian Heimes15ebc882008-02-04 18:48:49 +00001088static PyObject *
1089sys_clear_type_cache(PyObject* self, PyObject* args)
1090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 PyType_ClearCache();
1092 Py_RETURN_NONE;
Christian Heimes15ebc882008-02-04 18:48:49 +00001093}
1094
1095PyDoc_STRVAR(sys_clear_type_cache__doc__,
1096"_clear_type_cache() -> None\n\
1097Clear the internal type lookup cache.");
1098
1099
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001100static PyMethodDef sys_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 /* Might as well keep this in alphabetic order */
1102 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
1103 callstats_doc},
1104 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
1105 sys_clear_type_cache__doc__},
1106 {"_current_frames", sys_current_frames, METH_NOARGS,
1107 current_frames_doc},
1108 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
1109 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
1110 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
1111 {"exit", sys_exit, METH_VARARGS, exit_doc},
1112 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
1113 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001114#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
1116 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001117#endif
Antoine Pitrouf9d0b122012-12-09 14:28:26 +01001118 {"getallocatedblocks", (PyCFunction)sys_getallocatedblocks, METH_NOARGS,
1119 getallocatedblocks_doc},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001120#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001122#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001123#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +00001125#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
1127 METH_NOARGS, getfilesystemencoding_doc},
Guido van Rossum7f3f2c11996-05-23 22:45:41 +00001128#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +00001130#endif
1131#ifdef Py_REF_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001133#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
1135 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
1136 getrecursionlimit_doc},
1137 {"getsizeof", (PyCFunction)sys_getsizeof,
1138 METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
1139 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +00001140#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
1142 getwindowsversion_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +00001143#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 {"intern", sys_intern, METH_VARARGS, intern_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +00001145#ifdef USE_MALLOPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +00001147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
1149 setcheckinterval_doc},
1150 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
1151 getcheckinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001152#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS,
1154 setswitchinterval_doc},
1155 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS,
1156 getswitchinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001157#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001158#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
1160 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001161#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
1163 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
1164 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
1165 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001166#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001168#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 {"settrace", sys_settrace, METH_O, settrace_doc},
1170 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
1171 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
David Malcolm49526f42012-06-22 14:55:41 -04001172 {"_debugmallocstats", sys_debugmallocstats, METH_VARARGS,
1173 debugmallocstats_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 {NULL, NULL} /* sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001175};
1176
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001177static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001178list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +00001179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 PyObject *list = PyList_New(0);
1181 int i;
1182 if (list == NULL)
1183 return NULL;
1184 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1185 PyObject *name = PyUnicode_FromString(
1186 PyImport_Inittab[i].name);
1187 if (name == NULL)
1188 break;
1189 PyList_Append(list, name);
1190 Py_DECREF(name);
1191 }
1192 if (PyList_Sort(list) != 0) {
1193 Py_DECREF(list);
1194 list = NULL;
1195 }
1196 if (list) {
1197 PyObject *v = PyList_AsTuple(list);
1198 Py_DECREF(list);
1199 list = v;
1200 }
1201 return list;
Guido van Rossum34679b71993-01-26 13:33:44 +00001202}
1203
Guido van Rossum23fff912000-12-15 22:02:05 +00001204static PyObject *warnoptions = NULL;
1205
1206void
1207PySys_ResetWarnOptions(void)
1208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (warnoptions == NULL || !PyList_Check(warnoptions))
1210 return;
1211 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
Guido van Rossum23fff912000-12-15 22:02:05 +00001212}
1213
1214void
Victor Stinner9ca9c252010-05-19 16:53:30 +00001215PySys_AddWarnOptionUnicode(PyObject *unicode)
Guido van Rossum23fff912000-12-15 22:02:05 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
1218 Py_XDECREF(warnoptions);
1219 warnoptions = PyList_New(0);
1220 if (warnoptions == NULL)
1221 return;
1222 }
Victor Stinner9ca9c252010-05-19 16:53:30 +00001223 PyList_Append(warnoptions, unicode);
1224}
1225
1226void
1227PySys_AddWarnOption(const wchar_t *s)
1228{
1229 PyObject *unicode;
1230 unicode = PyUnicode_FromWideChar(s, -1);
1231 if (unicode == NULL)
1232 return;
1233 PySys_AddWarnOptionUnicode(unicode);
1234 Py_DECREF(unicode);
Guido van Rossum23fff912000-12-15 22:02:05 +00001235}
1236
Christian Heimes33fe8092008-04-13 13:53:33 +00001237int
1238PySys_HasWarnOptions(void)
1239{
1240 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
1241}
1242
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001243static PyObject *xoptions = NULL;
1244
1245static PyObject *
1246get_xoptions(void)
1247{
1248 if (xoptions == NULL || !PyDict_Check(xoptions)) {
1249 Py_XDECREF(xoptions);
1250 xoptions = PyDict_New();
1251 }
1252 return xoptions;
1253}
1254
1255void
1256PySys_AddXOption(const wchar_t *s)
1257{
1258 PyObject *opts;
1259 PyObject *name = NULL, *value = NULL;
1260 const wchar_t *name_end;
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001261
1262 opts = get_xoptions();
1263 if (opts == NULL)
1264 goto error;
1265
1266 name_end = wcschr(s, L'=');
1267 if (!name_end) {
1268 name = PyUnicode_FromWideChar(s, -1);
1269 value = Py_True;
1270 Py_INCREF(value);
1271 }
1272 else {
1273 name = PyUnicode_FromWideChar(s, name_end - s);
1274 value = PyUnicode_FromWideChar(name_end + 1, -1);
1275 }
1276 if (name == NULL || value == NULL)
1277 goto error;
Brett Cannonb94767f2011-02-22 20:15:44 +00001278 PyDict_SetItem(opts, name, value);
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001279 Py_DECREF(name);
1280 Py_DECREF(value);
1281 return;
1282
1283error:
1284 Py_XDECREF(name);
1285 Py_XDECREF(value);
1286 /* No return value, therefore clear error state if possible */
1287 if (_Py_atomic_load_relaxed(&_PyThreadState_Current))
1288 PyErr_Clear();
1289}
1290
1291PyObject *
1292PySys_GetXOptions(void)
1293{
1294 return get_xoptions();
1295}
1296
Guido van Rossum40552d01998-08-06 03:34:39 +00001297/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
1298 Two literals concatenated works just fine. If you have a K&R compiler
1299 or other abomination that however *does* understand longer strings,
1300 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001301PyDoc_VAR(sys_doc) =
1302PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001303"This module provides access to some objects used or maintained by the\n\
1304interpreter and to functions that interact strongly with the interpreter.\n\
1305\n\
1306Dynamic objects:\n\
1307\n\
1308argv -- command line arguments; argv[0] is the script pathname if known\n\
1309path -- module search path; path[0] is the script directory, else ''\n\
1310modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001311\n\
1312displayhook -- called to show results in an interactive session\n\
1313excepthook -- called to handle any uncaught exception other than SystemExit\n\
1314 To customize printing in an interactive session or to install a custom\n\
1315 top-level exception handler, assign other functions to replace these.\n\
1316\n\
Benjamin Peterson06157a42008-07-15 00:28:36 +00001317stdin -- standard input file object; used by input()\n\
Georg Brandl88fc6642007-02-09 21:28:07 +00001318stdout -- standard output file object; used by print()\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001319stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001320 By assigning other file objects (or objects that behave like files)\n\
1321 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001322\n\
1323last_type -- type of last uncaught exception\n\
1324last_value -- value of last uncaught exception\n\
1325last_traceback -- traceback of last uncaught exception\n\
1326 These three are only available in an interactive session after a\n\
1327 traceback has been printed.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001328"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001329)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001330/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001331PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001332"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001333Static objects:\n\
1334\n\
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001335builtin_module_names -- tuple of module names built into this interpreter\n\
1336copyright -- copyright notice pertaining to this interpreter\n\
1337exec_prefix -- prefix used to find the machine-specific Python library\n\
Petri Lehtinen4b0eab62012-02-02 21:23:15 +02001338executable -- absolute path of the executable binary of the Python interpreter\n\
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001339float_info -- a struct sequence with information about the float implementation.\n\
1340float_repr_style -- string indicating the style of repr() output for floats\n\
1341hexversion -- version information encoded as a single integer\n\
Barry Warsaw409da152012-06-03 16:18:47 -04001342implementation -- Python implementation information.\n\
Mark Dickinsonbd792642009-03-18 20:06:12 +00001343int_info -- a struct sequence with information about the int implementation.\n\
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001344maxsize -- the largest supported length of containers.\n\
Ezio Melotti48a2f8f2011-09-29 00:18:19 +03001345maxunicode -- the value of the largest Unicode codepoint\n\
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001346platform -- platform identifier\n\
1347prefix -- prefix used to find the Python library\n\
1348thread_info -- a struct sequence with information about the thread implementation.\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001349version -- the version of this interpreter as a string\n\
Eric Smith0e5b5622009-02-06 01:32:42 +00001350version_info -- version information as a named tuple\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001351"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001352)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001353#ifdef MS_WINDOWS
1354/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001355PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001356"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001357winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001358"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001359)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001360#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001361PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001362"__stdin__ -- the original stdin; don't touch!\n\
1363__stdout__ -- the original stdout; don't touch!\n\
1364__stderr__ -- the original stderr; don't touch!\n\
1365__displayhook__ -- the original displayhook; don't touch!\n\
1366__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001367\n\
1368Functions:\n\
1369\n\
Georg Brandl1a3284e2007-12-02 09:40:06 +00001370displayhook() -- print an object to the screen, and save it in builtins._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001371excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001372exc_info() -- return thread-safe information about the current exception\n\
1373exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001374getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001375getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001376getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001377getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001378getsizeof() -- return the size of an object in bytes\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001379gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001380setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001381setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001382setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001383setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001384settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001385"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001386)
Fred Drakeccede592000-08-14 20:59:57 +00001387/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001388
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001389
1390PyDoc_STRVAR(flags__doc__,
1391"sys.flags\n\
1392\n\
1393Flags provided through command line arguments or environment vars.");
1394
1395static PyTypeObject FlagsType;
1396
1397static PyStructSequence_Field flags_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 {"debug", "-d"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 {"inspect", "-i"},
1400 {"interactive", "-i"},
1401 {"optimize", "-O or -OO"},
1402 {"dont_write_bytecode", "-B"},
1403 {"no_user_site", "-s"},
1404 {"no_site", "-S"},
1405 {"ignore_environment", "-E"},
1406 {"verbose", "-v"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 /* {"unbuffered", "-u"}, */
1408 /* {"skip_first", "-x"}, */
Georg Brandl8aa7e992010-12-28 18:30:18 +00001409 {"bytes_warning", "-b"},
1410 {"quiet", "-q"},
Georg Brandl09a7c722012-02-20 21:31:46 +01001411 {"hash_randomization", "-R"},
Christian Heimesad73a9c2013-08-10 16:36:18 +02001412 {"isolated", "-I"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 {0}
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001414};
1415
1416static PyStructSequence_Desc flags_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 "sys.flags", /* name */
1418 flags__doc__, /* doc */
1419 flags_fields, /* fields */
Christian Heimesad73a9c2013-08-10 16:36:18 +02001420 13
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001421};
1422
1423static PyObject*
1424make_flags(void)
1425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 int pos = 0;
1427 PyObject *seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 seq = PyStructSequence_New(&FlagsType);
1430 if (seq == NULL)
1431 return NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001432
1433#define SetFlag(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 SetFlag(Py_DebugFlag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 SetFlag(Py_InspectFlag);
1438 SetFlag(Py_InteractiveFlag);
1439 SetFlag(Py_OptimizeFlag);
1440 SetFlag(Py_DontWriteBytecodeFlag);
1441 SetFlag(Py_NoUserSiteDirectory);
1442 SetFlag(Py_NoSiteFlag);
1443 SetFlag(Py_IgnoreEnvironmentFlag);
1444 SetFlag(Py_VerboseFlag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 /* SetFlag(saw_unbuffered_flag); */
1446 /* SetFlag(skipfirstline); */
Christian Heimes33fe8092008-04-13 13:53:33 +00001447 SetFlag(Py_BytesWarningFlag);
Georg Brandl8aa7e992010-12-28 18:30:18 +00001448 SetFlag(Py_QuietFlag);
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001449 SetFlag(Py_HashRandomizationFlag);
Christian Heimesad73a9c2013-08-10 16:36:18 +02001450 SetFlag(Py_IsolatedFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001451#undef SetFlag
1452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 if (PyErr_Occurred()) {
1454 return NULL;
1455 }
1456 return seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001457}
1458
Eric Smith0e5b5622009-02-06 01:32:42 +00001459PyDoc_STRVAR(version_info__doc__,
1460"sys.version_info\n\
1461\n\
1462Version information as a named tuple.");
1463
1464static PyTypeObject VersionInfoType;
1465
1466static PyStructSequence_Field version_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 {"major", "Major release number"},
1468 {"minor", "Minor release number"},
1469 {"micro", "Patch release number"},
1470 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1471 {"serial", "Serial release number"},
1472 {0}
Eric Smith0e5b5622009-02-06 01:32:42 +00001473};
1474
1475static PyStructSequence_Desc version_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 "sys.version_info", /* name */
1477 version_info__doc__, /* doc */
1478 version_info_fields, /* fields */
1479 5
Eric Smith0e5b5622009-02-06 01:32:42 +00001480};
1481
1482static PyObject *
1483make_version_info(void)
1484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 PyObject *version_info;
1486 char *s;
1487 int pos = 0;
Eric Smith0e5b5622009-02-06 01:32:42 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 version_info = PyStructSequence_New(&VersionInfoType);
1490 if (version_info == NULL) {
1491 return NULL;
1492 }
Eric Smith0e5b5622009-02-06 01:32:42 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 /*
1495 * These release level checks are mutually exclusive and cover
1496 * the field, so don't get too fancy with the pre-processor!
1497 */
Eric Smith0e5b5622009-02-06 01:32:42 +00001498#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 s = "alpha";
Eric Smith0e5b5622009-02-06 01:32:42 +00001500#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 s = "beta";
Eric Smith0e5b5622009-02-06 01:32:42 +00001502#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 s = "candidate";
Eric Smith0e5b5622009-02-06 01:32:42 +00001504#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 s = "final";
Eric Smith0e5b5622009-02-06 01:32:42 +00001506#endif
1507
1508#define SetIntItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001510#define SetStrItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 SetIntItem(PY_MAJOR_VERSION);
1514 SetIntItem(PY_MINOR_VERSION);
1515 SetIntItem(PY_MICRO_VERSION);
1516 SetStrItem(s);
1517 SetIntItem(PY_RELEASE_SERIAL);
Eric Smith0e5b5622009-02-06 01:32:42 +00001518#undef SetIntItem
1519#undef SetStrItem
1520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (PyErr_Occurred()) {
1522 Py_CLEAR(version_info);
1523 return NULL;
1524 }
1525 return version_info;
Eric Smith0e5b5622009-02-06 01:32:42 +00001526}
1527
Brett Cannon3adc7b72012-07-09 14:22:12 -04001528/* sys.implementation values */
1529#define NAME "cpython"
1530const char *_PySys_ImplName = NAME;
1531#define QUOTE(arg) #arg
1532#define STRIFY(name) QUOTE(name)
1533#define MAJOR STRIFY(PY_MAJOR_VERSION)
1534#define MINOR STRIFY(PY_MINOR_VERSION)
1535#define TAG NAME "-" MAJOR MINOR;
1536const char *_PySys_ImplCacheTag = TAG;
1537#undef NAME
1538#undef QUOTE
1539#undef STRIFY
1540#undef MAJOR
1541#undef MINOR
1542#undef TAG
1543
Barry Warsaw409da152012-06-03 16:18:47 -04001544static PyObject *
1545make_impl_info(PyObject *version_info)
1546{
1547 int res;
1548 PyObject *impl_info, *value, *ns;
1549
1550 impl_info = PyDict_New();
1551 if (impl_info == NULL)
1552 return NULL;
1553
1554 /* populate the dict */
1555
Brett Cannon3adc7b72012-07-09 14:22:12 -04001556 value = PyUnicode_FromString(_PySys_ImplName);
Barry Warsaw409da152012-06-03 16:18:47 -04001557 if (value == NULL)
1558 goto error;
1559 res = PyDict_SetItemString(impl_info, "name", value);
1560 Py_DECREF(value);
1561 if (res < 0)
1562 goto error;
1563
Brett Cannon3adc7b72012-07-09 14:22:12 -04001564 value = PyUnicode_FromString(_PySys_ImplCacheTag);
Barry Warsaw409da152012-06-03 16:18:47 -04001565 if (value == NULL)
1566 goto error;
1567 res = PyDict_SetItemString(impl_info, "cache_tag", value);
1568 Py_DECREF(value);
1569 if (res < 0)
1570 goto error;
Barry Warsaw409da152012-06-03 16:18:47 -04001571
1572 res = PyDict_SetItemString(impl_info, "version", version_info);
1573 if (res < 0)
1574 goto error;
1575
1576 value = PyLong_FromLong(PY_VERSION_HEX);
1577 if (value == NULL)
1578 goto error;
1579 res = PyDict_SetItemString(impl_info, "hexversion", value);
1580 Py_DECREF(value);
1581 if (res < 0)
1582 goto error;
1583
1584 /* dict ready */
1585
1586 ns = _PyNamespace_New(impl_info);
1587 Py_DECREF(impl_info);
1588 return ns;
1589
1590error:
1591 Py_CLEAR(impl_info);
1592 return NULL;
1593}
1594
Martin v. Löwis1a214512008-06-11 05:26:20 +00001595static struct PyModuleDef sysmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 PyModuleDef_HEAD_INIT,
1597 "sys",
1598 sys_doc,
1599 -1, /* multiple "initialization" just copies the module dict. */
1600 sys_methods,
1601 NULL,
1602 NULL,
1603 NULL,
1604 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001605};
1606
Guido van Rossum25ce5661997-08-02 03:10:38 +00001607PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001608_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609{
Victor Stinner58049602013-07-22 22:40:00 +02001610 PyObject *m, *sysdict, *version_info;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 m = PyModule_Create(&sysmodule);
1613 if (m == NULL)
1614 return NULL;
1615 sysdict = PyModule_GetDict(m);
Victor Stinner8fea2522013-10-27 17:15:42 +01001616#define SET_SYS_FROM_STRING_BORROW(key, value) \
Victor Stinner58049602013-07-22 22:40:00 +02001617 do { \
1618 int res; \
1619 PyObject *v = (value); \
1620 if (v == NULL) \
1621 return NULL; \
1622 res = PyDict_SetItemString(sysdict, key, v); \
1623 if (res < 0) { \
Victor Stinner8fea2522013-10-27 17:15:42 +01001624 return NULL; \
1625 } \
1626 } while (0)
1627#define SET_SYS_FROM_STRING(key, value) \
1628 do { \
1629 int res; \
1630 PyObject *v = (value); \
1631 if (v == NULL) \
1632 return NULL; \
1633 res = PyDict_SetItemString(sysdict, key, v); \
1634 Py_DECREF(v); \
1635 if (res < 0) { \
Victor Stinner58049602013-07-22 22:40:00 +02001636 return NULL; \
1637 } \
1638 } while (0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 /* Check that stdin is not a directory
1641 Using shell redirection, you can redirect stdin to a directory,
1642 crashing the Python interpreter. Catch this common mistake here
1643 and output a useful error message. Note that under MS Windows,
1644 the shell already prevents that. */
Martin v. Löwisec59d042009-01-12 07:59:10 +00001645#if !defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 {
1647 struct stat sb;
1648 if (fstat(fileno(stdin), &sb) == 0 &&
1649 S_ISDIR(sb.st_mode)) {
1650 /* There's nothing more we can do. */
1651 /* Py_FatalError() will core dump, so just exit. */
1652 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1653 exit(EXIT_FAILURE);
1654 }
1655 }
Martin v. Löwisec59d042009-01-12 07:59:10 +00001656#endif
Neal Norwitz11bd1192005-10-03 00:54:56 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 /* stdin/stdout/stderr are now set by pythonrun.c */
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001659
Victor Stinner8fea2522013-10-27 17:15:42 +01001660 SET_SYS_FROM_STRING_BORROW("__displayhook__",
1661 PyDict_GetItemString(sysdict, "displayhook"));
1662 SET_SYS_FROM_STRING_BORROW("__excepthook__",
1663 PyDict_GetItemString(sysdict, "excepthook"));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 SET_SYS_FROM_STRING("version",
1665 PyUnicode_FromString(Py_GetVersion()));
1666 SET_SYS_FROM_STRING("hexversion",
1667 PyLong_FromLong(PY_VERSION_HEX));
Georg Brandl1ca2e792011-03-05 20:51:24 +01001668 SET_SYS_FROM_STRING("_mercurial",
1669 Py_BuildValue("(szz)", "CPython", _Py_hgidentifier(),
1670 _Py_hgversion()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 SET_SYS_FROM_STRING("dont_write_bytecode",
1672 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1673 SET_SYS_FROM_STRING("api_version",
1674 PyLong_FromLong(PYTHON_API_VERSION));
1675 SET_SYS_FROM_STRING("copyright",
1676 PyUnicode_FromString(Py_GetCopyright()));
1677 SET_SYS_FROM_STRING("platform",
1678 PyUnicode_FromString(Py_GetPlatform()));
1679 SET_SYS_FROM_STRING("executable",
1680 PyUnicode_FromWideChar(
1681 Py_GetProgramFullPath(), -1));
1682 SET_SYS_FROM_STRING("prefix",
1683 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1684 SET_SYS_FROM_STRING("exec_prefix",
1685 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
Vinay Sajip7ded1f02012-05-26 03:45:29 +01001686 SET_SYS_FROM_STRING("base_prefix",
1687 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1688 SET_SYS_FROM_STRING("base_exec_prefix",
1689 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 SET_SYS_FROM_STRING("maxsize",
1691 PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1692 SET_SYS_FROM_STRING("float_info",
1693 PyFloat_GetInfo());
1694 SET_SYS_FROM_STRING("int_info",
1695 PyLong_GetInfo());
Mark Dickinsondc787d22010-05-23 13:33:13 +00001696 /* initialize hash_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +02001697 if (Hash_InfoType.tp_name == NULL) {
1698 if (PyStructSequence_InitType2(&Hash_InfoType, &hash_info_desc) < 0)
1699 return NULL;
1700 }
Mark Dickinsondc787d22010-05-23 13:33:13 +00001701 SET_SYS_FROM_STRING("hash_info",
1702 get_hash_info());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 SET_SYS_FROM_STRING("maxunicode",
Ezio Melotti48a2f8f2011-09-29 00:18:19 +03001704 PyLong_FromLong(0x10FFFF));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 SET_SYS_FROM_STRING("builtin_module_names",
1706 list_builtin_module_names());
Christian Heimes743e0cd2012-10-17 23:52:17 +02001707#if PY_BIG_ENDIAN
1708 SET_SYS_FROM_STRING("byteorder",
1709 PyUnicode_FromString("big"));
1710#else
1711 SET_SYS_FROM_STRING("byteorder",
1712 PyUnicode_FromString("little"));
1713#endif
Fred Drake099325e2000-08-14 15:47:03 +00001714
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001715#ifdef MS_COREDLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 SET_SYS_FROM_STRING("dllhandle",
1717 PyLong_FromVoidPtr(PyWin_DLLhModule));
1718 SET_SYS_FROM_STRING("winver",
1719 PyUnicode_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001720#endif
Barry Warsaw8cf4eae2010-10-16 01:04:07 +00001721#ifdef ABIFLAGS
1722 SET_SYS_FROM_STRING("abiflags",
1723 PyUnicode_FromString(ABIFLAGS));
1724#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (warnoptions == NULL) {
1726 warnoptions = PyList_New(0);
Victor Stinner58049602013-07-22 22:40:00 +02001727 if (warnoptions == NULL)
1728 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 }
1730 else {
1731 Py_INCREF(warnoptions);
1732 }
Victor Stinner8fea2522013-10-27 17:15:42 +01001733 SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions);
Tim Peters216b78b2006-01-06 02:40:53 +00001734
Victor Stinner8fea2522013-10-27 17:15:42 +01001735 SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions());
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 /* version_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +02001738 if (VersionInfoType.tp_name == NULL) {
1739 if (PyStructSequence_InitType2(&VersionInfoType,
1740 &version_info_desc) < 0)
1741 return NULL;
1742 }
Barry Warsaw409da152012-06-03 16:18:47 -04001743 version_info = make_version_info();
1744 SET_SYS_FROM_STRING("version_info", version_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 /* prevent user from creating new instances */
1746 VersionInfoType.tp_init = NULL;
1747 VersionInfoType.tp_new = NULL;
Eric Smith0e5b5622009-02-06 01:32:42 +00001748
Barry Warsaw409da152012-06-03 16:18:47 -04001749 /* implementation */
1750 SET_SYS_FROM_STRING("implementation", make_impl_info(version_info));
1751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 /* flags */
Victor Stinner1c8f0592013-07-22 22:24:54 +02001753 if (FlagsType.tp_name == 0) {
1754 if (PyStructSequence_InitType2(&FlagsType, &flags_desc) < 0)
1755 return NULL;
1756 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 SET_SYS_FROM_STRING("flags", make_flags());
1758 /* prevent user from creating new instances */
1759 FlagsType.tp_init = NULL;
1760 FlagsType.tp_new = NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001761
Eric Smithf7bb5782010-01-27 00:44:57 +00001762
1763#if defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 /* getwindowsversion */
1765 if (WindowsVersionType.tp_name == 0)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001766 if (PyStructSequence_InitType2(&WindowsVersionType,
1767 &windows_version_desc) < 0)
1768 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 /* prevent user from creating new instances */
1770 WindowsVersionType.tp_init = NULL;
1771 WindowsVersionType.tp_new = NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +00001772#endif
1773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001775#ifndef PY_NO_SHORT_FLOAT_REPR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 SET_SYS_FROM_STRING("float_repr_style",
1777 PyUnicode_FromString("short"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001778#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 SET_SYS_FROM_STRING("float_repr_style",
1780 PyUnicode_FromString("legacy"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001781#endif
1782
Victor Stinnerd5c355c2011-04-30 14:53:09 +02001783#ifdef WITH_THREAD
1784 SET_SYS_FROM_STRING("thread_info", PyThread_GetInfo());
1785#endif
1786
Christian Heimes7b3ce6a2008-01-31 14:31:45 +00001787#undef SET_SYS_FROM_STRING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (PyErr_Occurred())
1789 return NULL;
1790 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001791}
1792
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001793static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001794makepathobject(const wchar_t *path, wchar_t delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 int i, n;
1797 const wchar_t *p;
1798 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 n = 1;
1801 p = path;
1802 while ((p = wcschr(p, delim)) != NULL) {
1803 n++;
1804 p++;
1805 }
1806 v = PyList_New(n);
1807 if (v == NULL)
1808 return NULL;
1809 for (i = 0; ; i++) {
1810 p = wcschr(path, delim);
1811 if (p == NULL)
1812 p = path + wcslen(path); /* End of string */
1813 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
1814 if (w == NULL) {
1815 Py_DECREF(v);
1816 return NULL;
1817 }
1818 PyList_SetItem(v, i, w);
1819 if (*p == '\0')
1820 break;
1821 path = p+1;
1822 }
1823 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001824}
1825
1826void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001827PySys_SetPath(const wchar_t *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 PyObject *v;
1830 if ((v = makepathobject(path, DELIM)) == NULL)
1831 Py_FatalError("can't create sys.path");
Victor Stinnerbd303c12013-11-07 23:07:29 +01001832 if (_PySys_SetObjectId(&PyId_path, v) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 Py_FatalError("can't assign sys.path");
1834 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001835}
1836
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001837static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001838makeargvobject(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 PyObject *av;
1841 if (argc <= 0 || argv == NULL) {
1842 /* Ensure at least one (empty) argument is seen */
1843 static wchar_t *empty_argv[1] = {L""};
1844 argv = empty_argv;
1845 argc = 1;
1846 }
1847 av = PyList_New(argc);
1848 if (av != NULL) {
1849 int i;
1850 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001851#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 PyObject *v;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 /* argv[0] is the script pathname if known */
1855 if (i == 0) {
1856 char* fn = decc$translate_vms(argv[0]);
1857 if ((fn == (char *)0) || fn == (char *)-1)
1858 v = PyUnicode_FromString(argv[0]);
1859 else
1860 v = PyUnicode_FromString(
1861 decc$translate_vms(argv[0]));
1862 } else
1863 v = PyUnicode_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001864#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001866#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 if (v == NULL) {
1868 Py_DECREF(av);
1869 av = NULL;
1870 break;
1871 }
1872 PyList_SetItem(av, i, v);
1873 }
1874 }
1875 return av;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001876}
1877
Nick Coghland26c18a2010-08-17 13:06:11 +00001878#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
1879 (argc > 0 && argv0 != NULL && \
1880 wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001881
1882static void
1883sys_update_path(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001884{
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001885 wchar_t *argv0;
1886 wchar_t *p = NULL;
1887 Py_ssize_t n = 0;
1888 PyObject *a;
1889 PyObject *path;
1890#ifdef HAVE_READLINK
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001891 wchar_t link[MAXPATHLEN+1];
1892 wchar_t argv0copy[2*MAXPATHLEN+1];
1893 int nr = 0;
1894#endif
Guido van Rossum162e38c2003-02-19 15:25:10 +00001895#if defined(HAVE_REALPATH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 wchar_t fullpath[MAXPATHLEN];
Martin v. Löwisec59d042009-01-12 07:59:10 +00001897#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 wchar_t fullpath[MAX_PATH];
Thomas Heller27bb71e2003-01-08 14:33:48 +00001899#endif
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001900
Victor Stinnerbd303c12013-11-07 23:07:29 +01001901 path = _PySys_GetObjectId(&PyId_path);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001902 if (path == NULL)
1903 return;
1904
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001905 argv0 = argv[0];
1906
1907#ifdef HAVE_READLINK
1908 if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
1909 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
1910 if (nr > 0) {
1911 /* It's a symlink */
1912 link[nr] = '\0';
1913 if (link[0] == SEP)
1914 argv0 = link; /* Link to absolute path */
1915 else if (wcschr(link, SEP) == NULL)
1916 ; /* Link without path */
1917 else {
1918 /* Must join(dirname(argv0), link) */
1919 wchar_t *q = wcsrchr(argv0, SEP);
1920 if (q == NULL)
1921 argv0 = link; /* argv0 without path */
1922 else {
Christian Heimes60a60672013-07-22 12:53:32 +02001923 /* Must make a copy, argv0copy has room for 2 * MAXPATHLEN */
1924 wcsncpy(argv0copy, argv0, MAXPATHLEN);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001925 q = wcsrchr(argv0copy, SEP);
Christian Heimes60a60672013-07-22 12:53:32 +02001926 wcsncpy(q+1, link, MAXPATHLEN);
1927 q[MAXPATHLEN + 1] = L'\0';
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001928 argv0 = argv0copy;
1929 }
1930 }
1931 }
1932#endif /* HAVE_READLINK */
1933#if SEP == '\\' /* Special case for MS filename syntax */
1934 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1935 wchar_t *q;
1936#if defined(MS_WINDOWS) && !defined(MS_WINCE)
1937 /* This code here replaces the first element in argv with the full
1938 path that it represents. Under CE, there are no relative paths so
1939 the argument must be the full path anyway. */
1940 wchar_t *ptemp;
1941 if (GetFullPathNameW(argv0,
Victor Stinner63941882011-09-29 00:42:28 +02001942 Py_ARRAY_LENGTH(fullpath),
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001943 fullpath,
1944 &ptemp)) {
1945 argv0 = fullpath;
1946 }
1947#endif
1948 p = wcsrchr(argv0, SEP);
1949 /* Test for alternate separator */
1950 q = wcsrchr(p ? p : argv0, '/');
1951 if (q != NULL)
1952 p = q;
1953 if (p != NULL) {
1954 n = p + 1 - argv0;
1955 if (n > 1 && p[-1] != ':')
1956 n--; /* Drop trailing separator */
1957 }
1958 }
1959#else /* All other filename syntaxes */
1960 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1961#if defined(HAVE_REALPATH)
Victor Stinner015f4d82010-10-07 22:29:53 +00001962 if (_Py_wrealpath(argv0, fullpath, PATH_MAX)) {
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001963 argv0 = fullpath;
1964 }
1965#endif
1966 p = wcsrchr(argv0, SEP);
1967 }
1968 if (p != NULL) {
1969 n = p + 1 - argv0;
1970#if SEP == '/' /* Special case for Unix filename syntax */
1971 if (n > 1)
1972 n--; /* Drop trailing separator */
1973#endif /* Unix */
1974 }
1975#endif /* All others */
1976 a = PyUnicode_FromWideChar(argv0, n);
1977 if (a == NULL)
1978 Py_FatalError("no mem for sys.path insertion");
1979 if (PyList_Insert(path, 0, a) < 0)
1980 Py_FatalError("sys.path.insert(0) failed");
1981 Py_DECREF(a);
1982}
1983
1984void
1985PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
1986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 PyObject *av = makeargvobject(argc, argv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 if (av == NULL)
1989 Py_FatalError("no mem for sys.argv");
1990 if (PySys_SetObject("argv", av) != 0)
1991 Py_FatalError("can't assign sys.argv");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 Py_DECREF(av);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001993 if (updatepath)
1994 sys_update_path(argc, argv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001995}
Guido van Rossuma890e681998-05-12 14:59:24 +00001996
Antoine Pitrouf978fac2010-05-21 17:25:34 +00001997void
1998PySys_SetArgv(int argc, wchar_t **argv)
1999{
Christian Heimesad73a9c2013-08-10 16:36:18 +02002000 PySys_SetArgvEx(argc, argv, Py_IsolatedFlag == 0);
Antoine Pitrouf978fac2010-05-21 17:25:34 +00002001}
2002
Victor Stinner14284c22010-04-23 12:02:30 +00002003/* Reimplementation of PyFile_WriteString() no calling indirectly
2004 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
2005
2006static int
Victor Stinner79766632010-08-16 17:36:42 +00002007sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
Victor Stinner14284c22010-04-23 12:02:30 +00002008{
Victor Stinner79766632010-08-16 17:36:42 +00002009 PyObject *writer = NULL, *args = NULL, *result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 int err;
Victor Stinner14284c22010-04-23 12:02:30 +00002011
Victor Stinnerecccc4f2010-06-08 20:46:00 +00002012 if (file == NULL)
2013 return -1;
2014
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002015 writer = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 if (writer == NULL)
2017 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 args = PyTuple_Pack(1, unicode);
2020 if (args == NULL)
2021 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00002022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 result = PyEval_CallObject(writer, args);
2024 if (result == NULL) {
2025 goto error;
2026 } else {
2027 err = 0;
2028 goto finally;
2029 }
Victor Stinner14284c22010-04-23 12:02:30 +00002030
2031error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 err = -1;
Victor Stinner14284c22010-04-23 12:02:30 +00002033finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 Py_XDECREF(writer);
2035 Py_XDECREF(args);
2036 Py_XDECREF(result);
2037 return err;
Victor Stinner14284c22010-04-23 12:02:30 +00002038}
2039
Victor Stinner79766632010-08-16 17:36:42 +00002040static int
2041sys_pyfile_write(const char *text, PyObject *file)
2042{
2043 PyObject *unicode = NULL;
2044 int err;
2045
2046 if (file == NULL)
2047 return -1;
2048
2049 unicode = PyUnicode_FromString(text);
2050 if (unicode == NULL)
2051 return -1;
2052
2053 err = sys_pyfile_write_unicode(unicode, file);
2054 Py_DECREF(unicode);
2055 return err;
2056}
Guido van Rossuma890e681998-05-12 14:59:24 +00002057
2058/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
2059 Adapted from code submitted by Just van Rossum.
2060
2061 PySys_WriteStdout(format, ...)
2062 PySys_WriteStderr(format, ...)
2063
2064 The first function writes to sys.stdout; the second to sys.stderr. When
2065 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00002066 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00002067
Victor Stinner14284c22010-04-23 12:02:30 +00002068 PyErr_CheckSignals() is not called to avoid the execution of the Python
Victor Stinner79766632010-08-16 17:36:42 +00002069 signal handlers: they may raise a new exception whereas sys_write()
2070 ignores all exceptions.
Victor Stinner14284c22010-04-23 12:02:30 +00002071
Guido van Rossuma890e681998-05-12 14:59:24 +00002072 Both take a printf-style format string as their first argument followed
2073 by a variable length argument list determined by the format string.
2074
2075 *** WARNING ***
2076
2077 The format should limit the total size of the formatted output string to
2078 1000 bytes. In particular, this means that no unrestricted "%s" formats
2079 should occur; these should be limited using "%.<N>s where <N> is a
2080 decimal number calculated so that <N> plus the maximum size of other
2081 formatted text does not exceed 1000 bytes. Also watch out for "%f",
2082 which can print hundreds of digits for very large numbers.
2083
2084 */
2085
2086static void
Victor Stinner09054372013-11-06 22:41:44 +01002087sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00002088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 PyObject *file;
2090 PyObject *error_type, *error_value, *error_traceback;
2091 char buffer[1001];
2092 int written;
Guido van Rossuma890e681998-05-12 14:59:24 +00002093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Victor Stinner09054372013-11-06 22:41:44 +01002095 file = _PySys_GetObjectId(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
2097 if (sys_pyfile_write(buffer, file) != 0) {
2098 PyErr_Clear();
2099 fputs(buffer, fp);
2100 }
2101 if (written < 0 || (size_t)written >= sizeof(buffer)) {
2102 const char *truncated = "... truncated";
Victor Stinner79766632010-08-16 17:36:42 +00002103 if (sys_pyfile_write(truncated, file) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 fputs(truncated, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 }
2106 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00002107}
2108
2109void
Guido van Rossuma890e681998-05-12 14:59:24 +00002110PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00002111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00002113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 va_start(va, format);
Victor Stinnerbd303c12013-11-07 23:07:29 +01002115 sys_write(&PyId_stdout, stdout, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00002117}
2118
2119void
Guido van Rossuma890e681998-05-12 14:59:24 +00002120PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00002121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00002123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 va_start(va, format);
Victor Stinnerbd303c12013-11-07 23:07:29 +01002125 sys_write(&PyId_stderr, stderr, format, va);
Victor Stinner79766632010-08-16 17:36:42 +00002126 va_end(va);
2127}
2128
2129static void
Victor Stinner09054372013-11-06 22:41:44 +01002130sys_format(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
Victor Stinner79766632010-08-16 17:36:42 +00002131{
2132 PyObject *file, *message;
2133 PyObject *error_type, *error_value, *error_traceback;
2134 char *utf8;
2135
2136 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Victor Stinner09054372013-11-06 22:41:44 +01002137 file = _PySys_GetObjectId(key);
Victor Stinner79766632010-08-16 17:36:42 +00002138 message = PyUnicode_FromFormatV(format, va);
2139 if (message != NULL) {
2140 if (sys_pyfile_write_unicode(message, file) != 0) {
2141 PyErr_Clear();
2142 utf8 = _PyUnicode_AsString(message);
2143 if (utf8 != NULL)
2144 fputs(utf8, fp);
2145 }
2146 Py_DECREF(message);
2147 }
2148 PyErr_Restore(error_type, error_value, error_traceback);
2149}
2150
2151void
2152PySys_FormatStdout(const char *format, ...)
2153{
2154 va_list va;
2155
2156 va_start(va, format);
Victor Stinnerbd303c12013-11-07 23:07:29 +01002157 sys_format(&PyId_stdout, stdout, format, va);
Victor Stinner79766632010-08-16 17:36:42 +00002158 va_end(va);
2159}
2160
2161void
2162PySys_FormatStderr(const char *format, ...)
2163{
2164 va_list va;
2165
2166 va_start(va, format);
Victor Stinnerbd303c12013-11-07 23:07:29 +01002167 sys_format(&PyId_stderr, stderr, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00002169}