gh-112213: Add @critical_section target directive to Argument Clinic (gh-112232)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Donghee Na 2023-11-18 23:05:49 +00:00 • committed by GitHub
parent 607b5e30c6
commit e52cc80f7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 191 additions and 4 deletions

View file

@ -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