blob: 204c8c8cc93d17d166712ab7f2681380cd8ed1c8 [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"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossume2437a11992-03-23 18:20:18 +000021#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
Mark Hammond8696ebc2002-10-08 02:44:31 +000023#ifdef MS_WINDOWS
24#define WIN32_LEAN_AND_MEAN
Amaury Forgeot d'Arc06cfe952007-11-10 13:55:44 +000025#include <windows.h>
Mark Hammond8696ebc2002-10-08 02:44:31 +000026#endif /* MS_WINDOWS */
27
Guido van Rossum9b38a141996-09-11 23:12:24 +000028#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000029extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000030/* A string loaded from the DLL at startup: */
31extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000032#endif
33
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000034#ifdef __VMS
35#include <unixlib.h>
36#endif
37
Martin v. Löwis5467d4c2003-05-10 07:10:12 +000038#ifdef HAVE_LANGINFO_H
39#include <locale.h>
40#include <langinfo.h>
41#endif
42
Guido van Rossum65bf9f21997-04-29 18:33:38 +000043PyObject *
Neal Norwitzf3081322007-08-25 00:32:45 +000044PySys_GetObject(const char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 PyThreadState *tstate = PyThreadState_GET();
47 PyObject *sd = tstate->interp->sysdict;
48 if (sd == NULL)
49 return NULL;
50 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051}
52
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053int
Neal Norwitzf3081322007-08-25 00:32:45 +000054PySys_SetObject(const char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 PyThreadState *tstate = PyThreadState_GET();
57 PyObject *sd = tstate->interp->sysdict;
58 if (v == NULL) {
59 if (PyDict_GetItemString(sd, name) == NULL)
60 return 0;
61 else
62 return PyDict_DelItemString(sd, name);
63 }
64 else
65 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066}
67
Guido van Rossum65bf9f21997-04-29 18:33:38 +000068static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000069sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 PyObject *outf;
72 PyInterpreterState *interp = PyThreadState_GET()->interp;
73 PyObject *modules = interp->modules;
74 PyObject *builtins = PyDict_GetItemString(modules, "builtins");
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (builtins == NULL) {
77 PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
78 return NULL;
79 }
Moshe Zadka03897ea2001-07-23 13:32:43 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 /* Print value except if None */
82 /* After printing, also assign to '_' */
83 /* Before, set '_' to None to avoid recursion */
84 if (o == Py_None) {
85 Py_INCREF(Py_None);
86 return Py_None;
87 }
88 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
89 return NULL;
90 outf = PySys_GetObject("stdout");
91 if (outf == NULL || outf == Py_None) {
92 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
93 return NULL;
94 }
95 if (PyFile_WriteObject(o, outf, 0) != 0)
96 return NULL;
97 if (PyFile_WriteString("\n", outf) != 0)
98 return NULL;
99 if (PyObject_SetAttrString(builtins, "_", o) != 0)
100 return NULL;
101 Py_INCREF(Py_None);
102 return Py_None;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000103}
104
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000105PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000106"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000107"\n"
Florent Xicluna5749e852010-03-03 11:54:54 +0000108"Print an object to sys.stdout and also save it in builtins._\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000109);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000110
111static PyObject *
112sys_excepthook(PyObject* self, PyObject* args)
113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 PyObject *exc, *value, *tb;
115 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
116 return NULL;
117 PyErr_Display(exc, value, tb);
118 Py_INCREF(Py_None);
119 return Py_None;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000120}
121
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000122PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000123"excepthook(exctype, value, traceback) -> None\n"
124"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000125"Handle an exception by displaying it with a traceback on sys.stderr.\n"
126);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000127
128static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000129sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyThreadState *tstate;
132 tstate = PyThreadState_GET();
133 return Py_BuildValue(
134 "(OOO)",
135 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
136 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
137 tstate->exc_traceback != NULL ?
138 tstate->exc_traceback : Py_None);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000139}
140
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000141PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000142"exc_info() -> (type, value, traceback)\n\
143\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000144Return information about the most recent exception caught by an except\n\
145clause in the current stack frame or in an older stack frame."
146);
147
148static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000149sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyObject *exit_code = 0;
152 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
153 return NULL;
154 /* Raise SystemExit so callers may catch it or clean up. */
155 PyErr_SetObject(PyExc_SystemExit, exit_code);
156 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157}
158
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000159PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000160"exit([status])\n\
161\n\
162Exit the interpreter by raising SystemExit(status).\n\
163If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000164If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000165If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000166exit status will be one (i.e., failure)."
167);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000168
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000169
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000170static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000171sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
Fred Drake8b4d01d2000-05-09 19:57:01 +0000174}
175
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000176PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000177"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000178\n\
179Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000180implementation."
181);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000182
183static PyObject *
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000184sys_getfilesystemencoding(PyObject *self)
185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (Py_FileSystemDefaultEncoding)
187 return PyUnicode_FromString(Py_FileSystemDefaultEncoding);
188 Py_INCREF(Py_None);
189 return Py_None;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000190}
191
192PyDoc_STRVAR(getfilesystemencoding_doc,
193"getfilesystemencoding() -> string\n\
194\n\
195Return the encoding used to convert Unicode filenames in\n\
196operating system filenames."
197);
198
Martin v. Löwis04dc25c2008-10-03 16:09:28 +0000199static PyObject *
Georg Brandl66a796e2006-12-19 20:50:34 +0000200sys_intern(PyObject *self, PyObject *args)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyObject *s;
203 if (!PyArg_ParseTuple(args, "U:intern", &s))
204 return NULL;
205 if (PyUnicode_CheckExact(s)) {
206 Py_INCREF(s);
207 PyUnicode_InternInPlace(&s);
208 return s;
209 }
210 else {
211 PyErr_Format(PyExc_TypeError,
212 "can't intern %.400s", s->ob_type->tp_name);
213 return NULL;
214 }
Georg Brandl66a796e2006-12-19 20:50:34 +0000215}
216
217PyDoc_STRVAR(intern_doc,
218"intern(string) -> string\n\
219\n\
220``Intern'' the given string. This enters the string in the (global)\n\
221table of interned strings whose purpose is to speed up dictionary lookups.\n\
222Return the string itself or the previously interned string object with the\n\
223same value.");
224
225
Fred Drake5755ce62001-06-27 19:19:46 +0000226/*
227 * Cached interned string objects used for calling the profile and
228 * trace functions. Initialized by trace_init().
229 */
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000230static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Fred Drake5755ce62001-06-27 19:19:46 +0000231
232static int
233trace_init(void)
234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 static char *whatnames[7] = {"call", "exception", "line", "return",
236 "c_call", "c_exception", "c_return"};
237 PyObject *name;
238 int i;
239 for (i = 0; i < 7; ++i) {
240 if (whatstrings[i] == NULL) {
241 name = PyUnicode_InternFromString(whatnames[i]);
242 if (name == NULL)
243 return -1;
244 whatstrings[i] = name;
245 }
246 }
247 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000248}
249
250
251static PyObject *
252call_trampoline(PyThreadState *tstate, PyObject* callback,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 PyFrameObject *frame, int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 PyObject *args = PyTuple_New(3);
256 PyObject *whatstr;
257 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 if (args == NULL)
260 return NULL;
261 Py_INCREF(frame);
262 whatstr = whatstrings[what];
263 Py_INCREF(whatstr);
264 if (arg == NULL)
265 arg = Py_None;
266 Py_INCREF(arg);
267 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
268 PyTuple_SET_ITEM(args, 1, whatstr);
269 PyTuple_SET_ITEM(args, 2, arg);
Fred Drake5755ce62001-06-27 19:19:46 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* call the Python-level function */
272 PyFrame_FastToLocals(frame);
273 result = PyEval_CallObject(callback, args);
274 PyFrame_LocalsToFast(frame, 1);
275 if (result == NULL)
276 PyTraceBack_Here(frame);
Fred Drake5755ce62001-06-27 19:19:46 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* cleanup */
279 Py_DECREF(args);
280 return result;
Fred Drake5755ce62001-06-27 19:19:46 +0000281}
282
283static int
284profile_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 PyThreadState *tstate = frame->f_tstate;
288 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (arg == NULL)
291 arg = Py_None;
292 result = call_trampoline(tstate, self, frame, what, arg);
293 if (result == NULL) {
294 PyEval_SetProfile(NULL, NULL);
295 return -1;
296 }
297 Py_DECREF(result);
298 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000299}
300
301static int
302trace_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyThreadState *tstate = frame->f_tstate;
306 PyObject *callback;
307 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (what == PyTrace_CALL)
310 callback = self;
311 else
312 callback = frame->f_trace;
313 if (callback == NULL)
314 return 0;
315 result = call_trampoline(tstate, callback, frame, what, arg);
316 if (result == NULL) {
317 PyEval_SetTrace(NULL, NULL);
318 Py_XDECREF(frame->f_trace);
319 frame->f_trace = NULL;
320 return -1;
321 }
322 if (result != Py_None) {
323 PyObject *temp = frame->f_trace;
324 frame->f_trace = NULL;
325 Py_XDECREF(temp);
326 frame->f_trace = result;
327 }
328 else {
329 Py_DECREF(result);
330 }
331 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000332}
Fred Draked0838392001-06-16 21:02:31 +0000333
Fred Drake8b4d01d2000-05-09 19:57:01 +0000334static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 if (trace_init() == -1)
338 return NULL;
339 if (args == Py_None)
340 PyEval_SetTrace(NULL, NULL);
341 else
342 PyEval_SetTrace(trace_trampoline, args);
343 Py_INCREF(Py_None);
344 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000345}
346
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000347PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000348"settrace(function)\n\
349\n\
350Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000351function call. See the debugger chapter in the library manual."
352);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000353
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000354static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000355sys_gettrace(PyObject *self, PyObject *args)
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 PyThreadState *tstate = PyThreadState_GET();
358 PyObject *temp = tstate->c_traceobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (temp == NULL)
361 temp = Py_None;
362 Py_INCREF(temp);
363 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000364}
365
366PyDoc_STRVAR(gettrace_doc,
367"gettrace()\n\
368\n\
369Return the global debug tracing function set with sys.settrace.\n\
370See the debugger chapter in the library manual."
371);
372
373static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (trace_init() == -1)
377 return NULL;
378 if (args == Py_None)
379 PyEval_SetProfile(NULL, NULL);
380 else
381 PyEval_SetProfile(profile_trampoline, args);
382 Py_INCREF(Py_None);
383 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000384}
385
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000386PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000387"setprofile(function)\n\
388\n\
389Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000390and return. See the profiler chapter in the library manual."
391);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000392
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000393static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000394sys_getprofile(PyObject *self, PyObject *args)
395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyThreadState *tstate = PyThreadState_GET();
397 PyObject *temp = tstate->c_profileobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (temp == NULL)
400 temp = Py_None;
401 Py_INCREF(temp);
402 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000403}
404
405PyDoc_STRVAR(getprofile_doc,
406"getprofile()\n\
407\n\
408Return the profiling function set with sys.setprofile.\n\
409See the profiler chapter in the library manual."
410);
411
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000412static int _check_interval = 100;
413
Christian Heimes9bd667a2008-01-20 15:14:11 +0000414static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000415sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (PyErr_WarnEx(PyExc_DeprecationWarning,
418 "sys.getcheckinterval() and sys.setcheckinterval() "
419 "are deprecated. Use sys.setswitchinterval() "
420 "instead.", 1) < 0)
421 return NULL;
422 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval))
423 return NULL;
424 Py_INCREF(Py_None);
425 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000426}
427
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000428PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000429"setcheckinterval(n)\n\
430\n\
431Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000432n instructions. This also affects how often thread switches occur."
433);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000434
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000435static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000436sys_getcheckinterval(PyObject *self, PyObject *args)
437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (PyErr_WarnEx(PyExc_DeprecationWarning,
439 "sys.getcheckinterval() and sys.setcheckinterval() "
440 "are deprecated. Use sys.getswitchinterval() "
441 "instead.", 1) < 0)
442 return NULL;
443 return PyLong_FromLong(_check_interval);
Tim Peterse5e065b2003-07-06 18:36:54 +0000444}
445
446PyDoc_STRVAR(getcheckinterval_doc,
447"getcheckinterval() -> current check interval; see setcheckinterval()."
448);
449
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000450#ifdef WITH_THREAD
451static PyObject *
452sys_setswitchinterval(PyObject *self, PyObject *args)
453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 double d;
455 if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d))
456 return NULL;
457 if (d <= 0.0) {
458 PyErr_SetString(PyExc_ValueError,
459 "switch interval must be strictly positive");
460 return NULL;
461 }
462 _PyEval_SetSwitchInterval((unsigned long) (1e6 * d));
463 Py_INCREF(Py_None);
464 return Py_None;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000465}
466
467PyDoc_STRVAR(setswitchinterval_doc,
468"setswitchinterval(n)\n\
469\n\
470Set the ideal thread switching delay inside the Python interpreter\n\
471The actual frequency of switching threads can be lower if the\n\
472interpreter executes long sequences of uninterruptible code\n\
473(this is implementation-specific and workload-dependent).\n\
474\n\
475The parameter must represent the desired switching delay in seconds\n\
476A typical value is 0.005 (5 milliseconds)."
477);
478
479static PyObject *
480sys_getswitchinterval(PyObject *self, PyObject *args)
481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval());
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000483}
484
485PyDoc_STRVAR(getswitchinterval_doc,
486"getswitchinterval() -> current thread switch interval; see setswitchinterval()."
487);
488
489#endif /* WITH_THREAD */
490
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000491#ifdef WITH_TSC
492static PyObject *
493sys_settscdump(PyObject *self, PyObject *args)
494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 int bool;
496 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
499 return NULL;
500 if (bool)
501 tstate->interp->tscdump = 1;
502 else
503 tstate->interp->tscdump = 0;
504 Py_INCREF(Py_None);
505 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000506
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000507}
508
Tim Peters216b78b2006-01-06 02:40:53 +0000509PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000510"settscdump(bool)\n\
511\n\
512If true, tell the Python interpreter to dump VM measurements to\n\
513stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000514processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000515);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000516#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000517
Tim Peterse5e065b2003-07-06 18:36:54 +0000518static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000519sys_setrecursionlimit(PyObject *self, PyObject *args)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 int new_limit;
522 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
523 return NULL;
524 if (new_limit <= 0) {
525 PyErr_SetString(PyExc_ValueError,
526 "recursion limit must be positive");
527 return NULL;
528 }
529 Py_SetRecursionLimit(new_limit);
530 Py_INCREF(Py_None);
531 return Py_None;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000532}
533
Mark Dickinsondc787d22010-05-23 13:33:13 +0000534static PyTypeObject Hash_InfoType;
535
536PyDoc_STRVAR(hash_info_doc,
537"hash_info\n\
538\n\
539A struct sequence providing parameters used for computing\n\
540numeric hashes. The attributes are read only.");
541
542static PyStructSequence_Field hash_info_fields[] = {
543 {"width", "width of the type used for hashing, in bits"},
544 {"modulus", "prime number giving the modulus on which the hash "
545 "function is based"},
546 {"inf", "value to be used for hash of a positive infinity"},
547 {"nan", "value to be used for hash of a nan"},
548 {"imag", "multiplier used for the imaginary part of a complex number"},
549 {NULL, NULL}
550};
551
552static PyStructSequence_Desc hash_info_desc = {
553 "sys.hash_info",
554 hash_info_doc,
555 hash_info_fields,
556 5,
557};
558
Matthias Klosed885e952010-07-06 10:53:30 +0000559static PyObject *
Mark Dickinsondc787d22010-05-23 13:33:13 +0000560get_hash_info(void)
561{
562 PyObject *hash_info;
563 int field = 0;
564 hash_info = PyStructSequence_New(&Hash_InfoType);
565 if (hash_info == NULL)
566 return NULL;
567 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000568 PyLong_FromLong(8*sizeof(Py_hash_t)));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000569 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000570 PyLong_FromSsize_t(_PyHASH_MODULUS));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000571 PyStructSequence_SET_ITEM(hash_info, field++,
572 PyLong_FromLong(_PyHASH_INF));
573 PyStructSequence_SET_ITEM(hash_info, field++,
574 PyLong_FromLong(_PyHASH_NAN));
575 PyStructSequence_SET_ITEM(hash_info, field++,
576 PyLong_FromLong(_PyHASH_IMAG));
577 if (PyErr_Occurred()) {
578 Py_CLEAR(hash_info);
579 return NULL;
580 }
581 return hash_info;
582}
583
584
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000585PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000586"setrecursionlimit(n)\n\
587\n\
588Set the maximum depth of the Python interpreter stack to n. This\n\
589limit prevents infinite recursion from causing an overflow of the C\n\
590stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000591dependent."
592);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000593
594static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000595sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return PyLong_FromLong(Py_GetRecursionLimit());
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598}
599
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000600PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000601"getrecursionlimit()\n\
602\n\
603Return the current value of the recursion limit, the maximum depth\n\
604of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000605recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000606);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000607
Mark Hammond8696ebc2002-10-08 02:44:31 +0000608#ifdef MS_WINDOWS
609PyDoc_STRVAR(getwindowsversion_doc,
610"getwindowsversion()\n\
611\n\
Eric Smithf7bb5782010-01-27 00:44:57 +0000612Return information about the running version of Windows as a named tuple.\n\
613The members are named: major, minor, build, platform, service_pack,\n\
614service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\
615backward compatibiliy, only the first 5 items are available by indexing.\n\
616All elements are numbers, except service_pack which is a string. Platform\n\
617may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\
6183 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\
619controller, 3 for a server."
Mark Hammond8696ebc2002-10-08 02:44:31 +0000620);
621
Eric Smithf7bb5782010-01-27 00:44:57 +0000622static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};
623
624static PyStructSequence_Field windows_version_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 {"major", "Major version number"},
626 {"minor", "Minor version number"},
627 {"build", "Build number"},
628 {"platform", "Operating system platform"},
629 {"service_pack", "Latest Service Pack installed on the system"},
630 {"service_pack_major", "Service Pack major version number"},
631 {"service_pack_minor", "Service Pack minor version number"},
632 {"suite_mask", "Bit mask identifying available product suites"},
633 {"product_type", "System product type"},
634 {0}
Eric Smithf7bb5782010-01-27 00:44:57 +0000635};
636
637static PyStructSequence_Desc windows_version_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 "sys.getwindowsversion", /* name */
639 getwindowsversion_doc, /* doc */
640 windows_version_fields, /* fields */
641 5 /* For backward compatibility,
642 only the first 5 items are accessible
643 via indexing, the rest are name only */
Eric Smithf7bb5782010-01-27 00:44:57 +0000644};
645
Mark Hammond8696ebc2002-10-08 02:44:31 +0000646static PyObject *
647sys_getwindowsversion(PyObject *self)
648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyObject *version;
650 int pos = 0;
651 OSVERSIONINFOEX ver;
652 ver.dwOSVersionInfoSize = sizeof(ver);
653 if (!GetVersionEx((OSVERSIONINFO*) &ver))
654 return PyErr_SetFromWindowsErr(0);
Eric Smithf7bb5782010-01-27 00:44:57 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 version = PyStructSequence_New(&WindowsVersionType);
657 if (version == NULL)
658 return NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
661 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
662 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
663 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
664 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
665 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
666 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
667 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
668 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
Eric Smithf7bb5782010-01-27 00:44:57 +0000669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 return version;
Mark Hammond8696ebc2002-10-08 02:44:31 +0000671}
672
673#endif /* MS_WINDOWS */
674
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000675#ifdef HAVE_DLOPEN
676static PyObject *
677sys_setdlopenflags(PyObject *self, PyObject *args)
678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 int new_val;
680 PyThreadState *tstate = PyThreadState_GET();
681 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
682 return NULL;
683 if (!tstate)
684 return NULL;
685 tstate->interp->dlopenflags = new_val;
686 Py_INCREF(Py_None);
687 return Py_None;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000688}
689
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000690PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000691"setdlopenflags(n) -> None\n\
692\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000693Set the flags used by the interpreter for dlopen calls, such as when the\n\
694interpreter loads extension modules. Among other things, this will enable\n\
695a lazy resolving of symbols when importing a module, if called as\n\
696sys.setdlopenflags(0). To share symbols across extension modules, call as\n\
697sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n\
698can be either found in the ctypes module, or in the DLFCN module. If DLFCN\n\
699is not available, it can be generated from /usr/include/dlfcn.h using the\n\
700h2py script.");
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000701
702static PyObject *
703sys_getdlopenflags(PyObject *self, PyObject *args)
704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 PyThreadState *tstate = PyThreadState_GET();
706 if (!tstate)
707 return NULL;
708 return PyLong_FromLong(tstate->interp->dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000709}
710
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000711PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000712"getdlopenflags() -> int\n\
713\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000714Return the current value of the flags that are used for dlopen calls.\n\
715The flag constants are defined in the ctypes and DLFCN modules.");
716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717#endif /* HAVE_DLOPEN */
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000718
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000719#ifdef USE_MALLOPT
720/* Link with -lmalloc (or -lmpc) on an SGI */
721#include <malloc.h>
722
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000723static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000724sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 int flag;
727 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
728 return NULL;
729 mallopt(M_DEBUG, flag);
730 Py_INCREF(Py_None);
731 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000732}
733#endif /* USE_MALLOPT */
734
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000735static PyObject *
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000736sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 PyObject *res = NULL;
739 static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
740 static char *kwlist[] = {"object", "default", 0};
741 PyObject *o, *dflt = NULL;
742 PyObject *method;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
745 kwlist, &o, &dflt))
746 return NULL;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 /* Initialize static variable for GC head size */
749 if (gc_head_size == NULL) {
750 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
751 if (gc_head_size == NULL)
752 return NULL;
753 }
Benjamin Petersona5758c02009-05-09 18:15:04 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 /* Make sure the type is initialized. float gets initialized late */
756 if (PyType_Ready(Py_TYPE(o)) < 0)
757 return NULL;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 method = _PyObject_LookupSpecial(o, "__sizeof__",
760 &str__sizeof__);
761 if (method == NULL) {
762 if (!PyErr_Occurred())
763 PyErr_Format(PyExc_TypeError,
764 "Type %.100s doesn't define __sizeof__",
765 Py_TYPE(o)->tp_name);
766 }
767 else {
768 res = PyObject_CallFunctionObjArgs(method, NULL);
769 Py_DECREF(method);
770 }
771
772 /* Has a default value been given */
773 if ((res == NULL) && (dflt != NULL) &&
774 PyErr_ExceptionMatches(PyExc_TypeError))
775 {
776 PyErr_Clear();
777 Py_INCREF(dflt);
778 return dflt;
779 }
780 else if (res == NULL)
781 return res;
782
783 /* add gc_head size */
784 if (PyObject_IS_GC(o)) {
785 PyObject *tmp = res;
786 res = PyNumber_Add(tmp, gc_head_size);
787 Py_DECREF(tmp);
788 }
789 return res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000790}
791
792PyDoc_STRVAR(getsizeof_doc,
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000793"getsizeof(object, default) -> int\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000794\n\
795Return the size of object in bytes.");
796
797static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000798sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return PyLong_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000801}
802
Tim Peters4be93d02002-07-07 19:59:50 +0000803#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000804static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000805sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 return PyLong_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000808}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000809#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000810
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000811PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000812"getrefcount(object) -> integer\n\
813\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000814Return the reference count of object. The count returned is generally\n\
815one higher than you might expect, because it includes the (temporary)\n\
816reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000817);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000818
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000819#ifdef COUNT_ALLOCS
820static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000821sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 return get_counts();
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000826}
827#endif
828
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000829PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000830"_getframe([depth]) -> frameobject\n\
831\n\
832Return a frame object from the call stack. If optional integer depth is\n\
833given, return the frame object that many calls below the top of the stack.\n\
834If that is deeper than the call stack, ValueError is raised. The default\n\
835for depth is zero, returning the frame at the top of the call stack.\n\
836\n\
837This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000838purposes only."
839);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000840
841static PyObject *
842sys_getframe(PyObject *self, PyObject *args)
843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 PyFrameObject *f = PyThreadState_GET()->frame;
845 int depth = -1;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
848 return NULL;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 while (depth > 0 && f != NULL) {
851 f = f->f_back;
852 --depth;
853 }
854 if (f == NULL) {
855 PyErr_SetString(PyExc_ValueError,
856 "call stack is not deep enough");
857 return NULL;
858 }
859 Py_INCREF(f);
860 return (PyObject*)f;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000861}
862
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000863PyDoc_STRVAR(current_frames_doc,
864"_current_frames() -> dictionary\n\
865\n\
866Return a dictionary mapping each current thread T's thread id to T's\n\
867current stack frame.\n\
868\n\
869This function should be used for specialized purposes only."
870);
871
872static PyObject *
873sys_current_frames(PyObject *self, PyObject *noargs)
874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 return _PyThread_CurrentFrames();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000876}
877
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000878PyDoc_STRVAR(call_tracing_doc,
879"call_tracing(func, args) -> object\n\
880\n\
881Call func(*args), while tracing is enabled. The tracing state is\n\
882saved, and restored afterwards. This is intended to be called from\n\
883a debugger from a checkpoint, to recursively debug some other code."
884);
885
886static PyObject *
887sys_call_tracing(PyObject *self, PyObject *args)
888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject *func, *funcargs;
890 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
891 return NULL;
892 return _PyEval_CallTracing(func, funcargs);
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000893}
894
Jeremy Hylton985eba52003-02-05 23:13:00 +0000895PyDoc_STRVAR(callstats_doc,
896"callstats() -> tuple of integers\n\
897\n\
898Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
899when Python was built. Otherwise, return None.\n\
900\n\
901When enabled, this function returns detailed, implementation-specific\n\
902details about the number of function calls executed. The return value is\n\
903a 11-tuple where the entries in the tuple are counts of:\n\
9040. all function calls\n\
9051. calls to PyFunction_Type objects\n\
9062. PyFunction calls that do not create an argument tuple\n\
9073. PyFunction calls that do not create an argument tuple\n\
908 and bypass PyEval_EvalCodeEx()\n\
9094. PyMethod calls\n\
9105. PyMethod calls on bound methods\n\
9116. PyType calls\n\
9127. PyCFunction calls\n\
9138. generator calls\n\
9149. All other calls\n\
91510. Number of stack pops performed by call_function()"
916);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000917
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000918#ifdef __cplusplus
919extern "C" {
920#endif
921
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000922#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000923/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000924extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000925#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000926
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000927#ifdef DYNAMIC_EXECUTION_PROFILE
928/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000929extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000930#endif
931
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932#ifdef __cplusplus
933}
934#endif
935
Christian Heimes15ebc882008-02-04 18:48:49 +0000936static PyObject *
937sys_clear_type_cache(PyObject* self, PyObject* args)
938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 PyType_ClearCache();
940 Py_RETURN_NONE;
Christian Heimes15ebc882008-02-04 18:48:49 +0000941}
942
943PyDoc_STRVAR(sys_clear_type_cache__doc__,
944"_clear_type_cache() -> None\n\
945Clear the internal type lookup cache.");
946
947
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000948static PyMethodDef sys_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Might as well keep this in alphabetic order */
950 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
951 callstats_doc},
952 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
953 sys_clear_type_cache__doc__},
954 {"_current_frames", sys_current_frames, METH_NOARGS,
955 current_frames_doc},
956 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
957 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
958 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
959 {"exit", sys_exit, METH_VARARGS, exit_doc},
960 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
961 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000962#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
964 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000965#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000966#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000968#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000969#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000971#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
973 METH_NOARGS, getfilesystemencoding_doc},
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000974#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000976#endif
977#ifdef Py_REF_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000979#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
981 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
982 getrecursionlimit_doc},
983 {"getsizeof", (PyCFunction)sys_getsizeof,
984 METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
985 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000986#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
988 getwindowsversion_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000989#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 {"intern", sys_intern, METH_VARARGS, intern_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000991#ifdef USE_MALLOPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000993#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
995 setcheckinterval_doc},
996 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
997 getcheckinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000998#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS,
1000 setswitchinterval_doc},
1001 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS,
1002 getswitchinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001003#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001004#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
1006 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001007#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
1009 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
1010 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
1011 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001012#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001014#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 {"settrace", sys_settrace, METH_O, settrace_doc},
1016 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
1017 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
1018 {NULL, NULL} /* sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001019};
1020
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001021static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001022list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyObject *list = PyList_New(0);
1025 int i;
1026 if (list == NULL)
1027 return NULL;
1028 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1029 PyObject *name = PyUnicode_FromString(
1030 PyImport_Inittab[i].name);
1031 if (name == NULL)
1032 break;
1033 PyList_Append(list, name);
1034 Py_DECREF(name);
1035 }
1036 if (PyList_Sort(list) != 0) {
1037 Py_DECREF(list);
1038 list = NULL;
1039 }
1040 if (list) {
1041 PyObject *v = PyList_AsTuple(list);
1042 Py_DECREF(list);
1043 list = v;
1044 }
1045 return list;
Guido van Rossum34679b71993-01-26 13:33:44 +00001046}
1047
Guido van Rossum23fff912000-12-15 22:02:05 +00001048static PyObject *warnoptions = NULL;
1049
1050void
1051PySys_ResetWarnOptions(void)
1052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (warnoptions == NULL || !PyList_Check(warnoptions))
1054 return;
1055 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
Guido van Rossum23fff912000-12-15 22:02:05 +00001056}
1057
1058void
Victor Stinner9ca9c252010-05-19 16:53:30 +00001059PySys_AddWarnOptionUnicode(PyObject *unicode)
Guido van Rossum23fff912000-12-15 22:02:05 +00001060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
1062 Py_XDECREF(warnoptions);
1063 warnoptions = PyList_New(0);
1064 if (warnoptions == NULL)
1065 return;
1066 }
Victor Stinner9ca9c252010-05-19 16:53:30 +00001067 PyList_Append(warnoptions, unicode);
1068}
1069
1070void
1071PySys_AddWarnOption(const wchar_t *s)
1072{
1073 PyObject *unicode;
1074 unicode = PyUnicode_FromWideChar(s, -1);
1075 if (unicode == NULL)
1076 return;
1077 PySys_AddWarnOptionUnicode(unicode);
1078 Py_DECREF(unicode);
Guido van Rossum23fff912000-12-15 22:02:05 +00001079}
1080
Christian Heimes33fe8092008-04-13 13:53:33 +00001081int
1082PySys_HasWarnOptions(void)
1083{
1084 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
1085}
1086
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001087static PyObject *xoptions = NULL;
1088
1089static PyObject *
1090get_xoptions(void)
1091{
1092 if (xoptions == NULL || !PyDict_Check(xoptions)) {
1093 Py_XDECREF(xoptions);
1094 xoptions = PyDict_New();
1095 }
1096 return xoptions;
1097}
1098
1099void
1100PySys_AddXOption(const wchar_t *s)
1101{
1102 PyObject *opts;
1103 PyObject *name = NULL, *value = NULL;
1104 const wchar_t *name_end;
1105 int r;
1106
1107 opts = get_xoptions();
1108 if (opts == NULL)
1109 goto error;
1110
1111 name_end = wcschr(s, L'=');
1112 if (!name_end) {
1113 name = PyUnicode_FromWideChar(s, -1);
1114 value = Py_True;
1115 Py_INCREF(value);
1116 }
1117 else {
1118 name = PyUnicode_FromWideChar(s, name_end - s);
1119 value = PyUnicode_FromWideChar(name_end + 1, -1);
1120 }
1121 if (name == NULL || value == NULL)
1122 goto error;
1123 r = PyDict_SetItem(opts, name, value);
1124 Py_DECREF(name);
1125 Py_DECREF(value);
1126 return;
1127
1128error:
1129 Py_XDECREF(name);
1130 Py_XDECREF(value);
1131 /* No return value, therefore clear error state if possible */
1132 if (_Py_atomic_load_relaxed(&_PyThreadState_Current))
1133 PyErr_Clear();
1134}
1135
1136PyObject *
1137PySys_GetXOptions(void)
1138{
1139 return get_xoptions();
1140}
1141
Guido van Rossum40552d01998-08-06 03:34:39 +00001142/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
1143 Two literals concatenated works just fine. If you have a K&R compiler
1144 or other abomination that however *does* understand longer strings,
1145 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001146PyDoc_VAR(sys_doc) =
1147PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001148"This module provides access to some objects used or maintained by the\n\
1149interpreter and to functions that interact strongly with the interpreter.\n\
1150\n\
1151Dynamic objects:\n\
1152\n\
1153argv -- command line arguments; argv[0] is the script pathname if known\n\
1154path -- module search path; path[0] is the script directory, else ''\n\
1155modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001156\n\
1157displayhook -- called to show results in an interactive session\n\
1158excepthook -- called to handle any uncaught exception other than SystemExit\n\
1159 To customize printing in an interactive session or to install a custom\n\
1160 top-level exception handler, assign other functions to replace these.\n\
1161\n\
Benjamin Peterson06157a42008-07-15 00:28:36 +00001162stdin -- standard input file object; used by input()\n\
Georg Brandl88fc6642007-02-09 21:28:07 +00001163stdout -- standard output file object; used by print()\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001164stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001165 By assigning other file objects (or objects that behave like files)\n\
1166 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001167\n\
1168last_type -- type of last uncaught exception\n\
1169last_value -- value of last uncaught exception\n\
1170last_traceback -- traceback of last uncaught exception\n\
1171 These three are only available in an interactive session after a\n\
1172 traceback has been printed.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001173"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001174)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001175/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001176PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001177"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001178Static objects:\n\
1179\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001180float_info -- a dict with information about the float implementation.\n\
Mark Dickinsonbd792642009-03-18 20:06:12 +00001181int_info -- a struct sequence with information about the int implementation.\n\
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001182maxsize -- the largest supported length of containers.\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +00001183maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001184builtin_module_names -- tuple of module names built into this interpreter\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001185subversion -- subversion information of the build as tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001186version -- the version of this interpreter as a string\n\
Eric Smith0e5b5622009-02-06 01:32:42 +00001187version_info -- version information as a named tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001188hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001189copyright -- copyright notice pertaining to this interpreter\n\
1190platform -- platform identifier\n\
1191executable -- pathname of this Python interpreter\n\
1192prefix -- prefix used to find the Python library\n\
1193exec_prefix -- prefix used to find the machine-specific Python library\n\
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001194float_repr_style -- string indicating the style of repr() output for floats\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001195"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001196)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001197#ifdef MS_WINDOWS
1198/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001199PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001200"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001201winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001202"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001203)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001204#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001205PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001206"__stdin__ -- the original stdin; don't touch!\n\
1207__stdout__ -- the original stdout; don't touch!\n\
1208__stderr__ -- the original stderr; don't touch!\n\
1209__displayhook__ -- the original displayhook; don't touch!\n\
1210__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001211\n\
1212Functions:\n\
1213\n\
Georg Brandl1a3284e2007-12-02 09:40:06 +00001214displayhook() -- print an object to the screen, and save it in builtins._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001215excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001216exc_info() -- return thread-safe information about the current exception\n\
1217exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001218getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001219getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001220getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001221getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001222getsizeof() -- return the size of an object in bytes\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001223gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001224setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001225setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001226setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001227setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001228settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001229"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001230)
Fred Drakeccede592000-08-14 20:59:57 +00001231/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001232
Martin v. Löwis43b57802006-01-05 23:38:54 +00001233/* Subversion branch and revision management */
1234static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
1235static const char headurl[] = "$HeadURL$";
1236static int svn_initialized;
1237static char patchlevel_revision[50]; /* Just the number */
1238static char branch[50];
1239static char shortbranch[50];
1240static const char *svn_revision;
1241
Tim Peterse86e7a52006-01-06 02:42:46 +00001242static void
1243svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +00001244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 const char *python, *br_start, *br_end, *br_end2, *svnversion;
1246 Py_ssize_t len;
1247 int istag = 0;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (svn_initialized)
1250 return;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 python = strstr(headurl, "/python/");
1253 if (!python) {
1254 strcpy(branch, "unknown branch");
1255 strcpy(shortbranch, "unknown");
1256 }
1257 else {
1258 br_start = python + 8;
1259 br_end = strchr(br_start, '/');
1260 assert(br_end);
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 /* Works even for trunk,
1263 as we are in trunk/Python/sysmodule.c */
1264 br_end2 = strchr(br_end+1, '/');
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 istag = strncmp(br_start, "tags", 4) == 0;
1267 if (strncmp(br_start, "trunk", 5) == 0) {
1268 strcpy(branch, "trunk");
1269 strcpy(shortbranch, "trunk");
1270 }
1271 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1272 len = br_end2 - br_start;
1273 strncpy(branch, br_start, len);
1274 branch[len] = '\0';
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 len = br_end2 - (br_end + 1);
1277 strncpy(shortbranch, br_end + 1, len);
1278 shortbranch[len] = '\0';
1279 }
1280 else {
1281 Py_FatalError("bad HeadURL");
1282 return;
1283 }
1284 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001285
1286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 svnversion = _Py_svnversion();
1288 if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0)
1289 svn_revision = svnversion;
1290 else if (istag) {
1291 len = strlen(_patchlevel_revision);
1292 assert(len >= 13);
1293 assert(len < (sizeof(patchlevel_revision) + 13));
1294 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1295 len - 13);
1296 patchlevel_revision[len - 13] = '\0';
1297 svn_revision = patchlevel_revision;
1298 }
1299 else
1300 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 svn_initialized = 1;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001303}
1304
1305/* Return svnversion output if available.
1306 Else return Revision of patchlevel.h if on branch.
1307 Else return empty string */
1308const char*
1309Py_SubversionRevision()
1310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 svnversion_init();
1312 return svn_revision;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001313}
1314
1315const char*
1316Py_SubversionShortBranch()
1317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 svnversion_init();
1319 return shortbranch;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001320}
1321
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001322
1323PyDoc_STRVAR(flags__doc__,
1324"sys.flags\n\
1325\n\
1326Flags provided through command line arguments or environment vars.");
1327
1328static PyTypeObject FlagsType;
1329
1330static PyStructSequence_Field flags_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 {"debug", "-d"},
1332 {"division_warning", "-Q"},
1333 {"inspect", "-i"},
1334 {"interactive", "-i"},
1335 {"optimize", "-O or -OO"},
1336 {"dont_write_bytecode", "-B"},
1337 {"no_user_site", "-s"},
1338 {"no_site", "-S"},
1339 {"ignore_environment", "-E"},
1340 {"verbose", "-v"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001341#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 {"riscos_wimp", "???"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001343#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 /* {"unbuffered", "-u"}, */
1345 /* {"skip_first", "-x"}, */
1346 {"bytes_warning", "-b"},
1347 {0}
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001348};
1349
1350static PyStructSequence_Desc flags_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 "sys.flags", /* name */
1352 flags__doc__, /* doc */
1353 flags_fields, /* fields */
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001354#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 12
Georg Brandle1b5ac62008-06-04 13:06:58 +00001356#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 11
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001358#endif
1359};
1360
1361static PyObject*
1362make_flags(void)
1363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 int pos = 0;
1365 PyObject *seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 seq = PyStructSequence_New(&FlagsType);
1368 if (seq == NULL)
1369 return NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001370
1371#define SetFlag(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 SetFlag(Py_DebugFlag);
1375 SetFlag(Py_DivisionWarningFlag);
1376 SetFlag(Py_InspectFlag);
1377 SetFlag(Py_InteractiveFlag);
1378 SetFlag(Py_OptimizeFlag);
1379 SetFlag(Py_DontWriteBytecodeFlag);
1380 SetFlag(Py_NoUserSiteDirectory);
1381 SetFlag(Py_NoSiteFlag);
1382 SetFlag(Py_IgnoreEnvironmentFlag);
1383 SetFlag(Py_VerboseFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001384#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 SetFlag(Py_RISCOSWimpFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001386#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* SetFlag(saw_unbuffered_flag); */
1388 /* SetFlag(skipfirstline); */
Christian Heimes33fe8092008-04-13 13:53:33 +00001389 SetFlag(Py_BytesWarningFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001390#undef SetFlag
1391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (PyErr_Occurred()) {
1393 return NULL;
1394 }
1395 return seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001396}
1397
Eric Smith0e5b5622009-02-06 01:32:42 +00001398PyDoc_STRVAR(version_info__doc__,
1399"sys.version_info\n\
1400\n\
1401Version information as a named tuple.");
1402
1403static PyTypeObject VersionInfoType;
1404
1405static PyStructSequence_Field version_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 {"major", "Major release number"},
1407 {"minor", "Minor release number"},
1408 {"micro", "Patch release number"},
1409 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1410 {"serial", "Serial release number"},
1411 {0}
Eric Smith0e5b5622009-02-06 01:32:42 +00001412};
1413
1414static PyStructSequence_Desc version_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 "sys.version_info", /* name */
1416 version_info__doc__, /* doc */
1417 version_info_fields, /* fields */
1418 5
Eric Smith0e5b5622009-02-06 01:32:42 +00001419};
1420
1421static PyObject *
1422make_version_info(void)
1423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 PyObject *version_info;
1425 char *s;
1426 int pos = 0;
Eric Smith0e5b5622009-02-06 01:32:42 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 version_info = PyStructSequence_New(&VersionInfoType);
1429 if (version_info == NULL) {
1430 return NULL;
1431 }
Eric Smith0e5b5622009-02-06 01:32:42 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 /*
1434 * These release level checks are mutually exclusive and cover
1435 * the field, so don't get too fancy with the pre-processor!
1436 */
Eric Smith0e5b5622009-02-06 01:32:42 +00001437#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 s = "alpha";
Eric Smith0e5b5622009-02-06 01:32:42 +00001439#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 s = "beta";
Eric Smith0e5b5622009-02-06 01:32:42 +00001441#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 s = "candidate";
Eric Smith0e5b5622009-02-06 01:32:42 +00001443#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 s = "final";
Eric Smith0e5b5622009-02-06 01:32:42 +00001445#endif
1446
1447#define SetIntItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001449#define SetStrItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 SetIntItem(PY_MAJOR_VERSION);
1453 SetIntItem(PY_MINOR_VERSION);
1454 SetIntItem(PY_MICRO_VERSION);
1455 SetStrItem(s);
1456 SetIntItem(PY_RELEASE_SERIAL);
Eric Smith0e5b5622009-02-06 01:32:42 +00001457#undef SetIntItem
1458#undef SetStrItem
1459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (PyErr_Occurred()) {
1461 Py_CLEAR(version_info);
1462 return NULL;
1463 }
1464 return version_info;
Eric Smith0e5b5622009-02-06 01:32:42 +00001465}
1466
Martin v. Löwis1a214512008-06-11 05:26:20 +00001467static struct PyModuleDef sysmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 PyModuleDef_HEAD_INIT,
1469 "sys",
1470 sys_doc,
1471 -1, /* multiple "initialization" just copies the module dict. */
1472 sys_methods,
1473 NULL,
1474 NULL,
1475 NULL,
1476 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001477};
1478
Guido van Rossum25ce5661997-08-02 03:10:38 +00001479PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001480_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 PyObject *m, *v, *sysdict;
1483 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 m = PyModule_Create(&sysmodule);
1486 if (m == NULL)
1487 return NULL;
1488 sysdict = PyModule_GetDict(m);
1489#define SET_SYS_FROM_STRING(key, value) \
1490 v = value; \
1491 if (v != NULL) \
1492 PyDict_SetItemString(sysdict, key, v); \
1493 Py_XDECREF(v)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 /* Check that stdin is not a directory
1496 Using shell redirection, you can redirect stdin to a directory,
1497 crashing the Python interpreter. Catch this common mistake here
1498 and output a useful error message. Note that under MS Windows,
1499 the shell already prevents that. */
Martin v. Löwisec59d042009-01-12 07:59:10 +00001500#if !defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 {
1502 struct stat sb;
1503 if (fstat(fileno(stdin), &sb) == 0 &&
1504 S_ISDIR(sb.st_mode)) {
1505 /* There's nothing more we can do. */
1506 /* Py_FatalError() will core dump, so just exit. */
1507 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1508 exit(EXIT_FAILURE);
1509 }
1510 }
Martin v. Löwisec59d042009-01-12 07:59:10 +00001511#endif
Neal Norwitz11bd1192005-10-03 00:54:56 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* stdin/stdout/stderr are now set by pythonrun.c */
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 PyDict_SetItemString(sysdict, "__displayhook__",
1516 PyDict_GetItemString(sysdict, "displayhook"));
1517 PyDict_SetItemString(sysdict, "__excepthook__",
1518 PyDict_GetItemString(sysdict, "excepthook"));
1519 SET_SYS_FROM_STRING("version",
1520 PyUnicode_FromString(Py_GetVersion()));
1521 SET_SYS_FROM_STRING("hexversion",
1522 PyLong_FromLong(PY_VERSION_HEX));
1523 svnversion_init();
1524 SET_SYS_FROM_STRING("subversion",
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001525 Py_BuildValue("(sss)", "CPython", branch,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 svn_revision));
1527 SET_SYS_FROM_STRING("dont_write_bytecode",
1528 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1529 SET_SYS_FROM_STRING("api_version",
1530 PyLong_FromLong(PYTHON_API_VERSION));
1531 SET_SYS_FROM_STRING("copyright",
1532 PyUnicode_FromString(Py_GetCopyright()));
1533 SET_SYS_FROM_STRING("platform",
1534 PyUnicode_FromString(Py_GetPlatform()));
1535 SET_SYS_FROM_STRING("executable",
1536 PyUnicode_FromWideChar(
1537 Py_GetProgramFullPath(), -1));
1538 SET_SYS_FROM_STRING("prefix",
1539 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1540 SET_SYS_FROM_STRING("exec_prefix",
1541 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
1542 SET_SYS_FROM_STRING("maxsize",
1543 PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1544 SET_SYS_FROM_STRING("float_info",
1545 PyFloat_GetInfo());
1546 SET_SYS_FROM_STRING("int_info",
1547 PyLong_GetInfo());
Mark Dickinsondc787d22010-05-23 13:33:13 +00001548 /* initialize hash_info */
1549 if (Hash_InfoType.tp_name == 0)
1550 PyStructSequence_InitType(&Hash_InfoType, &hash_info_desc);
1551 SET_SYS_FROM_STRING("hash_info",
1552 get_hash_info());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 SET_SYS_FROM_STRING("maxunicode",
1554 PyLong_FromLong(PyUnicode_GetMax()));
1555 SET_SYS_FROM_STRING("builtin_module_names",
1556 list_builtin_module_names());
1557 {
1558 /* Assumes that longs are at least 2 bytes long.
1559 Should be safe! */
1560 unsigned long number = 1;
1561 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 s = (char *) &number;
1564 if (s[0] == 0)
1565 value = "big";
1566 else
1567 value = "little";
1568 SET_SYS_FROM_STRING("byteorder",
1569 PyUnicode_FromString(value));
1570 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001571#ifdef MS_COREDLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 SET_SYS_FROM_STRING("dllhandle",
1573 PyLong_FromVoidPtr(PyWin_DLLhModule));
1574 SET_SYS_FROM_STRING("winver",
1575 PyUnicode_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001576#endif
Barry Warsaw8cf4eae2010-10-16 01:04:07 +00001577#ifdef ABIFLAGS
1578 SET_SYS_FROM_STRING("abiflags",
1579 PyUnicode_FromString(ABIFLAGS));
1580#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 if (warnoptions == NULL) {
1582 warnoptions = PyList_New(0);
1583 }
1584 else {
1585 Py_INCREF(warnoptions);
1586 }
1587 if (warnoptions != NULL) {
1588 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1589 }
Tim Peters216b78b2006-01-06 02:40:53 +00001590
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001591 v = get_xoptions();
1592 if (v != NULL) {
1593 PyDict_SetItemString(sysdict, "_xoptions", v);
1594 }
1595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 /* version_info */
1597 if (VersionInfoType.tp_name == 0)
1598 PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
1599 SET_SYS_FROM_STRING("version_info", make_version_info());
1600 /* prevent user from creating new instances */
1601 VersionInfoType.tp_init = NULL;
1602 VersionInfoType.tp_new = NULL;
Eric Smith0e5b5622009-02-06 01:32:42 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 /* flags */
1605 if (FlagsType.tp_name == 0)
1606 PyStructSequence_InitType(&FlagsType, &flags_desc);
1607 SET_SYS_FROM_STRING("flags", make_flags());
1608 /* prevent user from creating new instances */
1609 FlagsType.tp_init = NULL;
1610 FlagsType.tp_new = NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001611
Eric Smithf7bb5782010-01-27 00:44:57 +00001612
1613#if defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 /* getwindowsversion */
1615 if (WindowsVersionType.tp_name == 0)
1616 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc);
1617 /* prevent user from creating new instances */
1618 WindowsVersionType.tp_init = NULL;
1619 WindowsVersionType.tp_new = NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +00001620#endif
1621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001623#ifndef PY_NO_SHORT_FLOAT_REPR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 SET_SYS_FROM_STRING("float_repr_style",
1625 PyUnicode_FromString("short"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001626#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 SET_SYS_FROM_STRING("float_repr_style",
1628 PyUnicode_FromString("legacy"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001629#endif
1630
Christian Heimes7b3ce6a2008-01-31 14:31:45 +00001631#undef SET_SYS_FROM_STRING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 if (PyErr_Occurred())
1633 return NULL;
1634 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001635}
1636
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001637static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001638makepathobject(const wchar_t *path, wchar_t delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 int i, n;
1641 const wchar_t *p;
1642 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 n = 1;
1645 p = path;
1646 while ((p = wcschr(p, delim)) != NULL) {
1647 n++;
1648 p++;
1649 }
1650 v = PyList_New(n);
1651 if (v == NULL)
1652 return NULL;
1653 for (i = 0; ; i++) {
1654 p = wcschr(path, delim);
1655 if (p == NULL)
1656 p = path + wcslen(path); /* End of string */
1657 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
1658 if (w == NULL) {
1659 Py_DECREF(v);
1660 return NULL;
1661 }
1662 PyList_SetItem(v, i, w);
1663 if (*p == '\0')
1664 break;
1665 path = p+1;
1666 }
1667 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
1669
1670void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001671PySys_SetPath(const wchar_t *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 PyObject *v;
1674 if ((v = makepathobject(path, DELIM)) == NULL)
1675 Py_FatalError("can't create sys.path");
1676 if (PySys_SetObject("path", v) != 0)
1677 Py_FatalError("can't assign sys.path");
1678 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001679}
1680
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001681static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001682makeargvobject(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 PyObject *av;
1685 if (argc <= 0 || argv == NULL) {
1686 /* Ensure at least one (empty) argument is seen */
1687 static wchar_t *empty_argv[1] = {L""};
1688 argv = empty_argv;
1689 argc = 1;
1690 }
1691 av = PyList_New(argc);
1692 if (av != NULL) {
1693 int i;
1694 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001695#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 PyObject *v;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 /* argv[0] is the script pathname if known */
1699 if (i == 0) {
1700 char* fn = decc$translate_vms(argv[0]);
1701 if ((fn == (char *)0) || fn == (char *)-1)
1702 v = PyUnicode_FromString(argv[0]);
1703 else
1704 v = PyUnicode_FromString(
1705 decc$translate_vms(argv[0]));
1706 } else
1707 v = PyUnicode_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001708#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001710#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (v == NULL) {
1712 Py_DECREF(av);
1713 av = NULL;
1714 break;
1715 }
1716 PyList_SetItem(av, i, v);
1717 }
1718 }
1719 return av;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001720}
1721
Nick Coghland26c18a2010-08-17 13:06:11 +00001722#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
1723 (argc > 0 && argv0 != NULL && \
1724 wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001725
1726static void
1727sys_update_path(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001728{
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001729 wchar_t *argv0;
1730 wchar_t *p = NULL;
1731 Py_ssize_t n = 0;
1732 PyObject *a;
1733 PyObject *path;
1734#ifdef HAVE_READLINK
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001735 wchar_t link[MAXPATHLEN+1];
1736 wchar_t argv0copy[2*MAXPATHLEN+1];
1737 int nr = 0;
1738#endif
Guido van Rossum162e38c2003-02-19 15:25:10 +00001739#if defined(HAVE_REALPATH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 wchar_t fullpath[MAXPATHLEN];
Martin v. Löwisec59d042009-01-12 07:59:10 +00001741#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 wchar_t fullpath[MAX_PATH];
Thomas Heller27bb71e2003-01-08 14:33:48 +00001743#endif
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001744
1745 path = PySys_GetObject("path");
1746 if (path == NULL)
1747 return;
1748
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001749 argv0 = argv[0];
1750
1751#ifdef HAVE_READLINK
1752 if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
1753 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
1754 if (nr > 0) {
1755 /* It's a symlink */
1756 link[nr] = '\0';
1757 if (link[0] == SEP)
1758 argv0 = link; /* Link to absolute path */
1759 else if (wcschr(link, SEP) == NULL)
1760 ; /* Link without path */
1761 else {
1762 /* Must join(dirname(argv0), link) */
1763 wchar_t *q = wcsrchr(argv0, SEP);
1764 if (q == NULL)
1765 argv0 = link; /* argv0 without path */
1766 else {
1767 /* Must make a copy */
1768 wcscpy(argv0copy, argv0);
1769 q = wcsrchr(argv0copy, SEP);
1770 wcscpy(q+1, link);
1771 argv0 = argv0copy;
1772 }
1773 }
1774 }
1775#endif /* HAVE_READLINK */
1776#if SEP == '\\' /* Special case for MS filename syntax */
1777 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1778 wchar_t *q;
1779#if defined(MS_WINDOWS) && !defined(MS_WINCE)
1780 /* This code here replaces the first element in argv with the full
1781 path that it represents. Under CE, there are no relative paths so
1782 the argument must be the full path anyway. */
1783 wchar_t *ptemp;
1784 if (GetFullPathNameW(argv0,
1785 sizeof(fullpath)/sizeof(fullpath[0]),
1786 fullpath,
1787 &ptemp)) {
1788 argv0 = fullpath;
1789 }
1790#endif
1791 p = wcsrchr(argv0, SEP);
1792 /* Test for alternate separator */
1793 q = wcsrchr(p ? p : argv0, '/');
1794 if (q != NULL)
1795 p = q;
1796 if (p != NULL) {
1797 n = p + 1 - argv0;
1798 if (n > 1 && p[-1] != ':')
1799 n--; /* Drop trailing separator */
1800 }
1801 }
1802#else /* All other filename syntaxes */
1803 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1804#if defined(HAVE_REALPATH)
Victor Stinner015f4d82010-10-07 22:29:53 +00001805 if (_Py_wrealpath(argv0, fullpath, PATH_MAX)) {
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001806 argv0 = fullpath;
1807 }
1808#endif
1809 p = wcsrchr(argv0, SEP);
1810 }
1811 if (p != NULL) {
1812 n = p + 1 - argv0;
1813#if SEP == '/' /* Special case for Unix filename syntax */
1814 if (n > 1)
1815 n--; /* Drop trailing separator */
1816#endif /* Unix */
1817 }
1818#endif /* All others */
1819 a = PyUnicode_FromWideChar(argv0, n);
1820 if (a == NULL)
1821 Py_FatalError("no mem for sys.path insertion");
1822 if (PyList_Insert(path, 0, a) < 0)
1823 Py_FatalError("sys.path.insert(0) failed");
1824 Py_DECREF(a);
1825}
1826
1827void
1828PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
1829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 PyObject *av = makeargvobject(argc, argv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 if (av == NULL)
1832 Py_FatalError("no mem for sys.argv");
1833 if (PySys_SetObject("argv", av) != 0)
1834 Py_FatalError("can't assign sys.argv");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 Py_DECREF(av);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001836 if (updatepath)
1837 sys_update_path(argc, argv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001838}
Guido van Rossuma890e681998-05-12 14:59:24 +00001839
Antoine Pitrouf978fac2010-05-21 17:25:34 +00001840void
1841PySys_SetArgv(int argc, wchar_t **argv)
1842{
1843 PySys_SetArgvEx(argc, argv, 1);
1844}
1845
Victor Stinner14284c22010-04-23 12:02:30 +00001846/* Reimplementation of PyFile_WriteString() no calling indirectly
1847 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
1848
1849static int
Victor Stinner79766632010-08-16 17:36:42 +00001850sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
Victor Stinner14284c22010-04-23 12:02:30 +00001851{
Victor Stinner79766632010-08-16 17:36:42 +00001852 PyObject *writer = NULL, *args = NULL, *result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 int err;
Victor Stinner14284c22010-04-23 12:02:30 +00001854
Victor Stinnerecccc4f2010-06-08 20:46:00 +00001855 if (file == NULL)
1856 return -1;
1857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 writer = PyObject_GetAttrString(file, "write");
1859 if (writer == NULL)
1860 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 args = PyTuple_Pack(1, unicode);
1863 if (args == NULL)
1864 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 result = PyEval_CallObject(writer, args);
1867 if (result == NULL) {
1868 goto error;
1869 } else {
1870 err = 0;
1871 goto finally;
1872 }
Victor Stinner14284c22010-04-23 12:02:30 +00001873
1874error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 err = -1;
Victor Stinner14284c22010-04-23 12:02:30 +00001876finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 Py_XDECREF(writer);
1878 Py_XDECREF(args);
1879 Py_XDECREF(result);
1880 return err;
Victor Stinner14284c22010-04-23 12:02:30 +00001881}
1882
Victor Stinner79766632010-08-16 17:36:42 +00001883static int
1884sys_pyfile_write(const char *text, PyObject *file)
1885{
1886 PyObject *unicode = NULL;
1887 int err;
1888
1889 if (file == NULL)
1890 return -1;
1891
1892 unicode = PyUnicode_FromString(text);
1893 if (unicode == NULL)
1894 return -1;
1895
1896 err = sys_pyfile_write_unicode(unicode, file);
1897 Py_DECREF(unicode);
1898 return err;
1899}
Guido van Rossuma890e681998-05-12 14:59:24 +00001900
1901/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1902 Adapted from code submitted by Just van Rossum.
1903
1904 PySys_WriteStdout(format, ...)
1905 PySys_WriteStderr(format, ...)
1906
1907 The first function writes to sys.stdout; the second to sys.stderr. When
1908 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001909 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001910
Victor Stinner14284c22010-04-23 12:02:30 +00001911 PyErr_CheckSignals() is not called to avoid the execution of the Python
Victor Stinner79766632010-08-16 17:36:42 +00001912 signal handlers: they may raise a new exception whereas sys_write()
1913 ignores all exceptions.
Victor Stinner14284c22010-04-23 12:02:30 +00001914
Guido van Rossuma890e681998-05-12 14:59:24 +00001915 Both take a printf-style format string as their first argument followed
1916 by a variable length argument list determined by the format string.
1917
1918 *** WARNING ***
1919
1920 The format should limit the total size of the formatted output string to
1921 1000 bytes. In particular, this means that no unrestricted "%s" formats
1922 should occur; these should be limited using "%.<N>s where <N> is a
1923 decimal number calculated so that <N> plus the maximum size of other
1924 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1925 which can print hundreds of digits for very large numbers.
1926
1927 */
1928
1929static void
Victor Stinner79766632010-08-16 17:36:42 +00001930sys_write(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 PyObject *file;
1933 PyObject *error_type, *error_value, *error_traceback;
1934 char buffer[1001];
1935 int written;
Guido van Rossuma890e681998-05-12 14:59:24 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1938 file = PySys_GetObject(name);
1939 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
1940 if (sys_pyfile_write(buffer, file) != 0) {
1941 PyErr_Clear();
1942 fputs(buffer, fp);
1943 }
1944 if (written < 0 || (size_t)written >= sizeof(buffer)) {
1945 const char *truncated = "... truncated";
Victor Stinner79766632010-08-16 17:36:42 +00001946 if (sys_pyfile_write(truncated, file) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 fputs(truncated, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
1949 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001950}
1951
1952void
Guido van Rossuma890e681998-05-12 14:59:24 +00001953PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001958 sys_write("stdout", stdout, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001960}
1961
1962void
Guido van Rossuma890e681998-05-12 14:59:24 +00001963PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001968 sys_write("stderr", stderr, format, va);
1969 va_end(va);
1970}
1971
1972static void
1973sys_format(char *name, FILE *fp, const char *format, va_list va)
1974{
1975 PyObject *file, *message;
1976 PyObject *error_type, *error_value, *error_traceback;
1977 char *utf8;
1978
1979 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1980 file = PySys_GetObject(name);
1981 message = PyUnicode_FromFormatV(format, va);
1982 if (message != NULL) {
1983 if (sys_pyfile_write_unicode(message, file) != 0) {
1984 PyErr_Clear();
1985 utf8 = _PyUnicode_AsString(message);
1986 if (utf8 != NULL)
1987 fputs(utf8, fp);
1988 }
1989 Py_DECREF(message);
1990 }
1991 PyErr_Restore(error_type, error_value, error_traceback);
1992}
1993
1994void
1995PySys_FormatStdout(const char *format, ...)
1996{
1997 va_list va;
1998
1999 va_start(va, format);
2000 sys_format("stdout", stdout, format, va);
2001 va_end(va);
2002}
2003
2004void
2005PySys_FormatStderr(const char *format, ...)
2006{
2007 va_list va;
2008
2009 va_start(va, format);
2010 sys_format("stderr", stderr, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00002012}