blob: 6be2262c7b0eac70d7db0860c5133dd221061aca [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++,
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000570 PyLong_FromLong(8*sizeof(Py_hash_t)));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000571 PyStructSequence_SET_ITEM(hash_info, field++,
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000572 PyLong_FromSsize_t(_PyHASH_MODULUS));
Mark Dickinsondc787d22010-05-23 13:33:13 +0000573 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
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001089static PyObject *xoptions = NULL;
1090
1091static PyObject *
1092get_xoptions(void)
1093{
1094 if (xoptions == NULL || !PyDict_Check(xoptions)) {
1095 Py_XDECREF(xoptions);
1096 xoptions = PyDict_New();
1097 }
1098 return xoptions;
1099}
1100
1101void
1102PySys_AddXOption(const wchar_t *s)
1103{
1104 PyObject *opts;
1105 PyObject *name = NULL, *value = NULL;
1106 const wchar_t *name_end;
1107 int r;
1108
1109 opts = get_xoptions();
1110 if (opts == NULL)
1111 goto error;
1112
1113 name_end = wcschr(s, L'=');
1114 if (!name_end) {
1115 name = PyUnicode_FromWideChar(s, -1);
1116 value = Py_True;
1117 Py_INCREF(value);
1118 }
1119 else {
1120 name = PyUnicode_FromWideChar(s, name_end - s);
1121 value = PyUnicode_FromWideChar(name_end + 1, -1);
1122 }
1123 if (name == NULL || value == NULL)
1124 goto error;
1125 r = PyDict_SetItem(opts, name, value);
1126 Py_DECREF(name);
1127 Py_DECREF(value);
1128 return;
1129
1130error:
1131 Py_XDECREF(name);
1132 Py_XDECREF(value);
1133 /* No return value, therefore clear error state if possible */
1134 if (_Py_atomic_load_relaxed(&_PyThreadState_Current))
1135 PyErr_Clear();
1136}
1137
1138PyObject *
1139PySys_GetXOptions(void)
1140{
1141 return get_xoptions();
1142}
1143
Guido van Rossum40552d01998-08-06 03:34:39 +00001144/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
1145 Two literals concatenated works just fine. If you have a K&R compiler
1146 or other abomination that however *does* understand longer strings,
1147 get rid of the !!! comment in the middle and the quotes that surround it. */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001148PyDoc_VAR(sys_doc) =
1149PyDoc_STR(
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001150"This module provides access to some objects used or maintained by the\n\
1151interpreter and to functions that interact strongly with the interpreter.\n\
1152\n\
1153Dynamic objects:\n\
1154\n\
1155argv -- command line arguments; argv[0] is the script pathname if known\n\
1156path -- module search path; path[0] is the script directory, else ''\n\
1157modules -- dictionary of loaded modules\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001158\n\
1159displayhook -- called to show results in an interactive session\n\
1160excepthook -- called to handle any uncaught exception other than SystemExit\n\
1161 To customize printing in an interactive session or to install a custom\n\
1162 top-level exception handler, assign other functions to replace these.\n\
1163\n\
Benjamin Peterson06157a42008-07-15 00:28:36 +00001164stdin -- standard input file object; used by input()\n\
Georg Brandl88fc6642007-02-09 21:28:07 +00001165stdout -- standard output file object; used by print()\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001166stderr -- standard error object; used for error messages\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001167 By assigning other file objects (or objects that behave like files)\n\
1168 to these, it is possible to redirect all of the interpreter's I/O.\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001169\n\
1170last_type -- type of last uncaught exception\n\
1171last_value -- value of last uncaught exception\n\
1172last_traceback -- traceback of last uncaught exception\n\
1173 These three are only available in an interactive session after a\n\
1174 traceback has been printed.\n\
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001175"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001176)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001177/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001178PyDoc_STR(
Guido van Rossuma71b5f41999-01-14 19:07:00 +00001179"\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001180Static objects:\n\
1181\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001182float_info -- a dict with information about the float implementation.\n\
Mark Dickinsonbd792642009-03-18 20:06:12 +00001183int_info -- a struct sequence with information about the int implementation.\n\
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001184maxsize -- the largest supported length of containers.\n\
Martin v. Löwisce9b5a52001-06-27 06:28:56 +00001185maxunicode -- the largest supported character\n\
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001186builtin_module_names -- tuple of module names built into this interpreter\n\
Christian Heimes2d378ab2007-12-15 01:28:04 +00001187subversion -- subversion information of the build as tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001188version -- the version of this interpreter as a string\n\
Eric Smith0e5b5622009-02-06 01:32:42 +00001189version_info -- version information as a named tuple\n\
Fred Drake801c08d2000-04-13 15:29:10 +00001190hexversion -- version information encoded as a single integer\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001191copyright -- copyright notice pertaining to this interpreter\n\
1192platform -- platform identifier\n\
1193executable -- pathname of this Python interpreter\n\
1194prefix -- prefix used to find the Python library\n\
1195exec_prefix -- prefix used to find the machine-specific Python library\n\
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001196float_repr_style -- string indicating the style of repr() output for floats\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001197"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001198)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001199#ifdef MS_WINDOWS
1200/* concatenating string here */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001201PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001202"dllhandle -- [Windows only] integer handle of the Python DLL\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001203winver -- [Windows only] version number of the Python DLL\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001204"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001205)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001206#endif /* MS_WINDOWS */
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001207PyDoc_STR(
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001208"__stdin__ -- the original stdin; don't touch!\n\
1209__stdout__ -- the original stdout; don't touch!\n\
1210__stderr__ -- the original stderr; don't touch!\n\
1211__displayhook__ -- the original displayhook; don't touch!\n\
1212__excepthook__ -- the original excepthook; don't touch!\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001213\n\
1214Functions:\n\
1215\n\
Georg Brandl1a3284e2007-12-02 09:40:06 +00001216displayhook() -- print an object to the screen, and save it in builtins._\n\
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001217excepthook() -- print an exception and its traceback to sys.stderr\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001218exc_info() -- return thread-safe information about the current exception\n\
1219exit() -- exit the interpreter by raising SystemExit\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001220getdlopenflags() -- returns flags to be used for dlopen() calls\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001221getprofile() -- get the global profiling function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001222getrefcount() -- return the reference count for an object (plus one :-)\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001223getrecursionlimit() -- return the max recursion depth for the interpreter\n\
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001224getsizeof() -- return the size of an object in bytes\n\
Christian Heimes9bd667a2008-01-20 15:14:11 +00001225gettrace() -- get the global debug tracing function\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001226setcheckinterval() -- control how often the interpreter checks for events\n\
Martin v. Löwisf0473d52001-07-18 16:17:16 +00001227setdlopenflags() -- set the flags to be used for dlopen() calls\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001228setprofile() -- set the global profiling function\n\
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +00001229setrecursionlimit() -- set the max recursion depth for the interpreter\n\
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001230settrace() -- set the global debug tracing function\n\
Fred Drakeccede592000-08-14 20:59:57 +00001231"
Martin v. Löwisa3fb4f72002-06-09 13:33:54 +00001232)
Fred Drakeccede592000-08-14 20:59:57 +00001233/* end of sys_doc */ ;
Guido van Rossumc3bc31e1998-06-27 19:43:25 +00001234
Martin v. Löwis43b57802006-01-05 23:38:54 +00001235/* Subversion branch and revision management */
1236static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
1237static const char headurl[] = "$HeadURL$";
1238static int svn_initialized;
1239static char patchlevel_revision[50]; /* Just the number */
1240static char branch[50];
1241static char shortbranch[50];
1242static const char *svn_revision;
1243
Tim Peterse86e7a52006-01-06 02:42:46 +00001244static void
1245svnversion_init(void)
Martin v. Löwis43b57802006-01-05 23:38:54 +00001246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 const char *python, *br_start, *br_end, *br_end2, *svnversion;
1248 Py_ssize_t len;
1249 int istag = 0;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (svn_initialized)
1252 return;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 python = strstr(headurl, "/python/");
1255 if (!python) {
1256 strcpy(branch, "unknown branch");
1257 strcpy(shortbranch, "unknown");
1258 }
1259 else {
1260 br_start = python + 8;
1261 br_end = strchr(br_start, '/');
1262 assert(br_end);
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* Works even for trunk,
1265 as we are in trunk/Python/sysmodule.c */
1266 br_end2 = strchr(br_end+1, '/');
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 istag = strncmp(br_start, "tags", 4) == 0;
1269 if (strncmp(br_start, "trunk", 5) == 0) {
1270 strcpy(branch, "trunk");
1271 strcpy(shortbranch, "trunk");
1272 }
1273 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1274 len = br_end2 - br_start;
1275 strncpy(branch, br_start, len);
1276 branch[len] = '\0';
Collin Winterd5a5f5d2007-08-22 19:45:07 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 len = br_end2 - (br_end + 1);
1279 strncpy(shortbranch, br_end + 1, len);
1280 shortbranch[len] = '\0';
1281 }
1282 else {
1283 Py_FatalError("bad HeadURL");
1284 return;
1285 }
1286 }
Martin v. Löwis43b57802006-01-05 23:38:54 +00001287
1288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 svnversion = _Py_svnversion();
1290 if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0)
1291 svn_revision = svnversion;
1292 else if (istag) {
1293 len = strlen(_patchlevel_revision);
1294 assert(len >= 13);
1295 assert(len < (sizeof(patchlevel_revision) + 13));
1296 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1297 len - 13);
1298 patchlevel_revision[len - 13] = '\0';
1299 svn_revision = patchlevel_revision;
1300 }
1301 else
1302 svn_revision = "";
Tim Peters216b78b2006-01-06 02:40:53 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 svn_initialized = 1;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001305}
1306
1307/* Return svnversion output if available.
1308 Else return Revision of patchlevel.h if on branch.
1309 Else return empty string */
1310const char*
1311Py_SubversionRevision()
1312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 svnversion_init();
1314 return svn_revision;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001315}
1316
1317const char*
1318Py_SubversionShortBranch()
1319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 svnversion_init();
1321 return shortbranch;
Martin v. Löwis43b57802006-01-05 23:38:54 +00001322}
1323
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001324
1325PyDoc_STRVAR(flags__doc__,
1326"sys.flags\n\
1327\n\
1328Flags provided through command line arguments or environment vars.");
1329
1330static PyTypeObject FlagsType;
1331
1332static PyStructSequence_Field flags_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 {"debug", "-d"},
1334 {"division_warning", "-Q"},
1335 {"inspect", "-i"},
1336 {"interactive", "-i"},
1337 {"optimize", "-O or -OO"},
1338 {"dont_write_bytecode", "-B"},
1339 {"no_user_site", "-s"},
1340 {"no_site", "-S"},
1341 {"ignore_environment", "-E"},
1342 {"verbose", "-v"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001343#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 {"riscos_wimp", "???"},
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001345#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 /* {"unbuffered", "-u"}, */
1347 /* {"skip_first", "-x"}, */
1348 {"bytes_warning", "-b"},
1349 {0}
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001350};
1351
1352static PyStructSequence_Desc flags_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 "sys.flags", /* name */
1354 flags__doc__, /* doc */
1355 flags_fields, /* fields */
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001356#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 12
Georg Brandle1b5ac62008-06-04 13:06:58 +00001358#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 11
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001360#endif
1361};
1362
1363static PyObject*
1364make_flags(void)
1365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 int pos = 0;
1367 PyObject *seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 seq = PyStructSequence_New(&FlagsType);
1370 if (seq == NULL)
1371 return NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001372
1373#define SetFlag(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 SetFlag(Py_DebugFlag);
1377 SetFlag(Py_DivisionWarningFlag);
1378 SetFlag(Py_InspectFlag);
1379 SetFlag(Py_InteractiveFlag);
1380 SetFlag(Py_OptimizeFlag);
1381 SetFlag(Py_DontWriteBytecodeFlag);
1382 SetFlag(Py_NoUserSiteDirectory);
1383 SetFlag(Py_NoSiteFlag);
1384 SetFlag(Py_IgnoreEnvironmentFlag);
1385 SetFlag(Py_VerboseFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001386#ifdef RISCOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 SetFlag(Py_RISCOSWimpFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 /* SetFlag(saw_unbuffered_flag); */
1390 /* SetFlag(skipfirstline); */
Christian Heimes33fe8092008-04-13 13:53:33 +00001391 SetFlag(Py_BytesWarningFlag);
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001392#undef SetFlag
1393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 if (PyErr_Occurred()) {
1395 return NULL;
1396 }
1397 return seq;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001398}
1399
Eric Smith0e5b5622009-02-06 01:32:42 +00001400PyDoc_STRVAR(version_info__doc__,
1401"sys.version_info\n\
1402\n\
1403Version information as a named tuple.");
1404
1405static PyTypeObject VersionInfoType;
1406
1407static PyStructSequence_Field version_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 {"major", "Major release number"},
1409 {"minor", "Minor release number"},
1410 {"micro", "Patch release number"},
1411 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1412 {"serial", "Serial release number"},
1413 {0}
Eric Smith0e5b5622009-02-06 01:32:42 +00001414};
1415
1416static PyStructSequence_Desc version_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 "sys.version_info", /* name */
1418 version_info__doc__, /* doc */
1419 version_info_fields, /* fields */
1420 5
Eric Smith0e5b5622009-02-06 01:32:42 +00001421};
1422
1423static PyObject *
1424make_version_info(void)
1425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 PyObject *version_info;
1427 char *s;
1428 int pos = 0;
Eric Smith0e5b5622009-02-06 01:32:42 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 version_info = PyStructSequence_New(&VersionInfoType);
1431 if (version_info == NULL) {
1432 return NULL;
1433 }
Eric Smith0e5b5622009-02-06 01:32:42 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 /*
1436 * These release level checks are mutually exclusive and cover
1437 * the field, so don't get too fancy with the pre-processor!
1438 */
Eric Smith0e5b5622009-02-06 01:32:42 +00001439#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 s = "alpha";
Eric Smith0e5b5622009-02-06 01:32:42 +00001441#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 s = "beta";
Eric Smith0e5b5622009-02-06 01:32:42 +00001443#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 s = "candidate";
Eric Smith0e5b5622009-02-06 01:32:42 +00001445#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 s = "final";
Eric Smith0e5b5622009-02-06 01:32:42 +00001447#endif
1448
1449#define SetIntItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001451#define SetStrItem(flag) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
Eric Smith0e5b5622009-02-06 01:32:42 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 SetIntItem(PY_MAJOR_VERSION);
1455 SetIntItem(PY_MINOR_VERSION);
1456 SetIntItem(PY_MICRO_VERSION);
1457 SetStrItem(s);
1458 SetIntItem(PY_RELEASE_SERIAL);
Eric Smith0e5b5622009-02-06 01:32:42 +00001459#undef SetIntItem
1460#undef SetStrItem
1461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 if (PyErr_Occurred()) {
1463 Py_CLEAR(version_info);
1464 return NULL;
1465 }
1466 return version_info;
Eric Smith0e5b5622009-02-06 01:32:42 +00001467}
1468
Martin v. Löwis1a214512008-06-11 05:26:20 +00001469static struct PyModuleDef sysmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 PyModuleDef_HEAD_INIT,
1471 "sys",
1472 sys_doc,
1473 -1, /* multiple "initialization" just copies the module dict. */
1474 sys_methods,
1475 NULL,
1476 NULL,
1477 NULL,
1478 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001479};
1480
Guido van Rossum25ce5661997-08-02 03:10:38 +00001481PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001482_PySys_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 PyObject *m, *v, *sysdict;
1485 char *s;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 m = PyModule_Create(&sysmodule);
1488 if (m == NULL)
1489 return NULL;
1490 sysdict = PyModule_GetDict(m);
1491#define SET_SYS_FROM_STRING(key, value) \
1492 v = value; \
1493 if (v != NULL) \
1494 PyDict_SetItemString(sysdict, key, v); \
1495 Py_XDECREF(v)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 /* Check that stdin is not a directory
1498 Using shell redirection, you can redirect stdin to a directory,
1499 crashing the Python interpreter. Catch this common mistake here
1500 and output a useful error message. Note that under MS Windows,
1501 the shell already prevents that. */
Martin v. Löwisec59d042009-01-12 07:59:10 +00001502#if !defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 {
1504 struct stat sb;
1505 if (fstat(fileno(stdin), &sb) == 0 &&
1506 S_ISDIR(sb.st_mode)) {
1507 /* There's nothing more we can do. */
1508 /* Py_FatalError() will core dump, so just exit. */
1509 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1510 exit(EXIT_FAILURE);
1511 }
1512 }
Martin v. Löwisec59d042009-01-12 07:59:10 +00001513#endif
Neal Norwitz11bd1192005-10-03 00:54:56 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* stdin/stdout/stderr are now set by pythonrun.c */
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 PyDict_SetItemString(sysdict, "__displayhook__",
1518 PyDict_GetItemString(sysdict, "displayhook"));
1519 PyDict_SetItemString(sysdict, "__excepthook__",
1520 PyDict_GetItemString(sysdict, "excepthook"));
1521 SET_SYS_FROM_STRING("version",
1522 PyUnicode_FromString(Py_GetVersion()));
1523 SET_SYS_FROM_STRING("hexversion",
1524 PyLong_FromLong(PY_VERSION_HEX));
1525 svnversion_init();
1526 SET_SYS_FROM_STRING("subversion",
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001527 Py_BuildValue("(sss)", "CPython", branch,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 svn_revision));
1529 SET_SYS_FROM_STRING("dont_write_bytecode",
1530 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1531 SET_SYS_FROM_STRING("api_version",
1532 PyLong_FromLong(PYTHON_API_VERSION));
1533 SET_SYS_FROM_STRING("copyright",
1534 PyUnicode_FromString(Py_GetCopyright()));
1535 SET_SYS_FROM_STRING("platform",
1536 PyUnicode_FromString(Py_GetPlatform()));
1537 SET_SYS_FROM_STRING("executable",
1538 PyUnicode_FromWideChar(
1539 Py_GetProgramFullPath(), -1));
1540 SET_SYS_FROM_STRING("prefix",
1541 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1542 SET_SYS_FROM_STRING("exec_prefix",
1543 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
1544 SET_SYS_FROM_STRING("maxsize",
1545 PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1546 SET_SYS_FROM_STRING("float_info",
1547 PyFloat_GetInfo());
1548 SET_SYS_FROM_STRING("int_info",
1549 PyLong_GetInfo());
Mark Dickinsondc787d22010-05-23 13:33:13 +00001550 /* initialize hash_info */
1551 if (Hash_InfoType.tp_name == 0)
1552 PyStructSequence_InitType(&Hash_InfoType, &hash_info_desc);
1553 SET_SYS_FROM_STRING("hash_info",
1554 get_hash_info());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 SET_SYS_FROM_STRING("maxunicode",
1556 PyLong_FromLong(PyUnicode_GetMax()));
1557 SET_SYS_FROM_STRING("builtin_module_names",
1558 list_builtin_module_names());
1559 {
1560 /* Assumes that longs are at least 2 bytes long.
1561 Should be safe! */
1562 unsigned long number = 1;
1563 char *value;
Fred Drake099325e2000-08-14 15:47:03 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 s = (char *) &number;
1566 if (s[0] == 0)
1567 value = "big";
1568 else
1569 value = "little";
1570 SET_SYS_FROM_STRING("byteorder",
1571 PyUnicode_FromString(value));
1572 }
Guido van Rossum8b9ea871996-08-23 18:14:47 +00001573#ifdef MS_COREDLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 SET_SYS_FROM_STRING("dllhandle",
1575 PyLong_FromVoidPtr(PyWin_DLLhModule));
1576 SET_SYS_FROM_STRING("winver",
1577 PyUnicode_FromString(PyWin_DLLVersionString));
Guido van Rossumc606fe11996-04-09 02:37:57 +00001578#endif
Barry Warsaw8cf4eae2010-10-16 01:04:07 +00001579#ifdef ABIFLAGS
1580 SET_SYS_FROM_STRING("abiflags",
1581 PyUnicode_FromString(ABIFLAGS));
1582#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 if (warnoptions == NULL) {
1584 warnoptions = PyList_New(0);
1585 }
1586 else {
1587 Py_INCREF(warnoptions);
1588 }
1589 if (warnoptions != NULL) {
1590 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1591 }
Tim Peters216b78b2006-01-06 02:40:53 +00001592
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001593 v = get_xoptions();
1594 if (v != NULL) {
1595 PyDict_SetItemString(sysdict, "_xoptions", v);
1596 }
1597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 /* version_info */
1599 if (VersionInfoType.tp_name == 0)
1600 PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
1601 SET_SYS_FROM_STRING("version_info", make_version_info());
1602 /* prevent user from creating new instances */
1603 VersionInfoType.tp_init = NULL;
1604 VersionInfoType.tp_new = NULL;
Eric Smith0e5b5622009-02-06 01:32:42 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 /* flags */
1607 if (FlagsType.tp_name == 0)
1608 PyStructSequence_InitType(&FlagsType, &flags_desc);
1609 SET_SYS_FROM_STRING("flags", make_flags());
1610 /* prevent user from creating new instances */
1611 FlagsType.tp_init = NULL;
1612 FlagsType.tp_new = NULL;
Christian Heimesd32ed6f2008-01-14 18:49:24 +00001613
Eric Smithf7bb5782010-01-27 00:44:57 +00001614
1615#if defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 /* getwindowsversion */
1617 if (WindowsVersionType.tp_name == 0)
1618 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc);
1619 /* prevent user from creating new instances */
1620 WindowsVersionType.tp_init = NULL;
1621 WindowsVersionType.tp_new = NULL;
Eric Smithf7bb5782010-01-27 00:44:57 +00001622#endif
1623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001625#ifndef PY_NO_SHORT_FLOAT_REPR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 SET_SYS_FROM_STRING("float_repr_style",
1627 PyUnicode_FromString("short"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001628#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 SET_SYS_FROM_STRING("float_repr_style",
1630 PyUnicode_FromString("legacy"));
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001631#endif
1632
Christian Heimes7b3ce6a2008-01-31 14:31:45 +00001633#undef SET_SYS_FROM_STRING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 if (PyErr_Occurred())
1635 return NULL;
1636 return m;
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001637}
1638
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001639static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001640makepathobject(const wchar_t *path, wchar_t delim)
Guido van Rossum5b3138b1990-11-18 17:41:40 +00001641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 int i, n;
1643 const wchar_t *p;
1644 PyObject *v, *w;
Tim Peters216b78b2006-01-06 02:40:53 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 n = 1;
1647 p = path;
1648 while ((p = wcschr(p, delim)) != NULL) {
1649 n++;
1650 p++;
1651 }
1652 v = PyList_New(n);
1653 if (v == NULL)
1654 return NULL;
1655 for (i = 0; ; i++) {
1656 p = wcschr(path, delim);
1657 if (p == NULL)
1658 p = path + wcslen(path); /* End of string */
1659 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
1660 if (w == NULL) {
1661 Py_DECREF(v);
1662 return NULL;
1663 }
1664 PyList_SetItem(v, i, w);
1665 if (*p == '\0')
1666 break;
1667 path = p+1;
1668 }
1669 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001670}
1671
1672void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001673PySys_SetPath(const wchar_t *path)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 PyObject *v;
1676 if ((v = makepathobject(path, DELIM)) == NULL)
1677 Py_FatalError("can't create sys.path");
1678 if (PySys_SetObject("path", v) != 0)
1679 Py_FatalError("can't assign sys.path");
1680 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001681}
1682
Guido van Rossum65bf9f21997-04-29 18:33:38 +00001683static PyObject *
Martin v. Löwis790465f2008-04-05 20:41:37 +00001684makeargvobject(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 PyObject *av;
1687 if (argc <= 0 || argv == NULL) {
1688 /* Ensure at least one (empty) argument is seen */
1689 static wchar_t *empty_argv[1] = {L""};
1690 argv = empty_argv;
1691 argc = 1;
1692 }
1693 av = PyList_New(argc);
1694 if (av != NULL) {
1695 int i;
1696 for (i = 0; i < argc; i++) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001697#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 PyObject *v;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* argv[0] is the script pathname if known */
1701 if (i == 0) {
1702 char* fn = decc$translate_vms(argv[0]);
1703 if ((fn == (char *)0) || fn == (char *)-1)
1704 v = PyUnicode_FromString(argv[0]);
1705 else
1706 v = PyUnicode_FromString(
1707 decc$translate_vms(argv[0]));
1708 } else
1709 v = PyUnicode_FromString(argv[i]);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001710#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001712#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 if (v == NULL) {
1714 Py_DECREF(av);
1715 av = NULL;
1716 break;
1717 }
1718 PyList_SetItem(av, i, v);
1719 }
1720 }
1721 return av;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001722}
1723
Nick Coghland26c18a2010-08-17 13:06:11 +00001724#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
1725 (argc > 0 && argv0 != NULL && \
1726 wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001727
1728static void
1729sys_update_path(int argc, wchar_t **argv)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001730{
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001731 wchar_t *argv0;
1732 wchar_t *p = NULL;
1733 Py_ssize_t n = 0;
1734 PyObject *a;
1735 PyObject *path;
1736#ifdef HAVE_READLINK
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001737 wchar_t link[MAXPATHLEN+1];
1738 wchar_t argv0copy[2*MAXPATHLEN+1];
1739 int nr = 0;
1740#endif
Guido van Rossum162e38c2003-02-19 15:25:10 +00001741#if defined(HAVE_REALPATH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 wchar_t fullpath[MAXPATHLEN];
Martin v. Löwisec59d042009-01-12 07:59:10 +00001743#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 wchar_t fullpath[MAX_PATH];
Thomas Heller27bb71e2003-01-08 14:33:48 +00001745#endif
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001746
1747 path = PySys_GetObject("path");
1748 if (path == NULL)
1749 return;
1750
1751 if (argc == 0)
1752 return;
1753 argv0 = argv[0];
1754
1755#ifdef HAVE_READLINK
1756 if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
1757 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
1758 if (nr > 0) {
1759 /* It's a symlink */
1760 link[nr] = '\0';
1761 if (link[0] == SEP)
1762 argv0 = link; /* Link to absolute path */
1763 else if (wcschr(link, SEP) == NULL)
1764 ; /* Link without path */
1765 else {
1766 /* Must join(dirname(argv0), link) */
1767 wchar_t *q = wcsrchr(argv0, SEP);
1768 if (q == NULL)
1769 argv0 = link; /* argv0 without path */
1770 else {
1771 /* Must make a copy */
1772 wcscpy(argv0copy, argv0);
1773 q = wcsrchr(argv0copy, SEP);
1774 wcscpy(q+1, link);
1775 argv0 = argv0copy;
1776 }
1777 }
1778 }
1779#endif /* HAVE_READLINK */
1780#if SEP == '\\' /* Special case for MS filename syntax */
1781 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1782 wchar_t *q;
1783#if defined(MS_WINDOWS) && !defined(MS_WINCE)
1784 /* This code here replaces the first element in argv with the full
1785 path that it represents. Under CE, there are no relative paths so
1786 the argument must be the full path anyway. */
1787 wchar_t *ptemp;
1788 if (GetFullPathNameW(argv0,
1789 sizeof(fullpath)/sizeof(fullpath[0]),
1790 fullpath,
1791 &ptemp)) {
1792 argv0 = fullpath;
1793 }
1794#endif
1795 p = wcsrchr(argv0, SEP);
1796 /* Test for alternate separator */
1797 q = wcsrchr(p ? p : argv0, '/');
1798 if (q != NULL)
1799 p = q;
1800 if (p != NULL) {
1801 n = p + 1 - argv0;
1802 if (n > 1 && p[-1] != ':')
1803 n--; /* Drop trailing separator */
1804 }
1805 }
1806#else /* All other filename syntaxes */
1807 if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
1808#if defined(HAVE_REALPATH)
Victor Stinner015f4d82010-10-07 22:29:53 +00001809 if (_Py_wrealpath(argv0, fullpath, PATH_MAX)) {
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001810 argv0 = fullpath;
1811 }
1812#endif
1813 p = wcsrchr(argv0, SEP);
1814 }
1815 if (p != NULL) {
1816 n = p + 1 - argv0;
1817#if SEP == '/' /* Special case for Unix filename syntax */
1818 if (n > 1)
1819 n--; /* Drop trailing separator */
1820#endif /* Unix */
1821 }
1822#endif /* All others */
1823 a = PyUnicode_FromWideChar(argv0, n);
1824 if (a == NULL)
1825 Py_FatalError("no mem for sys.path insertion");
1826 if (PyList_Insert(path, 0, a) < 0)
1827 Py_FatalError("sys.path.insert(0) failed");
1828 Py_DECREF(a);
1829}
1830
1831void
1832PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
1833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 PyObject *av = makeargvobject(argc, argv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (av == NULL)
1836 Py_FatalError("no mem for sys.argv");
1837 if (PySys_SetObject("argv", av) != 0)
1838 Py_FatalError("can't assign sys.argv");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 Py_DECREF(av);
Victor Stinnerc08ec9f2010-10-06 22:44:06 +00001840 if (updatepath)
1841 sys_update_path(argc, argv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001842}
Guido van Rossuma890e681998-05-12 14:59:24 +00001843
Antoine Pitrouf978fac2010-05-21 17:25:34 +00001844void
1845PySys_SetArgv(int argc, wchar_t **argv)
1846{
1847 PySys_SetArgvEx(argc, argv, 1);
1848}
1849
Victor Stinner14284c22010-04-23 12:02:30 +00001850/* Reimplementation of PyFile_WriteString() no calling indirectly
1851 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
1852
1853static int
Victor Stinner79766632010-08-16 17:36:42 +00001854sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
Victor Stinner14284c22010-04-23 12:02:30 +00001855{
Victor Stinner79766632010-08-16 17:36:42 +00001856 PyObject *writer = NULL, *args = NULL, *result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 int err;
Victor Stinner14284c22010-04-23 12:02:30 +00001858
Victor Stinnerecccc4f2010-06-08 20:46:00 +00001859 if (file == NULL)
1860 return -1;
1861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 writer = PyObject_GetAttrString(file, "write");
1863 if (writer == NULL)
1864 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 args = PyTuple_Pack(1, unicode);
1867 if (args == NULL)
1868 goto error;
Victor Stinner14284c22010-04-23 12:02:30 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 result = PyEval_CallObject(writer, args);
1871 if (result == NULL) {
1872 goto error;
1873 } else {
1874 err = 0;
1875 goto finally;
1876 }
Victor Stinner14284c22010-04-23 12:02:30 +00001877
1878error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 err = -1;
Victor Stinner14284c22010-04-23 12:02:30 +00001880finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 Py_XDECREF(writer);
1882 Py_XDECREF(args);
1883 Py_XDECREF(result);
1884 return err;
Victor Stinner14284c22010-04-23 12:02:30 +00001885}
1886
Victor Stinner79766632010-08-16 17:36:42 +00001887static int
1888sys_pyfile_write(const char *text, PyObject *file)
1889{
1890 PyObject *unicode = NULL;
1891 int err;
1892
1893 if (file == NULL)
1894 return -1;
1895
1896 unicode = PyUnicode_FromString(text);
1897 if (unicode == NULL)
1898 return -1;
1899
1900 err = sys_pyfile_write_unicode(unicode, file);
1901 Py_DECREF(unicode);
1902 return err;
1903}
Guido van Rossuma890e681998-05-12 14:59:24 +00001904
1905/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1906 Adapted from code submitted by Just van Rossum.
1907
1908 PySys_WriteStdout(format, ...)
1909 PySys_WriteStderr(format, ...)
1910
1911 The first function writes to sys.stdout; the second to sys.stderr. When
1912 there is a problem, they write to the real (C level) stdout or stderr;
Guido van Rossum8442af31998-10-12 18:22:10 +00001913 no exceptions are raised.
Guido van Rossuma890e681998-05-12 14:59:24 +00001914
Victor Stinner14284c22010-04-23 12:02:30 +00001915 PyErr_CheckSignals() is not called to avoid the execution of the Python
Victor Stinner79766632010-08-16 17:36:42 +00001916 signal handlers: they may raise a new exception whereas sys_write()
1917 ignores all exceptions.
Victor Stinner14284c22010-04-23 12:02:30 +00001918
Guido van Rossuma890e681998-05-12 14:59:24 +00001919 Both take a printf-style format string as their first argument followed
1920 by a variable length argument list determined by the format string.
1921
1922 *** WARNING ***
1923
1924 The format should limit the total size of the formatted output string to
1925 1000 bytes. In particular, this means that no unrestricted "%s" formats
1926 should occur; these should be limited using "%.<N>s where <N> is a
1927 decimal number calculated so that <N> plus the maximum size of other
1928 formatted text does not exceed 1000 bytes. Also watch out for "%f",
1929 which can print hundreds of digits for very large numbers.
1930
1931 */
1932
1933static void
Victor Stinner79766632010-08-16 17:36:42 +00001934sys_write(char *name, FILE *fp, const char *format, va_list va)
Guido van Rossuma890e681998-05-12 14:59:24 +00001935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 PyObject *file;
1937 PyObject *error_type, *error_value, *error_traceback;
1938 char buffer[1001];
1939 int written;
Guido van Rossuma890e681998-05-12 14:59:24 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1942 file = PySys_GetObject(name);
1943 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
1944 if (sys_pyfile_write(buffer, file) != 0) {
1945 PyErr_Clear();
1946 fputs(buffer, fp);
1947 }
1948 if (written < 0 || (size_t)written >= sizeof(buffer)) {
1949 const char *truncated = "... truncated";
Victor Stinner79766632010-08-16 17:36:42 +00001950 if (sys_pyfile_write(truncated, file) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 fputs(truncated, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 }
1953 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossuma890e681998-05-12 14:59:24 +00001954}
1955
1956void
Guido van Rossuma890e681998-05-12 14:59:24 +00001957PySys_WriteStdout(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001962 sys_write("stdout", stdout, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00001964}
1965
1966void
Guido van Rossuma890e681998-05-12 14:59:24 +00001967PySys_WriteStderr(const char *format, ...)
Guido van Rossuma890e681998-05-12 14:59:24 +00001968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 va_list va;
Guido van Rossuma890e681998-05-12 14:59:24 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 va_start(va, format);
Victor Stinner79766632010-08-16 17:36:42 +00001972 sys_write("stderr", stderr, format, va);
1973 va_end(va);
1974}
1975
1976static void
1977sys_format(char *name, FILE *fp, const char *format, va_list va)
1978{
1979 PyObject *file, *message;
1980 PyObject *error_type, *error_value, *error_traceback;
1981 char *utf8;
1982
1983 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1984 file = PySys_GetObject(name);
1985 message = PyUnicode_FromFormatV(format, va);
1986 if (message != NULL) {
1987 if (sys_pyfile_write_unicode(message, file) != 0) {
1988 PyErr_Clear();
1989 utf8 = _PyUnicode_AsString(message);
1990 if (utf8 != NULL)
1991 fputs(utf8, fp);
1992 }
1993 Py_DECREF(message);
1994 }
1995 PyErr_Restore(error_type, error_value, error_traceback);
1996}
1997
1998void
1999PySys_FormatStdout(const char *format, ...)
2000{
2001 va_list va;
2002
2003 va_start(va, format);
2004 sys_format("stdout", stdout, format, va);
2005 va_end(va);
2006}
2007
2008void
2009PySys_FormatStderr(const char *format, ...)
2010{
2011 va_list va;
2012
2013 va_start(va, format);
2014 sys_format("stderr", stderr, format, va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 va_end(va);
Guido van Rossuma890e681998-05-12 14:59:24 +00002016}