1 |
|
---|
2 | /* System module */
|
---|
3 |
|
---|
4 | /*
|
---|
5 | Various bits of information used by the interpreter are collected in
|
---|
6 | module 'sys'.
|
---|
7 | Function member:
|
---|
8 | - exit(sts): raise SystemExit
|
---|
9 | Data members:
|
---|
10 | - stdin, stdout, stderr: standard file objects
|
---|
11 | - modules: the table of modules (dictionary)
|
---|
12 | - path: module search path (list of strings)
|
---|
13 | - argv: script arguments (list of strings)
|
---|
14 | - ps1, ps2: optional primary and secondary prompts (strings)
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include "Python.h"
|
---|
18 | #include "code.h"
|
---|
19 | #include "frameobject.h"
|
---|
20 | #include "eval.h"
|
---|
21 |
|
---|
22 | #include "osdefs.h"
|
---|
23 |
|
---|
24 | #ifdef MS_WINDOWS
|
---|
25 | #define WIN32_LEAN_AND_MEAN
|
---|
26 | #include "windows.h"
|
---|
27 | #endif /* MS_WINDOWS */
|
---|
28 |
|
---|
29 | #ifdef MS_COREDLL
|
---|
30 | extern void *PyWin_DLLhModule;
|
---|
31 | /* A string loaded from the DLL at startup: */
|
---|
32 | extern const char *PyWin_DLLVersionString;
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #ifdef __VMS
|
---|
36 | #include <unixlib.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #ifdef MS_WINDOWS
|
---|
40 | #include <windows.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #ifdef HAVE_LANGINFO_H
|
---|
44 | #include <locale.h>
|
---|
45 | #include <langinfo.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | PyObject *
|
---|
49 | PySys_GetObject(char *name)
|
---|
50 | {
|
---|
51 | PyThreadState *tstate = PyThreadState_GET();
|
---|
52 | PyObject *sd = tstate->interp->sysdict;
|
---|
53 | if (sd == NULL)
|
---|
54 | return NULL;
|
---|
55 | return PyDict_GetItemString(sd, name);
|
---|
56 | }
|
---|
57 |
|
---|
58 | FILE *
|
---|
59 | PySys_GetFile(char *name, FILE *def)
|
---|
60 | {
|
---|
61 | FILE *fp = NULL;
|
---|
62 | PyObject *v = PySys_GetObject(name);
|
---|
63 | if (v != NULL && PyFile_Check(v))
|
---|
64 | fp = PyFile_AsFile(v);
|
---|
65 | if (fp == NULL)
|
---|
66 | fp = def;
|
---|
67 | return fp;
|
---|
68 | }
|
---|
69 |
|
---|
70 | int
|
---|
71 | PySys_SetObject(char *name, PyObject *v)
|
---|
72 | {
|
---|
73 | PyThreadState *tstate = PyThreadState_GET();
|
---|
74 | PyObject *sd = tstate->interp->sysdict;
|
---|
75 | if (v == NULL) {
|
---|
76 | if (PyDict_GetItemString(sd, name) == NULL)
|
---|
77 | return 0;
|
---|
78 | else
|
---|
79 | return PyDict_DelItemString(sd, name);
|
---|
80 | }
|
---|
81 | else
|
---|
82 | return PyDict_SetItemString(sd, name, v);
|
---|
83 | }
|
---|
84 |
|
---|
85 | static PyObject *
|
---|
86 | sys_displayhook(PyObject *self, PyObject *o)
|
---|
87 | {
|
---|
88 | PyObject *outf;
|
---|
89 | PyInterpreterState *interp = PyThreadState_GET()->interp;
|
---|
90 | PyObject *modules = interp->modules;
|
---|
91 | PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
|
---|
92 |
|
---|
93 | if (builtins == NULL) {
|
---|
94 | PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* Print value except if None */
|
---|
99 | /* After printing, also assign to '_' */
|
---|
100 | /* Before, set '_' to None to avoid recursion */
|
---|
101 | if (o == Py_None) {
|
---|
102 | Py_INCREF(Py_None);
|
---|
103 | return Py_None;
|
---|
104 | }
|
---|
105 | if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
|
---|
106 | return NULL;
|
---|
107 | if (Py_FlushLine() != 0)
|
---|
108 | return NULL;
|
---|
109 | outf = PySys_GetObject("stdout");
|
---|
110 | if (outf == NULL) {
|
---|
111 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
|
---|
112 | return NULL;
|
---|
113 | }
|
---|
114 | if (PyFile_WriteObject(o, outf, 0) != 0)
|
---|
115 | return NULL;
|
---|
116 | PyFile_SoftSpace(outf, 1);
|
---|
117 | if (Py_FlushLine() != 0)
|
---|
118 | return NULL;
|
---|
119 | if (PyObject_SetAttrString(builtins, "_", o) != 0)
|
---|
120 | return NULL;
|
---|
121 | Py_INCREF(Py_None);
|
---|
122 | return Py_None;
|
---|
123 | }
|
---|
124 |
|
---|
125 | PyDoc_STRVAR(displayhook_doc,
|
---|
126 | "displayhook(object) -> None\n"
|
---|
127 | "\n"
|
---|
128 | "Print an object to sys.stdout and also save it in __builtin__.\n"
|
---|
129 | );
|
---|
130 |
|
---|
131 | static PyObject *
|
---|
132 | sys_excepthook(PyObject* self, PyObject* args)
|
---|
133 | {
|
---|
134 | PyObject *exc, *value, *tb;
|
---|
135 | if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
|
---|
136 | return NULL;
|
---|
137 | PyErr_Display(exc, value, tb);
|
---|
138 | Py_INCREF(Py_None);
|
---|
139 | return Py_None;
|
---|
140 | }
|
---|
141 |
|
---|
142 | PyDoc_STRVAR(excepthook_doc,
|
---|
143 | "excepthook(exctype, value, traceback) -> None\n"
|
---|
144 | "\n"
|
---|
145 | "Handle an exception by displaying it with a traceback on sys.stderr.\n"
|
---|
146 | );
|
---|
147 |
|
---|
148 | static PyObject *
|
---|
149 | sys_exc_info(PyObject *self, PyObject *noargs)
|
---|
150 | {
|
---|
151 | PyThreadState *tstate;
|
---|
152 | tstate = PyThreadState_GET();
|
---|
153 | return Py_BuildValue(
|
---|
154 | "(OOO)",
|
---|
155 | tstate->exc_type != NULL ? tstate->exc_type : Py_None,
|
---|
156 | tstate->exc_value != NULL ? tstate->exc_value : Py_None,
|
---|
157 | tstate->exc_traceback != NULL ?
|
---|
158 | tstate->exc_traceback : Py_None);
|
---|
159 | }
|
---|
160 |
|
---|
161 | PyDoc_STRVAR(exc_info_doc,
|
---|
162 | "exc_info() -> (type, value, traceback)\n\
|
---|
163 | \n\
|
---|
164 | Return information about the most recent exception caught by an except\n\
|
---|
165 | clause in the current stack frame or in an older stack frame."
|
---|
166 | );
|
---|
167 |
|
---|
168 | static PyObject *
|
---|
169 | sys_exc_clear(PyObject *self, PyObject *noargs)
|
---|
170 | {
|
---|
171 | PyThreadState *tstate = PyThreadState_GET();
|
---|
172 | PyObject *tmp_type, *tmp_value, *tmp_tb;
|
---|
173 | tmp_type = tstate->exc_type;
|
---|
174 | tmp_value = tstate->exc_value;
|
---|
175 | tmp_tb = tstate->exc_traceback;
|
---|
176 | tstate->exc_type = NULL;
|
---|
177 | tstate->exc_value = NULL;
|
---|
178 | tstate->exc_traceback = NULL;
|
---|
179 | Py_XDECREF(tmp_type);
|
---|
180 | Py_XDECREF(tmp_value);
|
---|
181 | Py_XDECREF(tmp_tb);
|
---|
182 | /* For b/w compatibility */
|
---|
183 | PySys_SetObject("exc_type", Py_None);
|
---|
184 | PySys_SetObject("exc_value", Py_None);
|
---|
185 | PySys_SetObject("exc_traceback", Py_None);
|
---|
186 | Py_INCREF(Py_None);
|
---|
187 | return Py_None;
|
---|
188 | }
|
---|
189 |
|
---|
190 | PyDoc_STRVAR(exc_clear_doc,
|
---|
191 | "exc_clear() -> None\n\
|
---|
192 | \n\
|
---|
193 | Clear global information on the current exception. Subsequent calls to\n\
|
---|
194 | exc_info() will return (None,None,None) until another exception is raised\n\
|
---|
195 | in the current thread or the execution stack returns to a frame where\n\
|
---|
196 | another exception is being handled."
|
---|
197 | );
|
---|
198 |
|
---|
199 | static PyObject *
|
---|
200 | sys_exit(PyObject *self, PyObject *args)
|
---|
201 | {
|
---|
202 | PyObject *exit_code = 0;
|
---|
203 | if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
|
---|
204 | return NULL;
|
---|
205 | /* Raise SystemExit so callers may catch it or clean up. */
|
---|
206 | PyErr_SetObject(PyExc_SystemExit, exit_code);
|
---|
207 | return NULL;
|
---|
208 | }
|
---|
209 |
|
---|
210 | PyDoc_STRVAR(exit_doc,
|
---|
211 | "exit([status])\n\
|
---|
212 | \n\
|
---|
213 | Exit the interpreter by raising SystemExit(status).\n\
|
---|
214 | If the status is omitted or None, it defaults to zero (i.e., success).\n\
|
---|
215 | If the status is numeric, it will be used as the system exit status.\n\
|
---|
216 | If it is another kind of object, it will be printed and the system\n\
|
---|
217 | exit status will be one (i.e., failure)."
|
---|
218 | );
|
---|
219 |
|
---|
220 | #ifdef Py_USING_UNICODE
|
---|
221 |
|
---|
222 | static PyObject *
|
---|
223 | sys_getdefaultencoding(PyObject *self)
|
---|
224 | {
|
---|
225 | return PyString_FromString(PyUnicode_GetDefaultEncoding());
|
---|
226 | }
|
---|
227 |
|
---|
228 | PyDoc_STRVAR(getdefaultencoding_doc,
|
---|
229 | "getdefaultencoding() -> string\n\
|
---|
230 | \n\
|
---|
231 | Return the current default string encoding used by the Unicode \n\
|
---|
232 | implementation."
|
---|
233 | );
|
---|
234 |
|
---|
235 | static PyObject *
|
---|
236 | sys_setdefaultencoding(PyObject *self, PyObject *args)
|
---|
237 | {
|
---|
238 | char *encoding;
|
---|
239 | if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
|
---|
240 | return NULL;
|
---|
241 | if (PyUnicode_SetDefaultEncoding(encoding))
|
---|
242 | return NULL;
|
---|
243 | Py_INCREF(Py_None);
|
---|
244 | return Py_None;
|
---|
245 | }
|
---|
246 |
|
---|
247 | PyDoc_STRVAR(setdefaultencoding_doc,
|
---|
248 | "setdefaultencoding(encoding)\n\
|
---|
249 | \n\
|
---|
250 | Set the current default string encoding used by the Unicode implementation."
|
---|
251 | );
|
---|
252 |
|
---|
253 | static PyObject *
|
---|
254 | sys_getfilesystemencoding(PyObject *self)
|
---|
255 | {
|
---|
256 | if (Py_FileSystemDefaultEncoding)
|
---|
257 | return PyString_FromString(Py_FileSystemDefaultEncoding);
|
---|
258 | Py_INCREF(Py_None);
|
---|
259 | return Py_None;
|
---|
260 | }
|
---|
261 |
|
---|
262 | PyDoc_STRVAR(getfilesystemencoding_doc,
|
---|
263 | "getfilesystemencoding() -> string\n\
|
---|
264 | \n\
|
---|
265 | Return the encoding used to convert Unicode filenames in\n\
|
---|
266 | operating system filenames."
|
---|
267 | );
|
---|
268 |
|
---|
269 | #endif
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * Cached interned string objects used for calling the profile and
|
---|
273 | * trace functions. Initialized by trace_init().
|
---|
274 | */
|
---|
275 | static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
|
---|
276 |
|
---|
277 | static int
|
---|
278 | trace_init(void)
|
---|
279 | {
|
---|
280 | static char *whatnames[7] = {"call", "exception", "line", "return",
|
---|
281 | "c_call", "c_exception", "c_return"};
|
---|
282 | PyObject *name;
|
---|
283 | int i;
|
---|
284 | for (i = 0; i < 7; ++i) {
|
---|
285 | if (whatstrings[i] == NULL) {
|
---|
286 | name = PyString_InternFromString(whatnames[i]);
|
---|
287 | if (name == NULL)
|
---|
288 | return -1;
|
---|
289 | whatstrings[i] = name;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | return 0;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | static PyObject *
|
---|
297 | call_trampoline(PyThreadState *tstate, PyObject* callback,
|
---|
298 | PyFrameObject *frame, int what, PyObject *arg)
|
---|
299 | {
|
---|
300 | PyObject *args = PyTuple_New(3);
|
---|
301 | PyObject *whatstr;
|
---|
302 | PyObject *result;
|
---|
303 |
|
---|
304 | if (args == NULL)
|
---|
305 | return NULL;
|
---|
306 | Py_INCREF(frame);
|
---|
307 | whatstr = whatstrings[what];
|
---|
308 | Py_INCREF(whatstr);
|
---|
309 | if (arg == NULL)
|
---|
310 | arg = Py_None;
|
---|
311 | Py_INCREF(arg);
|
---|
312 | PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
|
---|
313 | PyTuple_SET_ITEM(args, 1, whatstr);
|
---|
314 | PyTuple_SET_ITEM(args, 2, arg);
|
---|
315 |
|
---|
316 | /* call the Python-level function */
|
---|
317 | PyFrame_FastToLocals(frame);
|
---|
318 | result = PyEval_CallObject(callback, args);
|
---|
319 | PyFrame_LocalsToFast(frame, 1);
|
---|
320 | if (result == NULL)
|
---|
321 | PyTraceBack_Here(frame);
|
---|
322 |
|
---|
323 | /* cleanup */
|
---|
324 | Py_DECREF(args);
|
---|
325 | return result;
|
---|
326 | }
|
---|
327 |
|
---|
328 | static int
|
---|
329 | profile_trampoline(PyObject *self, PyFrameObject *frame,
|
---|
330 | int what, PyObject *arg)
|
---|
331 | {
|
---|
332 | PyThreadState *tstate = frame->f_tstate;
|
---|
333 | PyObject *result;
|
---|
334 |
|
---|
335 | if (arg == NULL)
|
---|
336 | arg = Py_None;
|
---|
337 | result = call_trampoline(tstate, self, frame, what, arg);
|
---|
338 | if (result == NULL) {
|
---|
339 | PyEval_SetProfile(NULL, NULL);
|
---|
340 | return -1;
|
---|
341 | }
|
---|
342 | Py_DECREF(result);
|
---|
343 | return 0;
|
---|
344 | }
|
---|
345 |
|
---|
346 | static int
|
---|
347 | trace_trampoline(PyObject *self, PyFrameObject *frame,
|
---|
348 | int what, PyObject *arg)
|
---|
349 | {
|
---|
350 | PyThreadState *tstate = frame->f_tstate;
|
---|
351 | PyObject *callback;
|
---|
352 | PyObject *result;
|
---|
353 |
|
---|
354 | if (what == PyTrace_CALL)
|
---|
355 | callback = self;
|
---|
356 | else
|
---|
357 | callback = frame->f_trace;
|
---|
358 | if (callback == NULL)
|
---|
359 | return 0;
|
---|
360 | result = call_trampoline(tstate, callback, frame, what, arg);
|
---|
361 | if (result == NULL) {
|
---|
362 | PyEval_SetTrace(NULL, NULL);
|
---|
363 | Py_XDECREF(frame->f_trace);
|
---|
364 | frame->f_trace = NULL;
|
---|
365 | return -1;
|
---|
366 | }
|
---|
367 | if (result != Py_None) {
|
---|
368 | PyObject *temp = frame->f_trace;
|
---|
369 | frame->f_trace = NULL;
|
---|
370 | Py_XDECREF(temp);
|
---|
371 | frame->f_trace = result;
|
---|
372 | }
|
---|
373 | else {
|
---|
374 | Py_DECREF(result);
|
---|
375 | }
|
---|
376 | return 0;
|
---|
377 | }
|
---|
378 |
|
---|
379 | static PyObject *
|
---|
380 | sys_settrace(PyObject *self, PyObject *args)
|
---|
381 | {
|
---|
382 | if (trace_init() == -1)
|
---|
383 | return NULL;
|
---|
384 | if (args == Py_None)
|
---|
385 | PyEval_SetTrace(NULL, NULL);
|
---|
386 | else
|
---|
387 | PyEval_SetTrace(trace_trampoline, args);
|
---|
388 | Py_INCREF(Py_None);
|
---|
389 | return Py_None;
|
---|
390 | }
|
---|
391 |
|
---|
392 | PyDoc_STRVAR(settrace_doc,
|
---|
393 | "settrace(function)\n\
|
---|
394 | \n\
|
---|
395 | Set the global debug tracing function. It will be called on each\n\
|
---|
396 | function call. See the debugger chapter in the library manual."
|
---|
397 | );
|
---|
398 |
|
---|
399 | static PyObject *
|
---|
400 | sys_setprofile(PyObject *self, PyObject *args)
|
---|
401 | {
|
---|
402 | if (trace_init() == -1)
|
---|
403 | return NULL;
|
---|
404 | if (args == Py_None)
|
---|
405 | PyEval_SetProfile(NULL, NULL);
|
---|
406 | else
|
---|
407 | PyEval_SetProfile(profile_trampoline, args);
|
---|
408 | Py_INCREF(Py_None);
|
---|
409 | return Py_None;
|
---|
410 | }
|
---|
411 |
|
---|
412 | PyDoc_STRVAR(setprofile_doc,
|
---|
413 | "setprofile(function)\n\
|
---|
414 | \n\
|
---|
415 | Set the profiling function. It will be called on each function call\n\
|
---|
416 | and return. See the profiler chapter in the library manual."
|
---|
417 | );
|
---|
418 |
|
---|
419 | static PyObject *
|
---|
420 | sys_setcheckinterval(PyObject *self, PyObject *args)
|
---|
421 | {
|
---|
422 | if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
|
---|
423 | return NULL;
|
---|
424 | Py_INCREF(Py_None);
|
---|
425 | return Py_None;
|
---|
426 | }
|
---|
427 |
|
---|
428 | PyDoc_STRVAR(setcheckinterval_doc,
|
---|
429 | "setcheckinterval(n)\n\
|
---|
430 | \n\
|
---|
431 | Tell the Python interpreter to check for asynchronous events every\n\
|
---|
432 | n instructions. This also affects how often thread switches occur."
|
---|
433 | );
|
---|
434 |
|
---|
435 | static PyObject *
|
---|
436 | sys_getcheckinterval(PyObject *self, PyObject *args)
|
---|
437 | {
|
---|
438 | return PyInt_FromLong(_Py_CheckInterval);
|
---|
439 | }
|
---|
440 |
|
---|
441 | PyDoc_STRVAR(getcheckinterval_doc,
|
---|
442 | "getcheckinterval() -> current check interval; see setcheckinterval()."
|
---|
443 | );
|
---|
444 |
|
---|
445 | #ifdef WITH_TSC
|
---|
446 | static PyObject *
|
---|
447 | sys_settscdump(PyObject *self, PyObject *args)
|
---|
448 | {
|
---|
449 | int bool;
|
---|
450 | PyThreadState *tstate = PyThreadState_Get();
|
---|
451 |
|
---|
452 | if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
|
---|
453 | return NULL;
|
---|
454 | if (bool)
|
---|
455 | tstate->interp->tscdump = 1;
|
---|
456 | else
|
---|
457 | tstate->interp->tscdump = 0;
|
---|
458 | Py_INCREF(Py_None);
|
---|
459 | return Py_None;
|
---|
460 |
|
---|
461 | }
|
---|
462 |
|
---|
463 | PyDoc_STRVAR(settscdump_doc,
|
---|
464 | "settscdump(bool)\n\
|
---|
465 | \n\
|
---|
466 | If true, tell the Python interpreter to dump VM measurements to\n\
|
---|
467 | stderr. If false, turn off dump. The measurements are based on the\n\
|
---|
468 | processor's time-stamp counter."
|
---|
469 | );
|
---|
470 | #endif /* TSC */
|
---|
471 |
|
---|
472 | static PyObject *
|
---|
473 | sys_setrecursionlimit(PyObject *self, PyObject *args)
|
---|
474 | {
|
---|
475 | int new_limit;
|
---|
476 | if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
|
---|
477 | return NULL;
|
---|
478 | if (new_limit <= 0) {
|
---|
479 | PyErr_SetString(PyExc_ValueError,
|
---|
480 | "recursion limit must be positive");
|
---|
481 | return NULL;
|
---|
482 | }
|
---|
483 | Py_SetRecursionLimit(new_limit);
|
---|
484 | Py_INCREF(Py_None);
|
---|
485 | return Py_None;
|
---|
486 | }
|
---|
487 |
|
---|
488 | PyDoc_STRVAR(setrecursionlimit_doc,
|
---|
489 | "setrecursionlimit(n)\n\
|
---|
490 | \n\
|
---|
491 | Set the maximum depth of the Python interpreter stack to n. This\n\
|
---|
492 | limit prevents infinite recursion from causing an overflow of the C\n\
|
---|
493 | stack and crashing Python. The highest possible limit is platform-\n\
|
---|
494 | dependent."
|
---|
495 | );
|
---|
496 |
|
---|
497 | static PyObject *
|
---|
498 | sys_getrecursionlimit(PyObject *self)
|
---|
499 | {
|
---|
500 | return PyInt_FromLong(Py_GetRecursionLimit());
|
---|
501 | }
|
---|
502 |
|
---|
503 | PyDoc_STRVAR(getrecursionlimit_doc,
|
---|
504 | "getrecursionlimit()\n\
|
---|
505 | \n\
|
---|
506 | Return the current value of the recursion limit, the maximum depth\n\
|
---|
507 | of the Python interpreter stack. This limit prevents infinite\n\
|
---|
508 | recursion from causing an overflow of the C stack and crashing Python."
|
---|
509 | );
|
---|
510 |
|
---|
511 | #ifdef MS_WINDOWS
|
---|
512 | PyDoc_STRVAR(getwindowsversion_doc,
|
---|
513 | "getwindowsversion()\n\
|
---|
514 | \n\
|
---|
515 | Return information about the running version of Windows.\n\
|
---|
516 | The result is a tuple of (major, minor, build, platform, text)\n\
|
---|
517 | All elements are numbers, except text which is a string.\n\
|
---|
518 | Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
|
---|
519 | "
|
---|
520 | );
|
---|
521 |
|
---|
522 | static PyObject *
|
---|
523 | sys_getwindowsversion(PyObject *self)
|
---|
524 | {
|
---|
525 | OSVERSIONINFO ver;
|
---|
526 | ver.dwOSVersionInfoSize = sizeof(ver);
|
---|
527 | if (!GetVersionEx(&ver))
|
---|
528 | return PyErr_SetFromWindowsErr(0);
|
---|
529 | return Py_BuildValue("HHHHs",
|
---|
530 | ver.dwMajorVersion,
|
---|
531 | ver.dwMinorVersion,
|
---|
532 | ver.dwBuildNumber,
|
---|
533 | ver.dwPlatformId,
|
---|
534 | ver.szCSDVersion);
|
---|
535 | }
|
---|
536 |
|
---|
537 | #endif /* MS_WINDOWS */
|
---|
538 |
|
---|
539 | #ifdef HAVE_DLOPEN
|
---|
540 | static PyObject *
|
---|
541 | sys_setdlopenflags(PyObject *self, PyObject *args)
|
---|
542 | {
|
---|
543 | int new_val;
|
---|
544 | PyThreadState *tstate = PyThreadState_GET();
|
---|
545 | if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
|
---|
546 | return NULL;
|
---|
547 | if (!tstate)
|
---|
548 | return NULL;
|
---|
549 | tstate->interp->dlopenflags = new_val;
|
---|
550 | Py_INCREF(Py_None);
|
---|
551 | return Py_None;
|
---|
552 | }
|
---|
553 |
|
---|
554 | PyDoc_STRVAR(setdlopenflags_doc,
|
---|
555 | "setdlopenflags(n) -> None\n\
|
---|
556 | \n\
|
---|
557 | Set the flags that will be used for dlopen() calls. Among other\n\
|
---|
558 | things, this will enable a lazy resolving of symbols when importing\n\
|
---|
559 | a module, if called as sys.setdlopenflags(0)\n\
|
---|
560 | To share symbols across extension modules, call as\n\
|
---|
561 | sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"
|
---|
562 | );
|
---|
563 |
|
---|
564 | static PyObject *
|
---|
565 | sys_getdlopenflags(PyObject *self, PyObject *args)
|
---|
566 | {
|
---|
567 | PyThreadState *tstate = PyThreadState_GET();
|
---|
568 | if (!tstate)
|
---|
569 | return NULL;
|
---|
570 | return PyInt_FromLong(tstate->interp->dlopenflags);
|
---|
571 | }
|
---|
572 |
|
---|
573 | PyDoc_STRVAR(getdlopenflags_doc,
|
---|
574 | "getdlopenflags() -> int\n\
|
---|
575 | \n\
|
---|
576 | Return the current value of the flags that are used for dlopen()\n\
|
---|
577 | calls. The flag constants are defined in the dl module."
|
---|
578 | );
|
---|
579 | #endif
|
---|
580 |
|
---|
581 | #ifdef USE_MALLOPT
|
---|
582 | /* Link with -lmalloc (or -lmpc) on an SGI */
|
---|
583 | #include <malloc.h>
|
---|
584 |
|
---|
585 | static PyObject *
|
---|
586 | sys_mdebug(PyObject *self, PyObject *args)
|
---|
587 | {
|
---|
588 | int flag;
|
---|
589 | if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
|
---|
590 | return NULL;
|
---|
591 | mallopt(M_DEBUG, flag);
|
---|
592 | Py_INCREF(Py_None);
|
---|
593 | return Py_None;
|
---|
594 | }
|
---|
595 | #endif /* USE_MALLOPT */
|
---|
596 |
|
---|
597 | static PyObject *
|
---|
598 | sys_getrefcount(PyObject *self, PyObject *arg)
|
---|
599 | {
|
---|
600 | return PyInt_FromSsize_t(arg->ob_refcnt);
|
---|
601 | }
|
---|
602 |
|
---|
603 | #ifdef Py_REF_DEBUG
|
---|
604 | static PyObject *
|
---|
605 | sys_gettotalrefcount(PyObject *self)
|
---|
606 | {
|
---|
607 | return PyInt_FromSsize_t(_Py_GetRefTotal());
|
---|
608 | }
|
---|
609 | #endif /* Py_REF_DEBUG */
|
---|
610 |
|
---|
611 | PyDoc_STRVAR(getrefcount_doc,
|
---|
612 | "getrefcount(object) -> integer\n\
|
---|
613 | \n\
|
---|
614 | Return the reference count of object. The count returned is generally\n\
|
---|
615 | one higher than you might expect, because it includes the (temporary)\n\
|
---|
616 | reference as an argument to getrefcount()."
|
---|
617 | );
|
---|
618 |
|
---|
619 | #ifdef COUNT_ALLOCS
|
---|
620 | static PyObject *
|
---|
621 | sys_getcounts(PyObject *self)
|
---|
622 | {
|
---|
623 | extern PyObject *get_counts(void);
|
---|
624 |
|
---|
625 | return get_counts();
|
---|
626 | }
|
---|
627 | #endif
|
---|
628 |
|
---|
629 | PyDoc_STRVAR(getframe_doc,
|
---|
630 | "_getframe([depth]) -> frameobject\n\
|
---|
631 | \n\
|
---|
632 | Return a frame object from the call stack. If optional integer depth is\n\
|
---|
633 | given, return the frame object that many calls below the top of the stack.\n\
|
---|
634 | If that is deeper than the call stack, ValueError is raised. The default\n\
|
---|
635 | for depth is zero, returning the frame at the top of the call stack.\n\
|
---|
636 | \n\
|
---|
637 | This function should be used for internal and specialized\n\
|
---|
638 | purposes only."
|
---|
639 | );
|
---|
640 |
|
---|
641 | static PyObject *
|
---|
642 | sys_getframe(PyObject *self, PyObject *args)
|
---|
643 | {
|
---|
644 | PyFrameObject *f = PyThreadState_GET()->frame;
|
---|
645 | int depth = -1;
|
---|
646 |
|
---|
647 | if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
|
---|
648 | return NULL;
|
---|
649 |
|
---|
650 | while (depth > 0 && f != NULL) {
|
---|
651 | f = f->f_back;
|
---|
652 | --depth;
|
---|
653 | }
|
---|
654 | if (f == NULL) {
|
---|
655 | PyErr_SetString(PyExc_ValueError,
|
---|
656 | "call stack is not deep enough");
|
---|
657 | return NULL;
|
---|
658 | }
|
---|
659 | Py_INCREF(f);
|
---|
660 | return (PyObject*)f;
|
---|
661 | }
|
---|
662 |
|
---|
663 | PyDoc_STRVAR(current_frames_doc,
|
---|
664 | "_current_frames() -> dictionary\n\
|
---|
665 | \n\
|
---|
666 | Return a dictionary mapping each current thread T's thread id to T's\n\
|
---|
667 | current stack frame.\n\
|
---|
668 | \n\
|
---|
669 | This function should be used for specialized purposes only."
|
---|
670 | );
|
---|
671 |
|
---|
672 | static PyObject *
|
---|
673 | sys_current_frames(PyObject *self, PyObject *noargs)
|
---|
674 | {
|
---|
675 | return _PyThread_CurrentFrames();
|
---|
676 | }
|
---|
677 |
|
---|
678 | PyDoc_STRVAR(call_tracing_doc,
|
---|
679 | "call_tracing(func, args) -> object\n\
|
---|
680 | \n\
|
---|
681 | Call func(*args), while tracing is enabled. The tracing state is\n\
|
---|
682 | saved, and restored afterwards. This is intended to be called from\n\
|
---|
683 | a debugger from a checkpoint, to recursively debug some other code."
|
---|
684 | );
|
---|
685 |
|
---|
686 | static PyObject *
|
---|
687 | sys_call_tracing(PyObject *self, PyObject *args)
|
---|
688 | {
|
---|
689 | PyObject *func, *funcargs;
|
---|
690 | if (!PyArg_UnpackTuple(args, "call_tracing", 2, 2, &func, &funcargs))
|
---|
691 | return NULL;
|
---|
692 | return _PyEval_CallTracing(func, funcargs);
|
---|
693 | }
|
---|
694 |
|
---|
695 | PyDoc_STRVAR(callstats_doc,
|
---|
696 | "callstats() -> tuple of integers\n\
|
---|
697 | \n\
|
---|
698 | Return a tuple of function call statistics, if CALL_PROFILE was defined\n\
|
---|
699 | when Python was built. Otherwise, return None.\n\
|
---|
700 | \n\
|
---|
701 | When enabled, this function returns detailed, implementation-specific\n\
|
---|
702 | details about the number of function calls executed. The return value is\n\
|
---|
703 | a 11-tuple where the entries in the tuple are counts of:\n\
|
---|
704 | 0. all function calls\n\
|
---|
705 | 1. calls to PyFunction_Type objects\n\
|
---|
706 | 2. PyFunction calls that do not create an argument tuple\n\
|
---|
707 | 3. PyFunction calls that do not create an argument tuple\n\
|
---|
708 | and bypass PyEval_EvalCodeEx()\n\
|
---|
709 | 4. PyMethod calls\n\
|
---|
710 | 5. PyMethod calls on bound methods\n\
|
---|
711 | 6. PyType calls\n\
|
---|
712 | 7. PyCFunction calls\n\
|
---|
713 | 8. generator calls\n\
|
---|
714 | 9. All other calls\n\
|
---|
715 | 10. Number of stack pops performed by call_function()"
|
---|
716 | );
|
---|
717 |
|
---|
718 | #ifdef __cplusplus
|
---|
719 | extern "C" {
|
---|
720 | #endif
|
---|
721 |
|
---|
722 | #ifdef Py_TRACE_REFS
|
---|
723 | /* Defined in objects.c because it uses static globals if that file */
|
---|
724 | extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
|
---|
725 | #endif
|
---|
726 |
|
---|
727 | #ifdef DYNAMIC_EXECUTION_PROFILE
|
---|
728 | /* Defined in ceval.c because it uses static globals if that file */
|
---|
729 | extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
|
---|
730 | #endif
|
---|
731 |
|
---|
732 | #ifdef __cplusplus
|
---|
733 | }
|
---|
734 | #endif
|
---|
735 |
|
---|
736 | static PyMethodDef sys_methods[] = {
|
---|
737 | /* Might as well keep this in alphabetic order */
|
---|
738 | {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
|
---|
739 | callstats_doc},
|
---|
740 | {"_current_frames", sys_current_frames, METH_NOARGS,
|
---|
741 | current_frames_doc},
|
---|
742 | {"displayhook", sys_displayhook, METH_O, displayhook_doc},
|
---|
743 | {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
|
---|
744 | {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
|
---|
745 | {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
|
---|
746 | {"exit", sys_exit, METH_VARARGS, exit_doc},
|
---|
747 | #ifdef Py_USING_UNICODE
|
---|
748 | {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
|
---|
749 | METH_NOARGS, getdefaultencoding_doc},
|
---|
750 | #endif
|
---|
751 | #ifdef HAVE_DLOPEN
|
---|
752 | {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
|
---|
753 | getdlopenflags_doc},
|
---|
754 | #endif
|
---|
755 | #ifdef COUNT_ALLOCS
|
---|
756 | {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
|
---|
757 | #endif
|
---|
758 | #ifdef DYNAMIC_EXECUTION_PROFILE
|
---|
759 | {"getdxp", _Py_GetDXProfile, METH_VARARGS},
|
---|
760 | #endif
|
---|
761 | #ifdef Py_USING_UNICODE
|
---|
762 | {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
|
---|
763 | METH_NOARGS, getfilesystemencoding_doc},
|
---|
764 | #endif
|
---|
765 | #ifdef Py_TRACE_REFS
|
---|
766 | {"getobjects", _Py_GetObjects, METH_VARARGS},
|
---|
767 | #endif
|
---|
768 | #ifdef Py_REF_DEBUG
|
---|
769 | {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
|
---|
770 | #endif
|
---|
771 | {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
|
---|
772 | {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
|
---|
773 | getrecursionlimit_doc},
|
---|
774 | {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
|
---|
775 | #ifdef MS_WINDOWS
|
---|
776 | {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
|
---|
777 | getwindowsversion_doc},
|
---|
778 | #endif /* MS_WINDOWS */
|
---|
779 | #ifdef USE_MALLOPT
|
---|
780 | {"mdebug", sys_mdebug, METH_VARARGS},
|
---|
781 | #endif
|
---|
782 | #ifdef Py_USING_UNICODE
|
---|
783 | {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
|
---|
784 | setdefaultencoding_doc},
|
---|
785 | #endif
|
---|
786 | {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
|
---|
787 | setcheckinterval_doc},
|
---|
788 | {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
|
---|
789 | getcheckinterval_doc},
|
---|
790 | #ifdef HAVE_DLOPEN
|
---|
791 | {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
|
---|
792 | setdlopenflags_doc},
|
---|
793 | #endif
|
---|
794 | {"setprofile", sys_setprofile, METH_O, setprofile_doc},
|
---|
795 | {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
|
---|
796 | setrecursionlimit_doc},
|
---|
797 | #ifdef WITH_TSC
|
---|
798 | {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
|
---|
799 | #endif
|
---|
800 | {"settrace", sys_settrace, METH_O, settrace_doc},
|
---|
801 | {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
|
---|
802 | {NULL, NULL} /* sentinel */
|
---|
803 | };
|
---|
804 |
|
---|
805 | static PyObject *
|
---|
806 | list_builtin_module_names(void)
|
---|
807 | {
|
---|
808 | PyObject *list = PyList_New(0);
|
---|
809 | int i;
|
---|
810 | if (list == NULL)
|
---|
811 | return NULL;
|
---|
812 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
|
---|
813 | PyObject *name = PyString_FromString(
|
---|
814 | PyImport_Inittab[i].name);
|
---|
815 | if (name == NULL)
|
---|
816 | break;
|
---|
817 | PyList_Append(list, name);
|
---|
818 | Py_DECREF(name);
|
---|
819 | }
|
---|
820 | if (PyList_Sort(list) != 0) {
|
---|
821 | Py_DECREF(list);
|
---|
822 | list = NULL;
|
---|
823 | }
|
---|
824 | if (list) {
|
---|
825 | PyObject *v = PyList_AsTuple(list);
|
---|
826 | Py_DECREF(list);
|
---|
827 | list = v;
|
---|
828 | }
|
---|
829 | return list;
|
---|
830 | }
|
---|
831 |
|
---|
832 | static PyObject *warnoptions = NULL;
|
---|
833 |
|
---|
834 | void
|
---|
835 | PySys_ResetWarnOptions(void)
|
---|
836 | {
|
---|
837 | if (warnoptions == NULL || !PyList_Check(warnoptions))
|
---|
838 | return;
|
---|
839 | PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
|
---|
840 | }
|
---|
841 |
|
---|
842 | void
|
---|
843 | PySys_AddWarnOption(char *s)
|
---|
844 | {
|
---|
845 | PyObject *str;
|
---|
846 |
|
---|
847 | if (warnoptions == NULL || !PyList_Check(warnoptions)) {
|
---|
848 | Py_XDECREF(warnoptions);
|
---|
849 | warnoptions = PyList_New(0);
|
---|
850 | if (warnoptions == NULL)
|
---|
851 | return;
|
---|
852 | }
|
---|
853 | str = PyString_FromString(s);
|
---|
854 | if (str != NULL) {
|
---|
855 | PyList_Append(warnoptions, str);
|
---|
856 | Py_DECREF(str);
|
---|
857 | }
|
---|
858 | }
|
---|
859 |
|
---|
860 | /* XXX This doc string is too long to be a single string literal in VC++ 5.0.
|
---|
861 | Two literals concatenated works just fine. If you have a K&R compiler
|
---|
862 | or other abomination that however *does* understand longer strings,
|
---|
863 | get rid of the !!! comment in the middle and the quotes that surround it. */
|
---|
864 | PyDoc_VAR(sys_doc) =
|
---|
865 | PyDoc_STR(
|
---|
866 | "This module provides access to some objects used or maintained by the\n\
|
---|
867 | interpreter and to functions that interact strongly with the interpreter.\n\
|
---|
868 | \n\
|
---|
869 | Dynamic objects:\n\
|
---|
870 | \n\
|
---|
871 | argv -- command line arguments; argv[0] is the script pathname if known\n\
|
---|
872 | path -- module search path; path[0] is the script directory, else ''\n\
|
---|
873 | modules -- dictionary of loaded modules\n\
|
---|
874 | \n\
|
---|
875 | displayhook -- called to show results in an interactive session\n\
|
---|
876 | excepthook -- called to handle any uncaught exception other than SystemExit\n\
|
---|
877 | To customize printing in an interactive session or to install a custom\n\
|
---|
878 | top-level exception handler, assign other functions to replace these.\n\
|
---|
879 | \n\
|
---|
880 | exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
|
---|
881 | Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
|
---|
882 | \n\
|
---|
883 | stdin -- standard input file object; used by raw_input() and input()\n\
|
---|
884 | stdout -- standard output file object; used by the print statement\n\
|
---|
885 | stderr -- standard error object; used for error messages\n\
|
---|
886 | By assigning other file objects (or objects that behave like files)\n\
|
---|
887 | to these, it is possible to redirect all of the interpreter's I/O.\n\
|
---|
888 | \n\
|
---|
889 | last_type -- type of last uncaught exception\n\
|
---|
890 | last_value -- value of last uncaught exception\n\
|
---|
891 | last_traceback -- traceback of last uncaught exception\n\
|
---|
892 | These three are only available in an interactive session after a\n\
|
---|
893 | traceback has been printed.\n\
|
---|
894 | \n\
|
---|
895 | exc_type -- type of exception currently being handled\n\
|
---|
896 | exc_value -- value of exception currently being handled\n\
|
---|
897 | exc_traceback -- traceback of exception currently being handled\n\
|
---|
898 | The function exc_info() should be used instead of these three,\n\
|
---|
899 | because it is thread-safe.\n\
|
---|
900 | "
|
---|
901 | )
|
---|
902 | /* concatenating string here */
|
---|
903 | PyDoc_STR(
|
---|
904 | "\n\
|
---|
905 | Static objects:\n\
|
---|
906 | \n\
|
---|
907 | maxint -- the largest supported integer (the smallest is -maxint-1)\n\
|
---|
908 | maxunicode -- the largest supported character\n\
|
---|
909 | builtin_module_names -- tuple of module names built into this interpreter\n\
|
---|
910 | version -- the version of this interpreter as a string\n\
|
---|
911 | version_info -- version information as a tuple\n\
|
---|
912 | hexversion -- version information encoded as a single integer\n\
|
---|
913 | copyright -- copyright notice pertaining to this interpreter\n\
|
---|
914 | platform -- platform identifier\n\
|
---|
915 | executable -- pathname of this Python interpreter\n\
|
---|
916 | prefix -- prefix used to find the Python library\n\
|
---|
917 | exec_prefix -- prefix used to find the machine-specific Python library\n\
|
---|
918 | "
|
---|
919 | )
|
---|
920 | #ifdef MS_WINDOWS
|
---|
921 | /* concatenating string here */
|
---|
922 | PyDoc_STR(
|
---|
923 | "dllhandle -- [Windows only] integer handle of the Python DLL\n\
|
---|
924 | winver -- [Windows only] version number of the Python DLL\n\
|
---|
925 | "
|
---|
926 | )
|
---|
927 | #endif /* MS_WINDOWS */
|
---|
928 | PyDoc_STR(
|
---|
929 | "__stdin__ -- the original stdin; don't touch!\n\
|
---|
930 | __stdout__ -- the original stdout; don't touch!\n\
|
---|
931 | __stderr__ -- the original stderr; don't touch!\n\
|
---|
932 | __displayhook__ -- the original displayhook; don't touch!\n\
|
---|
933 | __excepthook__ -- the original excepthook; don't touch!\n\
|
---|
934 | \n\
|
---|
935 | Functions:\n\
|
---|
936 | \n\
|
---|
937 | displayhook() -- print an object to the screen, and save it in __builtin__._\n\
|
---|
938 | excepthook() -- print an exception and its traceback to sys.stderr\n\
|
---|
939 | exc_info() -- return thread-safe information about the current exception\n\
|
---|
940 | exc_clear() -- clear the exception state for the current thread\n\
|
---|
941 | exit() -- exit the interpreter by raising SystemExit\n\
|
---|
942 | getdlopenflags() -- returns flags to be used for dlopen() calls\n\
|
---|
943 | getrefcount() -- return the reference count for an object (plus one :-)\n\
|
---|
944 | getrecursionlimit() -- return the max recursion depth for the interpreter\n\
|
---|
945 | setcheckinterval() -- control how often the interpreter checks for events\n\
|
---|
946 | setdlopenflags() -- set the flags to be used for dlopen() calls\n\
|
---|
947 | setprofile() -- set the global profiling function\n\
|
---|
948 | setrecursionlimit() -- set the max recursion depth for the interpreter\n\
|
---|
949 | settrace() -- set the global debug tracing function\n\
|
---|
950 | "
|
---|
951 | )
|
---|
952 | /* end of sys_doc */ ;
|
---|
953 |
|
---|
954 | static int
|
---|
955 | _check_and_flush (FILE *stream)
|
---|
956 | {
|
---|
957 | int prev_fail = ferror (stream);
|
---|
958 | return fflush (stream) || prev_fail ? EOF : 0;
|
---|
959 | }
|
---|
960 |
|
---|
961 | /* Subversion branch and revision management */
|
---|
962 | static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
|
---|
963 | static const char headurl[] = "$HeadURL: svn+ssh://pythondev@svn.python.org/python/tags/r25/Python/sysmodule.c $";
|
---|
964 | static int svn_initialized;
|
---|
965 | static char patchlevel_revision[50]; /* Just the number */
|
---|
966 | static char branch[50];
|
---|
967 | static char shortbranch[50];
|
---|
968 | static const char *svn_revision;
|
---|
969 |
|
---|
970 | static void
|
---|
971 | svnversion_init(void)
|
---|
972 | {
|
---|
973 | const char *python, *br_start, *br_end, *br_end2, *svnversion;
|
---|
974 | Py_ssize_t len;
|
---|
975 | int istag;
|
---|
976 |
|
---|
977 | if (svn_initialized)
|
---|
978 | return;
|
---|
979 |
|
---|
980 | python = strstr(headurl, "/python/");
|
---|
981 | if (!python)
|
---|
982 | Py_FatalError("subversion keywords missing");
|
---|
983 |
|
---|
984 | br_start = python + 8;
|
---|
985 | br_end = strchr(br_start, '/');
|
---|
986 | /* Works even for trunk,
|
---|
987 | as we are in trunk/Python/sysmodule.c */
|
---|
988 | br_end2 = strchr(br_end+1, '/');
|
---|
989 |
|
---|
990 | istag = strncmp(br_start, "tags", 4) == 0;
|
---|
991 | if (strncmp(br_start, "trunk", 5) == 0) {
|
---|
992 | strcpy(branch, "trunk");
|
---|
993 | strcpy(shortbranch, "trunk");
|
---|
994 |
|
---|
995 | }
|
---|
996 | else if (istag || strncmp(br_start, "branches", 8) == 0) {
|
---|
997 | len = br_end2 - br_start;
|
---|
998 | strncpy(branch, br_start, len);
|
---|
999 | branch[len] = '\0';
|
---|
1000 |
|
---|
1001 | len = br_end2 - (br_end + 1);
|
---|
1002 | strncpy(shortbranch, br_end + 1, len);
|
---|
1003 | shortbranch[len] = '\0';
|
---|
1004 | }
|
---|
1005 | else {
|
---|
1006 | Py_FatalError("bad HeadURL");
|
---|
1007 | return;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 |
|
---|
1011 | svnversion = _Py_svnversion();
|
---|
1012 | if (strcmp(svnversion, "exported") != 0)
|
---|
1013 | svn_revision = svnversion;
|
---|
1014 | else if (istag) {
|
---|
1015 | len = strlen(_patchlevel_revision);
|
---|
1016 | strncpy(patchlevel_revision, _patchlevel_revision + 11,
|
---|
1017 | len - 13);
|
---|
1018 | patchlevel_revision[len - 13] = '\0';
|
---|
1019 | svn_revision = patchlevel_revision;
|
---|
1020 | }
|
---|
1021 | else
|
---|
1022 | svn_revision = "";
|
---|
1023 |
|
---|
1024 | svn_initialized = 1;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | /* Return svnversion output if available.
|
---|
1028 | Else return Revision of patchlevel.h if on branch.
|
---|
1029 | Else return empty string */
|
---|
1030 | const char*
|
---|
1031 | Py_SubversionRevision()
|
---|
1032 | {
|
---|
1033 | svnversion_init();
|
---|
1034 | return svn_revision;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | const char*
|
---|
1038 | Py_SubversionShortBranch()
|
---|
1039 | {
|
---|
1040 | svnversion_init();
|
---|
1041 | return shortbranch;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | PyObject *
|
---|
1045 | _PySys_Init(void)
|
---|
1046 | {
|
---|
1047 | PyObject *m, *v, *sysdict;
|
---|
1048 | PyObject *sysin, *sysout, *syserr;
|
---|
1049 | char *s;
|
---|
1050 | #ifdef MS_WINDOWS
|
---|
1051 | char buf[128];
|
---|
1052 | #endif
|
---|
1053 |
|
---|
1054 | m = Py_InitModule3("sys", sys_methods, sys_doc);
|
---|
1055 | if (m == NULL)
|
---|
1056 | return NULL;
|
---|
1057 | sysdict = PyModule_GetDict(m);
|
---|
1058 |
|
---|
1059 | {
|
---|
1060 | /* XXX: does this work on Win/Win64? (see posix_fstat) */
|
---|
1061 | struct stat sb;
|
---|
1062 | if (fstat(fileno(stdin), &sb) == 0 &&
|
---|
1063 | S_ISDIR(sb.st_mode)) {
|
---|
1064 | /* There's nothing more we can do. */
|
---|
1065 | /* Py_FatalError() will core dump, so just exit. */
|
---|
1066 | PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
|
---|
1067 | exit(EXIT_FAILURE);
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /* Closing the standard FILE* if sys.std* goes aways causes problems
|
---|
1072 | * for embedded Python usages. Closing them when somebody explicitly
|
---|
1073 | * invokes .close() might be possible, but the FAQ promises they get
|
---|
1074 | * never closed. However, we still need to get write errors when
|
---|
1075 | * writing fails (e.g. because stdout is redirected), so we flush the
|
---|
1076 | * streams and check for errors before the file objects are deleted.
|
---|
1077 | * On OS X, fflush()ing stdin causes an error, so we exempt stdin
|
---|
1078 | * from that procedure.
|
---|
1079 | */
|
---|
1080 | sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
|
---|
1081 | sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
|
---|
1082 | syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
|
---|
1083 | if (PyErr_Occurred())
|
---|
1084 | return NULL;
|
---|
1085 | #ifdef MS_WINDOWS
|
---|
1086 | if(isatty(_fileno(stdin))){
|
---|
1087 | sprintf(buf, "cp%d", GetConsoleCP());
|
---|
1088 | if (!PyFile_SetEncoding(sysin, buf))
|
---|
1089 | return NULL;
|
---|
1090 | }
|
---|
1091 | if(isatty(_fileno(stdout))) {
|
---|
1092 | sprintf(buf, "cp%d", GetConsoleOutputCP());
|
---|
1093 | if (!PyFile_SetEncoding(sysout, buf))
|
---|
1094 | return NULL;
|
---|
1095 | }
|
---|
1096 | if(isatty(_fileno(stderr))) {
|
---|
1097 | sprintf(buf, "cp%d", GetConsoleOutputCP());
|
---|
1098 | if (!PyFile_SetEncoding(syserr, buf))
|
---|
1099 | return NULL;
|
---|
1100 | }
|
---|
1101 | #endif
|
---|
1102 |
|
---|
1103 | PyDict_SetItemString(sysdict, "stdin", sysin);
|
---|
1104 | PyDict_SetItemString(sysdict, "stdout", sysout);
|
---|
1105 | PyDict_SetItemString(sysdict, "stderr", syserr);
|
---|
1106 | /* Make backup copies for cleanup */
|
---|
1107 | PyDict_SetItemString(sysdict, "__stdin__", sysin);
|
---|
1108 | PyDict_SetItemString(sysdict, "__stdout__", sysout);
|
---|
1109 | PyDict_SetItemString(sysdict, "__stderr__", syserr);
|
---|
1110 | PyDict_SetItemString(sysdict, "__displayhook__",
|
---|
1111 | PyDict_GetItemString(sysdict, "displayhook"));
|
---|
1112 | PyDict_SetItemString(sysdict, "__excepthook__",
|
---|
1113 | PyDict_GetItemString(sysdict, "excepthook"));
|
---|
1114 | Py_XDECREF(sysin);
|
---|
1115 | Py_XDECREF(sysout);
|
---|
1116 | Py_XDECREF(syserr);
|
---|
1117 | PyDict_SetItemString(sysdict, "version",
|
---|
1118 | v = PyString_FromString(Py_GetVersion()));
|
---|
1119 | Py_XDECREF(v);
|
---|
1120 | PyDict_SetItemString(sysdict, "hexversion",
|
---|
1121 | v = PyInt_FromLong(PY_VERSION_HEX));
|
---|
1122 | Py_XDECREF(v);
|
---|
1123 | svnversion_init();
|
---|
1124 | v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision);
|
---|
1125 | PyDict_SetItemString(sysdict, "subversion", v);
|
---|
1126 | Py_XDECREF(v);
|
---|
1127 | /*
|
---|
1128 | * These release level checks are mutually exclusive and cover
|
---|
1129 | * the field, so don't get too fancy with the pre-processor!
|
---|
1130 | */
|
---|
1131 | #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
|
---|
1132 | s = "alpha";
|
---|
1133 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
|
---|
1134 | s = "beta";
|
---|
1135 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
|
---|
1136 | s = "candidate";
|
---|
1137 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
|
---|
1138 | s = "final";
|
---|
1139 | #endif
|
---|
1140 |
|
---|
1141 | #define SET_SYS_FROM_STRING(key, value) \
|
---|
1142 | v = value; \
|
---|
1143 | if (v != NULL) \
|
---|
1144 | PyDict_SetItemString(sysdict, key, v); \
|
---|
1145 | Py_XDECREF(v)
|
---|
1146 |
|
---|
1147 | SET_SYS_FROM_STRING("version_info",
|
---|
1148 | Py_BuildValue("iiisi", PY_MAJOR_VERSION,
|
---|
1149 | PY_MINOR_VERSION,
|
---|
1150 | PY_MICRO_VERSION, s,
|
---|
1151 | PY_RELEASE_SERIAL));
|
---|
1152 | SET_SYS_FROM_STRING("api_version",
|
---|
1153 | PyInt_FromLong(PYTHON_API_VERSION));
|
---|
1154 | SET_SYS_FROM_STRING("copyright",
|
---|
1155 | PyString_FromString(Py_GetCopyright()));
|
---|
1156 | SET_SYS_FROM_STRING("platform",
|
---|
1157 | PyString_FromString(Py_GetPlatform()));
|
---|
1158 | SET_SYS_FROM_STRING("executable",
|
---|
1159 | PyString_FromString(Py_GetProgramFullPath()));
|
---|
1160 | SET_SYS_FROM_STRING("prefix",
|
---|
1161 | PyString_FromString(Py_GetPrefix()));
|
---|
1162 | SET_SYS_FROM_STRING("exec_prefix",
|
---|
1163 | PyString_FromString(Py_GetExecPrefix()));
|
---|
1164 | SET_SYS_FROM_STRING("maxint",
|
---|
1165 | PyInt_FromLong(PyInt_GetMax()));
|
---|
1166 | #ifdef Py_USING_UNICODE
|
---|
1167 | SET_SYS_FROM_STRING("maxunicode",
|
---|
1168 | PyInt_FromLong(PyUnicode_GetMax()));
|
---|
1169 | #endif
|
---|
1170 | SET_SYS_FROM_STRING("builtin_module_names",
|
---|
1171 | list_builtin_module_names());
|
---|
1172 | {
|
---|
1173 | /* Assumes that longs are at least 2 bytes long.
|
---|
1174 | Should be safe! */
|
---|
1175 | unsigned long number = 1;
|
---|
1176 | char *value;
|
---|
1177 |
|
---|
1178 | s = (char *) &number;
|
---|
1179 | if (s[0] == 0)
|
---|
1180 | value = "big";
|
---|
1181 | else
|
---|
1182 | value = "little";
|
---|
1183 | SET_SYS_FROM_STRING("byteorder",
|
---|
1184 | PyString_FromString(value));
|
---|
1185 | }
|
---|
1186 | #ifdef MS_COREDLL
|
---|
1187 | SET_SYS_FROM_STRING("dllhandle",
|
---|
1188 | PyLong_FromVoidPtr(PyWin_DLLhModule));
|
---|
1189 | SET_SYS_FROM_STRING("winver",
|
---|
1190 | PyString_FromString(PyWin_DLLVersionString));
|
---|
1191 | #endif
|
---|
1192 | #undef SET_SYS_FROM_STRING
|
---|
1193 | if (warnoptions == NULL) {
|
---|
1194 | warnoptions = PyList_New(0);
|
---|
1195 | }
|
---|
1196 | else {
|
---|
1197 | Py_INCREF(warnoptions);
|
---|
1198 | }
|
---|
1199 | if (warnoptions != NULL) {
|
---|
1200 | PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | if (PyErr_Occurred())
|
---|
1204 | return NULL;
|
---|
1205 | return m;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | static PyObject *
|
---|
1209 | makepathobject(char *path, int delim)
|
---|
1210 | {
|
---|
1211 | int i, n;
|
---|
1212 | char *p;
|
---|
1213 | PyObject *v, *w;
|
---|
1214 |
|
---|
1215 | n = 1;
|
---|
1216 | p = path;
|
---|
1217 | while ((p = strchr(p, delim)) != NULL) {
|
---|
1218 | n++;
|
---|
1219 | p++;
|
---|
1220 | }
|
---|
1221 | v = PyList_New(n);
|
---|
1222 | if (v == NULL)
|
---|
1223 | return NULL;
|
---|
1224 | for (i = 0; ; i++) {
|
---|
1225 | p = strchr(path, delim);
|
---|
1226 | if (p == NULL)
|
---|
1227 | p = strchr(path, '\0'); /* End of string */
|
---|
1228 | w = PyString_FromStringAndSize(path, (int) (p - path));
|
---|
1229 | if (w == NULL) {
|
---|
1230 | Py_DECREF(v);
|
---|
1231 | return NULL;
|
---|
1232 | }
|
---|
1233 | PyList_SetItem(v, i, w);
|
---|
1234 | if (*p == '\0')
|
---|
1235 | break;
|
---|
1236 | path = p+1;
|
---|
1237 | }
|
---|
1238 | return v;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | void
|
---|
1242 | PySys_SetPath(char *path)
|
---|
1243 | {
|
---|
1244 | PyObject *v;
|
---|
1245 | if ((v = makepathobject(path, DELIM)) == NULL)
|
---|
1246 | Py_FatalError("can't create sys.path");
|
---|
1247 | if (PySys_SetObject("path", v) != 0)
|
---|
1248 | Py_FatalError("can't assign sys.path");
|
---|
1249 | Py_DECREF(v);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | static PyObject *
|
---|
1253 | makeargvobject(int argc, char **argv)
|
---|
1254 | {
|
---|
1255 | PyObject *av;
|
---|
1256 | if (argc <= 0 || argv == NULL) {
|
---|
1257 | /* Ensure at least one (empty) argument is seen */
|
---|
1258 | static char *empty_argv[1] = {""};
|
---|
1259 | argv = empty_argv;
|
---|
1260 | argc = 1;
|
---|
1261 | }
|
---|
1262 | av = PyList_New(argc);
|
---|
1263 | if (av != NULL) {
|
---|
1264 | int i;
|
---|
1265 | for (i = 0; i < argc; i++) {
|
---|
1266 | #ifdef __VMS
|
---|
1267 | PyObject *v;
|
---|
1268 |
|
---|
1269 | /* argv[0] is the script pathname if known */
|
---|
1270 | if (i == 0) {
|
---|
1271 | char* fn = decc$translate_vms(argv[0]);
|
---|
1272 | if ((fn == (char *)0) || fn == (char *)-1)
|
---|
1273 | v = PyString_FromString(argv[0]);
|
---|
1274 | else
|
---|
1275 | v = PyString_FromString(
|
---|
1276 | decc$translate_vms(argv[0]));
|
---|
1277 | } else
|
---|
1278 | v = PyString_FromString(argv[i]);
|
---|
1279 | #else
|
---|
1280 | PyObject *v = PyString_FromString(argv[i]);
|
---|
1281 | #endif
|
---|
1282 | if (v == NULL) {
|
---|
1283 | Py_DECREF(av);
|
---|
1284 | av = NULL;
|
---|
1285 | break;
|
---|
1286 | }
|
---|
1287 | PyList_SetItem(av, i, v);
|
---|
1288 | }
|
---|
1289 | }
|
---|
1290 | return av;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | void
|
---|
1294 | PySys_SetArgv(int argc, char **argv)
|
---|
1295 | {
|
---|
1296 | #if defined(HAVE_REALPATH)
|
---|
1297 | char fullpath[MAXPATHLEN];
|
---|
1298 | #elif defined(MS_WINDOWS)
|
---|
1299 | char fullpath[MAX_PATH];
|
---|
1300 | #endif
|
---|
1301 | PyObject *av = makeargvobject(argc, argv);
|
---|
1302 | PyObject *path = PySys_GetObject("path");
|
---|
1303 | if (av == NULL)
|
---|
1304 | Py_FatalError("no mem for sys.argv");
|
---|
1305 | if (PySys_SetObject("argv", av) != 0)
|
---|
1306 | Py_FatalError("can't assign sys.argv");
|
---|
1307 | if (path != NULL) {
|
---|
1308 | char *argv0 = argv[0];
|
---|
1309 | char *p = NULL;
|
---|
1310 | Py_ssize_t n = 0;
|
---|
1311 | PyObject *a;
|
---|
1312 | #ifdef HAVE_READLINK
|
---|
1313 | char link[MAXPATHLEN+1];
|
---|
1314 | char argv0copy[2*MAXPATHLEN+1];
|
---|
1315 | int nr = 0;
|
---|
1316 | if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0)
|
---|
1317 | nr = readlink(argv0, link, MAXPATHLEN);
|
---|
1318 | if (nr > 0) {
|
---|
1319 | /* It's a symlink */
|
---|
1320 | link[nr] = '\0';
|
---|
1321 | if (link[0] == SEP)
|
---|
1322 | argv0 = link; /* Link to absolute path */
|
---|
1323 | else if (strchr(link, SEP) == NULL)
|
---|
1324 | ; /* Link without path */
|
---|
1325 | else {
|
---|
1326 | /* Must join(dirname(argv0), link) */
|
---|
1327 | char *q = strrchr(argv0, SEP);
|
---|
1328 | if (q == NULL)
|
---|
1329 | argv0 = link; /* argv0 without path */
|
---|
1330 | else {
|
---|
1331 | /* Must make a copy */
|
---|
1332 | strcpy(argv0copy, argv0);
|
---|
1333 | q = strrchr(argv0copy, SEP);
|
---|
1334 | strcpy(q+1, link);
|
---|
1335 | argv0 = argv0copy;
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 | #endif /* HAVE_READLINK */
|
---|
1340 | #if SEP == '\\' /* Special case for MS filename syntax */
|
---|
1341 | if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
|
---|
1342 | char *q;
|
---|
1343 | #ifdef MS_WINDOWS
|
---|
1344 | char *ptemp;
|
---|
1345 | if (GetFullPathName(argv0,
|
---|
1346 | sizeof(fullpath),
|
---|
1347 | fullpath,
|
---|
1348 | &ptemp)) {
|
---|
1349 | argv0 = fullpath;
|
---|
1350 | }
|
---|
1351 | #endif
|
---|
1352 | p = strrchr(argv0, SEP);
|
---|
1353 | /* Test for alternate separator */
|
---|
1354 | q = strrchr(p ? p : argv0, '/');
|
---|
1355 | if (q != NULL)
|
---|
1356 | p = q;
|
---|
1357 | if (p != NULL) {
|
---|
1358 | n = p + 1 - argv0;
|
---|
1359 | if (n > 1 && p[-1] != ':')
|
---|
1360 | n--; /* Drop trailing separator */
|
---|
1361 | }
|
---|
1362 | }
|
---|
1363 | #else /* All other filename syntaxes */
|
---|
1364 | if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {
|
---|
1365 | #if defined(HAVE_REALPATH)
|
---|
1366 | if (realpath(argv0, fullpath)) {
|
---|
1367 | argv0 = fullpath;
|
---|
1368 | }
|
---|
1369 | #endif
|
---|
1370 | p = strrchr(argv0, SEP);
|
---|
1371 | }
|
---|
1372 | if (p != NULL) {
|
---|
1373 | #ifndef RISCOS
|
---|
1374 | n = p + 1 - argv0;
|
---|
1375 | #else /* don't include trailing separator */
|
---|
1376 | n = p - argv0;
|
---|
1377 | #endif /* RISCOS */
|
---|
1378 | #if SEP == '/' /* Special case for Unix filename syntax */
|
---|
1379 | if (n > 1)
|
---|
1380 | n--; /* Drop trailing separator */
|
---|
1381 | #endif /* Unix */
|
---|
1382 | }
|
---|
1383 | #endif /* All others */
|
---|
1384 | a = PyString_FromStringAndSize(argv0, n);
|
---|
1385 | if (a == NULL)
|
---|
1386 | Py_FatalError("no mem for sys.path insertion");
|
---|
1387 | if (PyList_Insert(path, 0, a) < 0)
|
---|
1388 | Py_FatalError("sys.path.insert(0) failed");
|
---|
1389 | Py_DECREF(a);
|
---|
1390 | }
|
---|
1391 | Py_DECREF(av);
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 |
|
---|
1395 | /* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
|
---|
1396 | Adapted from code submitted by Just van Rossum.
|
---|
1397 |
|
---|
1398 | PySys_WriteStdout(format, ...)
|
---|
1399 | PySys_WriteStderr(format, ...)
|
---|
1400 |
|
---|
1401 | The first function writes to sys.stdout; the second to sys.stderr. When
|
---|
1402 | there is a problem, they write to the real (C level) stdout or stderr;
|
---|
1403 | no exceptions are raised.
|
---|
1404 |
|
---|
1405 | Both take a printf-style format string as their first argument followed
|
---|
1406 | by a variable length argument list determined by the format string.
|
---|
1407 |
|
---|
1408 | *** WARNING ***
|
---|
1409 |
|
---|
1410 | The format should limit the total size of the formatted output string to
|
---|
1411 | 1000 bytes. In particular, this means that no unrestricted "%s" formats
|
---|
1412 | should occur; these should be limited using "%.<N>s where <N> is a
|
---|
1413 | decimal number calculated so that <N> plus the maximum size of other
|
---|
1414 | formatted text does not exceed 1000 bytes. Also watch out for "%f",
|
---|
1415 | which can print hundreds of digits for very large numbers.
|
---|
1416 |
|
---|
1417 | */
|
---|
1418 |
|
---|
1419 | static void
|
---|
1420 | mywrite(char *name, FILE *fp, const char *format, va_list va)
|
---|
1421 | {
|
---|
1422 | PyObject *file;
|
---|
1423 | PyObject *error_type, *error_value, *error_traceback;
|
---|
1424 |
|
---|
1425 | PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
---|
1426 | file = PySys_GetObject(name);
|
---|
1427 | if (file == NULL || PyFile_AsFile(file) == fp)
|
---|
1428 | vfprintf(fp, format, va);
|
---|
1429 | else {
|
---|
1430 | char buffer[1001];
|
---|
1431 | const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
|
---|
1432 | format, va);
|
---|
1433 | if (PyFile_WriteString(buffer, file) != 0) {
|
---|
1434 | PyErr_Clear();
|
---|
1435 | fputs(buffer, fp);
|
---|
1436 | }
|
---|
1437 | if (written < 0 || (size_t)written >= sizeof(buffer)) {
|
---|
1438 | const char *truncated = "... truncated";
|
---|
1439 | if (PyFile_WriteString(truncated, file) != 0) {
|
---|
1440 | PyErr_Clear();
|
---|
1441 | fputs(truncated, fp);
|
---|
1442 | }
|
---|
1443 | }
|
---|
1444 | }
|
---|
1445 | PyErr_Restore(error_type, error_value, error_traceback);
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | void
|
---|
1449 | PySys_WriteStdout(const char *format, ...)
|
---|
1450 | {
|
---|
1451 | va_list va;
|
---|
1452 |
|
---|
1453 | va_start(va, format);
|
---|
1454 | mywrite("stdout", stdout, format, va);
|
---|
1455 | va_end(va);
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | void
|
---|
1459 | PySys_WriteStderr(const char *format, ...)
|
---|
1460 | {
|
---|
1461 | va_list va;
|
---|
1462 |
|
---|
1463 | va_start(va, format);
|
---|
1464 | mywrite("stderr", stderr, format, va);
|
---|
1465 | va_end(va);
|
---|
1466 | }
|
---|