From 86a58dabec40bfb4c112b918cabd7eb21e52183c Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Thu, 20 Nov 2014 21:39:37 +1000 Subject: Issue #22869: Split pythonrun into two modules - interpreter startup and shutdown code moved to a new pylifecycle.c module - Py_OptimizeFlag moved into the new module with the other global flags --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 106fc84fa9..6fb882f015 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1681,7 +1681,7 @@ _PySys_Init(void) } #endif - /* stdin/stdout/stderr are now set by pythonrun.c */ + /* stdin/stdout/stderr are set in pylifecycle.c */ SET_SYS_FROM_STRING_BORROW("__displayhook__", PyDict_GetItemString(sysdict, "displayhook")); -- cgit v1.2.1 From a8d66b4ff6f2d4115ccd355ab1b3bc8b263bde90 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sun, 7 Dec 2014 01:28:27 +0100 Subject: Issue #22696: Add function :func:`sys.is_finalizing` to know about interpreter shutdown. --- Python/sysmodule.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 6fb882f015..aa4046ff93 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1121,6 +1121,16 @@ PyDoc_STRVAR(sys_clear_type_cache__doc__, "_clear_type_cache() -> None\n\ Clear the internal type lookup cache."); +static PyObject * +sys_is_finalizing(PyObject* self, PyObject* args) +{ + 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 */ @@ -1167,6 +1177,7 @@ static PyMethodDef sys_methods[] = { 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 -- cgit v1.2.1 From 4b5c8edd294f02afef7407d9fd5236789bd4bc34 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sat, 21 Feb 2015 08:44:05 -0800 Subject: Issue #23152: Implement _Py_fstat() to support files larger than 2 GB on Windows. fstat() may fail with EOVERFLOW on files larger than 2 GB because the file size type is an signed 32-bit integer. --- Python/sysmodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 0639231282..292830bc25 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1681,8 +1681,8 @@ _PySys_Init(void) the shell already prevents that. */ #if !defined(MS_WINDOWS) { - struct stat sb; - if (fstat(fileno(stdin), &sb) == 0 && + struct _Py_stat_struct sb; + if (_Py_fstat(fileno(stdin), &sb) == 0 && S_ISDIR(sb.st_mode)) { /* There's nothing more we can do. */ /* Py_FatalError() will core dump, so just exit. */ -- cgit v1.2.1 From 547de7c7bdd902a96d8b74f35e3a24c4eeabaf2f Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Mon, 2 Mar 2015 08:01:10 -0800 Subject: Issue #23451: Update pyconfig.h for Windows to require Vista headers and remove unnecessary version checks. --- Python/sysmodule.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 292830bc25..471389cf57 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -772,6 +772,12 @@ static PyStructSequence_Desc windows_version_desc = { via indexing, the rest are name only */ }; +/* Disable deprecation warnings about GetVersionEx as the result is + being passed straight through to the caller, who is responsible for + using it correctly. */ +#pragma warning(push) +#pragma warning(disable:4996) + static PyObject * sys_getwindowsversion(PyObject *self) { @@ -803,6 +809,8 @@ sys_getwindowsversion(PyObject *self) return version; } +#pragma warning(pop) + #endif /* MS_WINDOWS */ #ifdef HAVE_DLOPEN -- cgit v1.2.1 From 0600ebc4d9a0fbb0f16689fb2c1d6a09482594cc Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 30 Mar 2015 10:09:31 +0200 Subject: Issue #23752: _Py_fstat() is now responsible to raise the Python exception Add _Py_fstat_noraise() function when a Python exception is not welcome. --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 471389cf57..9ec25216ed 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1690,7 +1690,7 @@ _PySys_Init(void) #if !defined(MS_WINDOWS) { struct _Py_stat_struct sb; - if (_Py_fstat(fileno(stdin), &sb) == 0 && + if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 && S_ISDIR(sb.st_mode)) { /* There's nothing more we can do. */ /* Py_FatalError() will core dump, so just exit. */ -- cgit v1.2.1 From ea639832c36905ecb07caba111f614c4bbf9bdd6 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Mon, 11 May 2015 22:57:16 -0400 Subject: PEP 0492 -- Coroutines with async and await syntax. Issue #24017. --- Python/sysmodule.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 9ec25216ed..bf9a96fb75 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -645,6 +645,49 @@ sys_setrecursionlimit(PyObject *self, PyObject *args) return Py_None; } +static PyObject * +sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper) +{ + if (wrapper != Py_None) { + if (!PyCallable_Check(wrapper)) { + PyErr_Format(PyExc_TypeError, + "callable expected, got %.50s", + Py_TYPE(wrapper)->tp_name); + return NULL; + } + + PyEval_SetCoroutineWrapper(wrapper); + } + else + PyEval_SetCoroutineWrapper(NULL); + Py_INCREF(Py_None); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(set_coroutine_wrapper_doc, +"set_coroutine_wrapper(wrapper)\n\ +\n\ +Set a wrapper for coroutine objects." +); + +static PyObject * +sys_get_coroutine_wrapper(PyObject *self, PyObject *args) +{ + PyObject *wrapper = PyEval_GetCoroutineWrapper(); + if (wrapper == NULL) { + wrapper = Py_None; + } + Py_INCREF(wrapper); + 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, @@ -1215,6 +1258,10 @@ static PyMethodDef sys_methods[] = { {"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}, {NULL, NULL} /* sentinel */ }; -- cgit v1.2.1 From 77a04205150a46220b09d9ef8106d0f5667e4558 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 12 May 2015 11:30:14 -0400 Subject: Issue #24017: Plug ref leak. --- Python/sysmodule.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index bf9a96fb75..5f7c0ebba6 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -655,12 +655,10 @@ sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper) Py_TYPE(wrapper)->tp_name); return NULL; } - PyEval_SetCoroutineWrapper(wrapper); } else PyEval_SetCoroutineWrapper(NULL); - Py_INCREF(Py_None); Py_RETURN_NONE; } -- cgit v1.2.1 From 096d40afdee98338a16811681c687475fa52c6c0 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 12 May 2015 11:32:41 -0400 Subject: use our normal bracing style --- Python/sysmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 5f7c0ebba6..149b76eea7 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -657,8 +657,9 @@ sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper) } PyEval_SetCoroutineWrapper(wrapper); } - else + else { PyEval_SetCoroutineWrapper(NULL); + } Py_RETURN_NONE; } -- cgit v1.2.1 From dab9b839d0e4eb315c219433c19c999729a39a95 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Mon, 1 Jun 2015 12:15:23 -0400 Subject: Issue 24017: Make PyEval_(Set|Get)CoroutineWrapper private --- Python/sysmodule.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 149b76eea7..f600baf10e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -655,10 +655,10 @@ sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper) Py_TYPE(wrapper)->tp_name); return NULL; } - PyEval_SetCoroutineWrapper(wrapper); + _PyEval_SetCoroutineWrapper(wrapper); } else { - PyEval_SetCoroutineWrapper(NULL); + _PyEval_SetCoroutineWrapper(NULL); } Py_RETURN_NONE; } @@ -672,7 +672,7 @@ Set a wrapper for coroutine objects." static PyObject * sys_get_coroutine_wrapper(PyObject *self, PyObject *args) { - PyObject *wrapper = PyEval_GetCoroutineWrapper(); + PyObject *wrapper = _PyEval_GetCoroutineWrapper(); if (wrapper == NULL) { wrapper = Py_None; } -- cgit v1.2.1 From 74bc8ad30176e4759485b13c3ca09b007ff82576 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 13 Oct 2015 00:11:21 +0200 Subject: sys.setrecursionlimit() now raises RecursionError Issue #25274: sys.setrecursionlimit() now raises a RecursionError if the new recursion limit is too low depending at the current recursion depth. Modify also the "lower-water mark" formula to make it monotonic. This mark is used to decide when the overflowed flag of the thread state is reset. --- Python/sysmodule.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index f600baf10e..334f5d0c8e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -632,14 +632,37 @@ processor's time-stamp counter." static PyObject * sys_setrecursionlimit(PyObject *self, PyObject *args) { - int new_limit; + int new_limit, mark; + PyThreadState *tstate; + if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) return NULL; - if (new_limit <= 0) { + + if (new_limit < 1) { PyErr_SetString(PyExc_ValueError, - "recursion limit must be positive"); + "recursion limit must be greater or equal than 1"); return NULL; } + + /* Issue #25274: When the recursion depth hits the recursion limit in + _Py_CheckRecursiveCall(), the overflowed flag of the thread state is + set to 1 and a RecursionError is raised. The overflowed flag is reset + to 0 when the recursion depth goes below the low-water mark: see + Py_LeaveRecursiveCall(). + + Reject too low new limit if the current recursion depth is higher than + the new low-water mark. Otherwise it may not be possible anymore to + reset the overflowed flag to 0. */ + mark = _Py_RecursionLimitLowerWaterMark(new_limit); + tstate = PyThreadState_GET(); + if (tstate->recursion_depth >= mark) { + PyErr_Format(PyExc_RecursionError, + "cannot set the recursion limit to %i at " + "the recursion depth %i: the limit is too low", + new_limit, tstate->recursion_depth); + return NULL; + } + Py_SetRecursionLimit(new_limit); Py_INCREF(Py_None); return Py_None; -- cgit v1.2.1 From b3b0102d41c7b1a25493b51d5d83d080fb32735b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 20 Jan 2016 11:12:38 +0100 Subject: Add _PyThreadState_UncheckedGet() Issue #26154: Add a new private _PyThreadState_UncheckedGet() function which gets the current thread state, but don't call Py_FatalError() if it is NULL. Python 3.5.1 removed the _PyThreadState_Current symbol from the Python C API to no more expose complex and private atomic types. Atomic types depends on the compiler or can even depend on compiler options. The new function _PyThreadState_UncheckedGet() allows to get the variable value without having to care of the exact implementation of atomic types. Changes: * Replace direct usage of the _PyThreadState_Current variable with a call to _PyThreadState_UncheckedGet(). * In pystate.c, replace direct usage of the _PyThreadState_Current variable with the PyThreadState_GET() macro for readability. * Document also PyThreadState_Get() in pystate.h --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 334f5d0c8e..8d7e05a465 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1397,7 +1397,7 @@ error: Py_XDECREF(name); Py_XDECREF(value); /* No return value, therefore clear error state if possible */ - if (_Py_atomic_load_relaxed(&_PyThreadState_Current)) + if (_PyThreadState_UncheckedGet()) PyErr_Clear(); } -- cgit v1.2.1 From a805c90a4423dd40a7ca58e952c3fe6a69640bf2 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sat, 17 Sep 2016 17:25:42 -0700 Subject: Issue #27932: Prevent memory leak in win32_ver(). --- Python/sysmodule.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 8d7e05a465..f688349efb 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -825,6 +825,7 @@ static PyStructSequence_Field windows_version_fields[] = { {"service_pack_minor", "Service Pack minor version number"}, {"suite_mask", "Bit mask identifying available product suites"}, {"product_type", "System product type"}, + {"_platform_version", "Diagnostic version number"}, {0} }; @@ -849,6 +850,12 @@ sys_getwindowsversion(PyObject *self) PyObject *version; int pos = 0; OSVERSIONINFOEX ver; + DWORD realMajor, realMinor, realBuild; + HANDLE hKernel32; + wchar_t kernel32_path[MAX_PATH]; + LPVOID verblock; + DWORD verblock_size; + ver.dwOSVersionInfoSize = sizeof(ver); if (!GetVersionEx((OSVERSIONINFO*) &ver)) return PyErr_SetFromWindowsErr(0); @@ -867,10 +874,40 @@ sys_getwindowsversion(PyObject *self) PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask)); PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType)); + realMajor = ver.dwMajorVersion; + realMinor = ver.dwMinorVersion; + realBuild = ver.dwBuildNumber; + + // GetVersion will lie if we are running in a compatibility mode. + // We need to read the version info from a system file resource + // to accurately identify the OS version. If we fail for any reason, + // just return whatever GetVersion said. + hKernel32 = GetModuleHandleW(L"kernel32.dll"); + if (hKernel32 && GetModuleFileNameW(hKernel32, kernel32_path, MAX_PATH) && + (verblock_size = GetFileVersionInfoSizeW(kernel32_path, NULL)) && + (verblock = PyMem_RawMalloc(verblock_size))) { + VS_FIXEDFILEINFO *ffi; + UINT ffi_len; + + if (GetFileVersionInfoW(kernel32_path, 0, verblock_size, verblock) && + VerQueryValueW(verblock, L"", (LPVOID)&ffi, &ffi_len)) { + realMajor = HIWORD(ffi->dwProductVersionMS); + realMinor = LOWORD(ffi->dwProductVersionMS); + realBuild = HIWORD(ffi->dwProductVersionLS); + } + PyMem_RawFree(verblock); + } + PyStructSequence_SET_ITEM(version, pos++, PyTuple_Pack(3, + PyLong_FromLong(realMajor), + PyLong_FromLong(realMinor), + PyLong_FromLong(realBuild) + )); + if (PyErr_Occurred()) { Py_DECREF(version); return NULL; } + return version; } -- cgit v1.2.1 From 24513d3decc7c9d11783bd77b1ffe5065cd1efd2 Mon Sep 17 00:00:00 2001 From: Ned Deily Date: Fri, 4 Nov 2016 17:03:34 -0400 Subject: Issue #28616: Correct help for sys.version_info releaselevel component. Patch by Anish Tambe. --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index f688349efb..1dc7d7cf6b 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1619,7 +1619,7 @@ static PyStructSequence_Field version_info_fields[] = { {"major", "Major release number"}, {"minor", "Minor release number"}, {"micro", "Patch release number"}, - {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"}, + {"releaselevel", "'alpha', 'beta', 'candidate', or 'final'"}, {"serial", "Serial release number"}, {0} }; -- cgit v1.2.1