Skip to content

Commit 4455670

Browse files
committed
Add a section describing PyUnstable_GetDefaultInterpreterRef's motivation.
1 parent 100d850 commit 4455670

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

peps/pep-0788.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,21 @@ The exact details of this deprecation aren't too clear. It's likely that
387387
the usual five-year deprecation (as specificed by :pep:`387`) will be too
388388
short, so for now, these functions will have no specific removal date.
389389

390+
Compatibility Shim for ``PyGILState_Ensure``
391+
--------------------------------------------
392+
393+
This proposal comes with :c:func:`PyUnstable_GetDefaultInterpreterRef` as a
394+
compatibility hack for some users of :c:func:`PyGILState_Ensure`. It is a
395+
thread-safe way to acquire a strong reference to the main (or "default")
396+
interpreter.
397+
398+
The main drawback to porting new code to :c:func:`PyThreadState_Ensure` is that
399+
it isn't a drop-in replacement for :c:func:`!PyGILState_Ensure`, as it needs
400+
an interpreter reference argument. In some large applications, refactoring to
401+
use a :c:type:`PyInterpreterRef` everywhere might be tricky; so, this function
402+
acts as a silver bullet for users who explicitly want to disallow support for
403+
subinterpreters.
404+
390405
Specification
391406
=============
392407

@@ -906,7 +921,7 @@ Example: Calling Python Without a Callback Parameter
906921
There are a few cases where callback functions don't take a callback parameter
907922
(``void *arg``), so it's impossible to acquire a reference to any specific
908923
interpreter. The solution to this problem is to acquire a reference to the main
909-
interpreter through :c:func:`PyUnstable_InterpreterRef_GetMain`.
924+
interpreter through :c:func:`PyUnstable_GetDefaultInterpreterRef`.
910925

911926
But wait, won't that break with subinterpreters, per
912927
:ref:`pep-788-subinterpreters-gilstate`? Fortunately, since the callback has
@@ -919,7 +934,7 @@ interpreter here.
919934
static void
920935
call_python(void)
921936
{
922-
PyInterpreterRef ref = PyUnstable_InterpreterRef_GetMain();
937+
PyInterpreterRef ref = PyUnstable_GetDefaultInterpreterRef();
923938
if (ref == 0) {
924939
fputs("Python has shut down.", stderr);
925940
return;

0 commit comments

Comments
 (0)