+++ /dev/null
-\r
-/* System module */\r
-\r
-/*\r
-Various bits of information used by the interpreter are collected in\r
-module 'sys'.\r
-Function member:\r
-- exit(sts): raise SystemExit\r
-Data members:\r
-- stdin, stdout, stderr: standard file objects\r
-- modules: the table of modules (dictionary)\r
-- path: module search path (list of strings)\r
-- argv: script arguments (list of strings)\r
-- ps1, ps2: optional primary and secondary prompts (strings)\r
-*/\r
-\r
-#include "Python.h"\r
-#include "structseq.h"\r
-#include "code.h"\r
-#include "frameobject.h"\r
-#include "eval.h"\r
-\r
-#include "osdefs.h"\r
-\r
-#ifdef MS_WINDOWS\r
-#define WIN32_LEAN_AND_MEAN\r
-#include "windows.h"\r
-#endif /* MS_WINDOWS */\r
-\r
-#ifdef MS_COREDLL\r
-extern void *PyWin_DLLhModule;\r
-/* A string loaded from the DLL at startup: */\r
-extern const char *PyWin_DLLVersionString;\r
-#endif\r
-\r
-#ifdef __VMS\r
-#include <unixlib.h>\r
-#endif\r
-\r
-#ifdef MS_WINDOWS\r
-#include <windows.h>\r
-#endif\r
-\r
-#ifdef HAVE_LANGINFO_H\r
-#include <locale.h>\r
-#include <langinfo.h>\r
-#endif\r
-\r
-PyObject *\r
-PySys_GetObject(char *name)\r
-{\r
- PyThreadState *tstate = PyThreadState_GET();\r
- PyObject *sd = tstate->interp->sysdict;\r
- if (sd == NULL)\r
- return NULL;\r
- return PyDict_GetItemString(sd, name);\r
-}\r
-\r
-FILE *\r
-PySys_GetFile(char *name, FILE *def)\r
-{\r
- FILE *fp = NULL;\r
- PyObject *v = PySys_GetObject(name);\r
- if (v != NULL && PyFile_Check(v))\r
- fp = PyFile_AsFile(v);\r
- if (fp == NULL)\r
- fp = def;\r
- return fp;\r
-}\r
-\r
-int\r
-PySys_SetObject(char *name, PyObject *v)\r
-{\r
- PyThreadState *tstate = PyThreadState_GET();\r
- PyObject *sd = tstate->interp->sysdict;\r
- if (v == NULL) {\r
- if (PyDict_GetItemString(sd, name) == NULL)\r
- return 0;\r
- else\r
- return PyDict_DelItemString(sd, name);\r
- }\r
- else\r
- return PyDict_SetItemString(sd, name, v);\r
-}\r
-\r
-static PyObject *\r
-sys_displayhook(PyObject *self, PyObject *o)\r
-{\r
- PyObject *outf;\r
- PyInterpreterState *interp = PyThreadState_GET()->interp;\r
- PyObject *modules = interp->modules;\r
- PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");\r
-\r
- if (builtins == NULL) {\r
- PyErr_SetString(PyExc_RuntimeError, "lost __builtin__");\r
- return NULL;\r
- }\r
-\r
- /* Print value except if None */\r
- /* After printing, also assign to '_' */\r
- /* Before, set '_' to None to avoid recursion */\r
- if (o == Py_None) {\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
- }\r
- if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)\r
- return NULL;\r
- if (Py_FlushLine() != 0)\r
- return NULL;\r
- outf = PySys_GetObject("stdout");\r
- if (outf == NULL) {\r
- PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");\r
- return NULL;\r
- }\r
- if (PyFile_WriteObject(o, outf, 0) != 0)\r
- return NULL;\r
- PyFile_SoftSpace(outf, 1);\r
- if (Py_FlushLine() != 0)\r
- return NULL;\r
- if (PyObject_SetAttrString(builtins, "_", o) != 0)\r
- return NULL;\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(displayhook_doc,\r
-"displayhook(object) -> None\n"\r
-"\n"\r
-"Print an object to sys.stdout and also save it in __builtin__._\n"\r
-);\r
-\r
-static PyObject *\r
-sys_excepthook(PyObject* self, PyObject* args)\r
-{\r
- PyObject *exc, *value, *tb;\r
- if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))\r
- return NULL;\r
- PyErr_Display(exc, value, tb);\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(excepthook_doc,\r
-"excepthook(exctype, value, traceback) -> None\n"\r
-"\n"\r
-"Handle an exception by displaying it with a traceback on sys.stderr.\n"\r
-);\r
-\r
-static PyObject *\r
-sys_exc_info(PyObject *self, PyObject *noargs)\r
-{\r
- PyThreadState *tstate;\r
- tstate = PyThreadState_GET();\r
- return Py_BuildValue(\r
- "(OOO)",\r
- tstate->exc_type != NULL ? tstate->exc_type : Py_None,\r
- tstate->exc_value != NULL ? tstate->exc_value : Py_None,\r
- tstate->exc_traceback != NULL ?\r
- tstate->exc_traceback : Py_None);\r
-}\r
-\r
-PyDoc_STRVAR(exc_info_doc,\r
-"exc_info() -> (type, value, traceback)\n\\r
-\n\\r
-Return information about the most recent exception caught by an except\n\\r
-clause in the current stack frame or in an older stack frame."\r
-);\r
-\r
-static PyObject *\r
-sys_exc_clear(PyObject *self, PyObject *noargs)\r
-{\r
- PyThreadState *tstate;\r
- PyObject *tmp_type, *tmp_value, *tmp_tb;\r
-\r
- if (PyErr_WarnPy3k("sys.exc_clear() not supported in 3.x; "\r
- "use except clauses", 1) < 0)\r
- return NULL;\r
-\r
- tstate = PyThreadState_GET();\r
- tmp_type = tstate->exc_type;\r
- tmp_value = tstate->exc_value;\r
- tmp_tb = tstate->exc_traceback;\r
- tstate->exc_type = NULL;\r
- tstate->exc_value = NULL;\r
- tstate->exc_traceback = NULL;\r
- Py_XDECREF(tmp_type);\r
- Py_XDECREF(tmp_value);\r
- Py_XDECREF(tmp_tb);\r
- /* For b/w compatibility */\r
- PySys_SetObject("exc_type", Py_None);\r
- PySys_SetObject("exc_value", Py_None);\r
- PySys_SetObject("exc_traceback", Py_None);\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(exc_clear_doc,\r
-"exc_clear() -> None\n\\r
-\n\\r
-Clear global information on the current exception. Subsequent calls to\n\\r
-exc_info() will return (None,None,None) until another exception is raised\n\\r
-in the current thread or the execution stack returns to a frame where\n\\r
-another exception is being handled."\r
-);\r
-\r
-static PyObject *\r
-sys_exit(PyObject *self, PyObject *args)\r
-{\r
- PyObject *exit_code = 0;\r
- if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))\r
- return NULL;\r
- /* Raise SystemExit so callers may catch it or clean up. */\r
- PyErr_SetObject(PyExc_SystemExit, exit_code);\r
- return NULL;\r
-}\r
-\r
-PyDoc_STRVAR(exit_doc,\r
-"exit([status])\n\\r
-\n\\r
-Exit the interpreter by raising SystemExit(status).\n\\r
-If the status is omitted or None, it defaults to zero (i.e., success).\n\\r
-If the status is an integer, it will be used as the system exit status.\n\\r
-If it is another kind of object, it will be printed and the system\n\\r
-exit status will be one (i.e., failure)."\r
-);\r
-\r
-#ifdef Py_USING_UNICODE\r
-\r
-static PyObject *\r
-sys_getdefaultencoding(PyObject *self)\r
-{\r
- return PyString_FromString(PyUnicode_GetDefaultEncoding());\r
-}\r
-\r
-PyDoc_STRVAR(getdefaultencoding_doc,\r
-"getdefaultencoding() -> string\n\\r
-\n\\r
-Return the current default string encoding used by the Unicode \n\\r
-implementation."\r
-);\r
-\r
-static PyObject *\r
-sys_setdefaultencoding(PyObject *self, PyObject *args)\r
-{\r
- char *encoding;\r
- if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))\r
- return NULL;\r
- if (PyUnicode_SetDefaultEncoding(encoding))\r
- return NULL;\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(setdefaultencoding_doc,\r
-"setdefaultencoding(encoding)\n\\r
-\n\\r
-Set the current default string encoding used by the Unicode implementation."\r
-);\r
-\r
-static PyObject *\r
-sys_getfilesystemencoding(PyObject *self)\r
-{\r
- if (Py_FileSystemDefaultEncoding)\r
- return PyString_FromString(Py_FileSystemDefaultEncoding);\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(getfilesystemencoding_doc,\r
-"getfilesystemencoding() -> string\n\\r
-\n\\r
-Return the encoding used to convert Unicode filenames in\n\\r
-operating system filenames."\r
-);\r
-\r
-#endif\r
-\r
-/*\r
- * Cached interned string objects used for calling the profile and\r
- * trace functions. Initialized by trace_init().\r
- */\r
-static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};\r
-\r
-static int\r
-trace_init(void)\r
-{\r
- static char *whatnames[7] = {"call", "exception", "line", "return",\r
- "c_call", "c_exception", "c_return"};\r
- PyObject *name;\r
- int i;\r
- for (i = 0; i < 7; ++i) {\r
- if (whatstrings[i] == NULL) {\r
- name = PyString_InternFromString(whatnames[i]);\r
- if (name == NULL)\r
- return -1;\r
- whatstrings[i] = name;\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-\r
-static PyObject *\r
-call_trampoline(PyThreadState *tstate, PyObject* callback,\r
- PyFrameObject *frame, int what, PyObject *arg)\r
-{\r
- PyObject *args = PyTuple_New(3);\r
- PyObject *whatstr;\r
- PyObject *result;\r
-\r
- if (args == NULL)\r
- return NULL;\r
- Py_INCREF(frame);\r
- whatstr = whatstrings[what];\r
- Py_INCREF(whatstr);\r
- if (arg == NULL)\r
- arg = Py_None;\r
- Py_INCREF(arg);\r
- PyTuple_SET_ITEM(args, 0, (PyObject *)frame);\r
- PyTuple_SET_ITEM(args, 1, whatstr);\r
- PyTuple_SET_ITEM(args, 2, arg);\r
-\r
- /* call the Python-level function */\r
- PyFrame_FastToLocals(frame);\r
- result = PyEval_CallObject(callback, args);\r
- PyFrame_LocalsToFast(frame, 1);\r
- if (result == NULL)\r
- PyTraceBack_Here(frame);\r
-\r
- /* cleanup */\r
- Py_DECREF(args);\r
- return result;\r
-}\r
-\r
-static int\r
-profile_trampoline(PyObject *self, PyFrameObject *frame,\r
- int what, PyObject *arg)\r
-{\r
- PyThreadState *tstate = frame->f_tstate;\r
- PyObject *result;\r
-\r
- if (arg == NULL)\r
- arg = Py_None;\r
- result = call_trampoline(tstate, self, frame, what, arg);\r
- if (result == NULL) {\r
- PyEval_SetProfile(NULL, NULL);\r
- return -1;\r
- }\r
- Py_DECREF(result);\r
- return 0;\r
-}\r
-\r
-static int\r
-trace_trampoline(PyObject *self, PyFrameObject *frame,\r
- int what, PyObject *arg)\r
-{\r
- PyThreadState *tstate = frame->f_tstate;\r
- PyObject *callback;\r
- PyObject *result;\r
-\r
- if (what == PyTrace_CALL)\r
- callback = self;\r
- else\r
- callback = frame->f_trace;\r
- if (callback == NULL)\r
- return 0;\r
- result = call_trampoline(tstate, callback, frame, what, arg);\r
- if (result == NULL) {\r
- PyEval_SetTrace(NULL, NULL);\r
- Py_CLEAR(frame->f_trace);\r
- return -1;\r
- }\r
- if (result != Py_None) {\r
- PyObject *temp = frame->f_trace;\r
- frame->f_trace = NULL;\r
- Py_XDECREF(temp);\r
- frame->f_trace = result;\r
- }\r
- else {\r
- Py_DECREF(result);\r
- }\r
- return 0;\r
-}\r
-\r
-static PyObject *\r
-sys_settrace(PyObject *self, PyObject *args)\r
-{\r
- if (trace_init() == -1)\r
- return NULL;\r
- if (args == Py_None)\r
- PyEval_SetTrace(NULL, NULL);\r
- else\r
- PyEval_SetTrace(trace_trampoline, args);\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(settrace_doc,\r
-"settrace(function)\n\\r
-\n\\r
-Set the global debug tracing function. It will be called on each\n\\r
-function call. See the debugger chapter in the library manual."\r
-);\r
-\r
-static PyObject *\r
-sys_gettrace(PyObject *self, PyObject *args)\r
-{\r
- PyThreadState *tstate = PyThreadState_GET();\r
- PyObject *temp = tstate->c_traceobj;\r
-\r
- if (temp == NULL)\r
- temp = Py_None;\r
- Py_INCREF(temp);\r
- return temp;\r
-}\r
-\r
-PyDoc_STRVAR(gettrace_doc,\r
-"gettrace()\n\\r
-\n\\r
-Return the global debug tracing function set with sys.settrace.\n\\r
-See the debugger chapter in the library manual."\r
-);\r
-\r
-static PyObject *\r
-sys_setprofile(PyObject *self, PyObject *args)\r
-{\r
- if (trace_init() == -1)\r
- return NULL;\r
- if (args == Py_None)\r
- PyEval_SetProfile(NULL, NULL);\r
- else\r
- PyEval_SetProfile(profile_trampoline, args);\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(setprofile_doc,\r
-"setprofile(function)\n\\r
-\n\\r
-Set the profiling function. It will be called on each function call\n\\r
-and return. See the profiler chapter in the library manual."\r
-);\r
-\r
-static PyObject *\r
-sys_getprofile(PyObject *self, PyObject *args)\r
-{\r
- PyThreadState *tstate = PyThreadState_GET();\r
- PyObject *temp = tstate->c_profileobj;\r
-\r
- if (temp == NULL)\r
- temp = Py_None;\r
- Py_INCREF(temp);\r
- return temp;\r
-}\r
-\r
-PyDoc_STRVAR(getprofile_doc,\r
-"getprofile()\n\\r
-\n\\r
-Return the profiling function set with sys.setprofile.\n\\r
-See the profiler chapter in the library manual."\r
-);\r
-\r
-static PyObject *\r
-sys_setcheckinterval(PyObject *self, PyObject *args)\r
-{\r
- if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))\r
- return NULL;\r
- _Py_Ticker = _Py_CheckInterval;\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(setcheckinterval_doc,\r
-"setcheckinterval(n)\n\\r
-\n\\r
-Tell the Python interpreter to check for asynchronous events every\n\\r
-n instructions. This also affects how often thread switches occur."\r
-);\r
-\r
-static PyObject *\r
-sys_getcheckinterval(PyObject *self, PyObject *args)\r
-{\r
- return PyInt_FromLong(_Py_CheckInterval);\r
-}\r
-\r
-PyDoc_STRVAR(getcheckinterval_doc,\r
-"getcheckinterval() -> current check interval; see setcheckinterval()."\r
-);\r
-\r
-#ifdef WITH_TSC\r
-static PyObject *\r
-sys_settscdump(PyObject *self, PyObject *args)\r
-{\r
- int bool;\r
- PyThreadState *tstate = PyThreadState_Get();\r
-\r
- if (!PyArg_ParseTuple(args, "i:settscdump", &bool))\r
- return NULL;\r
- if (bool)\r
- tstate->interp->tscdump = 1;\r
- else\r
- tstate->interp->tscdump = 0;\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-\r
-}\r
-\r
-PyDoc_STRVAR(settscdump_doc,\r
-"settscdump(bool)\n\\r
-\n\\r
-If true, tell the Python interpreter to dump VM measurements to\n\\r
-stderr. If false, turn off dump. The measurements are based on the\n\\r
-processor's time-stamp counter."\r
-);\r
-#endif /* TSC */\r
-\r
-static PyObject *\r
-sys_setrecursionlimit(PyObject *self, PyObject *args)\r
-{\r
- int new_limit;\r
- if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))\r
- return NULL;\r
- if (new_limit <= 0) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "recursion limit must be positive");\r
- return NULL;\r
- }\r
- Py_SetRecursionLimit(new_limit);\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(setrecursionlimit_doc,\r
-"setrecursionlimit(n)\n\\r
-\n\\r
-Set the maximum depth of the Python interpreter stack to n. This\n\\r
-limit prevents infinite recursion from causing an overflow of the C\n\\r
-stack and crashing Python. The highest possible limit is platform-\n\\r
-dependent."\r
-);\r
-\r
-static PyObject *\r
-sys_getrecursionlimit(PyObject *self)\r
-{\r
- return PyInt_FromLong(Py_GetRecursionLimit());\r
-}\r
-\r
-PyDoc_STRVAR(getrecursionlimit_doc,\r
-"getrecursionlimit()\n\\r
-\n\\r
-Return the current value of the recursion limit, the maximum depth\n\\r
-of the Python interpreter stack. This limit prevents infinite\n\\r
-recursion from causing an overflow of the C stack and crashing Python."\r
-);\r
-\r
-#ifdef MS_WINDOWS\r
-PyDoc_STRVAR(getwindowsversion_doc,\r
-"getwindowsversion()\n\\r
-\n\\r
-Return information about the running version of Windows as a named tuple.\n\\r
-The members are named: major, minor, build, platform, service_pack,\n\\r
-service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\\r
-backward compatibility, only the first 5 items are available by indexing.\n\\r
-All elements are numbers, except service_pack which is a string. Platform\n\\r
-may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\\r
-3 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\\r
-controller, 3 for a server."\r
-);\r
-\r
-static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};\r
-\r
-static PyStructSequence_Field windows_version_fields[] = {\r
- {"major", "Major version number"},\r
- {"minor", "Minor version number"},\r
- {"build", "Build number"},\r
- {"platform", "Operating system platform"},\r
- {"service_pack", "Latest Service Pack installed on the system"},\r
- {"service_pack_major", "Service Pack major version number"},\r
- {"service_pack_minor", "Service Pack minor version number"},\r
- {"suite_mask", "Bit mask identifying available product suites"},\r
- {"product_type", "System product type"},\r
- {0}\r
-};\r
-\r
-static PyStructSequence_Desc windows_version_desc = {\r
- "sys.getwindowsversion", /* name */\r
- getwindowsversion_doc, /* doc */\r
- windows_version_fields, /* fields */\r
- 5 /* For backward compatibility,\r
- only the first 5 items are accessible\r
- via indexing, the rest are name only */\r
-};\r
-\r
-static PyObject *\r
-sys_getwindowsversion(PyObject *self)\r
-{\r
- PyObject *version;\r
- int pos = 0;\r
- OSVERSIONINFOEX ver;\r
- ver.dwOSVersionInfoSize = sizeof(ver);\r
- if (!GetVersionEx((OSVERSIONINFO*) &ver))\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- version = PyStructSequence_New(&WindowsVersionType);\r
- if (version == NULL)\r
- return NULL;\r
-\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.dwMajorVersion));\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.dwMinorVersion));\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.dwBuildNumber));\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.dwPlatformId));\r
- PyStructSequence_SET_ITEM(version, pos++, PyString_FromString(ver.szCSDVersion));\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.wServicePackMajor));\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.wServicePackMinor));\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.wSuiteMask));\r
- PyStructSequence_SET_ITEM(version, pos++, PyInt_FromLong(ver.wProductType));\r
-\r
- if (PyErr_Occurred()) {\r
- Py_DECREF(version);\r
- return NULL;\r
- }\r
- return version;\r
-}\r
-\r
-#endif /* MS_WINDOWS */\r
-\r
-#ifdef HAVE_DLOPEN\r
-static PyObject *\r
-sys_setdlopenflags(PyObject *self, PyObject *args)\r
-{\r
- int new_val;\r
- PyThreadState *tstate = PyThreadState_GET();\r
- if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))\r
- return NULL;\r
- if (!tstate)\r
- return NULL;\r
- tstate->interp->dlopenflags = new_val;\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-PyDoc_STRVAR(setdlopenflags_doc,\r
-"setdlopenflags(n) -> None\n\\r
-\n\\r
-Set the flags used by the interpreter for dlopen calls, such as when the\n\\r
-interpreter loads extension modules. Among other things, this will enable\n\\r
-a lazy resolving of symbols when importing a module, if called as\n\\r
-sys.setdlopenflags(0). To share symbols across extension modules, call as\n\\r
-sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n\\r
-can be either found in the ctypes module, or in the DLFCN module. If DLFCN\n\\r
-is not available, it can be generated from /usr/include/dlfcn.h using the\n\\r
-h2py script.");\r
-\r
-static PyObject *\r
-sys_getdlopenflags(PyObject *self, PyObject *args)\r
-{\r
- PyThreadState *tstate = PyThreadState_GET();\r
- if (!tstate)\r
- return NULL;\r
- return PyInt_FromLong(tstate->interp->dlopenflags);\r
-}\r
-\r
-PyDoc_STRVAR(getdlopenflags_doc,\r
-"getdlopenflags() -> int\n\\r
-\n\\r
-Return the current value of the flags that are used for dlopen calls.\n\\r
-The flag constants are defined in the ctypes and DLFCN modules.");\r
-\r
-#endif /* HAVE_DLOPEN */\r
-\r
-#ifdef USE_MALLOPT\r
-/* Link with -lmalloc (or -lmpc) on an SGI */\r
-#include <malloc.h>\r
-\r
-static PyObject *\r
-sys_mdebug(PyObject *self, PyObject *args)\r
-{\r
- int flag;\r
- if (!PyArg_ParseTuple(args, "i:mdebug", &flag))\r
- return NULL;\r
- mallopt(M_DEBUG, flag);\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-#endif /* USE_MALLOPT */\r
-\r
-size_t\r
-_PySys_GetSizeOf(PyObject *o)\r
-{\r
- static PyObject *str__sizeof__ = NULL;\r
- PyObject *res = NULL;\r
- Py_ssize_t size;\r
-\r
- /* Make sure the type is initialized. float gets initialized late */\r
- if (PyType_Ready(Py_TYPE(o)) < 0)\r
- return (size_t)-1;\r
-\r
- /* Instance of old-style class */\r
- if (PyInstance_Check(o))\r
- size = PyInstance_Type.tp_basicsize;\r
- /* all other objects */\r
- else {\r
- PyObject *method = _PyObject_LookupSpecial(o, "__sizeof__",\r
- &str__sizeof__);\r
- if (method == NULL) {\r
- if (!PyErr_Occurred())\r
- PyErr_Format(PyExc_TypeError,\r
- "Type %.100s doesn't define __sizeof__",\r
- Py_TYPE(o)->tp_name);\r
- }\r
- else {\r
- res = PyObject_CallFunctionObjArgs(method, NULL);\r
- Py_DECREF(method);\r
- }\r
-\r
- if (res == NULL)\r
- return (size_t)-1;\r
-\r
- size = (size_t)PyInt_AsSsize_t(res);\r
- Py_DECREF(res);\r
- if (size == -1 && PyErr_Occurred())\r
- return (size_t)-1;\r
- }\r
-\r
- if (size < 0) {\r
- PyErr_SetString(PyExc_ValueError, "__sizeof__() should return >= 0");\r
- return (size_t)-1;\r
- }\r
-\r
- /* add gc_head size */\r
- if (PyObject_IS_GC(o))\r
- return ((size_t)size) + sizeof(PyGC_Head);\r
- return (size_t)size;\r
-}\r
-\r
-static PyObject *\r
-sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)\r
-{\r
- static char *kwlist[] = {"object", "default", 0};\r
- size_t size;\r
- PyObject *o, *dflt = NULL;\r
-\r
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",\r
- kwlist, &o, &dflt))\r
- return NULL;\r
-\r
- size = _PySys_GetSizeOf(o);\r
-\r
- if (size == (size_t)-1 && PyErr_Occurred()) {\r
- /* Has a default value been given */\r
- if (dflt != NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {\r
- PyErr_Clear();\r
- Py_INCREF(dflt);\r
- return dflt;\r
- }\r
- else\r
- return NULL;\r
- }\r
-\r
- return PyInt_FromSize_t(size);\r
-}\r
-\r
-PyDoc_STRVAR(getsizeof_doc,\r
-"getsizeof(object, default) -> int\n\\r
-\n\\r
-Return the size of object in bytes.");\r
-\r
-static PyObject *\r
-sys_getrefcount(PyObject *self, PyObject *arg)\r
-{\r
- return PyInt_FromSsize_t(arg->ob_refcnt);\r
-}\r
-\r
-#ifdef Py_REF_DEBUG\r
-static PyObject *\r
-sys_gettotalrefcount(PyObject *self)\r
-{\r
- return PyInt_FromSsize_t(_Py_GetRefTotal());\r
-}\r
-#endif /* Py_REF_DEBUG */\r
-\r
-PyDoc_STRVAR(getrefcount_doc,\r
-"getrefcount(object) -> integer\n\\r
-\n\\r
-Return the reference count of object. The count returned is generally\n\\r
-one higher than you might expect, because it includes the (temporary)\n\\r
-reference as an argument to getrefcount()."\r
-);\r
-\r
-#ifdef COUNT_ALLOCS\r
-static PyObject *\r
-sys_getcounts(PyObject *self)\r
-{\r
- extern PyObject *get_counts(void);\r
-\r
- return get_counts();\r
-}\r
-#endif\r
-\r
-PyDoc_STRVAR(getframe_doc,\r
-"_getframe([depth]) -> frameobject\n\\r
-\n\\r
-Return a frame object from the call stack. If optional integer depth is\n\\r
-given, return the frame object that many calls below the top of the stack.\n\\r
-If that is deeper than the call stack, ValueError is raised. The default\n\\r
-for depth is zero, returning the frame at the top of the call stack.\n\\r
-\n\\r
-This function should be used for internal and specialized\n\\r
-purposes only."\r
-);\r
-\r
-static PyObject *\r
-sys_getframe(PyObject *self, PyObject *args)\r
-{\r
- PyFrameObject *f = PyThreadState_GET()->frame;\r
- int depth = -1;\r
-\r
- if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))\r
- return NULL;\r
-\r
- while (depth > 0 && f != NULL) {\r
- f = f->f_back;\r
- --depth;\r
- }\r
- if (f == NULL) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "call stack is not deep enough");\r
- return NULL;\r
- }\r
- Py_INCREF(f);\r
- return (PyObject*)f;\r
-}\r
-\r
-PyDoc_STRVAR(current_frames_doc,\r
-"_current_frames() -> dictionary\n\\r
-\n\\r
-Return a dictionary mapping each current thread T's thread id to T's\n\\r
-current stack frame.\n\\r
-\n\\r
-This function should be used for specialized purposes only."\r
-);\r
-\r
-static PyObject *\r
-sys_current_frames(PyObject *self, PyObject *noargs)\r
-{\r
- return _PyThread_CurrentFrames();\r
-}\r
-\r
-PyDoc_STRVAR(call_tracing_doc,\r
-"call_tracing(func, args) -> object\n\\r
-\n\\r
-Call func(*args), while tracing is enabled. The tracing state is\n\\r
-saved, and restored afterwards. This is intended to be called from\n\\r
-a debugger from a checkpoint, to recursively debug some other code."\r
-);\r
-\r
-static PyObject *\r
-sys_call_tracing(PyObject *self, PyObject *args)\r
-{\r
- PyObject *func, *funcargs;\r
- if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))\r
- return NULL;\r
- return _PyEval_CallTracing(func, funcargs);\r
-}\r
-\r
-PyDoc_STRVAR(callstats_doc,\r
-"callstats() -> tuple of integers\n\\r
-\n\\r
-Return a tuple of function call statistics, if CALL_PROFILE was defined\n\\r
-when Python was built. Otherwise, return None.\n\\r
-\n\\r
-When enabled, this function returns detailed, implementation-specific\n\\r
-details about the number of function calls executed. The return value is\n\\r
-a 11-tuple where the entries in the tuple are counts of:\n\\r
-0. all function calls\n\\r
-1. calls to PyFunction_Type objects\n\\r
-2. PyFunction calls that do not create an argument tuple\n\\r
-3. PyFunction calls that do not create an argument tuple\n\\r
- and bypass PyEval_EvalCodeEx()\n\\r
-4. PyMethod calls\n\\r
-5. PyMethod calls on bound methods\n\\r
-6. PyType calls\n\\r
-7. PyCFunction calls\n\\r
-8. generator calls\n\\r
-9. All other calls\n\\r
-10. Number of stack pops performed by call_function()"\r
-);\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-#ifdef Py_TRACE_REFS\r
-/* Defined in objects.c because it uses static globals if that file */\r
-extern PyObject *_Py_GetObjects(PyObject *, PyObject *);\r
-#endif\r
-\r
-#ifdef DYNAMIC_EXECUTION_PROFILE\r
-/* Defined in ceval.c because it uses static globals if that file */\r
-extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);\r
-#endif\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r
-\r
-static PyObject *\r
-sys_clear_type_cache(PyObject* self, PyObject* args)\r
-{\r
- PyType_ClearCache();\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(sys_clear_type_cache__doc__,\r
-"_clear_type_cache() -> None\n\\r
-Clear the internal type lookup cache.");\r
-\r
-\r
-static PyMethodDef sys_methods[] = {\r
- /* Might as well keep this in alphabetic order */\r
- {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,\r
- callstats_doc},\r
- {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,\r
- sys_clear_type_cache__doc__},\r
- {"_current_frames", sys_current_frames, METH_NOARGS,\r
- current_frames_doc},\r
- {"displayhook", sys_displayhook, METH_O, displayhook_doc},\r
- {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},\r
- {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},\r
- {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},\r
- {"exit", sys_exit, METH_VARARGS, exit_doc},\r
-#ifdef Py_USING_UNICODE\r
- {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,\r
- METH_NOARGS, getdefaultencoding_doc},\r
-#endif\r
-#ifdef HAVE_DLOPEN\r
- {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,\r
- getdlopenflags_doc},\r
-#endif\r
-#ifdef COUNT_ALLOCS\r
- {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},\r
-#endif\r
-#ifdef DYNAMIC_EXECUTION_PROFILE\r
- {"getdxp", _Py_GetDXProfile, METH_VARARGS},\r
-#endif\r
-#ifdef Py_USING_UNICODE\r
- {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,\r
- METH_NOARGS, getfilesystemencoding_doc},\r
-#endif\r
-#ifdef Py_TRACE_REFS\r
- {"getobjects", _Py_GetObjects, METH_VARARGS},\r
-#endif\r
-#ifdef Py_REF_DEBUG\r
- {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},\r
-#endif\r
- {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},\r
- {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,\r
- getrecursionlimit_doc},\r
- {"getsizeof", (PyCFunction)sys_getsizeof,\r
- METH_VARARGS | METH_KEYWORDS, getsizeof_doc},\r
- {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},\r
-#ifdef MS_WINDOWS\r
- {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,\r
- getwindowsversion_doc},\r
-#endif /* MS_WINDOWS */\r
-#ifdef USE_MALLOPT\r
- {"mdebug", sys_mdebug, METH_VARARGS},\r
-#endif\r
-#ifdef Py_USING_UNICODE\r
- {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,\r
- setdefaultencoding_doc},\r
-#endif\r
- {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,\r
- setcheckinterval_doc},\r
- {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,\r
- getcheckinterval_doc},\r
-#ifdef HAVE_DLOPEN\r
- {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,\r
- setdlopenflags_doc},\r
-#endif\r
- {"setprofile", sys_setprofile, METH_O, setprofile_doc},\r
- {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},\r
- {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,\r
- setrecursionlimit_doc},\r
-#ifdef WITH_TSC\r
- {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},\r
-#endif\r
- {"settrace", sys_settrace, METH_O, settrace_doc},\r
- {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},\r
- {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},\r
- {NULL, NULL} /* sentinel */\r
-};\r
-\r
-static PyObject *\r
-list_builtin_module_names(void)\r
-{\r
- PyObject *list = PyList_New(0);\r
- int i;\r
- if (list == NULL)\r
- return NULL;\r
- for (i = 0; PyImport_Inittab[i].name != NULL; i++) {\r
- PyObject *name = PyString_FromString(\r
- PyImport_Inittab[i].name);\r
- if (name == NULL)\r
- break;\r
- PyList_Append(list, name);\r
- Py_DECREF(name);\r
- }\r
- if (PyList_Sort(list) != 0) {\r
- Py_DECREF(list);\r
- list = NULL;\r
- }\r
- if (list) {\r
- PyObject *v = PyList_AsTuple(list);\r
- Py_DECREF(list);\r
- list = v;\r
- }\r
- return list;\r
-}\r
-\r
-static PyObject *warnoptions = NULL;\r
-\r
-void\r
-PySys_ResetWarnOptions(void)\r
-{\r
- if (warnoptions == NULL || !PyList_Check(warnoptions))\r
- return;\r
- PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);\r
-}\r
-\r
-void\r
-PySys_AddWarnOption(char *s)\r
-{\r
- PyObject *str;\r
-\r
- if (warnoptions == NULL || !PyList_Check(warnoptions)) {\r
- Py_XDECREF(warnoptions);\r
- warnoptions = PyList_New(0);\r
- if (warnoptions == NULL)\r
- return;\r
- }\r
- str = PyString_FromString(s);\r
- if (str != NULL) {\r
- PyList_Append(warnoptions, str);\r
- Py_DECREF(str);\r
- }\r
-}\r
-\r
-int\r
-PySys_HasWarnOptions(void)\r
-{\r
- return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;\r
-}\r
-\r
-/* XXX This doc string is too long to be a single string literal in VC++ 5.0.\r
- Two literals concatenated works just fine. If you have a K&R compiler\r
- or other abomination that however *does* understand longer strings,\r
- get rid of the !!! comment in the middle and the quotes that surround it. */\r
-PyDoc_VAR(sys_doc) =\r
-PyDoc_STR(\r
-"This module provides access to some objects used or maintained by the\n\\r
-interpreter and to functions that interact strongly with the interpreter.\n\\r
-\n\\r
-Dynamic objects:\n\\r
-\n\\r
-argv -- command line arguments; argv[0] is the script pathname if known\n\\r
-path -- module search path; path[0] is the script directory, else ''\n\\r
-modules -- dictionary of loaded modules\n\\r
-\n\\r
-displayhook -- called to show results in an interactive session\n\\r
-excepthook -- called to handle any uncaught exception other than SystemExit\n\\r
- To customize printing in an interactive session or to install a custom\n\\r
- top-level exception handler, assign other functions to replace these.\n\\r
-\n\\r
-exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\\r
- Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\\r
-\n\\r
-stdin -- standard input file object; used by raw_input() and input()\n\\r
-stdout -- standard output file object; used by the print statement\n\\r
-stderr -- standard error object; used for error messages\n\\r
- By assigning other file objects (or objects that behave like files)\n\\r
- to these, it is possible to redirect all of the interpreter's I/O.\n\\r
-\n\\r
-last_type -- type of last uncaught exception\n\\r
-last_value -- value of last uncaught exception\n\\r
-last_traceback -- traceback of last uncaught exception\n\\r
- These three are only available in an interactive session after a\n\\r
- traceback has been printed.\n\\r
-\n\\r
-exc_type -- type of exception currently being handled\n\\r
-exc_value -- value of exception currently being handled\n\\r
-exc_traceback -- traceback of exception currently being handled\n\\r
- The function exc_info() should be used instead of these three,\n\\r
- because it is thread-safe.\n\\r
-"\r
-)\r
-/* concatenating string here */\r
-PyDoc_STR(\r
-"\n\\r
-Static objects:\n\\r
-\n\\r
-float_info -- a dict with information about the float inplementation.\n\\r
-long_info -- a struct sequence with information about the long implementation.\n\\r
-maxint -- the largest supported integer (the smallest is -maxint-1)\n\\r
-maxsize -- the largest supported length of containers.\n\\r
-maxunicode -- the largest supported character\n\\r
-builtin_module_names -- tuple of module names built into this interpreter\n\\r
-version -- the version of this interpreter as a string\n\\r
-version_info -- version information as a named tuple\n\\r
-hexversion -- version information encoded as a single integer\n\\r
-copyright -- copyright notice pertaining to this interpreter\n\\r
-platform -- platform identifier\n\\r
-executable -- absolute path of the executable binary of the Python interpreter\n\\r
-prefix -- prefix used to find the Python library\n\\r
-exec_prefix -- prefix used to find the machine-specific Python library\n\\r
-float_repr_style -- string indicating the style of repr() output for floats\n\\r
-"\r
-)\r
-#ifdef MS_WINDOWS\r
-/* concatenating string here */\r
-PyDoc_STR(\r
-"dllhandle -- [Windows only] integer handle of the Python DLL\n\\r
-winver -- [Windows only] version number of the Python DLL\n\\r
-"\r
-)\r
-#endif /* MS_WINDOWS */\r
-PyDoc_STR(\r
-"__stdin__ -- the original stdin; don't touch!\n\\r
-__stdout__ -- the original stdout; don't touch!\n\\r
-__stderr__ -- the original stderr; don't touch!\n\\r
-__displayhook__ -- the original displayhook; don't touch!\n\\r
-__excepthook__ -- the original excepthook; don't touch!\n\\r
-\n\\r
-Functions:\n\\r
-\n\\r
-displayhook() -- print an object to the screen, and save it in __builtin__._\n\\r
-excepthook() -- print an exception and its traceback to sys.stderr\n\\r
-exc_info() -- return thread-safe information about the current exception\n\\r
-exc_clear() -- clear the exception state for the current thread\n\\r
-exit() -- exit the interpreter by raising SystemExit\n\\r
-getdlopenflags() -- returns flags to be used for dlopen() calls\n\\r
-getprofile() -- get the global profiling function\n\\r
-getrefcount() -- return the reference count for an object (plus one :-)\n\\r
-getrecursionlimit() -- return the max recursion depth for the interpreter\n\\r
-getsizeof() -- return the size of an object in bytes\n\\r
-gettrace() -- get the global debug tracing function\n\\r
-setcheckinterval() -- control how often the interpreter checks for events\n\\r
-setdlopenflags() -- set the flags to be used for dlopen() calls\n\\r
-setprofile() -- set the global profiling function\n\\r
-setrecursionlimit() -- set the max recursion depth for the interpreter\n\\r
-settrace() -- set the global debug tracing function\n\\r
-"\r
-)\r
-/* end of sys_doc */ ;\r
-\r
-static int\r
-_check_and_flush (FILE *stream)\r
-{\r
- int prev_fail = ferror (stream);\r
- return fflush (stream) || prev_fail ? EOF : 0;\r
-}\r
-\r
-/* Subversion branch and revision management */\r
-static int svn_initialized;\r
-static char patchlevel_revision[50]; /* Just the number */\r
-static char branch[50];\r
-static char shortbranch[50];\r
-static const char *svn_revision;\r
-\r
-static void\r
-svnversion_init(void)\r
-{\r
- if (svn_initialized)\r
- return;\r
- svn_initialized = 1;\r
- *patchlevel_revision = '\0';\r
- strcpy(branch, "");\r
- strcpy(shortbranch, "unknown");\r
- svn_revision = "";\r
- return;\r
-}\r
-\r
-/* Return svnversion output if available.\r
- Else return Revision of patchlevel.h if on branch.\r
- Else return empty string */\r
-const char*\r
-Py_SubversionRevision()\r
-{\r
- svnversion_init();\r
- return svn_revision;\r
-}\r
-\r
-const char*\r
-Py_SubversionShortBranch()\r
-{\r
- svnversion_init();\r
- return shortbranch;\r
-}\r
-\r
-\r
-PyDoc_STRVAR(flags__doc__,\r
-"sys.flags\n\\r
-\n\\r
-Flags provided through command line arguments or environment vars.");\r
-\r
-static PyTypeObject FlagsType = {0, 0, 0, 0, 0, 0};\r
-\r
-static PyStructSequence_Field flags_fields[] = {\r
- {"debug", "-d"},\r
- {"py3k_warning", "-3"},\r
- {"division_warning", "-Q"},\r
- {"division_new", "-Qnew"},\r
- {"inspect", "-i"},\r
- {"interactive", "-i"},\r
- {"optimize", "-O or -OO"},\r
- {"dont_write_bytecode", "-B"},\r
- {"no_user_site", "-s"},\r
- {"no_site", "-S"},\r
- {"ignore_environment", "-E"},\r
- {"tabcheck", "-t or -tt"},\r
- {"verbose", "-v"},\r
-#ifdef RISCOS\r
- {"riscos_wimp", "???"},\r
-#endif\r
- /* {"unbuffered", "-u"}, */\r
- {"unicode", "-U"},\r
- /* {"skip_first", "-x"}, */\r
- {"bytes_warning", "-b"},\r
- {"hash_randomization", "-R"},\r
- {0}\r
-};\r
-\r
-static PyStructSequence_Desc flags_desc = {\r
- "sys.flags", /* name */\r
- flags__doc__, /* doc */\r
- flags_fields, /* fields */\r
-#ifdef RISCOS\r
- 17\r
-#else\r
- 16\r
-#endif\r
-};\r
-\r
-static PyObject*\r
-make_flags(void)\r
-{\r
- int pos = 0;\r
- PyObject *seq;\r
-\r
- seq = PyStructSequence_New(&FlagsType);\r
- if (seq == NULL)\r
- return NULL;\r
-\r
-#define SetFlag(flag) \\r
- PyStructSequence_SET_ITEM(seq, pos++, PyInt_FromLong(flag))\r
-\r
- SetFlag(Py_DebugFlag);\r
- SetFlag(Py_Py3kWarningFlag);\r
- SetFlag(Py_DivisionWarningFlag);\r
- SetFlag(_Py_QnewFlag);\r
- SetFlag(Py_InspectFlag);\r
- SetFlag(Py_InteractiveFlag);\r
- SetFlag(Py_OptimizeFlag);\r
- SetFlag(Py_DontWriteBytecodeFlag);\r
- SetFlag(Py_NoUserSiteDirectory);\r
- SetFlag(Py_NoSiteFlag);\r
- SetFlag(Py_IgnoreEnvironmentFlag);\r
- SetFlag(Py_TabcheckFlag);\r
- SetFlag(Py_VerboseFlag);\r
-#ifdef RISCOS\r
- SetFlag(Py_RISCOSWimpFlag);\r
-#endif\r
- /* SetFlag(saw_unbuffered_flag); */\r
- SetFlag(Py_UnicodeFlag);\r
- /* SetFlag(skipfirstline); */\r
- SetFlag(Py_BytesWarningFlag);\r
- SetFlag(Py_HashRandomizationFlag);\r
-#undef SetFlag\r
-\r
- if (PyErr_Occurred()) {\r
- Py_DECREF(seq);\r
- return NULL;\r
- }\r
- return seq;\r
-}\r
-\r
-PyDoc_STRVAR(version_info__doc__,\r
-"sys.version_info\n\\r
-\n\\r
-Version information as a named tuple.");\r
-\r
-static PyTypeObject VersionInfoType = {0, 0, 0, 0, 0, 0};\r
-\r
-static PyStructSequence_Field version_info_fields[] = {\r
- {"major", "Major release number"},\r
- {"minor", "Minor release number"},\r
- {"micro", "Patch release number"},\r
- {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},\r
- {"serial", "Serial release number"},\r
- {0}\r
-};\r
-\r
-static PyStructSequence_Desc version_info_desc = {\r
- "sys.version_info", /* name */\r
- version_info__doc__, /* doc */\r
- version_info_fields, /* fields */\r
- 5\r
-};\r
-\r
-static PyObject *\r
-make_version_info(void)\r
-{\r
- PyObject *version_info;\r
- char *s;\r
- int pos = 0;\r
-\r
- version_info = PyStructSequence_New(&VersionInfoType);\r
- if (version_info == NULL) {\r
- return NULL;\r
- }\r
-\r
- /*\r
- * These release level checks are mutually exclusive and cover\r
- * the field, so don't get too fancy with the pre-processor!\r
- */\r
-#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA\r
- s = "alpha";\r
-#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA\r
- s = "beta";\r
-#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA\r
- s = "candidate";\r
-#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL\r
- s = "final";\r
-#endif\r
-\r
-#define SetIntItem(flag) \\r
- PyStructSequence_SET_ITEM(version_info, pos++, PyInt_FromLong(flag))\r
-#define SetStrItem(flag) \\r
- PyStructSequence_SET_ITEM(version_info, pos++, PyString_FromString(flag))\r
-\r
- SetIntItem(PY_MAJOR_VERSION);\r
- SetIntItem(PY_MINOR_VERSION);\r
- SetIntItem(PY_MICRO_VERSION);\r
- SetStrItem(s);\r
- SetIntItem(PY_RELEASE_SERIAL);\r
-#undef SetIntItem\r
-#undef SetStrItem\r
-\r
- if (PyErr_Occurred()) {\r
- Py_CLEAR(version_info);\r
- return NULL;\r
- }\r
- return version_info;\r
-}\r
-\r
-PyObject *\r
-_PySys_Init(void)\r
-{\r
- PyObject *m, *v, *sysdict;\r
- PyObject *sysin, *sysout, *syserr;\r
- char *s;\r
-\r
- m = Py_InitModule3("sys", sys_methods, sys_doc);\r
- if (m == NULL)\r
- return NULL;\r
- sysdict = PyModule_GetDict(m);\r
-#define SET_SYS_FROM_STRING(key, value) \\r
- v = value; \\r
- if (v != NULL) \\r
- PyDict_SetItemString(sysdict, key, v); \\r
- Py_XDECREF(v)\r
-\r
- /* Check that stdin is not a directory\r
- Using shell redirection, you can redirect stdin to a directory,\r
- crashing the Python interpreter. Catch this common mistake here\r
- and output a useful error message. Note that under MS Windows,\r
- the shell already prevents that. */\r
-#if !defined(MS_WINDOWS)\r
- {\r
- struct stat sb;\r
- if (fstat(fileno(stdin), &sb) == 0 &&\r
- S_ISDIR(sb.st_mode)) {\r
- /* There's nothing more we can do. */\r
- /* Py_FatalError() will core dump, so just exit. */\r
- PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");\r
- exit(EXIT_FAILURE);\r
- }\r
- }\r
-#endif\r
-\r
- /* Closing the standard FILE* if sys.std* goes aways causes problems\r
- * for embedded Python usages. Closing them when somebody explicitly\r
- * invokes .close() might be possible, but the FAQ promises they get\r
- * never closed. However, we still need to get write errors when\r
- * writing fails (e.g. because stdout is redirected), so we flush the\r
- * streams and check for errors before the file objects are deleted.\r
- * On OS X, fflush()ing stdin causes an error, so we exempt stdin\r
- * from that procedure.\r
- */\r
- sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);\r
- sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);\r
- syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);\r
- if (PyErr_Occurred())\r
- return NULL;\r
-\r
- PyDict_SetItemString(sysdict, "stdin", sysin);\r
- PyDict_SetItemString(sysdict, "stdout", sysout);\r
- PyDict_SetItemString(sysdict, "stderr", syserr);\r
- /* Make backup copies for cleanup */\r
- PyDict_SetItemString(sysdict, "__stdin__", sysin);\r
- PyDict_SetItemString(sysdict, "__stdout__", sysout);\r
- PyDict_SetItemString(sysdict, "__stderr__", syserr);\r
- PyDict_SetItemString(sysdict, "__displayhook__",\r
- PyDict_GetItemString(sysdict, "displayhook"));\r
- PyDict_SetItemString(sysdict, "__excepthook__",\r
- PyDict_GetItemString(sysdict, "excepthook"));\r
- Py_XDECREF(sysin);\r
- Py_XDECREF(sysout);\r
- Py_XDECREF(syserr);\r
-\r
- SET_SYS_FROM_STRING("version",\r
- PyString_FromString(Py_GetVersion()));\r
- SET_SYS_FROM_STRING("hexversion",\r
- PyInt_FromLong(PY_VERSION_HEX));\r
- svnversion_init();\r
- SET_SYS_FROM_STRING("subversion",\r
- Py_BuildValue("(ssz)", "CPython", branch,\r
- svn_revision));\r
- SET_SYS_FROM_STRING("_mercurial",\r
- Py_BuildValue("(szz)", "CPython", _Py_hgidentifier(),\r
- _Py_hgversion()));\r
- SET_SYS_FROM_STRING("dont_write_bytecode",\r
- PyBool_FromLong(Py_DontWriteBytecodeFlag));\r
- SET_SYS_FROM_STRING("api_version",\r
- PyInt_FromLong(PYTHON_API_VERSION));\r
- SET_SYS_FROM_STRING("copyright",\r
- PyString_FromString(Py_GetCopyright()));\r
- SET_SYS_FROM_STRING("platform",\r
- PyString_FromString(Py_GetPlatform()));\r
- SET_SYS_FROM_STRING("executable",\r
- PyString_FromString(Py_GetProgramFullPath()));\r
- SET_SYS_FROM_STRING("prefix",\r
- PyString_FromString(Py_GetPrefix()));\r
- SET_SYS_FROM_STRING("exec_prefix",\r
- PyString_FromString(Py_GetExecPrefix()));\r
- SET_SYS_FROM_STRING("maxsize",\r
- PyInt_FromSsize_t(PY_SSIZE_T_MAX));\r
- SET_SYS_FROM_STRING("maxint",\r
- PyInt_FromLong(PyInt_GetMax()));\r
- SET_SYS_FROM_STRING("py3kwarning",\r
- PyBool_FromLong(Py_Py3kWarningFlag));\r
- SET_SYS_FROM_STRING("float_info",\r
- PyFloat_GetInfo());\r
- SET_SYS_FROM_STRING("long_info",\r
- PyLong_GetInfo());\r
-#ifdef Py_USING_UNICODE\r
- SET_SYS_FROM_STRING("maxunicode",\r
- PyInt_FromLong(PyUnicode_GetMax()));\r
-#endif\r
- SET_SYS_FROM_STRING("builtin_module_names",\r
- list_builtin_module_names());\r
- {\r
- /* Assumes that longs are at least 2 bytes long.\r
- Should be safe! */\r
- unsigned long number = 1;\r
- char *value;\r
-\r
- s = (char *) &number;\r
- if (s[0] == 0)\r
- value = "big";\r
- else\r
- value = "little";\r
- SET_SYS_FROM_STRING("byteorder",\r
- PyString_FromString(value));\r
- }\r
-#ifdef MS_COREDLL\r
- SET_SYS_FROM_STRING("dllhandle",\r
- PyLong_FromVoidPtr(PyWin_DLLhModule));\r
- SET_SYS_FROM_STRING("winver",\r
- PyString_FromString(PyWin_DLLVersionString));\r
-#endif\r
- if (warnoptions == NULL) {\r
- warnoptions = PyList_New(0);\r
- }\r
- else {\r
- Py_INCREF(warnoptions);\r
- }\r
- if (warnoptions != NULL) {\r
- PyDict_SetItemString(sysdict, "warnoptions", warnoptions);\r
- }\r
-\r
- /* version_info */\r
- if (VersionInfoType.tp_name == 0)\r
- PyStructSequence_InitType(&VersionInfoType, &version_info_desc);\r
- SET_SYS_FROM_STRING("version_info", make_version_info());\r
- /* prevent user from creating new instances */\r
- VersionInfoType.tp_init = NULL;\r
- VersionInfoType.tp_new = NULL;\r
-\r
- /* flags */\r
- if (FlagsType.tp_name == 0)\r
- PyStructSequence_InitType(&FlagsType, &flags_desc);\r
- SET_SYS_FROM_STRING("flags", make_flags());\r
- /* prevent user from creating new instances */\r
- FlagsType.tp_init = NULL;\r
- FlagsType.tp_new = NULL;\r
-\r
-\r
-#if defined(MS_WINDOWS)\r
- /* getwindowsversion */\r
- if (WindowsVersionType.tp_name == 0)\r
- PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc);\r
- /* prevent user from creating new instances */\r
- WindowsVersionType.tp_init = NULL;\r
- WindowsVersionType.tp_new = NULL;\r
-#endif\r
-\r
- /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */\r
-#ifndef PY_NO_SHORT_FLOAT_REPR\r
- SET_SYS_FROM_STRING("float_repr_style",\r
- PyString_FromString("short"));\r
-#else\r
- SET_SYS_FROM_STRING("float_repr_style",\r
- PyString_FromString("legacy"));\r
-#endif\r
-\r
-#undef SET_SYS_FROM_STRING\r
- if (PyErr_Occurred())\r
- return NULL;\r
- return m;\r
-}\r
-\r
-static PyObject *\r
-makepathobject(char *path, int delim)\r
-{\r
- int i, n;\r
- char *p;\r
- PyObject *v, *w;\r
-\r
- n = 1;\r
- p = path;\r
- while ((p = strchr(p, delim)) != NULL) {\r
- n++;\r
- p++;\r
- }\r
- v = PyList_New(n);\r
- if (v == NULL)\r
- return NULL;\r
- for (i = 0; ; i++) {\r
- p = strchr(path, delim);\r
- if (p == NULL)\r
- p = strchr(path, '\0'); /* End of string */\r
- w = PyString_FromStringAndSize(path, (Py_ssize_t) (p - path));\r
- if (w == NULL) {\r
- Py_DECREF(v);\r
- return NULL;\r
- }\r
- PyList_SetItem(v, i, w);\r
- if (*p == '\0')\r
- break;\r
- path = p+1;\r
- }\r
- return v;\r
-}\r
-\r
-void\r
-PySys_SetPath(char *path)\r
-{\r
- PyObject *v;\r
- if ((v = makepathobject(path, DELIM)) == NULL)\r
- Py_FatalError("can't create sys.path");\r
- if (PySys_SetObject("path", v) != 0)\r
- Py_FatalError("can't assign sys.path");\r
- Py_DECREF(v);\r
-}\r
-\r
-static PyObject *\r
-makeargvobject(int argc, char **argv)\r
-{\r
- PyObject *av;\r
- if (argc <= 0 || argv == NULL) {\r
- /* Ensure at least one (empty) argument is seen */\r
- static char *empty_argv[1] = {""};\r
- argv = empty_argv;\r
- argc = 1;\r
- }\r
- av = PyList_New(argc);\r
- if (av != NULL) {\r
- int i;\r
- for (i = 0; i < argc; i++) {\r
-#ifdef __VMS\r
- PyObject *v;\r
-\r
- /* argv[0] is the script pathname if known */\r
- if (i == 0) {\r
- char* fn = decc$translate_vms(argv[0]);\r
- if ((fn == (char *)0) || fn == (char *)-1)\r
- v = PyString_FromString(argv[0]);\r
- else\r
- v = PyString_FromString(\r
- decc$translate_vms(argv[0]));\r
- } else\r
- v = PyString_FromString(argv[i]);\r
-#else\r
- PyObject *v = PyString_FromString(argv[i]);\r
-#endif\r
- if (v == NULL) {\r
- Py_DECREF(av);\r
- av = NULL;\r
- break;\r
- }\r
- PyList_SetItem(av, i, v);\r
- }\r
- }\r
- return av;\r
-}\r
-\r
-void\r
-PySys_SetArgvEx(int argc, char **argv, int updatepath)\r
-{\r
-#if defined(HAVE_REALPATH)\r
- char fullpath[MAXPATHLEN];\r
-#elif defined(MS_WINDOWS) && !defined(MS_WINCE)\r
- char fullpath[MAX_PATH];\r
-#endif\r
- PyObject *av = makeargvobject(argc, argv);\r
- PyObject *path = PySys_GetObject("path");\r
- if (av == NULL)\r
- Py_FatalError("no mem for sys.argv");\r
- if (PySys_SetObject("argv", av) != 0)\r
- Py_FatalError("can't assign sys.argv");\r
- if (updatepath && path != NULL) {\r
- char *argv0 = argv[0];\r
- char *p = NULL;\r
- Py_ssize_t n = 0;\r
- PyObject *a;\r
-#ifdef HAVE_READLINK\r
- char link[MAXPATHLEN+1];\r
- char argv0copy[2*MAXPATHLEN+1];\r
- int nr = 0;\r
- if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0)\r
- nr = readlink(argv0, link, MAXPATHLEN);\r
- if (nr > 0) {\r
- /* It's a symlink */\r
- link[nr] = '\0';\r
- if (link[0] == SEP)\r
- argv0 = link; /* Link to absolute path */\r
- else if (strchr(link, SEP) == NULL)\r
- ; /* Link without path */\r
- else {\r
- /* Must join(dirname(argv0), link) */\r
- char *q = strrchr(argv0, SEP);\r
- if (q == NULL)\r
- argv0 = link; /* argv0 without path */\r
- else {\r
- /* Must make a copy */\r
- strcpy(argv0copy, argv0);\r
- q = strrchr(argv0copy, SEP);\r
- strcpy(q+1, link);\r
- argv0 = argv0copy;\r
- }\r
- }\r
- }\r
-#endif /* HAVE_READLINK */\r
-#if SEP == '\\' /* Special case for MS filename syntax */\r
- if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {\r
- char *q;\r
-#if defined(MS_WINDOWS) && !defined(MS_WINCE)\r
- /* This code here replaces the first element in argv with the full\r
- path that it represents. Under CE, there are no relative paths so\r
- the argument must be the full path anyway. */\r
- char *ptemp;\r
- if (GetFullPathName(argv0,\r
- sizeof(fullpath),\r
- fullpath,\r
- &ptemp)) {\r
- argv0 = fullpath;\r
- }\r
-#endif\r
- p = strrchr(argv0, SEP);\r
- /* Test for alternate separator */\r
- q = strrchr(p ? p : argv0, '/');\r
- if (q != NULL)\r
- p = q;\r
- if (p != NULL) {\r
- n = p + 1 - argv0;\r
- if (n > 1 && p[-1] != ':')\r
- n--; /* Drop trailing separator */\r
- }\r
- }\r
-#else /* All other filename syntaxes */\r
- if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {\r
-#if defined(HAVE_REALPATH)\r
- if (realpath(argv0, fullpath)) {\r
- argv0 = fullpath;\r
- }\r
-#endif\r
- p = strrchr(argv0, SEP);\r
- }\r
- if (p != NULL) {\r
-#ifndef RISCOS\r
- n = p + 1 - argv0;\r
-#else /* don't include trailing separator */\r
- n = p - argv0;\r
-#endif /* RISCOS */\r
-#if SEP == '/' /* Special case for Unix filename syntax */\r
- if (n > 1)\r
- n--; /* Drop trailing separator */\r
-#endif /* Unix */\r
- }\r
-#endif /* All others */\r
- a = PyString_FromStringAndSize(argv0, n);\r
- if (a == NULL)\r
- Py_FatalError("no mem for sys.path insertion");\r
- if (PyList_Insert(path, 0, a) < 0)\r
- Py_FatalError("sys.path.insert(0) failed");\r
- Py_DECREF(a);\r
- }\r
- Py_DECREF(av);\r
-}\r
-\r
-void\r
-PySys_SetArgv(int argc, char **argv)\r
-{\r
- PySys_SetArgvEx(argc, argv, 1);\r
-}\r
-\r
-\r
-/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.\r
- Adapted from code submitted by Just van Rossum.\r
-\r
- PySys_WriteStdout(format, ...)\r
- PySys_WriteStderr(format, ...)\r
-\r
- The first function writes to sys.stdout; the second to sys.stderr. When\r
- there is a problem, they write to the real (C level) stdout or stderr;\r
- no exceptions are raised.\r
-\r
- Both take a printf-style format string as their first argument followed\r
- by a variable length argument list determined by the format string.\r
-\r
- *** WARNING ***\r
-\r
- The format should limit the total size of the formatted output string to\r
- 1000 bytes. In particular, this means that no unrestricted "%s" formats\r
- should occur; these should be limited using "%.<N>s where <N> is a\r
- decimal number calculated so that <N> plus the maximum size of other\r
- formatted text does not exceed 1000 bytes. Also watch out for "%f",\r
- which can print hundreds of digits for very large numbers.\r
-\r
- */\r
-\r
-static void\r
-mywrite(char *name, FILE *fp, const char *format, va_list va)\r
-{\r
- PyObject *file;\r
- PyObject *error_type, *error_value, *error_traceback;\r
-\r
- PyErr_Fetch(&error_type, &error_value, &error_traceback);\r
- file = PySys_GetObject(name);\r
- if (file == NULL || PyFile_AsFile(file) == fp)\r
- vfprintf(fp, format, va);\r
- else {\r
- char buffer[1001];\r
- const int written = PyOS_vsnprintf(buffer, sizeof(buffer),\r
- format, va);\r
- if (PyFile_WriteString(buffer, file) != 0) {\r
- PyErr_Clear();\r
- fputs(buffer, fp);\r
- }\r
- if (written < 0 || (size_t)written >= sizeof(buffer)) {\r
- const char *truncated = "... truncated";\r
- if (PyFile_WriteString(truncated, file) != 0) {\r
- PyErr_Clear();\r
- fputs(truncated, fp);\r
- }\r
- }\r
- }\r
- PyErr_Restore(error_type, error_value, error_traceback);\r
-}\r
-\r
-void\r
-PySys_WriteStdout(const char *format, ...)\r
-{\r
- va_list va;\r
-\r
- va_start(va, format);\r
- mywrite("stdout", stdout, format, va);\r
- va_end(va);\r
-}\r
-\r
-void\r
-PySys_WriteStderr(const char *format, ...)\r
-{\r
- va_list va;\r
-\r
- va_start(va, format);\r
- mywrite("stderr", stderr, format, va);\r
- va_end(va);\r
-}\r