From 1dc3c1cb61120e608984065e6a8d19eab6c0e863 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Thu, 26 Jan 2023 10:51:28 +0100 Subject: [PATCH 1/2] Remove suggestions to use `internal` functions Fixes #2091. --- doc/source/dotnet.rst | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/doc/source/dotnet.rst b/doc/source/dotnet.rst index d02a6f0cb..cc24c48a4 100644 --- a/doc/source/dotnet.rst +++ b/doc/source/dotnet.rst @@ -42,14 +42,9 @@ application. Before interacting with any of the objects or APIs provided by the ``Python.Runtime`` namespace, calling code must have acquired the Python -global interpreter lock by calling the ``PythonEngine.AcquireLock`` -method. The only exception to this rule is the -``PythonEngine.Initialize`` method, which may be called at startup -without having acquired the GIL. - -When finished using Python APIs, managed code must call a corresponding -``PythonEngine.ReleaseLock`` to release the GIL and allow other threads -to use Python. +global interpreter lock by ``using'' ``Py.GIL()``. The only exception to +this rule is the ``PythonEngine.Initialize`` method, which may be called +at startup without having acquired the GIL. A ``using`` statement may be used to acquire and release the GIL: @@ -60,10 +55,10 @@ A ``using`` statement may be used to acquire and release the GIL: PythonEngine.Exec("doStuff()"); } -The AcquireLock and ReleaseLock methods are thin wrappers over the -unmanaged ``PyGILState_Ensure`` and ``PyGILState_Release`` functions -from the Python API, and the documentation for those APIs applies to the -managed versions. +The ``Py.GIL()'' object is a thin wrapper over the unmanaged +``PyGILState_Ensure`` (on construction) and ``PyGILState_Release`` (on +disposal) functions from the Python API, and the documentation for those +APIs applies to the managed versions. Passing C# Objects to the Python Engine --------------------------------------- From 6fb11e14529e4106f01980e85f76d93f2aeb19d9 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sat, 28 Jan 2023 12:45:57 +0100 Subject: [PATCH 2/2] Update dotnet.rst --- doc/source/dotnet.rst | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/doc/source/dotnet.rst b/doc/source/dotnet.rst index cc24c48a4..03d729ee5 100644 --- a/doc/source/dotnet.rst +++ b/doc/source/dotnet.rst @@ -44,9 +44,8 @@ Before interacting with any of the objects or APIs provided by the ``Python.Runtime`` namespace, calling code must have acquired the Python global interpreter lock by ``using'' ``Py.GIL()``. The only exception to this rule is the ``PythonEngine.Initialize`` method, which may be called -at startup without having acquired the GIL. - -A ``using`` statement may be used to acquire and release the GIL: +at startup without having acquired the GIL. The GIL is released again +by disposing the return value of `Py.GIL()`: .. code:: csharp @@ -54,6 +53,23 @@ A ``using`` statement may be used to acquire and release the GIL: { PythonEngine.Exec("doStuff()"); } + + // or + { + using var _ = Py.GIL() + PythonEngine.Exec("doStuff()"); + } + + // or + var gil = Py.GIL(); + try + { + PythonEngine.Exec("doStuff()"); + } + finally + { + gil.Dispose(); + } The ``Py.GIL()'' object is a thin wrapper over the unmanaged ``PyGILState_Ensure`` (on construction) and ``PyGILState_Release`` (on