[pypy-commit] pypy default: merge heads

arigo pypy.commits at gmail.com
Sat Feb 18 11:02:53 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r90179:7b6ea7df79b3
Date: 2017-02-18 17:00 +0100
http://bitbucket.org/pypy/pypy/changeset/7b6ea7df79b3/

Log:	merge heads

diff --git a/pypy/module/cpyext/pystate.py b/pypy/module/cpyext/pystate.py
--- a/pypy/module/cpyext/pystate.py
+++ b/pypy/module/cpyext/pystate.py
@@ -180,7 +180,10 @@
     Previously this could only be called when a current thread is active, and NULL
     meant that an exception was raised."""
     state = space.fromcache(InterpreterState)
-    return state.get_thread_state(space).c_dict
+    ts = state.get_thread_state(space)
+    if not ts:
+        return lltype.nullptr(PyObject.TO)
+    return ts.c_dict
 
 @cpython_api([PyThreadState], PyThreadState, error=CANNOT_FAIL)
 def PyThreadState_Swap(space, tstate):
diff --git a/pypy/module/cpyext/test/test_pystate.py b/pypy/module/cpyext/test/test_pystate.py
--- a/pypy/module/cpyext/test/test_pystate.py
+++ b/pypy/module/cpyext/test/test_pystate.py
@@ -35,7 +35,7 @@
                 PyEval_InitThreads();
                 state0 = PyGILState_Ensure(); /* hangs here */
                 if (val != 0)
-                { 
+                {
                     state1 = PyGILState_Ensure();
                     PyGILState_Release(state1);
                 }
@@ -141,6 +141,40 @@
         res = module.bounce()
         assert res == 3
 
+    def test_thread_and_gil(self):
+        module = self.import_extension('foo', [
+            ("bounce", "METH_NOARGS",
+            """
+            PyThreadState * tstate;
+            if (PyEval_ThreadsInitialized() == 0)
+            {
+            PyEval_InitThreads();
+            }
+            tstate = PyEval_SaveThread();
+            if (tstate == NULL) {
+                return PyLong_FromLong(0);
+            }
+            PyObject* dict = PyThreadState_GetDict();
+            if (dict != NULL) {
+            return PyLong_FromLong(1);
+            }
+            PyGILState_STATE gilstate = PyGILState_Ensure();
+            dict = PyThreadState_GetDict();
+            if (dict == NULL) {
+            return PyLong_FromLong(2);
+            }
+            PyGILState_Release(gilstate);
+            PyEval_RestoreThread(tstate);
+
+            if (PyThreadState_Get() != tstate) {
+                return PyLong_FromLong(3);
+            }
+
+            return PyLong_FromLong(4);
+            """)])
+        res = module.bounce()
+        assert res == 4
+
     def test_threadsinitialized(self):
         module = self.import_extension('foo', [
                 ("test", "METH_NOARGS",
@@ -151,8 +185,8 @@
         res = module.test()
         print "got", res
         assert res in (0, 1)
-        
-        
+
+
 class AppTestState(AppTestCpythonExtensionBase):
 
     def test_frame_tstate_tracing(self):


More information about the pypy-commit mailing list