summaryrefslogtreecommitdiffstats
path: root/Tools/clinic
diff options
context:
space:
mode:
authorDonghee Na <donghee.na@python.org>2023-11-18 23:05:49 (GMT)
committerGitHub <noreply@github.com>2023-11-18 23:05:49 (GMT)
commite52cc80f7fc3a560bf3d0053e0821a2db070cdd1 (patch)
tree4cc599be3fb9c5f828ed6699656cfae010ed0f21 /Tools/clinic
parent607b5e30c67bad35b90240d9ac176131e51423a5 (diff)
downloadcpython-e52cc80f7fc3a560bf3d0053e0821a2db070cdd1.zip
cpython-e52cc80f7fc3a560bf3d0053e0821a2db070cdd1.tar.gz
cpython-e52cc80f7fc3a560bf3d0053e0821a2db070cdd1.tar.bz2
gh-112213: Add @critical_section target directive to Argument Clinic (gh-112232)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Tools/clinic')
-rwxr-xr-xTools/clinic/clinic.py25
1 files changed, 21 insertions, 4 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index a205a51..f9326c1 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -1866,8 +1866,18 @@ class CLanguage(Language):
assert isinstance(f_self.converter, self_converter), "No self parameter in " + repr(f.full_name) + "!"
if f.critical_section:
- data.lock.append('Py_BEGIN_CRITICAL_SECTION({self_name});')
- data.unlock.append('Py_END_CRITICAL_SECTION();')
+ match len(f.target_critical_section):
+ case 0:
+ lock = 'Py_BEGIN_CRITICAL_SECTION({self_name});'
+ unlock = 'Py_END_CRITICAL_SECTION();'
+ case 1:
+ lock = 'Py_BEGIN_CRITICAL_SECTION({target_critical_section});'
+ unlock = 'Py_END_CRITICAL_SECTION();'
+ case _:
+ lock = 'Py_BEGIN_CRITICAL_SECTION2({target_critical_section});'
+ unlock = 'Py_END_CRITICAL_SECTION2();'
+ data.lock.append(lock)
+ data.unlock.append(unlock)
last_group = 0
first_optional = len(selfless)
@@ -1922,6 +1932,7 @@ class CLanguage(Language):
template_dict['docstring'] = self.docstring_for_c_string(f)
template_dict['self_name'] = template_dict['self_type'] = template_dict['self_type_check'] = ''
+ template_dict['target_critical_section'] = ', '.join(f.target_critical_section)
for converter in converters:
converter.set_template_dict(template_dict)
@@ -2970,6 +2981,7 @@ class Function:
# those accurately with inspect.Signature in 3.4.
docstring_only: bool = False
critical_section: bool = False
+ target_critical_section: list[str] = dc.field(default_factory=list)
def __post_init__(self) -> None:
self.parent = self.cls or self.module
@@ -5160,6 +5172,7 @@ class DSLParser:
self.parameter_continuation = ''
self.preserve_output = False
self.critical_section = False
+ self.target_critical_section: list[str] = []
def directive_version(self, required: str) -> None:
global version
@@ -5288,7 +5301,10 @@ class DSLParser:
fail("Can't set @classmethod, function is not a normal callable")
self.kind = CLASS_METHOD
- def at_critical_section(self) -> None:
+ def at_critical_section(self, *args: str) -> None:
+ if len(args) > 2:
+ fail("Up to 2 critical section variables are supported")
+ self.target_critical_section.extend(args)
self.critical_section = True
def at_staticmethod(self) -> None:
@@ -5514,7 +5530,8 @@ class DSLParser:
self.function = Function(name=function_name, full_name=full_name, module=module, cls=cls, c_basename=c_basename,
return_converter=return_converter, kind=self.kind, coexist=self.coexist,
- critical_section=self.critical_section)
+ critical_section=self.critical_section,
+ target_critical_section=self.target_critical_section)
self.block.signatures.append(self.function)
# insert a self converter automatically