diff --git a/peps/pep-0788.rst b/peps/pep-0788.rst index f5b7f255790..cf96c1928dd 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; } @@ -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. @@ -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. @@ -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 @@ -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; @@ -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; } @@ -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; @@ -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; }