blob: 1aa4271d76c03ee077fb8eb4f32e68112607ffb7 [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 Rossuma12fe4e2003-04-09 19:06:21 +000020#include "eval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossume2437a11992-03-23 18:20:18 +000022#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000023
Mark Hammond8696ebc2002-10-08 02:44:31 +000024#ifdef MS_WINDOWS
25#define WIN32_LEAN_AND_MEAN
Amaury Forgeot d'Arc06cfe952007-11-10 13:55:44 +000026#include <windows.h>
Mark Hammond8696ebc2002-10-08 02:44:31 +000027#endif /* MS_WINDOWS */
28
Guido van Rossum9b38a141996-09-11 23:12:24 +000029#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000030extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000031/* A string loaded from the DLL at startup: */
32extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000033#endif
34
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000035#ifdef __VMS
36#include <unixlib.h>
37#endif
38
Martin v. Löwis5467d4c2003-05-10 07:10:12 +000039#ifdef HAVE_LANGINFO_H
40#include <locale.h>
41#include <langinfo.h>
42#endif
43
Guido van Rossum65bf9f21997-04-29 18:33:38 +000044PyObject *
Neal Norwitzf3081322007-08-25 00:32:45 +000045PySys_GetObject(const char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 PyThreadState *tstate = PyThreadState_GET();
48 PyObject *sd = tstate->interp->sysdict;
49 if (sd == NULL)
50 return NULL;
51 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052}
53
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054int
Neal Norwitzf3081322007-08-25 00:32:45 +000055PySys_SetObject(const char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 PyThreadState *tstate = PyThreadState_GET();
58 PyObject *sd = tstate->interp->sysdict;
59 if (v == NULL) {
60 if (PyDict_GetItemString(sd, name) == NULL)
61 return 0;
62 else
63 return PyDict_DelItemString(sd, name);
64 }
65 else
66 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossum65bf9f21997-04-29 18:33:38 +000069static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000070sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 PyObject *outf;
73 PyInterpreterState *interp = PyThreadState_GET()->interp;
74 PyObject *modules = interp->modules;
75 PyObject *builtins = PyDict_GetItemString(modules, "builtins");
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (builtins == NULL) {
78 PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
79 return NULL;
80 }
Moshe Zadka03897ea2001-07-23 13:32:43 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 /* Print value except if None */
83 /* After printing, also assign to '_' */
84 /* Before, set '_' to None to avoid recursion */
85 if (o == Py_None) {
86 Py_INCREF(Py_None);
87 return Py_None;
88 }
89 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
90 return NULL;
91 outf = PySys_GetObject("stdout");
92 if (outf == NULL || outf == Py_None) {
93 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
94 return NULL;
95 }
96 if (PyFile_WriteObject(o, outf, 0) != 0)
97 return NULL;
98 if (PyFile_WriteString("\n", outf) != 0)
99 return NULL;
100 if (PyObject_SetAttrString(builtins, "_", o) != 0)
101 return NULL;
102 Py_INCREF(Py_None);
103 return Py_None;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000104}
105
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000106PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000107"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000108"\n"
Florent Xicluna5749e852010-03-03 11:54:54 +0000109"Print an object to sys.stdout and also save it in builtins._\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000110);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000111
112static PyObject *
113sys_excepthook(PyObject* self, PyObject* args)
114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyObject *exc, *value, *tb;
116 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
117 return NULL;
118 PyErr_Display(exc, value, tb);
119 Py_INCREF(Py_None);
120 return Py_None;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000121}
122
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000123PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000124"excepthook(exctype, value, traceback) -> None\n"
125"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000126"Handle an exception by displaying it with a traceback on sys.stderr.\n"
127);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000128
129static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000130sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 PyThreadState *tstate;
133 tstate = PyThreadState_GET();
134 return Py_BuildValue(
135 "(OOO)",
136 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
137 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
138 tstate->exc_traceback != NULL ?
139 tstate->exc_traceback : Py_None);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000140}
141
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000142PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000143"exc_info() -> (type, value, traceback)\n\
144\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000145Return information about the most recent exception caught by an except\n\
146clause in the current stack frame or in an older stack frame."
147);
148
149static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000150sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 PyObject *exit_code = 0;
153 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
154 return NULL;
155 /* Raise SystemExit so callers may catch it or clean up. */
156 PyErr_SetObject(PyExc_SystemExit, exit_code);
157 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}
159
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000160PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000161"exit([status])\n\
162\n\
163Exit the interpreter by raising SystemExit(status).\n\
164If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000165If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000166If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000167exit status will be one (i.e., failure)."
168);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000169
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000170
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000171static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000172sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
Fred Drake8b4d01d2000-05-09 19:57:01 +0000175}
176
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000177PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000178"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000179\n\
180Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000181implementation."
182);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000183
184static PyObject *
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000185sys_getfilesystemencoding(PyObject *self)
186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (Py_FileSystemDefaultEncoding)
188 return PyUnicode_FromString(Py_FileSystemDefaultEncoding);
189 Py_INCREF(Py_None);
190 return Py_None;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000191}
192
193PyDoc_STRVAR(getfilesystemencoding_doc,
194"getfilesystemencoding() -> string\n\
195\n\
196Return the encoding used to convert Unicode filenames in\n\
197operating system filenames."
198);
199
Martin v. Löwis04dc25c2008-10-03 16:09:28 +0000200static PyObject *
Georg Brandl66a796e2006-12-19 20:50:34 +0000201sys_intern(PyObject *self, PyObject *args)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 PyObject *s;
204 if (!PyArg_ParseTuple(args, "U:intern", &s))
205 return NULL;
206 if (PyUnicode_CheckExact(s)) {
207 Py_INCREF(s);
208 PyUnicode_InternInPlace(&s);
209 return s;
210 }
211 else {
212 PyErr_Format(PyExc_TypeError,
213 "can't intern %.400s", s->ob_type->tp_name);
214 return NULL;
215 }
Georg Brandl66a796e2006-12-19 20:50:34 +0000216}
217
218PyDoc_STRVAR(intern_doc,
219"intern(string) -> string\n\
220\n\
221``Intern'' the given string. This enters the string in the (global)\n\
222table of interned strings whose purpose is to speed up dictionary lookups.\n\
223Return the string itself or the previously interned string object with the\n\
224same value.");
225
226
Fred Drake5755ce62001-06-27 19:19:46 +0000227/*
228 * Cached interned string objects used for calling the profile and
229 * trace functions. Initialized by trace_init().
230 */
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000231static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Fred Drake5755ce62001-06-27 19:19:46 +0000232
233static int
234trace_init(void)
235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 static char *whatnames[7] = {"call", "exception", "line", "return",
237 "c_call", "c_exception", "c_return"};
238 PyObject *name;
239 int i;
240 for (i = 0; i < 7; ++i) {
241 if (whatstrings[i] == NULL) {
242 name = PyUnicode_InternFromString(whatnames[i]);
243 if (name == NULL)
244 return -1;
245 whatstrings[i] = name;
246 }
247 }
248 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000249}
250
251
252static PyObject *
253call_trampoline(PyThreadState *tstate, PyObject* callback,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyFrameObject *frame, int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyObject *args = PyTuple_New(3);
257 PyObject *whatstr;
258 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (args == NULL)
261 return NULL;
262 Py_INCREF(frame);
263 whatstr = whatstrings[what];
264 Py_INCREF(whatstr);
265 if (arg == NULL)
266 arg = Py_None;
267 Py_INCREF(arg);
268 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
269 PyTuple_SET_ITEM(args, 1, whatstr);
270 PyTuple_SET_ITEM(args, 2, arg);
Fred Drake5755ce62001-06-27 19:19:46 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* call the Python-level function */
273 PyFrame_FastToLocals(frame);
274 result = PyEval_CallObject(callback, args);
275 PyFrame_LocalsToFast(frame, 1);
276 if (result == NULL)
277 PyTraceBack_Here(frame);
Fred Drake5755ce62001-06-27 19:19:46 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 /* cleanup */
280 Py_DECREF(args);
281 return result;
Fred Drake5755ce62001-06-27 19:19:46 +0000282}
283
284static int
285profile_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 PyThreadState *tstate = frame->f_tstate;
289 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 if (arg == NULL)
292 arg = Py_None;
293 result = call_trampoline(tstate, self, frame, what, arg);
294 if (result == NULL) {
295 PyEval_SetProfile(NULL, NULL);
296 return -1;
297 }
298 Py_DECREF(result);
299 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000300}
301
302static int
303trace_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 PyThreadState *tstate = frame->f_tstate;
307 PyObject *callback;
308 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (what == PyTrace_CALL)
311 callback = self;
312 else
313 callback = frame->f_trace;
314 if (callback == NULL)
315 return 0;
316 result = call_trampoline(tstate, callback, frame, what, arg);
317 if (result == NULL) {
318 PyEval_SetTrace(NULL, NULL);
319 Py_XDECREF(frame->f_trace);
320 frame->f_trace = NULL;
321 return -1;
322 }
323 if (result != Py_None) {
324 PyObject *temp = frame->f_trace;
325 frame->f_trace = NULL;
326 Py_XDECREF(temp);
327 frame->f_trace = result;
328 }
329 else {
330 Py_DECREF(result);
331 }
332 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000333}
Fred Draked0838392001-06-16 21:02:31 +0000334
Fred Drake8b4d01d2000-05-09 19:57:01 +0000335static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (trace_init() == -1)
339 return NULL;
340 if (args == Py_None)
341 PyEval_SetTrace(NULL, NULL);
342 else
343 PyEval_SetTrace(trace_trampoline, args);
344 Py_INCREF(Py_None);
345 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000346}
347
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000348PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000349"settrace(function)\n\
350\n\
351Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000352function call. See the debugger chapter in the library manual."
353);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000354
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000355static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000356sys_gettrace(PyObject *self, PyObject *args)
357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 PyThreadState *tstate = PyThreadState_GET();
359 PyObject *temp = tstate->c_traceobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (temp == NULL)
362 temp = Py_None;
363 Py_INCREF(temp);
364 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000365}
366
367PyDoc_STRVAR(gettrace_doc,
368"gettrace()\n\
369\n\
370Return the global debug tracing function set with sys.settrace.\n\
371See the debugger chapter in the library manual."
372);
373
374static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000375sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (trace_init() == -1)
378 return NULL;
379 if (args == Py_None)
380 PyEval_SetProfile(NULL, NULL);
381 else
382 PyEval_SetProfile(profile_trampoline, args);
383 Py_INCREF(Py_None);
384 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000385}
386
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000387PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000388"setprofile(function)\n\
389\n\
390Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000391and return. See the profiler chapter in the library manual."
392);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000393
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000394static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000395sys_getprofile(PyObject *self, PyObject *args)
396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 PyThreadState *tstate = PyThreadState_GET();
398 PyObject *temp = tstate->c_profileobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (temp == NULL)
401 temp = Py_None;
402 Py_INCREF(temp);
403 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000404}
405
406PyDoc_STRVAR(getprofile_doc,
407"getprofile()\n\
408\n\
409Return the profiling function set with sys.setprofile.\n\
410See the profiler chapter in the library manual."
411);
412
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000413static int _check_interval = 100;
414
Christian Heimes9bd667a2008-01-20 15:14:11 +0000415static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (PyErr_WarnEx(PyExc_DeprecationWarning,
419 "sys.getcheckinterval() and sys.setcheckinterval() "
420 "are deprecated. Use sys.setswitchinterval() "
421 "instead.", 1) < 0)
422 return NULL;
423 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval))
424 return NULL;
425 Py_INCREF(Py_None);
426 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000427}
428
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000429PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000430"setcheckinterval(n)\n\
431\n\
432Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000433n instructions. This also affects how often thread switches occur."
434);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000435
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000436static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000437sys_getcheckinterval(PyObject *self, PyObject *args)
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (PyErr_WarnEx(PyExc_DeprecationWarning,
440 "sys.getcheckinterval() and sys.setcheckinterval() "
441 "are deprecated. Use sys.getswitchinterval() "
442 "instead.", 1) < 0)
443 return NULL;
444 return PyLong_FromLong(_check_interval);
Tim Peterse5e065b2003-07-06 18:36:54 +0000445}
446
447PyDoc_STRVAR(getcheckinterval_doc,
448"getcheckinterval() -> current check interval; see setcheckinterval()."
449);
450
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000451#ifdef WITH_THREAD
452static PyObject *
453sys_setswitchinterval(PyObject *self, PyObject *args)
454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 double d;
456 if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d))
457 return NULL;
458 if (d <= 0.0) {
459 PyErr_SetString(PyExc_ValueError,
460 "switch interval must be strictly positive");
461 return NULL;
462 }
463 _PyEval_SetSwitchInterval((unsigned long) (1e6 * d));
464 Py_INCREF(Py_None);
465 return Py_None;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000466}
467
468PyDoc_STRVAR(setswitchinterval_doc,
469"setswitchinterval(n)\n\
470\n\
471Set the ideal thread switching delay inside the Python interpreter\n\
472The actual frequency of switching threads can be lower if the\n\
473interpreter executes long sequences of uninterruptible code\n\
474(this is implementation-specific and workload-dependent).\n\
475\n\
476The parameter must represent the desired switching delay in seconds\n\
477A typical value is 0.005 (5 milliseconds)."
478);
479
480static PyObject *
481sys_getswitchinterval(PyObject *self, PyObject *args)
482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval());
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000484}
485
486PyDoc_STRVAR(getswitchinterval_doc,
487"getswitchinterval() -> current thread switch interval; see setswitchinterval()."
488);
489
490#endif /* WITH_THREAD */
491
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000492#ifdef WITH_TSC
493static PyObject *
494sys_settscdump(PyObject *self, PyObject *args)
495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 int bool;
497 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
500 return NULL;
501 if (bool)
502 tstate->interp->tscdump = 1;
503 else
504 tstate->interp->tscdump = 0;
505 Py_INCREF(Py_None);
506 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000507
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000508}
509
Tim Peters216b78b2006-01-06 02:40:53 +0000510PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000511"settscdump(bool)\n\
512\n\
513If true, tell the Python interpreter to dump VM measurements to\n\
514stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000515processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000516);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000517#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000518
Tim Peterse5e065b2003-07-06 18:36:54 +0000519static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000520sys_setrecursionlimit(PyObject *self, PyObject *args)
521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 int new_limit;
523 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
524 return NULL;
525 if (new_limit <= 0) {
526 PyErr_SetString(PyExc_ValueError,
527 "recursion limit must be positive");
528 return NULL;
529 }
530 Py_SetRecursionLimit(new_limit);
531 Py_INCREF(Py_None);
532 return Py_None;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000533}
534
Mark Dickinsondc787d22010-05-23 13:33:13 +0000535static PyTypeObject Hash_InfoType;
536
537PyDoc_STRVAR(hash_info_doc,
538"hash_info\n\
539\n\
540A struct sequence providing parameters used for computing\n\
541numeric hashes. The attributes are read only.");
542
543static PyStructSequence_Field hash_info_fields[] = {
544 {"width", "width of the type used for hashing, in bits"},
545 {"modulus", "prime number giving the modulus on which the hash "
546 "function is based"},
547 {"inf", "value to be used for hash of a positive infinity"},
548 {"nan", "value to be used for hash of a nan"},
549 {"imag", "multiplier used for the imaginary part of a complex number"},
550 {NULL, NULL}
551};
552
553static PyStructSequence_Desc hash_info_desc = {
554 "sys.hash_info",
555 hash_info_doc,
556 hash_info_fields,
557 5,
558};
559
Matthias Klosed885e952010-07-06 10:53:30 +0000560static PyObject *
Mark Dickinsondc787d22010-05-23 13:33:13 +0000561get_hash_info(void)
562{
563 PyObject *hash_info;
564 int field = 0;
565 hash_info = PyStructSequence_New(&Hash_InfoType);
566 if (hash_info == NULL)
567 return NULL;
568 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000569 PyLong_FromLong(8*sizeof(Py_hash_t)));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000570 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000571 PyLong_FromSsize_t(_PyHASH_MODULUS));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000572 PyStructSequence_SET_ITEM(hash_info, field++,
573 PyLong_FromLong(_PyHASH_INF));
574 PyStructSequence_SET_ITEM(hash_info, field++,
575 PyLong_FromLong(_PyHASH_NAN));
576 PyStructSequence_SET_ITEM(hash_info, field++,
577 PyLong_FromLong(_PyHASH_IMAG));
578 if (PyErr_Occurred()) {
579 Py_CLEAR(hash_info);
580 return NULL;
581 }
582 return hash_info;
583}
584
585
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000586PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000587"setrecursionlimit(n)\n\
588\n\
589Set the maximum depth of the Python interpreter stack to n. This\n\
590limit prevents infinite recursion from causing an overflow of the C\n\
591stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000592dependent."
593);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000594
595static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000596sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 return PyLong_FromLong(Py_GetRecursionLimit());
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000599}
600
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000601PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000602"getrecursionlimit()\n\
603\n\
604Return the current value of the recursion limit, the maximum depth\n\
605of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000606recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000607);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000608
Mark Hammond8696ebc2002-10-08 02:44:31 +0000609#ifdef MS_WINDOWS
610PyDoc_STRVAR(getwindowsversion_doc,
611"getwindowsversion()\n\
612\n\
Eric Smithf7bb5782010-01-27 00:44:57 +0000613Return information about the running version of Windows as a named tuple.\n\
614The members are named: major, minor, build, platform, service_pack,\n\
615service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\
616backward compatibiliy, only the first 5 items are available by indexing.\n\
617All elements are numbers, except service_pack which is a string. Platform\n\
618may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\
6193 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\
620controller, 3 for a server."
Mark Hammond8696ebc2002-10-08 02:44:31 +0000621);
622
Eric Smithf7bb5782010-01-27 00:44:57 +0000623static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};
624
625static PyStructSequence_Field windows_version_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 {"major", "Major version number"},
627 {"minor", "Minor version number"},
628 {"build", "Build number"},
629 {"platform", "Operating system platform"},
630 {"service_pack", "Latest Service Pack installed on the system"},
631 {"service_pack_major", "Service Pack major version number"},
632 {"service_pack_minor", "Service Pack minor version number"},
633 {"suite_mask", "Bit mask identifying available product suites"},
634 {"product_type", "System product type"},
635 {0}
Eric Smithf7bb5782010-01-27 00:44:57 +0000636};
637
638static PyStructSequence_Desc windows_version_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 "sys.getwindowsversion", /* name */
640 getwindowsversion_doc, /* doc */
641 windows_version_fields, /* fields */
642 5 /* For backward compatibility,
643 only the first 5 items are accessible
644 via indexing, the rest are name only */
Eric Smithf7bb5782010-01-27 00:44:57 +0000645};
646
Mark Hammond8696ebc2002-10-08 02:44:31 +0000647static PyObject *
648sys_getwindowsversion(PyObject *self)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyObject *version;
651 int pos = 0;
652 OSVERSIONINFOEX ver;
653 ver.dwOSVersionInfoSize = sizeof(ver);
654 if (!GetVersionEx((OSVERSIONINFO*) &ver))
655 return PyErr_SetFromWindowsErr(0);
Eric Smithf7bb5782010-01-27 00:44:57 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 version = PyStructSequence_New(&WindowsVersionType);
658 if (version == NULL)
659 return NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
662 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
663 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
664 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
665 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
666 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
667 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
668 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
669 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
Eric Smithf7bb5782010-01-27 00:44:57 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 return version;
Mark Hammond8696ebc2002-10-08 02:44:31 +0000672}
673
674#endif /* MS_WINDOWS */
675
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000676#ifdef HAVE_DLOPEN
677static PyObject *
678sys_setdlopenflags(PyObject *self, PyObject *args)
679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 int new_val;
681 PyThreadState *tstate = PyThreadState_GET();
682 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
683 return NULL;
684 if (!tstate)
685 return NULL;
686 tstate->interp->dlopenflags = new_val;
687 Py_INCREF(Py_None);
688 return Py_None;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000689}
690
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000691PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000692"setdlopenflags(n) -> None\n\
693\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000694Set the flags used by the interpreter for dlopen calls, such as when the\n\
695interpreter loads extension modules. Among other things, this will enable\n\
696a lazy resolving of symbols when importing a module, if called as\n\
697sys.setdlopenflags(0). To share symbols across extension modules, call as\n\
698sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n\
699can be either found in the ctypes module, or in the DLFCN module. If DLFCN\n\
700is not available, it can be generated from /usr/include/dlfcn.h using the\n\
701h2py script.");
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000702
703static PyObject *
704sys_getdlopenflags(PyObject *self, PyObject *args)
705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyThreadState *tstate = PyThreadState_GET();
707 if (!tstate)
708 return NULL;
709 return PyLong_FromLong(tstate->interp->dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000710}
711
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000712PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000713"getdlopenflags() -> int\n\
714\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000715Return the current value of the flags that are used for dlopen calls.\n\
716The flag constants are defined in the ctypes and DLFCN modules.");
717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718#endif /* HAVE_DLOPEN */
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000719
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000720#ifdef USE_MALLOPT
721/* Link with -lmalloc (or -lmpc) on an SGI */
722#include <malloc.h>
723
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000724static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000725sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 int flag;
728 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
729 return NULL;
730 mallopt(M_DEBUG, flag);
731 Py_INCREF(Py_None);
732 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000733}
734#endif /* USE_MALLOPT */
735
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000736static PyObject *
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000737sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 PyObject *res = NULL;
740 static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
741 static char *kwlist[] = {"object", "default", 0};
742 PyObject *o, *dflt = NULL;
743 PyObject *method;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
746 kwlist, &o, &dflt))
747 return NULL;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 /* Initialize static variable for GC head size */
750 if (gc_head_size == NULL) {
751 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
752 if (gc_head_size == NULL)
753 return NULL;
754 }
Benjamin Petersona5758c02009-05-09 18:15:04 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 /* Make sure the type is initialized. float gets initialized late */
757 if (PyType_Ready(Py_TYPE(o)) < 0)
758 return NULL;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 method = _PyObject_LookupSpecial(o, "__sizeof__",
761 &str__sizeof__);
762 if (method == NULL) {
763 if (!PyErr_Occurred())
764 PyErr_Format(PyExc_TypeError,
765 "Type %.100s doesn't define __sizeof__",
766 Py_TYPE(o)->tp_name);
767 }
768 else {
769 res = PyObject_CallFunctionObjArgs(method, NULL);
770 Py_DECREF(method);
771 }
772
773 /* Has a default value been given */
774 if ((res == NULL) && (dflt != NULL) &&
775 PyErr_ExceptionMatches(PyExc_TypeError))
776 {
777 PyErr_Clear();
778 Py_INCREF(dflt);
779 return dflt;
780 }
781 else if (res == NULL)
782 return res;
783
784 /* add gc_head size */
785 if (PyObject_IS_GC(o)) {
786 PyObject *tmp = res;
787 res = PyNumber_Add(tmp, gc_head_size);
788 Py_DECREF(tmp);
789 }
790 return res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000791}
792
793PyDoc_STRVAR(getsizeof_doc,
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000794"getsizeof(object, default) -> int\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000795\n\
796Return the size of object in bytes.");
797
798static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000799sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 return PyLong_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000802}
803
Tim Peters4be93d02002-07-07 19:59:50 +0000804#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000805static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000806sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 return PyLong_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000809}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000810#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000811
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000812PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000813"getrefcount(object) -> integer\n\
814\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000815Return the reference count of object. The count returned is generally\n\
816one higher than you might expect, because it includes the (temporary)\n\
817reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000818);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000819
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000820#ifdef COUNT_ALLOCS
821static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000822sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 return get_counts();
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000827}
828#endif
829
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000830PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000831"_getframe([depth]) -> frameobject\n\
832\n\
833Return a frame object from the call stack. If optional integer depth is\n\
834given, return the frame object that many calls below the top of the stack.\n\
835If that is deeper than the call stack, ValueError is raised. The default\n\
836for depth is zero, returning the frame at the top of the call stack.\n\
837\n\
838This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000839purposes only."
840);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000841
842static PyObject *
843sys_getframe(PyObject *self, PyObject *args)
844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 PyFrameObject *f = PyThreadState_GET()->frame;
846 int depth = -1;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
849 return NULL;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 while (depth > 0 && f != NULL) {
852 f = f->f_back;
853 --depth;
854 }
855 if (f == NULL) {
856 PyErr_SetString(PyExc_ValueError,
857 "call stack is not deep enough");
858 return NULL;
859 }
860 Py_INCREF(f);
861 return (PyObject*)f;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000862}
863
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000864PyDoc_STRVAR(current_frames_doc,
865"_current_frames() -> dictionary\n\
866\n\
867Return a dictionary mapping each current thread T's thread id to T's\n\
868current stack frame.\n\
869\n\
870This function should be used for specialized purposes only."
871);
872
873static PyObject *
874sys_current_frames(PyObject *self, PyObject *noargs)
875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 return _PyThread_CurrentFrames();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000877}
878
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000879PyDoc_STRVAR(call_tracing_doc,
880"call_tracing(func, args) -> object\n\
881\n\
882Call func(*args), while tracing is enabled. The tracing state is\n\
883saved, and restored afterwards. This is intended to be called from\n\
884a debugger from a checkpoint, to recursively debug some other code."
885);
886
887static PyObject *
888sys_call_tracing(PyObject *self, PyObject *args)
889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 PyObject *func, *funcargs;
891 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
892 return NULL;
893 return _PyEval_CallTracing(func, funcargs);
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000894}
895
Jeremy Hylton985eba52003-02-05 23:13:00 +0000896PyDoc_STRVAR(callstats_doc,
897"callstats() -> tuple of integers\n\
898\n\
899Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
900when Python was built. Otherwise, return None.\n\
901\n\
902When enabled, this function returns detailed, implementation-specific\n\
903details about the number of function calls executed. The return value is\n\
904a 11-tuple where the entries in the tuple are counts of:\n\
9050. all function calls\n\
9061. calls to PyFunction_Type objects\n\
9072. PyFunction calls that do not create an argument tuple\n\
9083. PyFunction calls that do not create an argument tuple\n\
909 and bypass PyEval_EvalCodeEx()\n\
9104. PyMethod calls\n\
9115. PyMethod calls on bound methods\n\
9126. PyType calls\n\
9137. PyCFunction calls\n\
9148. generator calls\n\
9159. All other calls\n\
91610. Number of stack pops performed by call_function()"
917);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000918
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919#ifdef __cplusplus
920extern "C" {
921#endif
922
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000923#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000924/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000925extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000926#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000927
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000928#ifdef DYNAMIC_EXECUTION_PROFILE
929/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000930extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000931#endif
932
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933#ifdef __cplusplus
934}
935#endif
936
Christian Heimes15ebc882008-02-04 18:48:49 +0000937static PyObject *
938sys_clear_type_cache(PyObject* self, PyObject* args)
939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 PyType_ClearCache();
941 Py_RETURN_NONE;
Christian Heimes15ebc882008-02-04 18:48:49 +0000942}
943
944PyDoc_STRVAR(sys_clear_type_cache__doc__,
945"_clear_type_cache() -> None\n\
946Clear the internal type lookup cache.");
947
948
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000949static PyMethodDef sys_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* Might as well keep this in alphabetic order */
951 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
952 callstats_doc},
953 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
954 sys_clear_type_cache__doc__},
955 {"_current_frames", sys_current_frames, METH_NOARGS,
956 current_frames_doc},
957 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
958 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
959 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
960 {"exit", sys_exit, METH_VARARGS, exit_doc},
961 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
962 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000963#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
965 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000966#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000967#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000969#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000970#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000972#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
974 METH_NOARGS, getfilesystemencoding_doc},
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000975#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000977#endif
978#ifdef Py_REF_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000980#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
982 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
983 getrecursionlimit_doc},
984 {"getsizeof", (PyCFunction)sys_getsizeof,
985 METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
986 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000987#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
989 getwindowsversion_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000990#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 {"intern", sys_intern, METH_VARARGS, intern_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000992#ifdef USE_MALLOPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000994#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
996 setcheckinterval_doc},
997 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
998 getcheckinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000999#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS,
1001 setswitchinterval_doc},
1002 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS,
1003 getswitchinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001004#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001005#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
1007 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001008#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
1010 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
1011 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
1012 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001013#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001015#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 {"settrace", sys_settrace, METH_O, settrace_doc},
1017 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
1018 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
1019 {NULL, NULL} /* sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001020};
1021
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001022static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001023list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +00001024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 PyObject *list = PyList_New(0);
1026 int i;
1027 if (list == NULL)
1028 return NULL;
1029 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1030 PyObject *name = PyUnicode_FromString(
1031 PyImport_Inittab[i].name);
1032 if (name == NULL)
1033 break;
1034 PyList_Append(list, name);
1035 Py_DECREF(name);
1036 }
1037 if (PyList_Sort(list) != 0) {
1038 Py_DECREF(list);
1039 list = NULL;
1040 }
1041 if (list) {
1042 PyObject *v = PyList_AsTuple(list);
1043 Py_DECREF(list);
1044 list = v;
1045 }
1046 return list;
Guido van Rossum34679b71993-01-26 13:33:44 +00001047}
1048
Guido van Rossum23fff912000-12-15 22:02:05 +00001049static PyObject *warnoptions = NULL;
1050
1051void
1052PySys_ResetWarnOptions(void)
1053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (warnoptions == NULL || !PyList_Check(warnoptions))
1055 return;
1056 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
Guido van Rossum23fff912000-12-15 22:02:05 +00001057}
1058
1059void
Victor Stinner9ca9c252010-05-19 16:53:30 +00001060PySys_AddWarnOptionUnicode(PyObject *unicode)
Guido van Rossum23fff912000-12-15 22:02:05 +00001061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
1063 Py_XDECREF(warnoptions);
1064 warnoptions = PyList_New(0);
1065 if (warnoptions == NULL)
1066 return;
1067 }
Victor Stinner9ca9c252010-05-19 16:53:30 +00001068 PyList_Append(warnoptions, unicode);
1069}
1070
1071void
1072PySys_AddWarnOption(const wchar_t *s)
1073{
1074 PyObject *unicode;
1075 unicode = PyUnicode_FromWideChar(s, -1);
1076 if (unicode == NULL)
1077 return;
1078 PySys_AddWarnOptionUnicode(unicode);
1079 Py_DECREF(unicode);
Guido van Rossum23fff912000-12-15 22:02:05 +00001080}
1081
Christian Heimes33fe8092008-04-13 13:53:33 +00001082int
1083PySys_HasWarnOptions(void)
1084{
1085 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
1086}
1087
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001088static PyObject *xoptions = NULL;
1089
1090static PyObject *
1091get_xoptions(void)
1092{
1093 if (xoptions == NULL || !PyDict_Check(xoptions)) {
1094 Py_XDECREF(xoptions);
1095 xoptions = PyDict_New();
1096 }
1097 return xoptions;
1098}
1099
1100void
1101PySys_AddXOption(const wchar_t *s)
1102{
1103 PyObject *opts;
1104 PyObject *name = NULL, *value = NULL;
1105 const wchar_t *name_end;
1106 int r;
1107
1108 opts = get_xoptions();
1109 if (opts == NULL)
1110 goto error;
1111
1112 name_end = wcschr(s, L'=');
1113 if (!name_end) {
1114 name = PyUnicode_FromWideChar(s, -1);
1115 value = Py_True;
1116 Py_INCREF(value);
1117 }
1118 else {
1119 name = PyUnicode_FromWideChar(s, name_end - s);
1120 value = PyUnicode_FromWideChar(name_end + 1, -1);
1121 }
1122 if (name == NULL || value == NULL)
1123 goto error;
1124 r = PyDict_SetItem(opts, name, value);
1125 Py_DECREF(name);
1126 Py_DECREF(value);
1127 return;
1128
1129error:
1130 Py_XDECREF(name);
1131 Py_XDECREF(value);
1132 /* No return value, therefore clear error state if possible */
1133 if (_Py_atomic_load_relaxed(&_PyThreadState_Current))
1134 PyErr_Clear();
1135}
1136
1137PyObject *
1138PySys_GetXOptions(void)
1139{
1140 return get_xoptions();
1141}
1142
Guido van Rossum40552d01998-08-06 03:34:39 +00001143/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
1144 Two literals concatenated works just fine. If you have a K&R compiler
1145 or other abomination that however *does* understand longer strings,
1146 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001147PyDoc_VAR(sys_doc) =
1148PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001149"This module provides access to some objects used or maintained by the\n\
1150interpreter and to functions that interact strongly with the interpreter.\n\
1151\n\
1152Dynamic objects:\n\
1153\n\
1154argv -- command line arguments; argv[0] is the script pathname if known\n\
1155path -- module search path; path[0] is the script directory, else ''\n\
1156modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001157\n\
1158displayhook -- called to show results in an interactive session\n\
1159excepthook -- called to handle any uncaught exception other than SystemExit\n\
1160 To customize printing in an interactive session or to install a custom\n\
1161 top-level exception handler, assign other functions to replace these.\n\
1162\n\
Benjamin Peterson06157a42008-07-15 00:28:36 +00001163stdin -- standard input file object; used by input()\n\
Georg Brandl88fc6642007-02-09 21:28:07 +00001164stdout -- standard output file object; used by print()\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001165stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001166 By assigning other file objects (or objects that behave like files)\n\
1167 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001168\n\
1169last_type -- type of last uncaught exception\n\
1170last_value -- value of last uncaught exception\n\
1171last_traceback -- traceback of last uncaught exception\n\
1172 These three are only available in an interactive session after a\n\
1173 traceback has been printed.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001174"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001175)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001176/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001177PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001178"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001179Static objects:\n\
1180\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001181float_info -- a dict with information about the float implementation.\n\
Mark Dickinsonbd792642009-03-18 20:06:12 +00001182int_info -- a struct sequence with information about the int implementation.\n\
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001183maxsize -- the largest supported length of containers.\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +00001184maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001185builtin_module_names -- tuple of module names built into this interpreter\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001186subversion -- subversion information of the build as tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001187version -- the version of this interpreter as a string\n\
Eric Smith0e5b5622009-02-06 01:32:42 +00001188version_info -- version information as a named tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001189hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001190copyright -- copyright notice pertaining to this interpreter\n\
1191platform -- platform identifier\n\
1192executable -- pathname of this Python interpreter\n\
1193prefix -- prefix used to find the Python library\n\
1194exec_prefix -- prefix used to find the machine-specific Python library\n\
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001195float_repr_style -- string indicating the style of repr() output for floats\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001196"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001197)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001198#ifdef MS_WINDOWS
1199/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001200PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001201"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001202winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001203"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001204)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001205#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001206PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001207"__stdin__ -- the original stdin; don't touch!\n\
1208__stdout__ -- the original stdout; don't touch!\n\
1209__stderr__ -- the original stderr; don't touch!\n\
1210__displayhook__ -- the original displayhook; don't touch!\n\
1211__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001212\n\
1213Functions:\n\
1214\n\
Georg Brandl1a3284e2007-12-02 09:40:06 +00001215displayhook() -- print an object to the screen, and save it in builtins._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001216excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001217exc_info() -- return thread-safe information about the current exception\n\
1218exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001219getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001220getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001221getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001222getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001223getsizeof() -- return the size of an object in bytes\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001224gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001225setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001226setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001227setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001228setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001229settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001230"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001231)
Fred Drakeccede592000-08-14 20:59:57 +00001232/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001233
Martin v. Löwis43b57802006-01-05 23:38:54 +00001234/* Subversion branch and revision management */
1235static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
1236static const char headurl[] = "$HeadURL$";
1237static int svn_initialized;
1238static char patchlevel_revision[50]; /* Just the number */
1239static char branch[50];
1240static char shortbranch[50];
1241static const char *svn_revision;
1242
Tim Peterse86e7a52006-01-06 02:42:46 +00001243static void
1244svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +00001245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 const char *python, *br_start, *br_end, *br_end2, *svnversion;
1247 Py_ssize_t len;
1248 int istag = 0;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (svn_initialized)
1251 return;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 python = strstr(headurl, "/python/");
1254 if (!python) {
1255 strcpy(branch, "unknown branch");
1256 strcpy(shortbranch, "unknown");
1257 }
1258 else {
1259 br_start = python + 8;
1260 br_end = strchr(br_start, '/');
1261 assert(br_end);
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 /* Works even for trunk,
1264 as we are in trunk/Python/sysmodule.c */
1265 br_end2 = strchr(br_end+1, '/');
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 istag = strncmp(br_start, "tags", 4) == 0;
1268 if (strncmp(br_start, "trunk", 5) == 0) {
1269 strcpy(branch, "trunk");
1270 strcpy(shortbranch, "trunk");
1271 }
1272 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1273 len = br_end2 - br_start;
1274 strncpy(branch, br_start, len);
1275 branch[len] = '\0';
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 len = br_end2 - (br_end + 1);
1278 strncpy(shortbranch, br_end + 1, len);
1279 shortbranch[len] = '\0';
1280 }
1281 else {
1282 Py_FatalError("bad HeadURL");
1283 return;
1284 }
1285 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001286
1287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 svnversion = _Py_svnversion();
1289 if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0)
1290 svn_revision = svnversion;
1291 else if (istag) {
1292 len = strlen(_patchlevel_revision);
1293 assert(len >= 13);
1294 assert(len < (sizeof(patchlevel_revision) + 13));
1295 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1296 len - 13);
1297 patchlevel_revision[len - 13] = '\0';
1298 svn_revision = patchlevel_revision;
1299 }
1300 else
1301 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 svn_initialized = 1;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001304}
1305
1306/* Return svnversion output if available.
1307 Else return Revision of patchlevel.h if on branch.
1308 Else return empty string */
1309const char*
1310Py_SubversionRevision()
1311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 svnversion_init();
1313 return svn_revision;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001314}
1315
1316const char*
1317Py_SubversionShortBranch()
1318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 svnversion_init();
1320 return shortbranch;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001321}
1322
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001323
1324PyDoc_STRVAR(flags__doc__,
1325"sys.flags\n\
1326\n\
1327Flags provided through command line arguments or environment vars.");
1328
1329static PyTypeObject FlagsType;
1330
1331static PyStructSequence_Field flags_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 {"debug", "-d"},
1333 {"division_warning", "-Q"},
1334 {"inspect", "-i"},
1335 {"interactive", "-i"},
1336 {"optimize", "-O or -OO"},
1337 {"dont_write_bytecode", "-B"},
1338 {"no_user_site", "-s"},
1339 {"no_site", "-S"},
1340 {"ignore_environment", "-E"},
1341 {"verbose", "-v"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001342#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 {"riscos_wimp", "???"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 /* {"unbuffered", "-u"}, */
1346 /* {"skip_first", "-x"}, */
1347 {"bytes_warning", "-b"},
1348 {0}
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001349};
1350
1351static PyStructSequence_Desc flags_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 "sys.flags", /* name */
1353 flags__doc__, /* doc */
1354 flags_fields, /* fields */
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001355#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 12
Georg Brandle1b5ac62008-06-04 13:06:58 +00001357#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 11
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001359#endif
1360};
1361
1362static PyObject*
1363make_flags(void)
1364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 int pos = 0;
1366 PyObject *seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 seq = PyStructSequence_New(&FlagsType);
1369 if (seq == NULL)
1370 return NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001371
1372#define SetFlag(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 SetFlag(Py_DebugFlag);
1376 SetFlag(Py_DivisionWarningFlag);
1377 SetFlag(Py_InspectFlag);
1378 SetFlag(Py_InteractiveFlag);
1379 SetFlag(Py_OptimizeFlag);
1380 SetFlag(Py_DontWriteBytecodeFlag);
1381 SetFlag(Py_NoUserSiteDirectory);
1382 SetFlag(Py_NoSiteFlag);
1383 SetFlag(Py_IgnoreEnvironmentFlag);
1384 SetFlag(Py_VerboseFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001385#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 SetFlag(Py_RISCOSWimpFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001387#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* SetFlag(saw_unbuffered_flag); */
1389 /* SetFlag(skipfirstline); */
Christian Heimes33fe8092008-04-13 13:53:33 +00001390 SetFlag(Py_BytesWarningFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001391#undef SetFlag
1392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (PyErr_Occurred()) {
1394 return NULL;
1395 }
1396 return seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001397}
1398
Eric Smith0e5b5622009-02-06 01:32:42 +00001399PyDoc_STRVAR(version_info__doc__,
1400"sys.version_info\n\
1401\n\
1402Version information as a named tuple.");
1403
1404static PyTypeObject VersionInfoType;
1405
1406static PyStructSequence_Field version_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 {"major", "Major release number"},
1408 {"minor", "Minor release number"},
1409 {"micro", "Patch release number"},
1410 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1411 {"serial", "Serial release number"},
1412 {0}
Eric Smith0e5b5622009-02-06 01:32:42 +00001413};
1414
1415static PyStructSequence_Desc version_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 "sys.version_info", /* name */
1417 version_info__doc__, /* doc */
1418 version_info_fields, /* fields */
1419 5
Eric Smith0e5b5622009-02-06 01:32:42 +00001420};
1421
1422static PyObject *
1423make_version_info(void)
1424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyObject *version_info;
1426 char *s;
1427 int pos = 0;
Eric Smith0e5b5622009-02-06 01:32:42 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 version_info = PyStructSequence_New(&VersionInfoType);
1430 if (version_info == NULL) {
1431 return NULL;
1432 }
Eric Smith0e5b5622009-02-06 01:32:42 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 /*
1435 * These release level checks are mutually exclusive and cover
1436 * the field, so don't get too fancy with the pre-processor!
1437 */
Eric Smith0e5b5622009-02-06 01:32:42 +00001438#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 s = "alpha";
Eric Smith0e5b5622009-02-06 01:32:42 +00001440#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 s = "beta";
Eric Smith0e5b5622009-02-06 01:32:42 +00001442#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 s = "candidate";
Eric Smith0e5b5622009-02-06 01:32:42 +00001444#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 s = "final";
Eric Smith0e5b5622009-02-06 01:32:42 +00001446#endif
1447
1448#define SetIntItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001450#define SetStrItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 SetIntItem(PY_MAJOR_VERSION);
1454 SetIntItem(PY_MINOR_VERSION);
1455 SetIntItem(PY_MICRO_VERSION);
1456 SetStrItem(s);
1457 SetIntItem(PY_RELEASE_SERIAL);
Eric Smith0e5b5622009-02-06 01:32:42 +00001458#undef SetIntItem
1459#undef SetStrItem
1460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 if (PyErr_Occurred()) {
1462 Py_CLEAR(version_info);
1463 return NULL;
1464 }
1465 return version_info;
Eric Smith0e5b5622009-02-06 01:32:42 +00001466}
1467
Martin v. Löwis1a214512008-06-11 05:26:20 +00001468static struct PyModuleDef sysmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 PyModuleDef_HEAD_INIT,
1470 "sys",
1471 sys_doc,
1472 -1, /* multiple "initialization" just copies the module dict. */
1473 sys_methods,
1474 NULL,
1475 NULL,
1476 NULL,
1477 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001478};
1479
Guido van Rossum25ce5661997-08-02 03:10:38 +00001480PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001481_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 PyObject *m, *v, *sysdict;
1484 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 m = PyModule_Create(&sysmodule);
1487 if (m == NULL)
1488 return NULL;
1489 sysdict = PyModule_GetDict(m);
1490#define SET_SYS_FROM_STRING(key, value) \
1491 v = value; \
1492 if (v != NULL) \
1493 PyDict_SetItemString(sysdict, key, v); \
1494 Py_XDECREF(v)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 /* Check that stdin is not a directory
1497 Using shell redirection, you can redirect stdin to a directory,
1498 crashing the Python interpreter. Catch this common mistake here
1499 and output a useful error message. Note that under MS Windows,
1500 the shell already prevents that. */
Martin v. Löwisec59d042009-01-12 07:59:10 +00001501#if !defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 {
1503 struct stat sb;
1504 if (fstat(fileno(stdin), &sb) == 0 &&
1505 S_ISDIR(sb.st_mode)) {
1506 /* There's nothing more we can do. */
1507 /* Py_FatalError() will core dump, so just exit. */
1508 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1509 exit(EXIT_FAILURE);
1510 }
1511 }
Martin v. Löwisec59d042009-01-12 07:59:10 +00001512#endif
Neal Norwitz11bd1192005-10-03 00:54:56 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 /* stdin/stdout/stderr are now set by pythonrun.c */
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 PyDict_SetItemString(sysdict, "__displayhook__",
1517 PyDict_GetItemString(sysdict, "displayhook"));
1518 PyDict_SetItemString(sysdict, "__excepthook__",
1519 PyDict_GetItemString(sysdict, "excepthook"));
1520 SET_SYS_FROM_STRING("version",
1521 PyUnicode_FromString(Py_GetVersion()));
1522 SET_SYS_FROM_STRING("hexversion",
1523 PyLong_FromLong(PY_VERSION_HEX));
1524 svnversion_init();
1525 SET_SYS_FROM_STRING("subversion",
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001526 Py_BuildValue("(sss)", "CPython", branch,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 svn_revision));
1528 SET_SYS_FROM_STRING("dont_write_bytecode",
1529 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1530 SET_SYS_FROM_STRING("api_version",
1531 PyLong_FromLong(PYTHON_API_VERSION));
1532 SET_SYS_FROM_STRING("copyright",
1533 PyUnicode_FromString(Py_GetCopyright()));
1534 SET_SYS_FROM_STRING("platform",
1535 PyUnicode_FromString(Py_GetPlatform()));
1536 SET_SYS_FROM_STRING("executable",
1537 PyUnicode_FromWideChar(
1538 Py_GetProgramFullPath(), -1));
1539 SET_SYS_FROM_STRING("prefix",
1540 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1541 SET_SYS_FROM_STRING("exec_prefix",
1542 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
1543 SET_SYS_FROM_STRING("maxsize",
1544 PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1545 SET_SYS_FROM_STRING("float_info",
1546 PyFloat_GetInfo());
1547 SET_SYS_FROM_STRING("int_info",
1548 PyLong_GetInfo());
Mark Dickinsondc787d22010-05-23 13:33:13 +00001549 /* initialize hash_info */
1550 if (Hash_InfoType.tp_name == 0)
1551 PyStructSequence_InitType(&Hash_InfoType, &hash_info_desc);
1552 SET_SYS_FROM_STRING("hash_info",
1553 get_hash_info());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 SET_SYS_FROM_STRING("maxunicode",
1555 PyLong_FromLong(PyUnicode_GetMax()));
1556 SET_SYS_FROM_STRING("builtin_module_names",
1557 list_builtin_module_names());
1558 {
1559 /* Assumes that longs are at least 2 bytes long.
1560 Should be safe! */
1561 unsigned long number = 1;
1562 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 s = (char *) &number;
1565 if (s[0] == 0)
1566 value = "big";
1567 else
1568 value = "little";
1569 SET_SYS_FROM_STRING("byteorder",
1570 PyUnicode_FromString(value));
1571 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001572#ifdef MS_COREDLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 SET_SYS_FROM_STRING("dllhandle",
1574 PyLong_FromVoidPtr(PyWin_DLLhModule));
1575 SET_SYS_FROM_STRING("winver",
1576 PyUnicode_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001577#endif
Barry Warsaw8cf4eae2010-10-16 01:04:07 +00001578#ifdef ABIFLAGS
1579 SET_SYS_FROM_STRING("abiflags",
1580 PyUnicode_FromString(ABIFLAGS));
1581#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (warnoptions == NULL) {
1583 warnoptions = PyList_New(0);
1584 }
1585 else {
1586 Py_INCREF(warnoptions);
1587 }
1588 if (warnoptions != NULL) {
1589 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1590 }
Tim Peters216b78b2006-01-06 02:40:53 +00001591
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001592 v = get_xoptions();
1593 if (v != NULL) {
1594 PyDict_SetItemString(sysdict, "_xoptions", v);
1595 }
1596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 /* version_info */
1598 if (VersionInfoType.tp_name == 0)
1599 PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
1600 SET_SYS_FROM_STRING("version_info", make_version_info());
1601 /* prevent user from creating new instances */
1602 VersionInfoType.tp_init = NULL;
1603 VersionInfoType.tp_new = NULL;
Eric Smith0e5b5622009-02-06 01:32:42 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 /* flags */
1606 if (FlagsType.tp_name == 0)
1607 PyStructSequence_InitType(&FlagsType, &flags_desc);
1608 SET_SYS_FROM_STRING("flags", make_flags());
1609 /* prevent user from creating new instances */
1610 FlagsType.tp_init = NULL;
1611 FlagsType.tp_new = NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001612
Eric Smithf7bb5782010-01-27 00:44:57 +00001613
1614#if defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 /* getwindowsversion */
1616 if (WindowsVersionType.tp_name == 0)
1617 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc);
1618 /* prevent user from creating new instances */
1619 WindowsVersionType.tp_init = NULL;
1620 WindowsVersionType.tp_new = NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +00001621#endif
1622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001624#ifndef PY_NO_SHORT_FLOAT_REPR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 SET_SYS_FROM_STRING("float_repr_style",
1626 PyUnicode_FromString("short"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001627#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 SET_SYS_FROM_STRING("float_repr_style",
1629 PyUnicode_FromString("legacy"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001630#endif
1631
Christian Heimes7b3ce6a2008-01-31 14:31:45 +00001632#undef SET_SYS_FROM_STRING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (PyErr_Occurred())
1634 return NULL;
1635 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001636}
1637
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001638static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001639makepathobject(const wchar_t *path, wchar_t delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 int i, n;
1642 const wchar_t *p;
1643 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 n = 1;
1646 p = path;
1647 while ((p = wcschr(p, delim)) != NULL) {
1648 n++;
1649 p++;
1650 }
1651 v = PyList_New(n);
1652 if (v == NULL)
1653 return NULL;
1654 for (i = 0; ; i++) {
1655 p = wcschr(path, delim);
1656 if (p == NULL)
1657 p = path + wcslen(path); /* End of string */
1658 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
1659 if (w == NULL) {
1660 Py_DECREF(v);
1661 return NULL;
1662 }
1663 PyList_SetItem(v, i, w);
1664 if (*p == '\0')
1665 break;
1666 path = p+1;
1667 }
1668 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001669}
1670
1671void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001672PySys_SetPath(const wchar_t *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PyObject *v;
1675 if ((v = makepathobject(path, DELIM)) == NULL)
1676 Py_FatalError("can't create sys.path");
1677 if (PySys_SetObject("path", v) != 0)
1678 Py_FatalError("can't assign sys.path");
1679 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001680}
1681
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001682static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001683makeargvobject(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 PyObject *av;
1686 if (argc <= 0 || argv == NULL) {
1687 /* Ensure at least one (empty) argument is seen */
1688 static wchar_t *empty_argv[1] = {L""};
1689 argv = empty_argv;
1690 argc = 1;
1691 }
1692 av = PyList_New(argc);
1693 if (av != NULL) {
1694 int i;
1695 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001696#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 PyObject *v;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 /* argv[0] is the script pathname if known */
1700 if (i == 0) {
1701 char* fn = decc$translate_vms(argv[0]);
1702 if ((fn == (char *)0) || fn == (char *)-1)
1703 v = PyUnicode_FromString(argv[0]);
1704 else
1705 v = PyUnicode_FromString(
1706 decc$translate_vms(argv[0]));
1707 } else
1708 v = PyUnicode_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001709#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001711#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 if (v == NULL) {
1713 Py_DECREF(av);
1714 av = NULL;
1715 break;
1716 }
1717 PyList_SetItem(av, i, v);
1718 }
1719 }
1720 return av;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001721}
1722
Nick Coghland26c18a2010-08-17 13:06:11 +00001723#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
1724 (argc > 0 && argv0 != NULL && \
1725 wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001726
1727static void
1728sys_update_path(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001729{
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001730 wchar_t *argv0;
1731 wchar_t *p = NULL;
1732 Py_ssize_t n = 0;
1733 PyObject *a;
1734 PyObject *path;
1735#ifdef HAVE_READLINK
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001736 wchar_t link[MAXPATHLEN+1];
1737 wchar_t argv0copy[2*MAXPATHLEN+1];
1738 int nr = 0;
1739#endif
Guido van Rossum162e38c2003-02-19 15:25:10 +00001740#if defined(HAVE_REALPATH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 wchar_t fullpath[MAXPATHLEN];
Martin v. Löwisec59d042009-01-12 07:59:10 +00001742#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 wchar_t fullpath[MAX_PATH];
Thomas Heller27bb71e2003-01-08 14:33:48 +00001744#endif
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001745
1746 path = PySys_GetObject("path");
1747 if (path == NULL)
1748 return;
1749
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001750 argv0 = argv[0];
1751
1752#ifdef HAVE_READLINK
1753 if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
1754 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
1755 if (nr > 0) {
1756 /* It's a symlink */
1757 link[nr] = '\0';
1758 if (link[0] == SEP)
1759 argv0 = link; /* Link to absolute path */
1760 else if (wcschr(link, SEP) == NULL)
1761 ; /* Link without path */
1762 else {
1763 /* Must join(dirname(argv0), link) */
1764 wchar_t *q = wcsrchr(argv0, SEP);
1765 if (q == NULL)
1766 argv0 = link; /* argv0 without path */
1767 else {
1768 /* Must make a copy */
1769 wcscpy(argv0copy, argv0);
1770 q = wcsrchr(argv0copy, SEP);
1771 wcscpy(q+1, link);
1772 argv0 = argv0copy;
1773 }
1774 }
1775 }
1776#endif /* HAVE_READLINK */
1777#if SEP == '\\' /* Special case for MS filename syntax */
1778 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1779 wchar_t *q;
1780#if defined(MS_WINDOWS) && !defined(MS_WINCE)
1781 /* This code here replaces the first element in argv with the full
1782 path that it represents. Under CE, there are no relative paths so
1783 the argument must be the full path anyway. */
1784 wchar_t *ptemp;
1785 if (GetFullPathNameW(argv0,
1786 sizeof(fullpath)/sizeof(fullpath[0]),
1787 fullpath,
1788 &ptemp)) {
1789 argv0 = fullpath;
1790 }
1791#endif
1792 p = wcsrchr(argv0, SEP);
1793 /* Test for alternate separator */
1794 q = wcsrchr(p ? p : argv0, '/');
1795 if (q != NULL)
1796 p = q;
1797 if (p != NULL) {
1798 n = p + 1 - argv0;
1799 if (n > 1 && p[-1] != ':')
1800 n--; /* Drop trailing separator */
1801 }
1802 }
1803#else /* All other filename syntaxes */
1804 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1805#if defined(HAVE_REALPATH)
Victor Stinner015f4d82010-10-07 22:29:53 +00001806 if (_Py_wrealpath(argv0, fullpath, PATH_MAX)) {
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001807 argv0 = fullpath;
1808 }
1809#endif
1810 p = wcsrchr(argv0, SEP);
1811 }
1812 if (p != NULL) {
1813 n = p + 1 - argv0;
1814#if SEP == '/' /* Special case for Unix filename syntax */
1815 if (n > 1)
1816 n--; /* Drop trailing separator */
1817#endif /* Unix */
1818 }
1819#endif /* All others */
1820 a = PyUnicode_FromWideChar(argv0, n);
1821 if (a == NULL)
1822 Py_FatalError("no mem for sys.path insertion");
1823 if (PyList_Insert(path, 0, a) < 0)
1824 Py_FatalError("sys.path.insert(0) failed");
1825 Py_DECREF(a);
1826}
1827
1828void
1829PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
1830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 PyObject *av = makeargvobject(argc, argv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 if (av == NULL)
1833 Py_FatalError("no mem for sys.argv");
1834 if (PySys_SetObject("argv", av) != 0)
1835 Py_FatalError("can't assign sys.argv");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 Py_DECREF(av);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001837 if (updatepath)
1838 sys_update_path(argc, argv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001839}
Guido van Rossuma890e681998-05-12 14:59:24 +00001840
Antoine Pitrouf978fac2010-05-21 17:25:34 +00001841void
1842PySys_SetArgv(int argc, wchar_t **argv)
1843{
1844 PySys_SetArgvEx(argc, argv, 1);
1845}
1846
Victor Stinner14284c22010-04-23 12:02:30 +00001847/* Reimplementation of PyFile_WriteString() no calling indirectly
1848 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
1849
1850static int
Victor Stinner79766632010-08-16 17:36:42 +00001851sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
Victor Stinner14284c22010-04-23 12:02:30 +00001852{
Victor Stinner79766632010-08-16 17:36:42 +00001853 PyObject *writer = NULL, *args = NULL, *result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 int err;
Victor Stinner14284c22010-04-23 12:02:30 +00001855
Victor Stinnerecccc4f2010-06-08 20:46:00 +00001856 if (file == NULL)
1857 return -1;
1858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 writer = PyObject_GetAttrString(file, "write");
1860 if (writer == NULL)
1861 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 args = PyTuple_Pack(1, unicode);
1864 if (args == NULL)
1865 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 result = PyEval_CallObject(writer, args);
1868 if (result == NULL) {
1869 goto error;
1870 } else {
1871 err = 0;
1872 goto finally;
1873 }
Victor Stinner14284c22010-04-23 12:02:30 +00001874
1875error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 err = -1;
Victor Stinner14284c22010-04-23 12:02:30 +00001877finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 Py_XDECREF(writer);
1879 Py_XDECREF(args);
1880 Py_XDECREF(result);
1881 return err;
Victor Stinner14284c22010-04-23 12:02:30 +00001882}
1883
Victor Stinner79766632010-08-16 17:36:42 +00001884static int
1885sys_pyfile_write(const char *text, PyObject *file)
1886{
1887 PyObject *unicode = NULL;
1888 int err;
1889
1890 if (file == NULL)
1891 return -1;
1892
1893 unicode = PyUnicode_FromString(text);
1894 if (unicode == NULL)
1895 return -1;
1896
1897 err = sys_pyfile_write_unicode(unicode, file);
1898 Py_DECREF(unicode);
1899 return err;
1900}
Guido van Rossuma890e681998-05-12 14:59:24 +00001901
1902/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1903 Adapted from code submitted by Just van Rossum.
1904
1905 PySys_WriteStdout(format, ...)
1906 PySys_WriteStderr(format, ...)
1907
1908 The first function writes to sys.stdout; the second to sys.stderr. When
1909 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001910 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001911
Victor Stinner14284c22010-04-23 12:02:30 +00001912 PyErr_CheckSignals() is not called to avoid the execution of the Python
Victor Stinner79766632010-08-16 17:36:42 +00001913 signal handlers: they may raise a new exception whereas sys_write()
1914 ignores all exceptions.
Victor Stinner14284c22010-04-23 12:02:30 +00001915
Guido van Rossuma890e681998-05-12 14:59:24 +00001916 Both take a printf-style format string as their first argument followed
1917 by a variable length argument list determined by the format string.
1918
1919 *** WARNING ***
1920
1921 The format should limit the total size of the formatted output string to
1922 1000 bytes. In particular, this means that no unrestricted "%s" formats
1923 should occur; these should be limited using "%.<N>s where <N> is a
1924 decimal number calculated so that <N> plus the maximum size of other
1925 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1926 which can print hundreds of digits for very large numbers.
1927
1928 */
1929
1930static void
Victor Stinner79766632010-08-16 17:36:42 +00001931sys_write(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 PyObject *file;
1934 PyObject *error_type, *error_value, *error_traceback;
1935 char buffer[1001];
1936 int written;
Guido van Rossuma890e681998-05-12 14:59:24 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1939 file = PySys_GetObject(name);
1940 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
1941 if (sys_pyfile_write(buffer, file) != 0) {
1942 PyErr_Clear();
1943 fputs(buffer, fp);
1944 }
1945 if (written < 0 || (size_t)written >= sizeof(buffer)) {
1946 const char *truncated = "... truncated";
Victor Stinner79766632010-08-16 17:36:42 +00001947 if (sys_pyfile_write(truncated, file) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 fputs(truncated, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 }
1950 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001951}
1952
1953void
Guido van Rossuma890e681998-05-12 14:59:24 +00001954PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001959 sys_write("stdout", stdout, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001961}
1962
1963void
Guido van Rossuma890e681998-05-12 14:59:24 +00001964PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001969 sys_write("stderr", stderr, format, va);
1970 va_end(va);
1971}
1972
1973static void
1974sys_format(char *name, FILE *fp, const char *format, va_list va)
1975{
1976 PyObject *file, *message;
1977 PyObject *error_type, *error_value, *error_traceback;
1978 char *utf8;
1979
1980 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1981 file = PySys_GetObject(name);
1982 message = PyUnicode_FromFormatV(format, va);
1983 if (message != NULL) {
1984 if (sys_pyfile_write_unicode(message, file) != 0) {
1985 PyErr_Clear();
1986 utf8 = _PyUnicode_AsString(message);
1987 if (utf8 != NULL)
1988 fputs(utf8, fp);
1989 }
1990 Py_DECREF(message);
1991 }
1992 PyErr_Restore(error_type, error_value, error_traceback);
1993}
1994
1995void
1996PySys_FormatStdout(const char *format, ...)
1997{
1998 va_list va;
1999
2000 va_start(va, format);
2001 sys_format("stdout", stdout, format, va);
2002 va_end(va);
2003}
2004
2005void
2006PySys_FormatStderr(const char *format, ...)
2007{
2008 va_list va;
2009
2010 va_start(va, format);
2011 sys_format("stderr", stderr, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00002013}