summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2023-11-14 10:47:46 (GMT)
committerGitHub <noreply@github.com>2023-11-14 10:47:46 (GMT)
commit324531df909721978446d504186738a33ab03fd5 (patch)
treefb3959265c6530ad8049e7511b428334c1657ac5 /Modules/_io
parent16055c160412544e2a49794aaf3aa70c584f843a (diff)
downloadcpython-324531df909721978446d504186738a33ab03fd5.zip
cpython-324531df909721978446d504186738a33ab03fd5.tar.gz
cpython-324531df909721978446d504186738a33ab03fd5.tar.bz2
gh-111903: Add `@critical_section` directive to Argument Clinic. (#111904)
The `@critical_section` directive instructs Argument Clinic to generate calls to `Py_BEGIN_CRITICAL_SECTION()` and `Py_END_CRITICAL_SECTION()` around the bound function. In `--disable-gil` builds, these calls will lock and unlock the `self` object. They are no-ops in the default build. This is used in one place (`_io._Buffered.close`) as a demonstration. Subsequent PRs will use it more widely in the `_io.Buffered` bindings.
Diffstat (limited to 'Modules/_io')
-rw-r--r--Modules/_io/bufferedio.c4
-rw-r--r--Modules/_io/clinic/bufferedio.c.h10
2 files changed, 11 insertions, 3 deletions
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index e8caf9f..169b2b3 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -10,6 +10,7 @@
#include "Python.h"
#include "pycore_bytesobject.h" // _PyBytes_Join()
#include "pycore_call.h" // _PyObject_CallNoArgs()
+#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
@@ -521,12 +522,13 @@ buffered_closed_get(buffered *self, void *context)
}
/*[clinic input]
+@critical_section
_io._Buffered.close
[clinic start generated code]*/
static PyObject *
_io__Buffered_close_impl(buffered *self)
-/*[clinic end generated code: output=7280b7b42033be0c input=d20b83d1ddd7d805]*/
+/*[clinic end generated code: output=7280b7b42033be0c input=56d95935b03fd326]*/
{
PyObject *res = NULL;
int r;
diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h
index f6ac269..3fe0350 100644
--- a/Modules/_io/clinic/bufferedio.c.h
+++ b/Modules/_io/clinic/bufferedio.c.h
@@ -324,7 +324,13 @@ _io__Buffered_close_impl(buffered *self);
static PyObject *
_io__Buffered_close(buffered *self, PyObject *Py_UNUSED(ignored))
{
- return _io__Buffered_close_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io__Buffered_close_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io__Buffered_detach__doc__,
@@ -1075,4 +1081,4 @@ skip_optional_pos:
exit:
return return_value;
}
-/*[clinic end generated code: output=090e70253e35fc22 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=b23847480eba3d9b input=a9049054013a1b77]*/