From bc5b80bac1d3db5779fcace4922bfc7eb8b964fa Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 11 Oct 2015 09:54:42 +0200 Subject: [PATCH 1/2] Close #24784: Fix compilation without thread support Add "#ifdef WITH_THREAD" around cals to: * PyGILState_Check() * _PyImport_AcquireLock() * _PyImport_ReleaseLock() --- Modules/_posixsubprocess.c | 12 ++++++++++-- Modules/socketmodule.c | 2 ++ Python/fileutils.c | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c index a327fc56b21..800b3019c8f 100644 --- a/Modules/_posixsubprocess.c +++ b/Modules/_posixsubprocess.c @@ -549,7 +549,9 @@ subprocess_fork_exec(PyObject* self, PyObject *args) int need_to_reenable_gc = 0; char *const *exec_array, *const *argv = NULL, *const *envp = NULL; Py_ssize_t arg_num; +#ifdef WITH_THREAD int import_lock_held = 0; +#endif if (!PyArg_ParseTuple( args, "OOpOOOiiiiiiiiiiO:fork_exec", @@ -644,8 +646,10 @@ subprocess_fork_exec(PyObject* self, PyObject *args) preexec_fn_args_tuple = PyTuple_New(0); if (!preexec_fn_args_tuple) goto cleanup; +#ifdef WITH_THREAD _PyImport_AcquireLock(); import_lock_held = 1; +#endif } if (cwd_obj != Py_None) { @@ -688,12 +692,14 @@ subprocess_fork_exec(PyObject* self, PyObject *args) /* Capture the errno exception before errno can be clobbered. */ PyErr_SetFromErrno(PyExc_OSError); } - if (preexec_fn != Py_None && - _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) { +#ifdef WITH_THREAD + if (preexec_fn != Py_None + && _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); } import_lock_held = 0; +#endif /* Parent process */ if (envp) @@ -716,8 +722,10 @@ subprocess_fork_exec(PyObject* self, PyObject *args) return PyLong_FromPid(pid); cleanup: +#ifdef WITH_THREAD if (import_lock_held) _PyImport_ReleaseLock(); +#endif if (envp) _Py_FreeCharPArray(envp); if (argv) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index d9c70f86654..bae9634ef2b 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -719,8 +719,10 @@ sock_call_ex(PySocketSockObject *s, int deadline_initialized = 0; int res; +#ifdef WITH_THREAD /* sock_call() must be called with the GIL held. */ assert(PyGILState_Check()); +#endif /* outer loop to retry select() when select() is interrupted by a signal or to retry select()+sock_func() on false positive (see above) */ diff --git a/Python/fileutils.c b/Python/fileutils.c index bccd32145c6..079918c4a30 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -986,8 +986,10 @@ _Py_open_impl(const char *pathname, int flags, int gil_held) int _Py_open(const char *pathname, int flags) { +#ifdef WITH_THREAD /* _Py_open() must be called with the GIL held. */ assert(PyGILState_Check()); +#endif return _Py_open_impl(pathname, flags, 1); } @@ -1080,7 +1082,9 @@ _Py_fopen_obj(PyObject *path, const char *mode) wchar_t wmode[10]; int usize; +#ifdef WITH_THREAD assert(PyGILState_Check()); +#endif if (!PyUnicode_Check(path)) { PyErr_Format(PyExc_TypeError, @@ -1108,7 +1112,9 @@ _Py_fopen_obj(PyObject *path, const char *mode) PyObject *bytes; char *path_bytes; +#ifdef WITH_THREAD assert(PyGILState_Check()); +#endif if (!PyUnicode_FSConverter(path, &bytes)) return NULL; From b45c0f7e4833615067dbe74c1e813fd1b45ce181 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 11 Oct 2015 10:10:31 +0200 Subject: [PATCH 2/2] Close #25367: Fix test_coroutines with no thread support Skip test_asyncio_1() when the asyncio module cannot be imported because CPython is compiled with no thread support. --- Lib/test/test_coroutines.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 10de85644ee..71fbe8212fb 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -1322,7 +1322,9 @@ class CoroutineTest(unittest.TestCase): class CoroAsyncIOCompatTest(unittest.TestCase): def test_asyncio_1(self): - import asyncio + # asyncio cannot be imported when Python is compiled without thread + # support + support.import_module('asyncio') class MyException(Exception): pass