Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r89107:836ec0a42c70
Date: 2016-12-17 09:24 +0100
http://bitbucket.org/pypy/pypy/changeset/836ec0a42c70/

Log:    _PyImport_{Acquire,Release}Lock()

diff --git a/pypy/module/cpyext/import_.py b/pypy/module/cpyext/import_.py
--- a/pypy/module/cpyext/import_.py
+++ b/pypy/module/cpyext/import_.py
@@ -1,6 +1,6 @@
 from pypy.interpreter import module
 from pypy.module.cpyext.api import (
-    generic_cpy_call, cpython_api, PyObject, CONST_STRING)
+    generic_cpy_call, cpython_api, PyObject, CONST_STRING, CANNOT_FAIL)
 from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.module import Module
@@ -124,3 +124,24 @@
     w_mod = importing.add_module(space, w_name)
     space.setattr(w_mod, space.wrap('__file__'), space.wrap(pathname))
     return importing.exec_code_module(space, w_mod, code, w_name)
+
+@cpython_api([], lltype.Void, error=CANNOT_FAIL)
+def _PyImport_AcquireLock(space):
+    """Locking primitive to prevent parallel imports of the same module
+    in different threads to return with a partially loaded module.
+    These calls are serialized by the global interpreter lock."""
+    try:
+        w_func = space.getbuiltinmodule('imp').get('acquire_lock')
+        space.call_function(w_func)
+    except OperationError as e:
+        e.write_unraisable(space, "_PyImport_AcquireLock")
+
+@cpython_api([], rffi.INT_real, error=CANNOT_FAIL)
+def _PyImport_ReleaseLock(space):
+    try:
+        w_func = space.getbuiltinmodule('imp').get('release_lock')
+        space.call_function(w_func)
+        return 1
+    except OperationError as e:
+        e.write_unraisable(space, "_PyImport_ReleaseLock")
+        return -1
diff --git a/pypy/module/cpyext/test/test_import.py 
b/pypy/module/cpyext/test/test_import.py
--- a/pypy/module/cpyext/test/test_import.py
+++ b/pypy/module/cpyext/test/test_import.py
@@ -37,6 +37,14 @@
         stat = api.PyImport_ReloadModule(stat)
         assert space.getattr(stat, space.wrap("S_IMODE"))
 
+    def test_lock(self, space, api):
+        # "does not crash"
+        api._PyImport_AcquireLock()
+        api._PyImport_AcquireLock()
+        api._PyImport_ReleaseLock()
+        api._PyImport_ReleaseLock()
+
+
 class AppTestImportLogic(AppTestCpythonExtensionBase):
     def test_import_logic(self):
         import sys, os
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to