blob: 97809d27b0b2d33bb142733f6e9f219ad3601dfd [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* System module */
3
4/*
5Various bits of information used by the interpreter are collected in
6module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +00007Function member:
Guido van Rossumcc8914f1995-03-20 15:09:40 +00008- exit(sts): raise SystemExit
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009Data members:
10- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000012- path: module search path (list of strings)
13- argv: script arguments (list of strings)
14- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015*/
16
Guido van Rossum65bf9f21997-04-29 18:33:38 +000017#include "Python.h"
Christian Heimesd32ed6f2008-01-14 18:49:24 +000018#include "structseq.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019#include "code.h"
Barry Warsawb6a54d22000-12-06 21:47:46 +000020#include "frameobject.h"
Guido van Rossuma12fe4e2003-04-09 19:06:21 +000021#include "eval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Guido van Rossume2437a11992-03-23 18:20:18 +000023#include "osdefs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000024
Mark Hammond8696ebc2002-10-08 02:44:31 +000025#ifdef MS_WINDOWS
26#define WIN32_LEAN_AND_MEAN
Amaury Forgeot d'Arc06cfe952007-11-10 13:55:44 +000027#include <windows.h>
Mark Hammond8696ebc2002-10-08 02:44:31 +000028#endif /* MS_WINDOWS */
29
Guido van Rossum9b38a141996-09-11 23:12:24 +000030#ifdef MS_COREDLL
Guido van Rossumc606fe11996-04-09 02:37:57 +000031extern void *PyWin_DLLhModule;
Guido van Rossum6c1e5f21997-09-29 23:34:23 +000032/* A string loaded from the DLL at startup: */
33extern const char *PyWin_DLLVersionString;
Guido van Rossumc606fe11996-04-09 02:37:57 +000034#endif
35
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000036#ifdef __VMS
37#include <unixlib.h>
38#endif
39
Martin v. Löwis5467d4c2003-05-10 07:10:12 +000040#ifdef HAVE_LANGINFO_H
41#include <locale.h>
42#include <langinfo.h>
43#endif
44
Guido van Rossum65bf9f21997-04-29 18:33:38 +000045PyObject *
Neal Norwitzf3081322007-08-25 00:32:45 +000046PySys_GetObject(const char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 PyThreadState *tstate = PyThreadState_GET();
49 PyObject *sd = tstate->interp->sysdict;
50 if (sd == NULL)
51 return NULL;
52 return PyDict_GetItemString(sd, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053}
54
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055int
Neal Norwitzf3081322007-08-25 00:32:45 +000056PySys_SetObject(const char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 PyThreadState *tstate = PyThreadState_GET();
59 PyObject *sd = tstate->interp->sysdict;
60 if (v == NULL) {
61 if (PyDict_GetItemString(sd, name) == NULL)
62 return 0;
63 else
64 return PyDict_DelItemString(sd, name);
65 }
66 else
67 return PyDict_SetItemString(sd, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossum65bf9f21997-04-29 18:33:38 +000070static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000071sys_displayhook(PyObject *self, PyObject *o)
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 PyObject *outf;
74 PyInterpreterState *interp = PyThreadState_GET()->interp;
75 PyObject *modules = interp->modules;
76 PyObject *builtins = PyDict_GetItemString(modules, "builtins");
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (builtins == NULL) {
79 PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
80 return NULL;
81 }
Moshe Zadka03897ea2001-07-23 13:32:43 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 /* Print value except if None */
84 /* After printing, also assign to '_' */
85 /* Before, set '_' to None to avoid recursion */
86 if (o == Py_None) {
87 Py_INCREF(Py_None);
88 return Py_None;
89 }
90 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
91 return NULL;
92 outf = PySys_GetObject("stdout");
93 if (outf == NULL || outf == Py_None) {
94 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
95 return NULL;
96 }
97 if (PyFile_WriteObject(o, outf, 0) != 0)
98 return NULL;
99 if (PyFile_WriteString("\n", outf) != 0)
100 return NULL;
101 if (PyObject_SetAttrString(builtins, "_", o) != 0)
102 return NULL;
103 Py_INCREF(Py_None);
104 return Py_None;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000105}
106
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000107PyDoc_STRVAR(displayhook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000108"displayhook(object) -> None\n"
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000109"\n"
Florent Xicluna5749e852010-03-03 11:54:54 +0000110"Print an object to sys.stdout and also save it in builtins._\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000111);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000112
113static PyObject *
114sys_excepthook(PyObject* self, PyObject* args)
115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *exc, *value, *tb;
117 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
118 return NULL;
119 PyErr_Display(exc, value, tb);
120 Py_INCREF(Py_None);
121 return Py_None;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000122}
123
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000124PyDoc_STRVAR(excepthook_doc,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000125"excepthook(exctype, value, traceback) -> None\n"
126"\n"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000127"Handle an exception by displaying it with a traceback on sys.stderr.\n"
128);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +0000129
130static PyObject *
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000131sys_exc_info(PyObject *self, PyObject *noargs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyThreadState *tstate;
134 tstate = PyThreadState_GET();
135 return Py_BuildValue(
136 "(OOO)",
137 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
138 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
139 tstate->exc_traceback != NULL ?
140 tstate->exc_traceback : Py_None);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000141}
142
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000143PyDoc_STRVAR(exc_info_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000144"exc_info() -> (type, value, traceback)\n\
145\n\
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000146Return information about the most recent exception caught by an except\n\
147clause in the current stack frame or in an older stack frame."
148);
149
150static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000151sys_exit(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 PyObject *exit_code = 0;
154 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
155 return NULL;
156 /* Raise SystemExit so callers may catch it or clean up. */
157 PyErr_SetObject(PyExc_SystemExit, exit_code);
158 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159}
160
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000161PyDoc_STRVAR(exit_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000162"exit([status])\n\
163\n\
164Exit the interpreter by raising SystemExit(status).\n\
165If the status is omitted or None, it defaults to zero (i.e., success).\n\
Neil Schemenauer0f2103f2002-03-23 20:46:35 +0000166If the status is numeric, it will be used as the system exit status.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000167If it is another kind of object, it will be printed and the system\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000168exit status will be one (i.e., failure)."
169);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000170
Martin v. Löwis107b7da2001-11-09 20:59:39 +0000171
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000172static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000173sys_getdefaultencoding(PyObject *self)
Fred Drake8b4d01d2000-05-09 19:57:01 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
Fred Drake8b4d01d2000-05-09 19:57:01 +0000176}
177
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000178PyDoc_STRVAR(getdefaultencoding_doc,
Marc-André Lemburg99964b82000-06-07 09:13:41 +0000179"getdefaultencoding() -> string\n\
Fred Drake8b4d01d2000-05-09 19:57:01 +0000180\n\
181Return the current default string encoding used by the Unicode \n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000182implementation."
183);
Fred Drake8b4d01d2000-05-09 19:57:01 +0000184
185static PyObject *
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000186sys_getfilesystemencoding(PyObject *self)
187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (Py_FileSystemDefaultEncoding)
189 return PyUnicode_FromString(Py_FileSystemDefaultEncoding);
190 Py_INCREF(Py_None);
191 return Py_None;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000192}
193
194PyDoc_STRVAR(getfilesystemencoding_doc,
195"getfilesystemencoding() -> string\n\
196\n\
197Return the encoding used to convert Unicode filenames in\n\
198operating system filenames."
199);
200
Martin v. Löwis04dc25c2008-10-03 16:09:28 +0000201static PyObject *
Georg Brandl66a796e2006-12-19 20:50:34 +0000202sys_intern(PyObject *self, PyObject *args)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyObject *s;
205 if (!PyArg_ParseTuple(args, "U:intern", &s))
206 return NULL;
207 if (PyUnicode_CheckExact(s)) {
208 Py_INCREF(s);
209 PyUnicode_InternInPlace(&s);
210 return s;
211 }
212 else {
213 PyErr_Format(PyExc_TypeError,
214 "can't intern %.400s", s->ob_type->tp_name);
215 return NULL;
216 }
Georg Brandl66a796e2006-12-19 20:50:34 +0000217}
218
219PyDoc_STRVAR(intern_doc,
220"intern(string) -> string\n\
221\n\
222``Intern'' the given string. This enters the string in the (global)\n\
223table of interned strings whose purpose is to speed up dictionary lookups.\n\
224Return the string itself or the previously interned string object with the\n\
225same value.");
226
227
Fred Drake5755ce62001-06-27 19:19:46 +0000228/*
229 * Cached interned string objects used for calling the profile and
230 * trace functions. Initialized by trace_init().
231 */
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000232static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Fred Drake5755ce62001-06-27 19:19:46 +0000233
234static int
235trace_init(void)
236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 static char *whatnames[7] = {"call", "exception", "line", "return",
238 "c_call", "c_exception", "c_return"};
239 PyObject *name;
240 int i;
241 for (i = 0; i < 7; ++i) {
242 if (whatstrings[i] == NULL) {
243 name = PyUnicode_InternFromString(whatnames[i]);
244 if (name == NULL)
245 return -1;
246 whatstrings[i] = name;
247 }
248 }
249 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000250}
251
252
253static PyObject *
254call_trampoline(PyThreadState *tstate, PyObject* callback,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 PyFrameObject *frame, int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 PyObject *args = PyTuple_New(3);
258 PyObject *whatstr;
259 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (args == NULL)
262 return NULL;
263 Py_INCREF(frame);
264 whatstr = whatstrings[what];
265 Py_INCREF(whatstr);
266 if (arg == NULL)
267 arg = Py_None;
268 Py_INCREF(arg);
269 PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
270 PyTuple_SET_ITEM(args, 1, whatstr);
271 PyTuple_SET_ITEM(args, 2, arg);
Fred Drake5755ce62001-06-27 19:19:46 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 /* call the Python-level function */
274 PyFrame_FastToLocals(frame);
275 result = PyEval_CallObject(callback, args);
276 PyFrame_LocalsToFast(frame, 1);
277 if (result == NULL)
278 PyTraceBack_Here(frame);
Fred Drake5755ce62001-06-27 19:19:46 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 /* cleanup */
281 Py_DECREF(args);
282 return result;
Fred Drake5755ce62001-06-27 19:19:46 +0000283}
284
285static int
286profile_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 PyThreadState *tstate = frame->f_tstate;
290 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 if (arg == NULL)
293 arg = Py_None;
294 result = call_trampoline(tstate, self, frame, what, arg);
295 if (result == NULL) {
296 PyEval_SetProfile(NULL, NULL);
297 return -1;
298 }
299 Py_DECREF(result);
300 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000301}
302
303static int
304trace_trampoline(PyObject *self, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 int what, PyObject *arg)
Fred Drake5755ce62001-06-27 19:19:46 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyThreadState *tstate = frame->f_tstate;
308 PyObject *callback;
309 PyObject *result;
Fred Drake5755ce62001-06-27 19:19:46 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (what == PyTrace_CALL)
312 callback = self;
313 else
314 callback = frame->f_trace;
315 if (callback == NULL)
316 return 0;
317 result = call_trampoline(tstate, callback, frame, what, arg);
318 if (result == NULL) {
319 PyEval_SetTrace(NULL, NULL);
320 Py_XDECREF(frame->f_trace);
321 frame->f_trace = NULL;
322 return -1;
323 }
324 if (result != Py_None) {
325 PyObject *temp = frame->f_trace;
326 frame->f_trace = NULL;
327 Py_XDECREF(temp);
328 frame->f_trace = result;
329 }
330 else {
331 Py_DECREF(result);
332 }
333 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +0000334}
Fred Draked0838392001-06-16 21:02:31 +0000335
Fred Drake8b4d01d2000-05-09 19:57:01 +0000336static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000337sys_settrace(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (trace_init() == -1)
340 return NULL;
341 if (args == Py_None)
342 PyEval_SetTrace(NULL, NULL);
343 else
344 PyEval_SetTrace(trace_trampoline, args);
345 Py_INCREF(Py_None);
346 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000347}
348
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000349PyDoc_STRVAR(settrace_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000350"settrace(function)\n\
351\n\
352Set the global debug tracing function. It will be called on each\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000353function call. See the debugger chapter in the library manual."
354);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000355
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000356static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000357sys_gettrace(PyObject *self, PyObject *args)
358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 PyThreadState *tstate = PyThreadState_GET();
360 PyObject *temp = tstate->c_traceobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (temp == NULL)
363 temp = Py_None;
364 Py_INCREF(temp);
365 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000366}
367
368PyDoc_STRVAR(gettrace_doc,
369"gettrace()\n\
370\n\
371Return the global debug tracing function set with sys.settrace.\n\
372See the debugger chapter in the library manual."
373);
374
375static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376sys_setprofile(PyObject *self, PyObject *args)
Guido van Rossume2437a11992-03-23 18:20:18 +0000377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (trace_init() == -1)
379 return NULL;
380 if (args == Py_None)
381 PyEval_SetProfile(NULL, NULL);
382 else
383 PyEval_SetProfile(profile_trampoline, args);
384 Py_INCREF(Py_None);
385 return Py_None;
Guido van Rossume2437a11992-03-23 18:20:18 +0000386}
387
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000388PyDoc_STRVAR(setprofile_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000389"setprofile(function)\n\
390\n\
391Set the profiling function. It will be called on each function call\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000392and return. See the profiler chapter in the library manual."
393);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000394
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000395static PyObject *
Christian Heimes9bd667a2008-01-20 15:14:11 +0000396sys_getprofile(PyObject *self, PyObject *args)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 PyThreadState *tstate = PyThreadState_GET();
399 PyObject *temp = tstate->c_profileobj;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (temp == NULL)
402 temp = Py_None;
403 Py_INCREF(temp);
404 return temp;
Christian Heimes9bd667a2008-01-20 15:14:11 +0000405}
406
407PyDoc_STRVAR(getprofile_doc,
408"getprofile()\n\
409\n\
410Return the profiling function set with sys.setprofile.\n\
411See the profiler chapter in the library manual."
412);
413
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000414static int _check_interval = 100;
415
Christian Heimes9bd667a2008-01-20 15:14:11 +0000416static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000417sys_setcheckinterval(PyObject *self, PyObject *args)
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (PyErr_WarnEx(PyExc_DeprecationWarning,
420 "sys.getcheckinterval() and sys.setcheckinterval() "
421 "are deprecated. Use sys.setswitchinterval() "
422 "instead.", 1) < 0)
423 return NULL;
424 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval))
425 return NULL;
426 Py_INCREF(Py_None);
427 return Py_None;
Guido van Rossuma0d7a231995-01-09 17:46:13 +0000428}
429
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000430PyDoc_STRVAR(setcheckinterval_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000431"setcheckinterval(n)\n\
432\n\
433Tell the Python interpreter to check for asynchronous events every\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000434n instructions. This also affects how often thread switches occur."
435);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000436
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000437static PyObject *
Tim Peterse5e065b2003-07-06 18:36:54 +0000438sys_getcheckinterval(PyObject *self, PyObject *args)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (PyErr_WarnEx(PyExc_DeprecationWarning,
441 "sys.getcheckinterval() and sys.setcheckinterval() "
442 "are deprecated. Use sys.getswitchinterval() "
443 "instead.", 1) < 0)
444 return NULL;
445 return PyLong_FromLong(_check_interval);
Tim Peterse5e065b2003-07-06 18:36:54 +0000446}
447
448PyDoc_STRVAR(getcheckinterval_doc,
449"getcheckinterval() -> current check interval; see setcheckinterval()."
450);
451
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000452#ifdef WITH_THREAD
453static PyObject *
454sys_setswitchinterval(PyObject *self, PyObject *args)
455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 double d;
457 if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d))
458 return NULL;
459 if (d <= 0.0) {
460 PyErr_SetString(PyExc_ValueError,
461 "switch interval must be strictly positive");
462 return NULL;
463 }
464 _PyEval_SetSwitchInterval((unsigned long) (1e6 * d));
465 Py_INCREF(Py_None);
466 return Py_None;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000467}
468
469PyDoc_STRVAR(setswitchinterval_doc,
470"setswitchinterval(n)\n\
471\n\
472Set the ideal thread switching delay inside the Python interpreter\n\
473The actual frequency of switching threads can be lower if the\n\
474interpreter executes long sequences of uninterruptible code\n\
475(this is implementation-specific and workload-dependent).\n\
476\n\
477The parameter must represent the desired switching delay in seconds\n\
478A typical value is 0.005 (5 milliseconds)."
479);
480
481static PyObject *
482sys_getswitchinterval(PyObject *self, PyObject *args)
483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval());
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000485}
486
487PyDoc_STRVAR(getswitchinterval_doc,
488"getswitchinterval() -> current thread switch interval; see setswitchinterval()."
489);
490
491#endif /* WITH_THREAD */
492
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000493#ifdef WITH_TSC
494static PyObject *
495sys_settscdump(PyObject *self, PyObject *args)
496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 int bool;
498 PyThreadState *tstate = PyThreadState_Get();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
501 return NULL;
502 if (bool)
503 tstate->interp->tscdump = 1;
504 else
505 tstate->interp->tscdump = 0;
506 Py_INCREF(Py_None);
507 return Py_None;
Tim Peters216b78b2006-01-06 02:40:53 +0000508
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000509}
510
Tim Peters216b78b2006-01-06 02:40:53 +0000511PyDoc_STRVAR(settscdump_doc,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000512"settscdump(bool)\n\
513\n\
514If true, tell the Python interpreter to dump VM measurements to\n\
515stderr. If false, turn off dump. The measurements are based on the\n\
Michael W. Hudson800ba232004-08-12 18:19:17 +0000516processor's time-stamp counter."
Tim Peters216b78b2006-01-06 02:40:53 +0000517);
Neal Norwitz0f5aed42004-06-13 20:32:17 +0000518#endif /* TSC */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000519
Tim Peterse5e065b2003-07-06 18:36:54 +0000520static PyObject *
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000521sys_setrecursionlimit(PyObject *self, PyObject *args)
522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 int new_limit;
524 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
525 return NULL;
526 if (new_limit <= 0) {
527 PyErr_SetString(PyExc_ValueError,
528 "recursion limit must be positive");
529 return NULL;
530 }
531 Py_SetRecursionLimit(new_limit);
532 Py_INCREF(Py_None);
533 return Py_None;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000534}
535
Mark Dickinsondc787d22010-05-23 13:33:13 +0000536static PyTypeObject Hash_InfoType;
537
538PyDoc_STRVAR(hash_info_doc,
539"hash_info\n\
540\n\
541A struct sequence providing parameters used for computing\n\
542numeric hashes. The attributes are read only.");
543
544static PyStructSequence_Field hash_info_fields[] = {
545 {"width", "width of the type used for hashing, in bits"},
546 {"modulus", "prime number giving the modulus on which the hash "
547 "function is based"},
548 {"inf", "value to be used for hash of a positive infinity"},
549 {"nan", "value to be used for hash of a nan"},
550 {"imag", "multiplier used for the imaginary part of a complex number"},
551 {NULL, NULL}
552};
553
554static PyStructSequence_Desc hash_info_desc = {
555 "sys.hash_info",
556 hash_info_doc,
557 hash_info_fields,
558 5,
559};
560
Matthias Klosed885e952010-07-06 10:53:30 +0000561static PyObject *
Mark Dickinsondc787d22010-05-23 13:33:13 +0000562get_hash_info(void)
563{
564 PyObject *hash_info;
565 int field = 0;
566 hash_info = PyStructSequence_New(&Hash_InfoType);
567 if (hash_info == NULL)
568 return NULL;
569 PyStructSequence_SET_ITEM(hash_info, field++,
570 PyLong_FromLong(8*sizeof(long)));
571 PyStructSequence_SET_ITEM(hash_info, field++,
572 PyLong_FromLong(_PyHASH_MODULUS));
573 PyStructSequence_SET_ITEM(hash_info, field++,
574 PyLong_FromLong(_PyHASH_INF));
575 PyStructSequence_SET_ITEM(hash_info, field++,
576 PyLong_FromLong(_PyHASH_NAN));
577 PyStructSequence_SET_ITEM(hash_info, field++,
578 PyLong_FromLong(_PyHASH_IMAG));
579 if (PyErr_Occurred()) {
580 Py_CLEAR(hash_info);
581 return NULL;
582 }
583 return hash_info;
584}
585
586
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000587PyDoc_STRVAR(setrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000588"setrecursionlimit(n)\n\
589\n\
590Set the maximum depth of the Python interpreter stack to n. This\n\
591limit prevents infinite recursion from causing an overflow of the C\n\
592stack and crashing Python. The highest possible limit is platform-\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000593dependent."
594);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000595
596static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000597sys_getrecursionlimit(PyObject *self)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 return PyLong_FromLong(Py_GetRecursionLimit());
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000600}
601
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000602PyDoc_STRVAR(getrecursionlimit_doc,
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000603"getrecursionlimit()\n\
604\n\
605Return the current value of the recursion limit, the maximum depth\n\
606of the Python interpreter stack. This limit prevents infinite\n\
Jack Jansene739a0d2002-06-26 20:39:20 +0000607recursion from causing an overflow of the C stack and crashing Python."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000608);
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000609
Mark Hammond8696ebc2002-10-08 02:44:31 +0000610#ifdef MS_WINDOWS
611PyDoc_STRVAR(getwindowsversion_doc,
612"getwindowsversion()\n\
613\n\
Eric Smithf7bb5782010-01-27 00:44:57 +0000614Return information about the running version of Windows as a named tuple.\n\
615The members are named: major, minor, build, platform, service_pack,\n\
616service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\
617backward compatibiliy, only the first 5 items are available by indexing.\n\
618All elements are numbers, except service_pack which is a string. Platform\n\
619may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\
6203 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\
621controller, 3 for a server."
Mark Hammond8696ebc2002-10-08 02:44:31 +0000622);
623
Eric Smithf7bb5782010-01-27 00:44:57 +0000624static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};
625
626static PyStructSequence_Field windows_version_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 {"major", "Major version number"},
628 {"minor", "Minor version number"},
629 {"build", "Build number"},
630 {"platform", "Operating system platform"},
631 {"service_pack", "Latest Service Pack installed on the system"},
632 {"service_pack_major", "Service Pack major version number"},
633 {"service_pack_minor", "Service Pack minor version number"},
634 {"suite_mask", "Bit mask identifying available product suites"},
635 {"product_type", "System product type"},
636 {0}
Eric Smithf7bb5782010-01-27 00:44:57 +0000637};
638
639static PyStructSequence_Desc windows_version_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 "sys.getwindowsversion", /* name */
641 getwindowsversion_doc, /* doc */
642 windows_version_fields, /* fields */
643 5 /* For backward compatibility,
644 only the first 5 items are accessible
645 via indexing, the rest are name only */
Eric Smithf7bb5782010-01-27 00:44:57 +0000646};
647
Mark Hammond8696ebc2002-10-08 02:44:31 +0000648static PyObject *
649sys_getwindowsversion(PyObject *self)
650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PyObject *version;
652 int pos = 0;
653 OSVERSIONINFOEX ver;
654 ver.dwOSVersionInfoSize = sizeof(ver);
655 if (!GetVersionEx((OSVERSIONINFO*) &ver))
656 return PyErr_SetFromWindowsErr(0);
Eric Smithf7bb5782010-01-27 00:44:57 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 version = PyStructSequence_New(&WindowsVersionType);
659 if (version == NULL)
660 return NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
663 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
664 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
665 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
666 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
667 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
668 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
669 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
670 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
Eric Smithf7bb5782010-01-27 00:44:57 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 return version;
Mark Hammond8696ebc2002-10-08 02:44:31 +0000673}
674
675#endif /* MS_WINDOWS */
676
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000677#ifdef HAVE_DLOPEN
678static PyObject *
679sys_setdlopenflags(PyObject *self, PyObject *args)
680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 int new_val;
682 PyThreadState *tstate = PyThreadState_GET();
683 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
684 return NULL;
685 if (!tstate)
686 return NULL;
687 tstate->interp->dlopenflags = new_val;
688 Py_INCREF(Py_None);
689 return Py_None;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000690}
691
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000692PyDoc_STRVAR(setdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000693"setdlopenflags(n) -> None\n\
694\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000695Set the flags used by the interpreter for dlopen calls, such as when the\n\
696interpreter loads extension modules. Among other things, this will enable\n\
697a lazy resolving of symbols when importing a module, if called as\n\
698sys.setdlopenflags(0). To share symbols across extension modules, call as\n\
699sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n\
700can be either found in the ctypes module, or in the DLFCN module. If DLFCN\n\
701is not available, it can be generated from /usr/include/dlfcn.h using the\n\
702h2py script.");
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000703
704static PyObject *
705sys_getdlopenflags(PyObject *self, PyObject *args)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyThreadState *tstate = PyThreadState_GET();
708 if (!tstate)
709 return NULL;
710 return PyLong_FromLong(tstate->interp->dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000711}
712
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000713PyDoc_STRVAR(getdlopenflags_doc,
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000714"getdlopenflags() -> int\n\
715\n\
Alexandre Vassalotti260484d2009-07-17 11:43:26 +0000716Return the current value of the flags that are used for dlopen calls.\n\
717The flag constants are defined in the ctypes and DLFCN modules.");
718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719#endif /* HAVE_DLOPEN */
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000720
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000721#ifdef USE_MALLOPT
722/* Link with -lmalloc (or -lmpc) on an SGI */
723#include <malloc.h>
724
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000725static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000726sys_mdebug(PyObject *self, PyObject *args)
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 int flag;
729 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
730 return NULL;
731 mallopt(M_DEBUG, flag);
732 Py_INCREF(Py_None);
733 return Py_None;
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000734}
735#endif /* USE_MALLOPT */
736
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000737static PyObject *
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000738sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PyObject *res = NULL;
741 static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
742 static char *kwlist[] = {"object", "default", 0};
743 PyObject *o, *dflt = NULL;
744 PyObject *method;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
747 kwlist, &o, &dflt))
748 return NULL;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 /* Initialize static variable for GC head size */
751 if (gc_head_size == NULL) {
752 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
753 if (gc_head_size == NULL)
754 return NULL;
755 }
Benjamin Petersona5758c02009-05-09 18:15:04 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 /* Make sure the type is initialized. float gets initialized late */
758 if (PyType_Ready(Py_TYPE(o)) < 0)
759 return NULL;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 method = _PyObject_LookupSpecial(o, "__sizeof__",
762 &str__sizeof__);
763 if (method == NULL) {
764 if (!PyErr_Occurred())
765 PyErr_Format(PyExc_TypeError,
766 "Type %.100s doesn't define __sizeof__",
767 Py_TYPE(o)->tp_name);
768 }
769 else {
770 res = PyObject_CallFunctionObjArgs(method, NULL);
771 Py_DECREF(method);
772 }
773
774 /* Has a default value been given */
775 if ((res == NULL) && (dflt != NULL) &&
776 PyErr_ExceptionMatches(PyExc_TypeError))
777 {
778 PyErr_Clear();
779 Py_INCREF(dflt);
780 return dflt;
781 }
782 else if (res == NULL)
783 return res;
784
785 /* add gc_head size */
786 if (PyObject_IS_GC(o)) {
787 PyObject *tmp = res;
788 res = PyNumber_Add(tmp, gc_head_size);
789 Py_DECREF(tmp);
790 }
791 return res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000792}
793
794PyDoc_STRVAR(getsizeof_doc,
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000795"getsizeof(object, default) -> int\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +0000796\n\
797Return the size of object in bytes.");
798
799static PyObject *
Fred Drakea7688822001-10-24 20:47:48 +0000800sys_getrefcount(PyObject *self, PyObject *arg)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 return PyLong_FromSsize_t(arg->ob_refcnt);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000803}
804
Tim Peters4be93d02002-07-07 19:59:50 +0000805#ifdef Py_REF_DEBUG
Mark Hammond440d8982000-06-20 08:12:48 +0000806static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000807sys_gettotalrefcount(PyObject *self)
Mark Hammond440d8982000-06-20 08:12:48 +0000808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 return PyLong_FromSsize_t(_Py_GetRefTotal());
Mark Hammond440d8982000-06-20 08:12:48 +0000810}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000811#endif /* Py_REF_DEBUG */
Mark Hammond440d8982000-06-20 08:12:48 +0000812
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000813PyDoc_STRVAR(getrefcount_doc,
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000814"getrefcount(object) -> integer\n\
815\n\
Fred Drakeba3ff1b2002-06-20 21:36:19 +0000816Return the reference count of object. The count returned is generally\n\
817one higher than you might expect, because it includes the (temporary)\n\
818reference as an argument to getrefcount()."
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000819);
Guido van Rossumc3bc31e1998-06-27 19:43:25 +0000820
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000821#ifdef COUNT_ALLOCS
822static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000823sys_getcounts(PyObject *self)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 extern PyObject *get_counts(void);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 return get_counts();
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000828}
829#endif
830
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000831PyDoc_STRVAR(getframe_doc,
Barry Warsawb6a54d22000-12-06 21:47:46 +0000832"_getframe([depth]) -> frameobject\n\
833\n\
834Return a frame object from the call stack. If optional integer depth is\n\
835given, return the frame object that many calls below the top of the stack.\n\
836If that is deeper than the call stack, ValueError is raised. The default\n\
837for depth is zero, returning the frame at the top of the call stack.\n\
838\n\
839This function should be used for internal and specialized\n\
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +0000840purposes only."
841);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000842
843static PyObject *
844sys_getframe(PyObject *self, PyObject *args)
845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 PyFrameObject *f = PyThreadState_GET()->frame;
847 int depth = -1;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
850 return NULL;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 while (depth > 0 && f != NULL) {
853 f = f->f_back;
854 --depth;
855 }
856 if (f == NULL) {
857 PyErr_SetString(PyExc_ValueError,
858 "call stack is not deep enough");
859 return NULL;
860 }
861 Py_INCREF(f);
862 return (PyObject*)f;
Barry Warsawb6a54d22000-12-06 21:47:46 +0000863}
864
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000865PyDoc_STRVAR(current_frames_doc,
866"_current_frames() -> dictionary\n\
867\n\
868Return a dictionary mapping each current thread T's thread id to T's\n\
869current stack frame.\n\
870\n\
871This function should be used for specialized purposes only."
872);
873
874static PyObject *
875sys_current_frames(PyObject *self, PyObject *noargs)
876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 return _PyThread_CurrentFrames();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000878}
879
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000880PyDoc_STRVAR(call_tracing_doc,
881"call_tracing(func, args) -> object\n\
882\n\
883Call func(*args), while tracing is enabled. The tracing state is\n\
884saved, and restored afterwards. This is intended to be called from\n\
885a debugger from a checkpoint, to recursively debug some other code."
886);
887
888static PyObject *
889sys_call_tracing(PyObject *self, PyObject *args)
890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PyObject *func, *funcargs;
892 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
893 return NULL;
894 return _PyEval_CallTracing(func, funcargs);
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000895}
896
Jeremy Hylton985eba52003-02-05 23:13:00 +0000897PyDoc_STRVAR(callstats_doc,
898"callstats() -> tuple of integers\n\
899\n\
900Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
901when Python was built. Otherwise, return None.\n\
902\n\
903When enabled, this function returns detailed, implementation-specific\n\
904details about the number of function calls executed. The return value is\n\
905a 11-tuple where the entries in the tuple are counts of:\n\
9060. all function calls\n\
9071. calls to PyFunction_Type objects\n\
9082. PyFunction calls that do not create an argument tuple\n\
9093. PyFunction calls that do not create an argument tuple\n\
910 and bypass PyEval_EvalCodeEx()\n\
9114. PyMethod calls\n\
9125. PyMethod calls on bound methods\n\
9136. PyType calls\n\
9147. PyCFunction calls\n\
9158. generator calls\n\
9169. All other calls\n\
91710. Number of stack pops performed by call_function()"
918);
Barry Warsawb6a54d22000-12-06 21:47:46 +0000919
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000920#ifdef __cplusplus
921extern "C" {
922#endif
923
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000924#ifdef Py_TRACE_REFS
Guido van Rossumded690f1996-05-24 20:48:31 +0000925/* Defined in objects.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000926extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000927#endif
Guido van Rossumded690f1996-05-24 20:48:31 +0000928
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000929#ifdef DYNAMIC_EXECUTION_PROFILE
930/* Defined in ceval.c because it uses static globals if that file */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000931extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000932#endif
933
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000934#ifdef __cplusplus
935}
936#endif
937
Christian Heimes15ebc882008-02-04 18:48:49 +0000938static PyObject *
939sys_clear_type_cache(PyObject* self, PyObject* args)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyType_ClearCache();
942 Py_RETURN_NONE;
Christian Heimes15ebc882008-02-04 18:48:49 +0000943}
944
945PyDoc_STRVAR(sys_clear_type_cache__doc__,
946"_clear_type_cache() -> None\n\
947Clear the internal type lookup cache.");
948
949
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000950static PyMethodDef sys_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* Might as well keep this in alphabetic order */
952 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
953 callstats_doc},
954 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
955 sys_clear_type_cache__doc__},
956 {"_current_frames", sys_current_frames, METH_NOARGS,
957 current_frames_doc},
958 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
959 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
960 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
961 {"exit", sys_exit, METH_VARARGS, exit_doc},
962 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
963 METH_NOARGS, getdefaultencoding_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000964#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
966 getdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000967#endif
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000968#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000970#endif
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000971#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
Guido van Rossum43f1b8d1997-01-24 04:07:45 +0000973#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
975 METH_NOARGS, getfilesystemencoding_doc},
Guido van Rossum7f3f2c11996-05-23 22:45:41 +0000976#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 {"getobjects", _Py_GetObjects, METH_VARARGS},
Tim Peters4be93d02002-07-07 19:59:50 +0000978#endif
979#ifdef Py_REF_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000981#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
983 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
984 getrecursionlimit_doc},
985 {"getsizeof", (PyCFunction)sys_getsizeof,
986 METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
987 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000988#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
990 getwindowsversion_doc},
Mark Hammond8696ebc2002-10-08 02:44:31 +0000991#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 {"intern", sys_intern, METH_VARARGS, intern_doc},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000993#ifdef USE_MALLOPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 {"mdebug", sys_mdebug, METH_VARARGS},
Guido van Rossum14b4adb1992-09-03 20:25:30 +0000995#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
997 setcheckinterval_doc},
998 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
999 getcheckinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001000#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS,
1002 setswitchinterval_doc},
1003 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS,
1004 getswitchinterval_doc},
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001005#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001006#ifdef HAVE_DLOPEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
1008 setdlopenflags_doc},
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001009#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
1011 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
1012 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
1013 setrecursionlimit_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001014#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001016#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 {"settrace", sys_settrace, METH_O, settrace_doc},
1018 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
1019 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
1020 {NULL, NULL} /* sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001021};
1022
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001023static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001024list_builtin_module_names(void)
Guido van Rossum34679b71993-01-26 13:33:44 +00001025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 PyObject *list = PyList_New(0);
1027 int i;
1028 if (list == NULL)
1029 return NULL;
1030 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1031 PyObject *name = PyUnicode_FromString(
1032 PyImport_Inittab[i].name);
1033 if (name == NULL)
1034 break;
1035 PyList_Append(list, name);
1036 Py_DECREF(name);
1037 }
1038 if (PyList_Sort(list) != 0) {
1039 Py_DECREF(list);
1040 list = NULL;
1041 }
1042 if (list) {
1043 PyObject *v = PyList_AsTuple(list);
1044 Py_DECREF(list);
1045 list = v;
1046 }
1047 return list;
Guido van Rossum34679b71993-01-26 13:33:44 +00001048}
1049
Guido van Rossum23fff912000-12-15 22:02:05 +00001050static PyObject *warnoptions = NULL;
1051
1052void
1053PySys_ResetWarnOptions(void)
1054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (warnoptions == NULL || !PyList_Check(warnoptions))
1056 return;
1057 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
Guido van Rossum23fff912000-12-15 22:02:05 +00001058}
1059
1060void
Victor Stinner9ca9c252010-05-19 16:53:30 +00001061PySys_AddWarnOptionUnicode(PyObject *unicode)
Guido van Rossum23fff912000-12-15 22:02:05 +00001062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
1064 Py_XDECREF(warnoptions);
1065 warnoptions = PyList_New(0);
1066 if (warnoptions == NULL)
1067 return;
1068 }
Victor Stinner9ca9c252010-05-19 16:53:30 +00001069 PyList_Append(warnoptions, unicode);
1070}
1071
1072void
1073PySys_AddWarnOption(const wchar_t *s)
1074{
1075 PyObject *unicode;
1076 unicode = PyUnicode_FromWideChar(s, -1);
1077 if (unicode == NULL)
1078 return;
1079 PySys_AddWarnOptionUnicode(unicode);
1080 Py_DECREF(unicode);
Guido van Rossum23fff912000-12-15 22:02:05 +00001081}
1082
Christian Heimes33fe8092008-04-13 13:53:33 +00001083int
1084PySys_HasWarnOptions(void)
1085{
1086 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
1087}
1088
Guido van Rossum40552d01998-08-06 03:34:39 +00001089/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
1090 Two literals concatenated works just fine. If you have a K&R compiler
1091 or other abomination that however *does* understand longer strings,
1092 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001093PyDoc_VAR(sys_doc) =
1094PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001095"This module provides access to some objects used or maintained by the\n\
1096interpreter and to functions that interact strongly with the interpreter.\n\
1097\n\
1098Dynamic objects:\n\
1099\n\
1100argv -- command line arguments; argv[0] is the script pathname if known\n\
1101path -- module search path; path[0] is the script directory, else ''\n\
1102modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001103\n\
1104displayhook -- called to show results in an interactive session\n\
1105excepthook -- called to handle any uncaught exception other than SystemExit\n\
1106 To customize printing in an interactive session or to install a custom\n\
1107 top-level exception handler, assign other functions to replace these.\n\
1108\n\
Benjamin Peterson06157a42008-07-15 00:28:36 +00001109stdin -- standard input file object; used by input()\n\
Georg Brandl88fc6642007-02-09 21:28:07 +00001110stdout -- standard output file object; used by print()\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001111stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001112 By assigning other file objects (or objects that behave like files)\n\
1113 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001114\n\
1115last_type -- type of last uncaught exception\n\
1116last_value -- value of last uncaught exception\n\
1117last_traceback -- traceback of last uncaught exception\n\
1118 These three are only available in an interactive session after a\n\
1119 traceback has been printed.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001120"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001121)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001122/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001123PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001124"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001125Static objects:\n\
1126\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001127float_info -- a dict with information about the float implementation.\n\
Mark Dickinsonbd792642009-03-18 20:06:12 +00001128int_info -- a struct sequence with information about the int implementation.\n\
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001129maxsize -- the largest supported length of containers.\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +00001130maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001131builtin_module_names -- tuple of module names built into this interpreter\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001132subversion -- subversion information of the build as tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001133version -- the version of this interpreter as a string\n\
Eric Smith0e5b5622009-02-06 01:32:42 +00001134version_info -- version information as a named tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001135hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001136copyright -- copyright notice pertaining to this interpreter\n\
1137platform -- platform identifier\n\
1138executable -- pathname of this Python interpreter\n\
1139prefix -- prefix used to find the Python library\n\
1140exec_prefix -- prefix used to find the machine-specific Python library\n\
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001141float_repr_style -- string indicating the style of repr() output for floats\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001142"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001143)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001144#ifdef MS_WINDOWS
1145/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001146PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001147"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001148winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001149"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001150)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001151#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001152PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001153"__stdin__ -- the original stdin; don't touch!\n\
1154__stdout__ -- the original stdout; don't touch!\n\
1155__stderr__ -- the original stderr; don't touch!\n\
1156__displayhook__ -- the original displayhook; don't touch!\n\
1157__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001158\n\
1159Functions:\n\
1160\n\
Georg Brandl1a3284e2007-12-02 09:40:06 +00001161displayhook() -- print an object to the screen, and save it in builtins._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001162excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001163exc_info() -- return thread-safe information about the current exception\n\
1164exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001165getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001166getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001167getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001168getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001169getsizeof() -- return the size of an object in bytes\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001170gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001171setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001172setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001173setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001174setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001175settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001176"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001177)
Fred Drakeccede592000-08-14 20:59:57 +00001178/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001179
Martin v. Löwis43b57802006-01-05 23:38:54 +00001180/* Subversion branch and revision management */
1181static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
1182static const char headurl[] = "$HeadURL$";
1183static int svn_initialized;
1184static char patchlevel_revision[50]; /* Just the number */
1185static char branch[50];
1186static char shortbranch[50];
1187static const char *svn_revision;
1188
Tim Peterse86e7a52006-01-06 02:42:46 +00001189static void
1190svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +00001191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 const char *python, *br_start, *br_end, *br_end2, *svnversion;
1193 Py_ssize_t len;
1194 int istag = 0;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 if (svn_initialized)
1197 return;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 python = strstr(headurl, "/python/");
1200 if (!python) {
1201 strcpy(branch, "unknown branch");
1202 strcpy(shortbranch, "unknown");
1203 }
1204 else {
1205 br_start = python + 8;
1206 br_end = strchr(br_start, '/');
1207 assert(br_end);
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 /* Works even for trunk,
1210 as we are in trunk/Python/sysmodule.c */
1211 br_end2 = strchr(br_end+1, '/');
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 istag = strncmp(br_start, "tags", 4) == 0;
1214 if (strncmp(br_start, "trunk", 5) == 0) {
1215 strcpy(branch, "trunk");
1216 strcpy(shortbranch, "trunk");
1217 }
1218 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1219 len = br_end2 - br_start;
1220 strncpy(branch, br_start, len);
1221 branch[len] = '\0';
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 len = br_end2 - (br_end + 1);
1224 strncpy(shortbranch, br_end + 1, len);
1225 shortbranch[len] = '\0';
1226 }
1227 else {
1228 Py_FatalError("bad HeadURL");
1229 return;
1230 }
1231 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001232
1233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 svnversion = _Py_svnversion();
1235 if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0)
1236 svn_revision = svnversion;
1237 else if (istag) {
1238 len = strlen(_patchlevel_revision);
1239 assert(len >= 13);
1240 assert(len < (sizeof(patchlevel_revision) + 13));
1241 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1242 len - 13);
1243 patchlevel_revision[len - 13] = '\0';
1244 svn_revision = patchlevel_revision;
1245 }
1246 else
1247 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 svn_initialized = 1;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001250}
1251
1252/* Return svnversion output if available.
1253 Else return Revision of patchlevel.h if on branch.
1254 Else return empty string */
1255const char*
1256Py_SubversionRevision()
1257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 svnversion_init();
1259 return svn_revision;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001260}
1261
1262const char*
1263Py_SubversionShortBranch()
1264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 svnversion_init();
1266 return shortbranch;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001267}
1268
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001269
1270PyDoc_STRVAR(flags__doc__,
1271"sys.flags\n\
1272\n\
1273Flags provided through command line arguments or environment vars.");
1274
1275static PyTypeObject FlagsType;
1276
1277static PyStructSequence_Field flags_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 {"debug", "-d"},
1279 {"division_warning", "-Q"},
1280 {"inspect", "-i"},
1281 {"interactive", "-i"},
1282 {"optimize", "-O or -OO"},
1283 {"dont_write_bytecode", "-B"},
1284 {"no_user_site", "-s"},
1285 {"no_site", "-S"},
1286 {"ignore_environment", "-E"},
1287 {"verbose", "-v"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001288#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 {"riscos_wimp", "???"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* {"unbuffered", "-u"}, */
1292 /* {"skip_first", "-x"}, */
1293 {"bytes_warning", "-b"},
1294 {0}
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001295};
1296
1297static PyStructSequence_Desc flags_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 "sys.flags", /* name */
1299 flags__doc__, /* doc */
1300 flags_fields, /* fields */
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001301#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 12
Georg Brandle1b5ac62008-06-04 13:06:58 +00001303#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 11
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001305#endif
1306};
1307
1308static PyObject*
1309make_flags(void)
1310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 int pos = 0;
1312 PyObject *seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 seq = PyStructSequence_New(&FlagsType);
1315 if (seq == NULL)
1316 return NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001317
1318#define SetFlag(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 SetFlag(Py_DebugFlag);
1322 SetFlag(Py_DivisionWarningFlag);
1323 SetFlag(Py_InspectFlag);
1324 SetFlag(Py_InteractiveFlag);
1325 SetFlag(Py_OptimizeFlag);
1326 SetFlag(Py_DontWriteBytecodeFlag);
1327 SetFlag(Py_NoUserSiteDirectory);
1328 SetFlag(Py_NoSiteFlag);
1329 SetFlag(Py_IgnoreEnvironmentFlag);
1330 SetFlag(Py_VerboseFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001331#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 SetFlag(Py_RISCOSWimpFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001333#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 /* SetFlag(saw_unbuffered_flag); */
1335 /* SetFlag(skipfirstline); */
Christian Heimes33fe8092008-04-13 13:53:33 +00001336 SetFlag(Py_BytesWarningFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001337#undef SetFlag
1338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (PyErr_Occurred()) {
1340 return NULL;
1341 }
1342 return seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001343}
1344
Eric Smith0e5b5622009-02-06 01:32:42 +00001345PyDoc_STRVAR(version_info__doc__,
1346"sys.version_info\n\
1347\n\
1348Version information as a named tuple.");
1349
1350static PyTypeObject VersionInfoType;
1351
1352static PyStructSequence_Field version_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 {"major", "Major release number"},
1354 {"minor", "Minor release number"},
1355 {"micro", "Patch release number"},
1356 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1357 {"serial", "Serial release number"},
1358 {0}
Eric Smith0e5b5622009-02-06 01:32:42 +00001359};
1360
1361static PyStructSequence_Desc version_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 "sys.version_info", /* name */
1363 version_info__doc__, /* doc */
1364 version_info_fields, /* fields */
1365 5
Eric Smith0e5b5622009-02-06 01:32:42 +00001366};
1367
1368static PyObject *
1369make_version_info(void)
1370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyObject *version_info;
1372 char *s;
1373 int pos = 0;
Eric Smith0e5b5622009-02-06 01:32:42 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 version_info = PyStructSequence_New(&VersionInfoType);
1376 if (version_info == NULL) {
1377 return NULL;
1378 }
Eric Smith0e5b5622009-02-06 01:32:42 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 /*
1381 * These release level checks are mutually exclusive and cover
1382 * the field, so don't get too fancy with the pre-processor!
1383 */
Eric Smith0e5b5622009-02-06 01:32:42 +00001384#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 s = "alpha";
Eric Smith0e5b5622009-02-06 01:32:42 +00001386#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 s = "beta";
Eric Smith0e5b5622009-02-06 01:32:42 +00001388#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 s = "candidate";
Eric Smith0e5b5622009-02-06 01:32:42 +00001390#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 s = "final";
Eric Smith0e5b5622009-02-06 01:32:42 +00001392#endif
1393
1394#define SetIntItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001396#define SetStrItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 SetIntItem(PY_MAJOR_VERSION);
1400 SetIntItem(PY_MINOR_VERSION);
1401 SetIntItem(PY_MICRO_VERSION);
1402 SetStrItem(s);
1403 SetIntItem(PY_RELEASE_SERIAL);
Eric Smith0e5b5622009-02-06 01:32:42 +00001404#undef SetIntItem
1405#undef SetStrItem
1406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if (PyErr_Occurred()) {
1408 Py_CLEAR(version_info);
1409 return NULL;
1410 }
1411 return version_info;
Eric Smith0e5b5622009-02-06 01:32:42 +00001412}
1413
Martin v. Löwis1a214512008-06-11 05:26:20 +00001414static struct PyModuleDef sysmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 PyModuleDef_HEAD_INIT,
1416 "sys",
1417 sys_doc,
1418 -1, /* multiple "initialization" just copies the module dict. */
1419 sys_methods,
1420 NULL,
1421 NULL,
1422 NULL,
1423 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001424};
1425
Guido van Rossum25ce5661997-08-02 03:10:38 +00001426PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001427_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 PyObject *m, *v, *sysdict;
1430 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 m = PyModule_Create(&sysmodule);
1433 if (m == NULL)
1434 return NULL;
1435 sysdict = PyModule_GetDict(m);
1436#define SET_SYS_FROM_STRING(key, value) \
1437 v = value; \
1438 if (v != NULL) \
1439 PyDict_SetItemString(sysdict, key, v); \
1440 Py_XDECREF(v)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 /* Check that stdin is not a directory
1443 Using shell redirection, you can redirect stdin to a directory,
1444 crashing the Python interpreter. Catch this common mistake here
1445 and output a useful error message. Note that under MS Windows,
1446 the shell already prevents that. */
Martin v. Löwisec59d042009-01-12 07:59:10 +00001447#if !defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 {
1449 struct stat sb;
1450 if (fstat(fileno(stdin), &sb) == 0 &&
1451 S_ISDIR(sb.st_mode)) {
1452 /* There's nothing more we can do. */
1453 /* Py_FatalError() will core dump, so just exit. */
1454 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1455 exit(EXIT_FAILURE);
1456 }
1457 }
Martin v. Löwisec59d042009-01-12 07:59:10 +00001458#endif
Neal Norwitz11bd1192005-10-03 00:54:56 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 /* stdin/stdout/stderr are now set by pythonrun.c */
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 PyDict_SetItemString(sysdict, "__displayhook__",
1463 PyDict_GetItemString(sysdict, "displayhook"));
1464 PyDict_SetItemString(sysdict, "__excepthook__",
1465 PyDict_GetItemString(sysdict, "excepthook"));
1466 SET_SYS_FROM_STRING("version",
1467 PyUnicode_FromString(Py_GetVersion()));
1468 SET_SYS_FROM_STRING("hexversion",
1469 PyLong_FromLong(PY_VERSION_HEX));
1470 svnversion_init();
1471 SET_SYS_FROM_STRING("subversion",
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001472 Py_BuildValue("(sss)", "CPython", branch,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 svn_revision));
1474 SET_SYS_FROM_STRING("dont_write_bytecode",
1475 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1476 SET_SYS_FROM_STRING("api_version",
1477 PyLong_FromLong(PYTHON_API_VERSION));
1478 SET_SYS_FROM_STRING("copyright",
1479 PyUnicode_FromString(Py_GetCopyright()));
1480 SET_SYS_FROM_STRING("platform",
1481 PyUnicode_FromString(Py_GetPlatform()));
1482 SET_SYS_FROM_STRING("executable",
1483 PyUnicode_FromWideChar(
1484 Py_GetProgramFullPath(), -1));
1485 SET_SYS_FROM_STRING("prefix",
1486 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1487 SET_SYS_FROM_STRING("exec_prefix",
1488 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
1489 SET_SYS_FROM_STRING("maxsize",
1490 PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1491 SET_SYS_FROM_STRING("float_info",
1492 PyFloat_GetInfo());
1493 SET_SYS_FROM_STRING("int_info",
1494 PyLong_GetInfo());
Mark Dickinsondc787d22010-05-23 13:33:13 +00001495 /* initialize hash_info */
1496 if (Hash_InfoType.tp_name == 0)
1497 PyStructSequence_InitType(&Hash_InfoType, &hash_info_desc);
1498 SET_SYS_FROM_STRING("hash_info",
1499 get_hash_info());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 SET_SYS_FROM_STRING("maxunicode",
1501 PyLong_FromLong(PyUnicode_GetMax()));
1502 SET_SYS_FROM_STRING("builtin_module_names",
1503 list_builtin_module_names());
1504 {
1505 /* Assumes that longs are at least 2 bytes long.
1506 Should be safe! */
1507 unsigned long number = 1;
1508 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 s = (char *) &number;
1511 if (s[0] == 0)
1512 value = "big";
1513 else
1514 value = "little";
1515 SET_SYS_FROM_STRING("byteorder",
1516 PyUnicode_FromString(value));
1517 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001518#ifdef MS_COREDLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 SET_SYS_FROM_STRING("dllhandle",
1520 PyLong_FromVoidPtr(PyWin_DLLhModule));
1521 SET_SYS_FROM_STRING("winver",
1522 PyUnicode_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001523#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 if (warnoptions == NULL) {
1525 warnoptions = PyList_New(0);
1526 }
1527 else {
1528 Py_INCREF(warnoptions);
1529 }
1530 if (warnoptions != NULL) {
1531 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1532 }
Tim Peters216b78b2006-01-06 02:40:53 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 /* version_info */
1535 if (VersionInfoType.tp_name == 0)
1536 PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
1537 SET_SYS_FROM_STRING("version_info", make_version_info());
1538 /* prevent user from creating new instances */
1539 VersionInfoType.tp_init = NULL;
1540 VersionInfoType.tp_new = NULL;
Eric Smith0e5b5622009-02-06 01:32:42 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 /* flags */
1543 if (FlagsType.tp_name == 0)
1544 PyStructSequence_InitType(&FlagsType, &flags_desc);
1545 SET_SYS_FROM_STRING("flags", make_flags());
1546 /* prevent user from creating new instances */
1547 FlagsType.tp_init = NULL;
1548 FlagsType.tp_new = NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001549
Eric Smithf7bb5782010-01-27 00:44:57 +00001550
1551#if defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 /* getwindowsversion */
1553 if (WindowsVersionType.tp_name == 0)
1554 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc);
1555 /* prevent user from creating new instances */
1556 WindowsVersionType.tp_init = NULL;
1557 WindowsVersionType.tp_new = NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +00001558#endif
1559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001561#ifndef PY_NO_SHORT_FLOAT_REPR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 SET_SYS_FROM_STRING("float_repr_style",
1563 PyUnicode_FromString("short"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001564#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 SET_SYS_FROM_STRING("float_repr_style",
1566 PyUnicode_FromString("legacy"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001567#endif
1568
Christian Heimes7b3ce6a2008-01-31 14:31:45 +00001569#undef SET_SYS_FROM_STRING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (PyErr_Occurred())
1571 return NULL;
1572 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001573}
1574
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001575static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001576makepathobject(const wchar_t *path, wchar_t delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 int i, n;
1579 const wchar_t *p;
1580 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 n = 1;
1583 p = path;
1584 while ((p = wcschr(p, delim)) != NULL) {
1585 n++;
1586 p++;
1587 }
1588 v = PyList_New(n);
1589 if (v == NULL)
1590 return NULL;
1591 for (i = 0; ; i++) {
1592 p = wcschr(path, delim);
1593 if (p == NULL)
1594 p = path + wcslen(path); /* End of string */
1595 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
1596 if (w == NULL) {
1597 Py_DECREF(v);
1598 return NULL;
1599 }
1600 PyList_SetItem(v, i, w);
1601 if (*p == '\0')
1602 break;
1603 path = p+1;
1604 }
1605 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001606}
1607
1608void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001609PySys_SetPath(const wchar_t *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 PyObject *v;
1612 if ((v = makepathobject(path, DELIM)) == NULL)
1613 Py_FatalError("can't create sys.path");
1614 if (PySys_SetObject("path", v) != 0)
1615 Py_FatalError("can't assign sys.path");
1616 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001617}
1618
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001619static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001620makeargvobject(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 PyObject *av;
1623 if (argc <= 0 || argv == NULL) {
1624 /* Ensure at least one (empty) argument is seen */
1625 static wchar_t *empty_argv[1] = {L""};
1626 argv = empty_argv;
1627 argc = 1;
1628 }
1629 av = PyList_New(argc);
1630 if (av != NULL) {
1631 int i;
1632 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001633#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 PyObject *v;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 /* argv[0] is the script pathname if known */
1637 if (i == 0) {
1638 char* fn = decc$translate_vms(argv[0]);
1639 if ((fn == (char *)0) || fn == (char *)-1)
1640 v = PyUnicode_FromString(argv[0]);
1641 else
1642 v = PyUnicode_FromString(
1643 decc$translate_vms(argv[0]));
1644 } else
1645 v = PyUnicode_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001646#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001648#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (v == NULL) {
1650 Py_DECREF(av);
1651 av = NULL;
1652 break;
1653 }
1654 PyList_SetItem(av, i, v);
1655 }
1656 }
1657 return av;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001658}
1659
Martin v. Löwis790465f2008-04-05 20:41:37 +00001660#ifdef HAVE_REALPATH
1661static wchar_t*
1662_wrealpath(const wchar_t *path, wchar_t *resolved_path)
1663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 char cpath[PATH_MAX];
1665 char cresolved_path[PATH_MAX];
1666 char *res;
1667 size_t r;
1668 r = wcstombs(cpath, path, PATH_MAX);
1669 if (r == (size_t)-1 || r >= PATH_MAX) {
1670 errno = EINVAL;
1671 return NULL;
1672 }
1673 res = realpath(cpath, cresolved_path);
1674 if (res == NULL)
1675 return NULL;
1676 r = mbstowcs(resolved_path, cresolved_path, PATH_MAX);
1677 if (r == (size_t)-1 || r >= PATH_MAX) {
1678 errno = EINVAL;
1679 return NULL;
1680 }
1681 return resolved_path;
Martin v. Löwis790465f2008-04-05 20:41:37 +00001682}
1683#endif
1684
Nick Coghland26c18a2010-08-17 13:06:11 +00001685#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
1686 (argc > 0 && argv0 != NULL && \
1687 wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001688
1689static void
1690sys_update_path(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001691{
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001692 wchar_t *argv0;
1693 wchar_t *p = NULL;
1694 Py_ssize_t n = 0;
1695 PyObject *a;
1696 PyObject *path;
1697#ifdef HAVE_READLINK
1698 extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t);
1699 wchar_t link[MAXPATHLEN+1];
1700 wchar_t argv0copy[2*MAXPATHLEN+1];
1701 int nr = 0;
1702#endif
Guido van Rossum162e38c2003-02-19 15:25:10 +00001703#if defined(HAVE_REALPATH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 wchar_t fullpath[MAXPATHLEN];
Martin v. Löwisec59d042009-01-12 07:59:10 +00001705#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 wchar_t fullpath[MAX_PATH];
Thomas Heller27bb71e2003-01-08 14:33:48 +00001707#endif
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001708
1709 path = PySys_GetObject("path");
1710 if (path == NULL)
1711 return;
1712
1713 if (argc == 0)
1714 return;
1715 argv0 = argv[0];
1716
1717#ifdef HAVE_READLINK
1718 if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
1719 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
1720 if (nr > 0) {
1721 /* It's a symlink */
1722 link[nr] = '\0';
1723 if (link[0] == SEP)
1724 argv0 = link; /* Link to absolute path */
1725 else if (wcschr(link, SEP) == NULL)
1726 ; /* Link without path */
1727 else {
1728 /* Must join(dirname(argv0), link) */
1729 wchar_t *q = wcsrchr(argv0, SEP);
1730 if (q == NULL)
1731 argv0 = link; /* argv0 without path */
1732 else {
1733 /* Must make a copy */
1734 wcscpy(argv0copy, argv0);
1735 q = wcsrchr(argv0copy, SEP);
1736 wcscpy(q+1, link);
1737 argv0 = argv0copy;
1738 }
1739 }
1740 }
1741#endif /* HAVE_READLINK */
1742#if SEP == '\\' /* Special case for MS filename syntax */
1743 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1744 wchar_t *q;
1745#if defined(MS_WINDOWS) && !defined(MS_WINCE)
1746 /* This code here replaces the first element in argv with the full
1747 path that it represents. Under CE, there are no relative paths so
1748 the argument must be the full path anyway. */
1749 wchar_t *ptemp;
1750 if (GetFullPathNameW(argv0,
1751 sizeof(fullpath)/sizeof(fullpath[0]),
1752 fullpath,
1753 &ptemp)) {
1754 argv0 = fullpath;
1755 }
1756#endif
1757 p = wcsrchr(argv0, SEP);
1758 /* Test for alternate separator */
1759 q = wcsrchr(p ? p : argv0, '/');
1760 if (q != NULL)
1761 p = q;
1762 if (p != NULL) {
1763 n = p + 1 - argv0;
1764 if (n > 1 && p[-1] != ':')
1765 n--; /* Drop trailing separator */
1766 }
1767 }
1768#else /* All other filename syntaxes */
1769 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1770#if defined(HAVE_REALPATH)
1771 if (_wrealpath(argv0, fullpath)) {
1772 argv0 = fullpath;
1773 }
1774#endif
1775 p = wcsrchr(argv0, SEP);
1776 }
1777 if (p != NULL) {
1778 n = p + 1 - argv0;
1779#if SEP == '/' /* Special case for Unix filename syntax */
1780 if (n > 1)
1781 n--; /* Drop trailing separator */
1782#endif /* Unix */
1783 }
1784#endif /* All others */
1785 a = PyUnicode_FromWideChar(argv0, n);
1786 if (a == NULL)
1787 Py_FatalError("no mem for sys.path insertion");
1788 if (PyList_Insert(path, 0, a) < 0)
1789 Py_FatalError("sys.path.insert(0) failed");
1790 Py_DECREF(a);
1791}
1792
1793void
1794PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
1795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 PyObject *av = makeargvobject(argc, argv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (av == NULL)
1798 Py_FatalError("no mem for sys.argv");
1799 if (PySys_SetObject("argv", av) != 0)
1800 Py_FatalError("can't assign sys.argv");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 Py_DECREF(av);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001802 if (updatepath)
1803 sys_update_path(argc, argv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001804}
Guido van Rossuma890e681998-05-12 14:59:24 +00001805
Antoine Pitrouf978fac2010-05-21 17:25:34 +00001806void
1807PySys_SetArgv(int argc, wchar_t **argv)
1808{
1809 PySys_SetArgvEx(argc, argv, 1);
1810}
1811
Victor Stinner14284c22010-04-23 12:02:30 +00001812/* Reimplementation of PyFile_WriteString() no calling indirectly
1813 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
1814
1815static int
Victor Stinner79766632010-08-16 17:36:42 +00001816sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
Victor Stinner14284c22010-04-23 12:02:30 +00001817{
Victor Stinner79766632010-08-16 17:36:42 +00001818 PyObject *writer = NULL, *args = NULL, *result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 int err;
Victor Stinner14284c22010-04-23 12:02:30 +00001820
Victor Stinnerecccc4f2010-06-08 20:46:00 +00001821 if (file == NULL)
1822 return -1;
1823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 writer = PyObject_GetAttrString(file, "write");
1825 if (writer == NULL)
1826 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 args = PyTuple_Pack(1, unicode);
1829 if (args == NULL)
1830 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 result = PyEval_CallObject(writer, args);
1833 if (result == NULL) {
1834 goto error;
1835 } else {
1836 err = 0;
1837 goto finally;
1838 }
Victor Stinner14284c22010-04-23 12:02:30 +00001839
1840error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 err = -1;
Victor Stinner14284c22010-04-23 12:02:30 +00001842finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 Py_XDECREF(writer);
1844 Py_XDECREF(args);
1845 Py_XDECREF(result);
1846 return err;
Victor Stinner14284c22010-04-23 12:02:30 +00001847}
1848
Victor Stinner79766632010-08-16 17:36:42 +00001849static int
1850sys_pyfile_write(const char *text, PyObject *file)
1851{
1852 PyObject *unicode = NULL;
1853 int err;
1854
1855 if (file == NULL)
1856 return -1;
1857
1858 unicode = PyUnicode_FromString(text);
1859 if (unicode == NULL)
1860 return -1;
1861
1862 err = sys_pyfile_write_unicode(unicode, file);
1863 Py_DECREF(unicode);
1864 return err;
1865}
Guido van Rossuma890e681998-05-12 14:59:24 +00001866
1867/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1868 Adapted from code submitted by Just van Rossum.
1869
1870 PySys_WriteStdout(format, ...)
1871 PySys_WriteStderr(format, ...)
1872
1873 The first function writes to sys.stdout; the second to sys.stderr. When
1874 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001875 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001876
Victor Stinner14284c22010-04-23 12:02:30 +00001877 PyErr_CheckSignals() is not called to avoid the execution of the Python
Victor Stinner79766632010-08-16 17:36:42 +00001878 signal handlers: they may raise a new exception whereas sys_write()
1879 ignores all exceptions.
Victor Stinner14284c22010-04-23 12:02:30 +00001880
Guido van Rossuma890e681998-05-12 14:59:24 +00001881 Both take a printf-style format string as their first argument followed
1882 by a variable length argument list determined by the format string.
1883
1884 *** WARNING ***
1885
1886 The format should limit the total size of the formatted output string to
1887 1000 bytes. In particular, this means that no unrestricted "%s" formats
1888 should occur; these should be limited using "%.<N>s where <N> is a
1889 decimal number calculated so that <N> plus the maximum size of other
1890 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1891 which can print hundreds of digits for very large numbers.
1892
1893 */
1894
1895static void
Victor Stinner79766632010-08-16 17:36:42 +00001896sys_write(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 PyObject *file;
1899 PyObject *error_type, *error_value, *error_traceback;
1900 char buffer[1001];
1901 int written;
Guido van Rossuma890e681998-05-12 14:59:24 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1904 file = PySys_GetObject(name);
1905 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
1906 if (sys_pyfile_write(buffer, file) != 0) {
1907 PyErr_Clear();
1908 fputs(buffer, fp);
1909 }
1910 if (written < 0 || (size_t)written >= sizeof(buffer)) {
1911 const char *truncated = "... truncated";
Victor Stinner79766632010-08-16 17:36:42 +00001912 if (sys_pyfile_write(truncated, file) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 fputs(truncated, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 }
1915 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001916}
1917
1918void
Guido van Rossuma890e681998-05-12 14:59:24 +00001919PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001924 sys_write("stdout", stdout, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001926}
1927
1928void
Guido van Rossuma890e681998-05-12 14:59:24 +00001929PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001934 sys_write("stderr", stderr, format, va);
1935 va_end(va);
1936}
1937
1938static void
1939sys_format(char *name, FILE *fp, const char *format, va_list va)
1940{
1941 PyObject *file, *message;
1942 PyObject *error_type, *error_value, *error_traceback;
1943 char *utf8;
1944
1945 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1946 file = PySys_GetObject(name);
1947 message = PyUnicode_FromFormatV(format, va);
1948 if (message != NULL) {
1949 if (sys_pyfile_write_unicode(message, file) != 0) {
1950 PyErr_Clear();
1951 utf8 = _PyUnicode_AsString(message);
1952 if (utf8 != NULL)
1953 fputs(utf8, fp);
1954 }
1955 Py_DECREF(message);
1956 }
1957 PyErr_Restore(error_type, error_value, error_traceback);
1958}
1959
1960void
1961PySys_FormatStdout(const char *format, ...)
1962{
1963 va_list va;
1964
1965 va_start(va, format);
1966 sys_format("stdout", stdout, format, va);
1967 va_end(va);
1968}
1969
1970void
1971PySys_FormatStderr(const char *format, ...)
1972{
1973 va_list va;
1974
1975 va_start(va, format);
1976 sys_format("stderr", stderr, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001978}