diff --git a/Python/sysmodule.c b/Python/sysmodule.c --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -37,6 +37,13 @@ #include #endif +#include "clinic/sysmodule.c.h" + +/*[clinic input] +module sys +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3726b388feee8cea]*/ + _Py_IDENTIFIER(_); _Py_IDENTIFIER(__sizeof__); _Py_IDENTIFIER(buffer); @@ -159,8 +166,18 @@ return ret; } +/*[clinic input] +sys.displayhook + + object: object + / + +Print an object to sys.stdout and also save it in builtins._ +[clinic start generated code]*/ + static PyObject * -sys_displayhook(PyObject *self, PyObject *o) +sys_displayhook(PyModuleDef *module, PyObject *object) +/*[clinic end generated code: output=581d054165ca5788 input=e08de2bfe683416c]*/ { PyObject *outf; PyInterpreterState *interp = PyThreadState_GET()->interp; @@ -178,7 +195,7 @@ /* Print value except if None */ /* After printing, also assign to '_' */ /* Before, set '_' to None to avoid recursion */ - if (o == Py_None) { + if (object == Py_None) { Py_INCREF(Py_None); return Py_None; } @@ -189,12 +206,12 @@ PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); return NULL; } - if (PyFile_WriteObject(o, outf, 0) != 0) { + if (PyFile_WriteObject(object, outf, 0) != 0) { if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { - /* repr(o) is not encodable to sys.stdout.encoding with + /* repr(object) is not encodable to sys.stdout.encoding with * sys.stdout.errors error handler (which is probably 'strict') */ PyErr_Clear(); - err = sys_displayhook_unencodable(outf, o); + err = sys_displayhook_unencodable(outf, object); if (err) return NULL; } @@ -209,37 +226,46 @@ } if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0) return NULL; - if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0) + if (_PyObject_SetAttrId(builtins, &PyId__, object) != 0) return NULL; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } -PyDoc_STRVAR(displayhook_doc, -"displayhook(object) -> None\n" -"\n" -"Print an object to sys.stdout and also save it in builtins._\n" -); + +/*[clinic input] +sys.excepthook + + exctype: object + value: object + traceback: object + / + +Handle an exception by displaying it with a traceback on sys.stderr. +[clinic start generated code]*/ static PyObject * -sys_excepthook(PyObject* self, PyObject* args) +sys_excepthook_impl(PyModuleDef *module, PyObject *exctype, PyObject *value, + PyObject *traceback) +/*[clinic end generated code: output=a052b202ece4bb51 input=ecf606fa826f19d9]*/ { - PyObject *exc, *value, *tb; - if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb)) - return NULL; - PyErr_Display(exc, value, tb); - Py_INCREF(Py_None); - return Py_None; + PyErr_Display(exctype, value, traceback); + Py_RETURN_NONE; } -PyDoc_STRVAR(excepthook_doc, -"excepthook(exctype, value, traceback) -> None\n" -"\n" -"Handle an exception by displaying it with a traceback on sys.stderr.\n" -); + +/*[clinic input] +sys.exc_info + +Return current exception information. + +This is the type, value and traceback of the most recent exception +caught by an except clause in the current stack frame or +in an older stack frame. +[clinic start generated code]*/ static PyObject * -sys_exc_info(PyObject *self, PyObject *noargs) +sys_exc_info_impl(PyModuleDef *module) +/*[clinic end generated code: output=05596f16396192b5 input=e162ffc3102b6c5a]*/ { PyThreadState *tstate; tstate = PyThreadState_GET(); @@ -251,50 +277,55 @@ tstate->exc_traceback : Py_None); } -PyDoc_STRVAR(exc_info_doc, -"exc_info() -> (type, value, traceback)\n\ -\n\ -Return information about the most recent exception caught by an except\n\ -clause in the current stack frame or in an older stack frame." -); + +/*[clinic input] +sys.exit + + status: object = NULL + / + +Exit the interpreter by raising SystemExit(status). + +If the status is omitted or None, it defaults to zero (i.e., success). +If the status is an integer, it will be used as the system exit status. +If it is another kind of object, it will be printed and the system +exit status will be one (i.e., failure). +[clinic start generated code]*/ static PyObject * -sys_exit(PyObject *self, PyObject *args) +sys_exit_impl(PyModuleDef *module, PyObject *status) +/*[clinic end generated code: output=ca485b049e54a389 input=a737351f86685e9c]*/ { - PyObject *exit_code = 0; - if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) - return NULL; /* Raise SystemExit so callers may catch it or clean up. */ - PyErr_SetObject(PyExc_SystemExit, exit_code); + PyErr_SetObject(PyExc_SystemExit, status); return NULL; } -PyDoc_STRVAR(exit_doc, -"exit([status])\n\ -\n\ -Exit the interpreter by raising SystemExit(status).\n\ -If the status is omitted or None, it defaults to zero (i.e., success).\n\ -If the status is an integer, it will be used as the system exit status.\n\ -If it is another kind of object, it will be printed and the system\n\ -exit status will be one (i.e., failure)." -); +/*[clinic input] +sys.getdefaultencoding + +Return the current default string encoding used by the Unicode implementation. +[clinic start generated code]*/ + static PyObject * -sys_getdefaultencoding(PyObject *self) +sys_getdefaultencoding_impl(PyModuleDef *module) +/*[clinic end generated code: output=18f438b2c63d8405 input=436f7292b82bc04c]*/ { return PyUnicode_FromString(PyUnicode_GetDefaultEncoding()); } -PyDoc_STRVAR(getdefaultencoding_doc, -"getdefaultencoding() -> string\n\ -\n\ -Return the current default string encoding used by the Unicode \n\ -implementation." -); + +/*[clinic input] +sys.getfilesystemencoding + +Return the encoding used to convert Unicode filenames in operating system filenames. +[clinic start generated code]*/ static PyObject * -sys_getfilesystemencoding(PyObject *self) +sys_getfilesystemencoding_impl(PyModuleDef *module) +/*[clinic end generated code: output=6676272dd0f3b0ef input=c90a32bdddb10e21]*/ { if (Py_FileSystemDefaultEncoding) return PyUnicode_FromString(Py_FileSystemDefaultEncoding); @@ -303,39 +334,37 @@ return NULL; } -PyDoc_STRVAR(getfilesystemencoding_doc, -"getfilesystemencoding() -> string\n\ -\n\ -Return the encoding used to convert Unicode filenames in\n\ -operating system filenames." -); + +/*[clinic input] +sys.intern + + string: unicode + / + +``Intern'' the given string. + +This enters the string in the (global) table of interned strings whose purpose +is to speed up dictionary lookups. +Return the string itself or the previously interned string object with the +same value. +[clinic start generated code]*/ static PyObject * -sys_intern(PyObject *self, PyObject *args) +sys_intern_impl(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=187ff489a57cc25c input=38a34959fe6a5b9e]*/ { - PyObject *s; - if (!PyArg_ParseTuple(args, "U:intern", &s)) - return NULL; - if (PyUnicode_CheckExact(s)) { - Py_INCREF(s); - PyUnicode_InternInPlace(&s); - return s; + if (PyUnicode_CheckExact(string)) { + Py_INCREF(string); + PyUnicode_InternInPlace(&string); + return string; } else { PyErr_Format(PyExc_TypeError, - "can't intern %.400s", s->ob_type->tp_name); + "can't intern %.400s", string->ob_type->tp_name); return NULL; } } -PyDoc_STRVAR(intern_doc, -"intern(string) -> string\n\ -\n\ -``Intern'' the given string. This enters the string in the (global)\n\ -table of interned strings whose purpose is to speed up dictionary lookups.\n\ -Return the string itself or the previously interned string object with the\n\ -same value."); - /* * Cached interned string objects used for calling the profile and @@ -445,28 +474,44 @@ return 0; } + +/*[clinic input] +sys.settrace + + function: object + / + +Set the global debug tracing function. + +It will be called on each function call. +See the debugger chapter in the library manual. +[clinic start generated code]*/ + static PyObject * -sys_settrace(PyObject *self, PyObject *args) +sys_settrace(PyModuleDef *module, PyObject *function) +/*[clinic end generated code: output=237449f444ce1e3a input=b46cb377e25a6dbb]*/ { if (trace_init() == -1) return NULL; - if (args == Py_None) + if (function == Py_None) PyEval_SetTrace(NULL, NULL); else - PyEval_SetTrace(trace_trampoline, args); - Py_INCREF(Py_None); - return Py_None; + PyEval_SetTrace(trace_trampoline, function); + Py_RETURN_NONE; } -PyDoc_STRVAR(settrace_doc, -"settrace(function)\n\ -\n\ -Set the global debug tracing function. It will be called on each\n\ -function call. See the debugger chapter in the library manual." -); + +/*[clinic input] +sys.gettrace + +Return the global debug tracing function set with sys.settrace(). + +See the debugger chapter in the library manual. +[clinic start generated code]*/ static PyObject * -sys_gettrace(PyObject *self, PyObject *args) +sys_gettrace_impl(PyModuleDef *module) +/*[clinic end generated code: output=81d4b8a3fde95f00 input=067f7d7a09234309]*/ { PyThreadState *tstate = PyThreadState_GET(); PyObject *temp = tstate->c_traceobj; @@ -477,35 +522,44 @@ return temp; } -PyDoc_STRVAR(gettrace_doc, -"gettrace()\n\ -\n\ -Return the global debug tracing function set with sys.settrace.\n\ -See the debugger chapter in the library manual." -); + +/*[clinic input] +sys.setprofile + + function: object + / + +Set the profiling function. + +It will be called on each function call and return. +See the profiler chapter in the library manual. +[clinic start generated code]*/ static PyObject * -sys_setprofile(PyObject *self, PyObject *args) +sys_setprofile(PyModuleDef *module, PyObject *function) +/*[clinic end generated code: output=2652acc08838121c input=27eca0b1f9b03cd5]*/ { if (trace_init() == -1) return NULL; - if (args == Py_None) + if (function == Py_None) PyEval_SetProfile(NULL, NULL); else - PyEval_SetProfile(profile_trampoline, args); - Py_INCREF(Py_None); - return Py_None; + PyEval_SetProfile(profile_trampoline, function); + Py_RETURN_NONE; } -PyDoc_STRVAR(setprofile_doc, -"setprofile(function)\n\ -\n\ -Set the profiling function. It will be called on each function call\n\ -and return. See the profiler chapter in the library manual." -); + +/*[clinic input] +sys.getprofile + +Return the profiling function set with sys.setprofile(). + +See the profiler chapter in the library manual. +[clinic start generated code]*/ static PyObject * -sys_getprofile(PyObject *self, PyObject *args) +sys_getprofile_impl(PyModuleDef *module) +/*[clinic end generated code: output=01d462c04d1b9ecd input=38eaf2e7cc156f7b]*/ { PyThreadState *tstate = PyThreadState_GET(); PyObject *temp = tstate->c_profileobj; @@ -516,38 +570,43 @@ return temp; } -PyDoc_STRVAR(getprofile_doc, -"getprofile()\n\ -\n\ -Return the profiling function set with sys.setprofile.\n\ -See the profiler chapter in the library manual." -); static int _check_interval = 100; +/*[clinic input] +sys.setcheckinterval + + n: int + / + +Tell the Python interpreter to check for asynchronous events every n instructions. + +This also affects how often thread switches occur. +[clinic start generated code]*/ + static PyObject * -sys_setcheckinterval(PyObject *self, PyObject *args) +sys_setcheckinterval_impl(PyModuleDef *module, int n) +/*[clinic end generated code: output=0a379d1d640e52c3 input=88c4246c7077f2f7]*/ { if (PyErr_WarnEx(PyExc_DeprecationWarning, "sys.getcheckinterval() and sys.setcheckinterval() " "are deprecated. Use sys.setswitchinterval() " "instead.", 1) < 0) return NULL; - if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval)) - return NULL; - Py_INCREF(Py_None); - return Py_None; + _check_interval = n; + Py_RETURN_NONE; } -PyDoc_STRVAR(setcheckinterval_doc, -"setcheckinterval(n)\n\ -\n\ -Tell the Python interpreter to check for asynchronous events every\n\ -n instructions. This also affects how often thread switches occur." -); + +/*[clinic input] +sys.getcheckinterval + +Return the current check interval; see sys.setcheckinterval(). +[clinic start generated code]*/ static PyObject * -sys_getcheckinterval(PyObject *self, PyObject *args) +sys_getcheckinterval_impl(PyModuleDef *module) +/*[clinic end generated code: output=f1ab0e8bd4abfe0a input=4b6589cbcca1db4e]*/ { if (PyErr_WarnEx(PyExc_DeprecationWarning, "sys.getcheckinterval() and sys.setcheckinterval() " @@ -557,96 +616,122 @@ return PyLong_FromLong(_check_interval); } -PyDoc_STRVAR(getcheckinterval_doc, -"getcheckinterval() -> current check interval; see setcheckinterval()." -); #ifdef WITH_THREAD + +/*[clinic input] +sys.setswitchinterval + + interval: double + / + +Set the ideal thread switching delay inside the Python interpreter. + +The actual frequency of switching threads can be lower if the +interpreter executes long sequences of uninterruptible code +(this is implementation-specific and workload-dependent). + +The parameter must represent the desired switching delay in seconds +A typical value is 0.005 (5 milliseconds). +[clinic start generated code]*/ + static PyObject * -sys_setswitchinterval(PyObject *self, PyObject *args) +sys_setswitchinterval_impl(PyModuleDef *module, double interval) +/*[clinic end generated code: output=cf480aee7cfdb8ba input=561b477134df91d9]*/ { - double d; - if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d)) - return NULL; - if (d <= 0.0) { + if (interval <= 0.0) { PyErr_SetString(PyExc_ValueError, "switch interval must be strictly positive"); return NULL; } - _PyEval_SetSwitchInterval((unsigned long) (1e6 * d)); - Py_INCREF(Py_None); - return Py_None; + _PyEval_SetSwitchInterval((unsigned long) (1e6 * interval)); + Py_RETURN_NONE; } -PyDoc_STRVAR(setswitchinterval_doc, -"setswitchinterval(n)\n\ -\n\ -Set the ideal thread switching delay inside the Python interpreter\n\ -The actual frequency of switching threads can be lower if the\n\ -interpreter executes long sequences of uninterruptible code\n\ -(this is implementation-specific and workload-dependent).\n\ -\n\ -The parameter must represent the desired switching delay in seconds\n\ -A typical value is 0.005 (5 milliseconds)." -); -static PyObject * -sys_getswitchinterval(PyObject *self, PyObject *args) +/*[clinic input] +sys.getswitchinterval -> double + +Return the current thread switch interval; see sys.setswitchinterval(). +[clinic start generated code]*/ + +static double +sys_getswitchinterval_impl(PyModuleDef *module) +/*[clinic end generated code: output=e05215dea5adfe55 input=bdf9d39c0ebbbb6f]*/ { - return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval()); + return 1e-6 * _PyEval_GetSwitchInterval(); } -PyDoc_STRVAR(getswitchinterval_doc, -"getswitchinterval() -> current thread switch interval; see setswitchinterval()." -); - #endif /* WITH_THREAD */ + #ifdef WITH_TSC + +/*[clinic input] +sys.settscdump + + on: int + / + +If on is true, tell the Python interpreter to dump VM measurements to stderr. + +If on is false, turn off dump. The measurements are based on the +processor's time-stamp counter. +[clinic start generated code]*/ + static PyObject * -sys_settscdump(PyObject *self, PyObject *args) +sys_settscdump_impl(PyModuleDef *module, int on) +/*[clinic end generated code: output=c63ce5b91bc36212 input=c190bf5c4a23c364]*/ { - int bool; PyThreadState *tstate = PyThreadState_Get(); - if (!PyArg_ParseTuple(args, "i:settscdump", &bool)) - return NULL; - if (bool) + if (on) tstate->interp->tscdump = 1; else tstate->interp->tscdump = 0; - Py_INCREF(Py_None); - return Py_None; - + Py_RETURN_NONE; } -PyDoc_STRVAR(settscdump_doc, -"settscdump(bool)\n\ -\n\ -If true, tell the Python interpreter to dump VM measurements to\n\ -stderr. If false, turn off dump. The measurements are based on the\n\ -processor's time-stamp counter." -); #endif /* TSC */ + +/*[clinic input] +sys.setrecursionlimit + + limit: int + / + +Set the maximum depth of the Python interpreter stack to limit. + +This limit prevents infinite recursion from causing an overflow of the C +stack and crashing Python. The highest possible limit is platform-dependent. +[clinic start generated code]*/ + static PyObject * -sys_setrecursionlimit(PyObject *self, PyObject *args) +sys_setrecursionlimit_impl(PyModuleDef *module, int limit) +/*[clinic end generated code: output=78092de0d650c220 input=8e9e9f4aaca07f74]*/ { - int new_limit; - if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) - return NULL; - if (new_limit <= 0) { + if (limit <= 0) { PyErr_SetString(PyExc_ValueError, "recursion limit must be positive"); return NULL; } - Py_SetRecursionLimit(new_limit); - Py_INCREF(Py_None); - return Py_None; + Py_SetRecursionLimit(limit); + Py_RETURN_NONE; } +/*[clinic input] +sys.set_coroutine_wrapper + + wrapper: object + / + +Set a wrapper for coroutine objects. +[clinic start generated code]*/ + static PyObject * -sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper) +sys_set_coroutine_wrapper(PyModuleDef *module, PyObject *wrapper) +/*[clinic end generated code: output=0616b8831e31c302 input=df6ac09a06afef34]*/ { if (wrapper != Py_None) { if (!PyCallable_Check(wrapper)) { @@ -663,14 +748,15 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(set_coroutine_wrapper_doc, -"set_coroutine_wrapper(wrapper)\n\ -\n\ -Set a wrapper for coroutine objects." -); +/*[clinic input] +sys.get_coroutine_wrapper -> object + +Return the wrapper for coroutine objects set by sys.set_coroutine_wrapper. +[clinic start generated code]*/ static PyObject * -sys_get_coroutine_wrapper(PyObject *self, PyObject *args) +sys_get_coroutine_wrapper_impl(PyModuleDef *module) +/*[clinic end generated code: output=086f748ff1fa1733 input=d472f5e27ff68b63]*/ { PyObject *wrapper = PyEval_GetCoroutineWrapper(); if (wrapper == NULL) { @@ -680,21 +766,9 @@ return wrapper; } -PyDoc_STRVAR(get_coroutine_wrapper_doc, -"get_coroutine_wrapper()\n\ -\n\ -Return the wrapper for coroutine objects set by sys.set_coroutine_wrapper." -); - static PyTypeObject Hash_InfoType; -PyDoc_STRVAR(hash_info_doc, -"hash_info\n\ -\n\ -A struct sequence providing parameters used for computing\n\ -hashes. The attributes are read only."); - static PyStructSequence_Field hash_info_fields[] = { {"width", "width of the type used for hashing, in bits"}, {"modulus", "prime number giving the modulus on which the hash " @@ -710,6 +784,12 @@ {NULL, NULL} }; +PyDoc_STRVAR(hash_info_doc, +"hash_info\n\ +\n\ +A struct sequence providing parameters used for computing\n\ +hashes. The attributes are read only."); + static PyStructSequence_Desc hash_info_desc = { "sys.hash_info", hash_info_doc, @@ -753,35 +833,25 @@ } -PyDoc_STRVAR(setrecursionlimit_doc, -"setrecursionlimit(n)\n\ -\n\ -Set the maximum depth of the Python interpreter stack to n. This\n\ -limit prevents infinite recursion from causing an overflow of the C\n\ -stack and crashing Python. The highest possible limit is platform-\n\ -dependent." -); +/*[clinic input] +sys.getrecursionlimit + +Return the current value of the recursion limit. + +See sys.setrecursionlimit() for details. +[clinic start generated code]*/ static PyObject * -sys_getrecursionlimit(PyObject *self) +sys_getrecursionlimit_impl(PyModuleDef *module) +/*[clinic end generated code: output=0d6feb2b1936329c input=902f1ddb1781b4c2]*/ { return PyLong_FromLong(Py_GetRecursionLimit()); } -PyDoc_STRVAR(getrecursionlimit_doc, -"getrecursionlimit()\n\ -\n\ -Return the current value of the recursion limit, the maximum depth\n\ -of the Python interpreter stack. This limit prevents infinite\n\ -recursion from causing an overflow of the C stack and crashing Python." -); #ifdef MS_WINDOWS -PyDoc_STRVAR(getwindowsversion_doc, -"getwindowsversion()\n\ -\n\ -Return information about the running version of Windows as a named tuple.\n\ -The members are named: major, minor, build, platform, service_pack,\n\ +PyDoc_STRVAR(windows_version_doc, +"The members are named: major, minor, build, platform, service_pack,\n\ service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\ backward compatibility, only the first 5 items are available by indexing.\n\ All elements are numbers, except service_pack which is a string. Platform\n\ @@ -807,7 +877,7 @@ static PyStructSequence_Desc windows_version_desc = { "sys.getwindowsversion", /* name */ - getwindowsversion_doc, /* doc */ + windows_version_doc, /* doc */ windows_version_fields, /* fields */ 5 /* For backward compatibility, only the first 5 items are accessible @@ -820,8 +890,23 @@ #pragma warning(push) #pragma warning(disable:4996) +/*[clinic input] +sys.getwindowsversion + +Return information about the running version of Windows as a named tuple. + +The members are named: major, minor, build, platform, service_pack, +service_pack_major, service_pack_minor, suite_mask, and product_type. For +backward compatibility, only the first 5 items are available by indexing. +All elements are numbers, except service_pack which is a string. Platform +may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7, +3 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain +controller, 3 for a server. +[clinic start generated code]*/ + static PyObject * -sys_getwindowsversion(PyObject *self) +sys_getwindowsversion_impl(PyModuleDef *module) +/*[clinic end generated code: output=2943ce0bfc2e02f6 input=b616b6ff271bd44a]*/ { PyObject *version; int pos = 0; @@ -856,32 +941,46 @@ #endif /* MS_WINDOWS */ #ifdef HAVE_DLOPEN + +/*[clinic input] +sys.setdlopenflags + + flags: int + / + +Set the flags used by the interpreter for dlopen calls. + +This is used for example when the interpreter loads extension modules. +Among other things, this will enable a lazy resolving of symbols when +importing a module, if called as sys.setdlopenflags(0). To share symbols +across extension modules, call as sys.setdlopenflags(os.RTLD_GLOBAL). +Symbolic names for the flag modules can be found in the os module +(RTLD_xxx constants, e.g. os.RTLD_LAZY). +[clinic start generated code]*/ + static PyObject * -sys_setdlopenflags(PyObject *self, PyObject *args) +sys_setdlopenflags_impl(PyModuleDef *module, int flags) +/*[clinic end generated code: output=f0eb365004cc90b1 input=616d6b74d56d14a8]*/ { - int new_val; PyThreadState *tstate = PyThreadState_GET(); - if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) - return NULL; if (!tstate) return NULL; - tstate->interp->dlopenflags = new_val; - Py_INCREF(Py_None); - return Py_None; + tstate->interp->dlopenflags = flags; + Py_RETURN_NONE; } -PyDoc_STRVAR(setdlopenflags_doc, -"setdlopenflags(n) -> None\n\ -\n\ -Set the flags used by the interpreter for dlopen calls, such as when the\n\ -interpreter loads extension modules. Among other things, this will enable\n\ -a lazy resolving of symbols when importing a module, if called as\n\ -sys.setdlopenflags(0). To share symbols across extension modules, call as\n\ -sys.setdlopenflags(os.RTLD_GLOBAL). Symbolic names for the flag modules\n\ -can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY)."); + +/*[clinic input] +sys.getdlopenflags + +Return the current value of the flags that are used for dlopen calls. + +The flag constants are defined in the os module. +[clinic start generated code]*/ static PyObject * -sys_getdlopenflags(PyObject *self, PyObject *args) +sys_getdlopenflags_impl(PyModuleDef *module) +/*[clinic end generated code: output=d7b5b1ebb448088b input=dc4ea0899c53b4b6]*/ { PyThreadState *tstate = PyThreadState_GET(); if (!tstate) @@ -889,30 +988,31 @@ return PyLong_FromLong(tstate->interp->dlopenflags); } -PyDoc_STRVAR(getdlopenflags_doc, -"getdlopenflags() -> int\n\ -\n\ -Return the current value of the flags that are used for dlopen calls.\n\ -The flag constants are defined in the os module."); +#endif /* HAVE_DLOPEN */ -#endif /* HAVE_DLOPEN */ #ifdef USE_MALLOPT /* Link with -lmalloc (or -lmpc) on an SGI */ #include +/*[clinic input] +sys.mdebug + + flag: int + / +[clinic start generated code]*/ + static PyObject * -sys_mdebug(PyObject *self, PyObject *args) +sys_mdebug_impl(PyModuleDef *module, int flag) +/*[clinic end generated code: output=8f227b7ea315cd3d input=151d150ae1636f8a]*/ { int flag; - if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) - return NULL; mallopt(M_DEBUG, flag); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } #endif /* USE_MALLOPT */ + size_t _PySys_GetSizeOf(PyObject *o) { @@ -955,25 +1055,30 @@ return (size_t)size; } +/*[clinic input] +sys.getsizeof + + object: object + default: object = NULL + +Return the size of object in bytes. +[clinic start generated code]*/ + static PyObject * -sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) +sys_getsizeof_impl(PyModuleDef *module, PyObject *object, + PyObject *default_value) +/*[clinic end generated code: output=c20fb351726e5931 input=f2f17de88a3207cb]*/ { - static char *kwlist[] = {"object", "default", 0}; size_t size; - PyObject *o, *dflt = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", - kwlist, &o, &dflt)) - return NULL; - - size = _PySys_GetSizeOf(o); + size = _PySys_GetSizeOf(object); if (size == (size_t)-1 && PyErr_Occurred()) { /* Has a default value been given */ - if (dflt != NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { + if (default_value != NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { PyErr_Clear(); - Py_INCREF(dflt); - return dflt; + Py_INCREF(default_value); + return default_value; } else return NULL; @@ -982,76 +1087,97 @@ return PyLong_FromSize_t(size); } -PyDoc_STRVAR(getsizeof_doc, -"getsizeof(object, default) -> int\n\ -\n\ -Return the size of object in bytes."); + +/*[clinic input] +sys.getrefcount -> Py_ssize_t + + object: object + / + +Return the reference count of object. + +The count returned is generally one higher than you might expect, +because it includes the (temporary) reference as an argument to +getrefcount(). +[clinic start generated code]*/ + +static Py_ssize_t +sys_getrefcount_impl(PyModuleDef *module, PyObject *object) +/*[clinic end generated code: output=a13a0675e4f709b7 input=bf474efd50a21535]*/ +{ + return object->ob_refcnt; +} + + +#ifdef Py_REF_DEBUG + +/*[clinic input] +sys.gettotalrefcount -> Py_ssize_t +[clinic start generated code]*/ + +static Py_ssize_t +sys_gettotalrefcount_impl(PyModuleDef *module) +/*[clinic end generated code: output=69491aa903968ec7 input=53b744faa5d2e4f6]*/ +{ + return _Py_GetRefTotal(); +} + +#endif /* Py_REF_DEBUG */ + + +/*[clinic input] +sys.getallocatedblocks -> Py_ssize_t + +Return the number of memory blocks currently allocated, regardless of their size. +[clinic start generated code]*/ + +static Py_ssize_t +sys_getallocatedblocks_impl(PyModuleDef *module) +/*[clinic end generated code: output=06f4be191b50d4fe input=ce784d876c0f9947]*/ +{ + return _Py_GetAllocatedBlocks(); +} + + +#ifdef COUNT_ALLOCS + +/*[clinic input] +sys.getcounts +[clinic start generated code]*/ static PyObject * -sys_getrefcount(PyObject *self, PyObject *arg) -{ - return PyLong_FromSsize_t(arg->ob_refcnt); -} - -#ifdef Py_REF_DEBUG -static PyObject * -sys_gettotalrefcount(PyObject *self) -{ - return PyLong_FromSsize_t(_Py_GetRefTotal()); -} -#endif /* Py_REF_DEBUG */ - -PyDoc_STRVAR(getrefcount_doc, -"getrefcount(object) -> integer\n\ -\n\ -Return the reference count of object. The count returned is generally\n\ -one higher than you might expect, because it includes the (temporary)\n\ -reference as an argument to getrefcount()." -); - -static PyObject * -sys_getallocatedblocks(PyObject *self) -{ - return PyLong_FromSsize_t(_Py_GetAllocatedBlocks()); -} - -PyDoc_STRVAR(getallocatedblocks_doc, -"getallocatedblocks() -> integer\n\ -\n\ -Return the number of memory blocks currently allocated, regardless of their\n\ -size." -); - -#ifdef COUNT_ALLOCS -static PyObject * -sys_getcounts(PyObject *self) +sys_getcounts_impl(PyModuleDef *module) +/*[clinic end generated code: output=ab3f2ec161f3e1dd input=ad2ec7bda5424953]*/ { extern PyObject *get_counts(void); return get_counts(); } + #endif -PyDoc_STRVAR(getframe_doc, -"_getframe([depth]) -> frameobject\n\ -\n\ -Return a frame object from the call stack. If optional integer depth is\n\ -given, return the frame object that many calls below the top of the stack.\n\ -If that is deeper than the call stack, ValueError is raised. The default\n\ -for depth is zero, returning the frame at the top of the call stack.\n\ -\n\ -This function should be used for internal and specialized\n\ -purposes only." -); + +/*[clinic input] +sys._getframe + + depth: int = 0 + / + +Return the top frame object from the call stack. + +If optional integer depth is positive, return the frame object that many +calls below the top of the stack. If that is deeper than the call stack, +ValueError is raised. The default for depth is zero, returning the frame +at the top of the call stack. + +This function should be used for internal and specialized purposes only. +[clinic start generated code]*/ static PyObject * -sys_getframe(PyObject *self, PyObject *args) +sys__getframe_impl(PyModuleDef *module, int depth) +/*[clinic end generated code: output=37cee744edba9db4 input=06dcf1dba213774f]*/ { PyFrameObject *f = PyThreadState_GET()->frame; - int depth = -1; - - if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) - return NULL; while (depth > 0 && f != NULL) { f = f->f_back; @@ -1063,70 +1189,64 @@ return NULL; } Py_INCREF(f); - return (PyObject*)f; + return (PyObject *)f; } -PyDoc_STRVAR(current_frames_doc, -"_current_frames() -> dictionary\n\ -\n\ -Return a dictionary mapping each current thread T's thread id to T's\n\ -current stack frame.\n\ -\n\ -This function should be used for specialized purposes only." -); + +/*[clinic input] +sys._current_frames + +Return a dictionary mapping each current thread T's thread id to T's current stack frame. + +This function should be used for internal and specialized purposes only. +[clinic start generated code]*/ static PyObject * -sys_current_frames(PyObject *self, PyObject *noargs) +sys__current_frames_impl(PyModuleDef *module) +/*[clinic end generated code: output=d74ce37174bb6c09 input=4d6abe9c2b9d14eb]*/ { return _PyThread_CurrentFrames(); } -PyDoc_STRVAR(call_tracing_doc, -"call_tracing(func, args) -> object\n\ -\n\ -Call func(*args), while tracing is enabled. The tracing state is\n\ -saved, and restored afterwards. This is intended to be called from\n\ -a debugger from a checkpoint, to recursively debug some other code." -); + +/*[clinic input] +sys.call_tracing + + func: object + args as args_value: object(subclass_of='&PyTuple_Type') + / + +Call func(*args), while tracing is enabled. + +The tracing state is saved, and restored afterwards. +This is intended to be called from a debugger from a checkpoint, +to recursively debug some other code. +[clinic start generated code]*/ static PyObject * -sys_call_tracing(PyObject *self, PyObject *args) +sys_call_tracing_impl(PyModuleDef *module, PyObject *func, + PyObject *args_value) +/*[clinic end generated code: output=c4dfb52e1b13dece input=f23e37bbffed90a9]*/ { - PyObject *func, *funcargs; - if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs)) - return NULL; - return _PyEval_CallTracing(func, funcargs); + return _PyEval_CallTracing(func, args_value); } -PyDoc_STRVAR(callstats_doc, -"callstats() -> tuple of integers\n\ -\n\ -Return a tuple of function call statistics, if CALL_PROFILE was defined\n\ -when Python was built. Otherwise, return None.\n\ -\n\ -When enabled, this function returns detailed, implementation-specific\n\ -details about the number of function calls executed. The return value is\n\ -a 11-tuple where the entries in the tuple are counts of:\n\ -0. all function calls\n\ -1. calls to PyFunction_Type objects\n\ -2. PyFunction calls that do not create an argument tuple\n\ -3. PyFunction calls that do not create an argument tuple\n\ - and bypass PyEval_EvalCodeEx()\n\ -4. PyMethod calls\n\ -5. PyMethod calls on bound methods\n\ -6. PyType calls\n\ -7. PyCFunction calls\n\ -8. generator calls\n\ -9. All other calls\n\ -10. Number of stack pops performed by call_function()" -); #ifdef __cplusplus extern "C" { #endif +/*[clinic input] +sys._debugmallocstats + +Print summary info to stderr about the state of pymalloc's structures. + +In Py_DEBUG mode, also perform some expensive internal consistency checks. +[clinic start generated code]*/ + static PyObject * -sys_debugmallocstats(PyObject *self, PyObject *args) +sys__debugmallocstats_impl(PyModuleDef *module) +/*[clinic end generated code: output=86ed75e0c0f9baf1 input=0602cf4a7f2ea1db]*/ { #ifdef WITH_PYMALLOC _PyObject_DebugMallocStats(stderr); @@ -1136,15 +1256,7 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(debugmallocstats_doc, -"_debugmallocstats()\n\ -\n\ -Print summary info to stderr about the state of\n\ -pymalloc's structures.\n\ -\n\ -In Py_DEBUG mode, also perform some expensive internal consistency\n\ -checks.\n\ -"); + #ifdef Py_TRACE_REFS /* Defined in objects.c because it uses static globals if that file */ @@ -1160,107 +1272,132 @@ } #endif + +/*[clinic input] +sys._clear_type_cache + +Clear the internal type lookup cache. +[clinic start generated code]*/ + static PyObject * -sys_clear_type_cache(PyObject* self, PyObject* args) +sys__clear_type_cache_impl(PyModuleDef *module) +/*[clinic end generated code: output=06f384c5a402367b input=127f3e04a8d9b555]*/ { PyType_ClearCache(); Py_RETURN_NONE; } -PyDoc_STRVAR(sys_clear_type_cache__doc__, -"_clear_type_cache() -> None\n\ -Clear the internal type lookup cache."); + +/*[clinic input] +sys.callstats -> object + +Return a tuple of function call statistics. + +This is only enabled if CALL_PROFILE was defined when Python was built. +Otherwise, this returns None. + +When enabled, this function returns detailed, implementation-specific +details about the number of function calls executed. The return value is +a 11-tuple where the entries in the tuple are counts of: +0. all function calls +1. calls to PyFunction_Type objects +2. PyFunction calls that do not create an argument tuple +3. PyFunction calls that do not create an argument tuple + and bypass PyEval_EvalCodeEx() +4. PyMethod calls +5. PyMethod calls on bound methods +6. PyType calls +7. PyCFunction calls +8. generator calls +9. all other calls +10. number of stack pops performed by call_function() +[clinic start generated code]*/ static PyObject * -sys_is_finalizing(PyObject* self, PyObject* args) +sys_callstats_impl(PyModuleDef *module) +/*[clinic end generated code: output=dba5f57ac8a314b1 input=8b71f6402011c920]*/ +{ + return PyEval_GetCallStats((PyObject *)module); +} + + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef Py_TRACE_REFS +/* Defined in objects.c because it uses static globals if that file */ +extern PyObject *_Py_GetObjects(PyObject *, PyObject *); +#endif + +#ifdef DYNAMIC_EXECUTION_PROFILE +/* Defined in ceval.c because it uses static globals if that file */ +extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); +#endif + +#ifdef __cplusplus +} +#endif + + +/*[clinic input] +sys.is_finalizing + +Return True if Python is exiting. +[clinic start generated code]*/ + +static PyObject * +sys_is_finalizing_impl(PyModuleDef *module) +/*[clinic end generated code: output=57e8e590512b94fa input=f0df747a039948a5]*/ { return PyBool_FromLong(_Py_Finalizing != NULL); } -PyDoc_STRVAR(is_finalizing_doc, -"is_finalizing()\n\ -Return True if Python is exiting."); - static PyMethodDef sys_methods[] = { /* Might as well keep this in alphabetic order */ - {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, - callstats_doc}, - {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS, - sys_clear_type_cache__doc__}, - {"_current_frames", sys_current_frames, METH_NOARGS, - current_frames_doc}, - {"displayhook", sys_displayhook, METH_O, displayhook_doc}, - {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc}, - {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc}, - {"exit", sys_exit, METH_VARARGS, exit_doc}, - {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, - METH_NOARGS, getdefaultencoding_doc}, -#ifdef HAVE_DLOPEN - {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS, - getdlopenflags_doc}, -#endif - {"getallocatedblocks", (PyCFunction)sys_getallocatedblocks, METH_NOARGS, - getallocatedblocks_doc}, -#ifdef COUNT_ALLOCS - {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS}, -#endif + SYS_CALLSTATS_METHODDEF + SYS__CLEAR_TYPE_CACHE_METHODDEF + SYS__CURRENT_FRAMES_METHODDEF + SYS_DISPLAYHOOK_METHODDEF + SYS_EXC_INFO_METHODDEF + SYS_EXCEPTHOOK_METHODDEF + SYS_EXIT_METHODDEF + SYS_GETDEFAULTENCODING_METHODDEF + SYS_GETFILESYSTEMENCODING_METHODDEF + SYS_GETDLOPENFLAGS_METHODDEF + SYS_GETALLOCATEDBLOCKS_METHODDEF + SYS_GETCOUNTS_METHODDEF #ifdef DYNAMIC_EXECUTION_PROFILE {"getdxp", _Py_GetDXProfile, METH_VARARGS}, #endif - {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, - METH_NOARGS, getfilesystemencoding_doc}, #ifdef Py_TRACE_REFS {"getobjects", _Py_GetObjects, METH_VARARGS}, #endif -#ifdef Py_REF_DEBUG - {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, -#endif - {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, - {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, - getrecursionlimit_doc}, - {"getsizeof", (PyCFunction)sys_getsizeof, - METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, - {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, -#ifdef MS_WINDOWS - {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, - getwindowsversion_doc}, -#endif /* MS_WINDOWS */ - {"intern", sys_intern, METH_VARARGS, intern_doc}, - {"is_finalizing", sys_is_finalizing, METH_NOARGS, is_finalizing_doc}, -#ifdef USE_MALLOPT - {"mdebug", sys_mdebug, METH_VARARGS}, -#endif - {"setcheckinterval", sys_setcheckinterval, METH_VARARGS, - setcheckinterval_doc}, - {"getcheckinterval", sys_getcheckinterval, METH_NOARGS, - getcheckinterval_doc}, -#ifdef WITH_THREAD - {"setswitchinterval", sys_setswitchinterval, METH_VARARGS, - setswitchinterval_doc}, - {"getswitchinterval", sys_getswitchinterval, METH_NOARGS, - getswitchinterval_doc}, -#endif -#ifdef HAVE_DLOPEN - {"setdlopenflags", sys_setdlopenflags, METH_VARARGS, - setdlopenflags_doc}, -#endif - {"setprofile", sys_setprofile, METH_O, setprofile_doc}, - {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc}, - {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, - setrecursionlimit_doc}, -#ifdef WITH_TSC - {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc}, -#endif - {"settrace", sys_settrace, METH_O, settrace_doc}, - {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc}, - {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, - {"_debugmallocstats", sys_debugmallocstats, METH_NOARGS, - debugmallocstats_doc}, - {"set_coroutine_wrapper", sys_set_coroutine_wrapper, METH_O, - set_coroutine_wrapper_doc}, - {"get_coroutine_wrapper", sys_get_coroutine_wrapper, METH_NOARGS, - get_coroutine_wrapper_doc}, + SYS_GETTOTALREFCOUNT_METHODDEF + SYS_GETREFCOUNT_METHODDEF + SYS_GETRECURSIONLIMIT_METHODDEF + SYS_GETSIZEOF_METHODDEF + SYS__GETFRAME_METHODDEF + SYS_GETWINDOWSVERSION_METHODDEF + SYS_INTERN_METHODDEF + SYS_IS_FINALIZING_METHODDEF + SYS_MDEBUG_METHODDEF + SYS_SETCHECKINTERVAL_METHODDEF + SYS_GETCHECKINTERVAL_METHODDEF + SYS_SETSWITCHINTERVAL_METHODDEF + SYS_GETSWITCHINTERVAL_METHODDEF + SYS_SETDLOPENFLAGS_METHODDEF + SYS_SETPROFILE_METHODDEF + SYS_GETPROFILE_METHODDEF + SYS_SETRECURSIONLIMIT_METHODDEF + SYS_SETTSCDUMP_METHODDEF + SYS_SETTRACE_METHODDEF + SYS_GETTRACE_METHODDEF + SYS_CALL_TRACING_METHODDEF + SYS__DEBUGMALLOCSTATS_METHODDEF + SYS_SET_COROUTINE_WRAPPER_METHODDEF + SYS_GET_COROUTINE_WRAPPER_METHODDEF {NULL, NULL} /* sentinel */ };