[pypy-commit] pypy py3.6: issue #3041

arigo pypy.commits at gmail.com
Mon Jul 22 22:07:29 EDT 2019


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.6
Changeset: r97016:0fcdbc3de300
Date: 2019-07-23 04:06 +0200
http://bitbucket.org/pypy/pypy/changeset/0fcdbc3de300/

Log:	issue #3041

	Implement PyGILState_Check()

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -986,8 +986,9 @@
     gil_release = (gil == "release" or gil == "around")
     pygilstate_ensure = (gil == "pygilstate_ensure")
     pygilstate_release = (gil == "pygilstate_release")
+    pygilstate_check = (gil == "pygilstate_check")
     assert (gil is None or gil_acquire or gil_release
-            or pygilstate_ensure or pygilstate_release)
+            or pygilstate_ensure or pygilstate_release or pygilstate_check)
     expected_nb_args = len(argtypesw) + pygilstate_ensure
 
     if isinstance(restype, lltype.Ptr) and error_value == 0:
@@ -1027,6 +1028,9 @@
             else:
                 rgil.acquire()
                 args += (pystate.PyGILState_UNLOCKED,)
+        elif pygilstate_check:
+            result = cpyext_glob_tid_ptr[0] == tid
+            return rffi.cast(restype, result)
         else:
             if cpyext_glob_tid_ptr[0] != tid:
                 no_gil_error(pname)
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
@@ -252,6 +252,11 @@
     space.threadlocals.get_ec = get_possibly_deleted_ec
 
 
+ at cpython_api([], rffi.INT_real, error=CANNOT_FAIL, gil="pygilstate_check")
+def PyGILState_Check(space):
+    assert False, "the logic is completely inside wrapper_second_level"
+
+
 @cpython_api([], PyGILState_STATE, error=CANNOT_FAIL, gil="pygilstate_ensure")
 def PyGILState_Ensure(space, previous_state):
     # The argument 'previous_state' is not part of the API; it is inserted
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
@@ -154,6 +154,8 @@
             if (tstate == NULL) {
                 return PyLong_FromLong(0);
             }
+            if (PyGILState_Check() != 0)
+                return PyLong_FromLong(-1);
             dict = PyThreadState_GetDict();
             if (dict != NULL) {
             return PyLong_FromLong(1);
@@ -163,12 +165,18 @@
             if (dict == NULL) {
             return PyLong_FromLong(2);
             }
+            if (PyGILState_Check() != 1)
+                return PyLong_FromLong(-2);
             PyGILState_Release(gilstate);
+            if (PyGILState_Check() != 0)
+                return PyLong_FromLong(-3);
             PyEval_RestoreThread(tstate);
 
             if (PyThreadState_Get() != tstate) {
                 return PyLong_FromLong(3);
             }
+            if (PyGILState_Check() != 1)
+                return PyLong_FromLong(-4);
 
             return PyLong_FromLong(4);
             """)])


More information about the pypy-commit mailing list