From 765127d164c9b37334875d14ea82227f77b10198 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sat, 4 Oct 2025 10:15:18 -0400 Subject: [PATCH 1/5] Rename PyInterpreterLock_AcquireView() to PyInterpreterLock_FromView(). --- peps/pep-0788.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/peps/pep-0788.rst b/peps/pep-0788.rst index f5b7f255790..cd86a6b2402 100644 --- a/peps/pep-0788.rst +++ b/peps/pep-0788.rst @@ -24,7 +24,7 @@ interpreter. For example: static int thread_function(PyInterpreterView view) { - PyInterpreterLock lock = PyInterpreterLock_AcquireView(view); + PyInterpreterLock lock = PyInterpreterLock_FromView(view); if (lock == 0) { return -1; } @@ -310,7 +310,7 @@ Interpreter Locks The caller must hold an :term:`attached thread state`. -.. c:function:: PyInterpreterLock PyInterpreterLock_AcquireView(PyInterpreterView view) +.. c:function:: PyInterpreterLock PyInterpreterLock_FromView(PyInterpreterView view) Acquire a lock to an interpreter through a view. @@ -364,7 +364,7 @@ Interpreter Views Create a view to the current interpreter. This function is generally meant to be used in tandem with - :c:func:`PyInterpreterLock_AcquireView`. + :c:func:`PyInterpreterLock_FromView`. On success, this function returns a view to the current interpreter, and returns ``0`` with an exception set on failure. @@ -508,7 +508,7 @@ With this PEP, you would implement it like this: PyObject *file, PyObject *text) { - PyInterpreterLock lock = PyInterpreterLock_AcquireView(view); + PyInterpreterLock lock = PyInterpreterLock_FromView(view); if (lock == 0) { /* Python interpreter has shut down */ return -1; @@ -716,7 +716,7 @@ Example: An Asynchronous Callback { ThreadData *tdata = (ThreadData *)arg; PyInterpreterView view = tdata->view; - PyInterpreterLock lock = PyInterpreterLock_AcquireView(view); + PyInterpreterLock lock = PyInterpreterLock_FromView(view); if (lock == 0) { fputs("Python has shut down!\n", stderr); return -1; From 86c278ef48a0bc2a3943bc50da1b12f70fe4840b Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sat, 4 Oct 2025 10:15:49 -0400 Subject: [PATCH 2/5] Rename PyInterpreterLock_AcquireCurrent() to PyInterpreterLock_FromCurrent(). --- peps/pep-0788.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/peps/pep-0788.rst b/peps/pep-0788.rst index cd86a6b2402..2ad14894148 100644 --- a/peps/pep-0788.rst +++ b/peps/pep-0788.rst @@ -300,7 +300,7 @@ Interpreter Locks This type is guaranteed to be pointer-sized. -.. c:function:: PyInterpreterLock PyInterpreterLock_AcquireCurrent(void) +.. c:function:: PyInterpreterLock PyInterpreterLock_FromCurrent(void) Acquire a lock for the current interpreter. @@ -556,7 +556,7 @@ held. Any future finalizer that attempted to acquire the lock would be deadlocke my_critical_operation(PyObject *self, PyObject *Py_UNUSED(args)) { assert(PyThreadState_GetUnchecked() != NULL); - PyInterpreterLock lock = PyInterpreterLock_AcquireCurrent(); + PyInterpreterLock lock = PyInterpreterLock_FromCurrent(); if (lock == 0) { /* Python interpreter has shut down */ return NULL; @@ -639,7 +639,7 @@ This is the same code, rewritten to use the new functions: PyThread_handle_t handle; PyThead_indent_t indent; - PyInterpreterLock lock = PyInterpreterLock_AcquireCurrent(); + PyInterpreterLock lock = PyInterpreterLock_FromCurrent(); if (lock == 0) { return NULL; } @@ -690,7 +690,7 @@ hang the current thread forever). PyThread_handle_t handle; PyThead_indent_t indent; - PyInterpreterLock lock = PyInterpreterLock_AcquireCurrent(); + PyInterpreterLock lock = PyInterpreterLock_FromCurrent(); if (lock == 0) { return NULL; } From 7f655a086d73b701093aaf453548c2f33259a9a5 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sat, 4 Oct 2025 10:21:40 -0400 Subject: [PATCH 3/5] Editorial fix: Add missing view code in example. --- peps/pep-0788.rst | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/peps/pep-0788.rst b/peps/pep-0788.rst index 2ad14894148..e24a395eba8 100644 --- a/peps/pep-0788.rst +++ b/peps/pep-0788.rst @@ -771,7 +771,13 @@ interpreter through :c:func:`PyUnstable_InterpreterView_FromDefault`. static void call_python(void) { - PyInterpreterLock lock = PyUnstable_InterpreterView_FromDefault(); + PyInterpreterView view = PyUnstable_InterpreterView_FromDefault(); + if (lock == 0) { + fputs("Python has shut down.", stderr); + return; + } + + PyInterpreterLock lock = PyInterpreterLock_FromView(view); if (lock == 0) { fputs("Python has shut down.", stderr); return; @@ -780,6 +786,7 @@ interpreter through :c:func:`PyUnstable_InterpreterView_FromDefault`. PyThreadView thread_view = PyThreadState_Ensure(lock); if (thread_view == 0) { PyInterpreterLock_Release(lock); + PyInterpreterView_Close(view); return -1; } if (PyRun_SimpleString("print(42)") < 0) { @@ -787,6 +794,7 @@ interpreter through :c:func:`PyUnstable_InterpreterView_FromDefault`. } PyThreadState_Release(thread_view); PyInterpreterLock_Release(lock); + PyInterpreterView_Close(view); return 0; } From f001abfec6e5452980a0e4b982a34912a4fd8f45 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sat, 4 Oct 2025 10:25:34 -0400 Subject: [PATCH 4/5] Editorial: FIx title casing. --- peps/pep-0788.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/peps/pep-0788.rst b/peps/pep-0788.rst index e24a395eba8..3331775824c 100644 --- a/peps/pep-0788.rst +++ b/peps/pep-0788.rst @@ -1,5 +1,5 @@ PEP: 788 -Title: Protecting the C API from Interpreter Finalization +Title: Protecting the C API From Interpreter Finalization Author: Peter Bierma Sponsor: Victor Stinner Discussions-To: https://discuss.python.org/t/104150 @@ -402,7 +402,7 @@ Interpreter Views The caller does not need to hold an :term:`attached thread state`. -Ensuring And Releasing Thread States +Ensuring and Releasing Thread States ------------------------------------ This proposal includes two new high-level threading APIs that intend to From 9eab727865d03f6152ca6dac2e7632968833de16 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sat, 4 Oct 2025 10:28:33 -0400 Subject: [PATCH 5/5] Apparently, people can't always agree on what "from" should do in a title. --- peps/pep-0788.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peps/pep-0788.rst b/peps/pep-0788.rst index 3331775824c..cf96c1928dd 100644 --- a/peps/pep-0788.rst +++ b/peps/pep-0788.rst @@ -1,5 +1,5 @@ PEP: 788 -Title: Protecting the C API From Interpreter Finalization +Title: Protecting the C API from Interpreter Finalization Author: Peter Bierma Sponsor: Victor Stinner Discussions-To: https://discuss.python.org/t/104150