Debian Codegraph @

Tools/clinic/libclinic/parse_args.py

raw
1 from __future__ import annotations
2 from typing import TYPE_CHECKING, Final
3
4 import libclinic
5 from libclinic import fail, warn
6 from libclinic.function import (
7 Function, Parameter,
8 GETTER, SETTER, METHOD_NEW)
9 from libclinic.converter import CConverter
10 from libclinic.converters import (
11 defining_class_converter, object_converter, self_converter)
12 if TYPE_CHECKING:
13 from libclinic.clanguage import CLanguage
14 from libclinic.codegen import CodeGen
15
16
17 def declare_parser(
18 f: Function,
19 *,
20 hasformat: bool = False,
21 codegen: CodeGen,
22 ) -> str:
23 """
24 Generates the code template for a static local PyArg_Parser variable,
25 with an initializer. For core code (incl. builtin modules) the
26 kwtuple field is also statically initialized. Otherwise
27 it is initialized at runtime.
28 """
29 limited_capi = codegen.limited_capi
30 if hasformat:
31 fname = ''
32 format_ = '.format = "{format_units}:{name}",'
33 else:
34 fname = '.fname = "{name}",'
35 format_ = ''
36
37 num_keywords = len([
38 p for p in f.parameters.values()
39 if not p.is_positional_only() and not p.is_vararg()
40 ])
41
42 condition = '#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)'
43 if limited_capi:
44 declarations = """
45 #define KWTUPLE NULL
46 """
47 elif num_keywords == 0:
48 declarations = """
49 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
50 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
51 #else
52 # define KWTUPLE NULL
53 #endif
54 """
55
56 codegen.add_include('pycore_runtime.h', '_Py_SINGLETON()',
57 condition=condition)
58 else:
59 # XXX Why do we not statically allocate the tuple
60 # for non-builtin modules?
61 declarations = """
62 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
63
64 #define NUM_KEYWORDS %d
65 static struct {{
66 PyGC_Head _this_is_not_used;
67 PyObject_VAR_HEAD
68 PyObject *ob_item[NUM_KEYWORDS];
69 }} _kwtuple = {{
70 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
71 .ob_item = {{ {keywords_py} }},
72 }};
73 #undef NUM_KEYWORDS
74 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
75
76 #else // !Py_BUILD_CORE
77 # define KWTUPLE NULL
78 #endif // !Py_BUILD_CORE
79 """ % num_keywords
80
81 codegen.add_include('pycore_gc.h', 'PyGC_Head',
82 condition=condition)
83 codegen.add_include('pycore_runtime.h', '_Py_ID()',
84 condition=condition)
85
86 declarations += """
87 static const char * const _keywords[] = {{{keywords_c} NULL}};
88 static _PyArg_Parser _parser = {{
89 .keywords = _keywords,
90 %s
91 .kwtuple = KWTUPLE,
92 }};
93 #undef KWTUPLE
94 """ % (format_ or fname)
95 return libclinic.normalize_snippet(declarations)
96
97
98 NO_VARARG: Final[str] = "PY_SSIZE_T_MAX"
99 PARSER_PROTOTYPE_KEYWORD: Final[str] = libclinic.normalize_snippet("""
100 static PyObject *
101 {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs)
102 """)
103 PARSER_PROTOTYPE_KEYWORD___INIT__: Final[str] = libclinic.normalize_snippet("""
104 static int
105 {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs)
106 """)
107 PARSER_PROTOTYPE_VARARGS: Final[str] = libclinic.normalize_snippet("""
108 static PyObject *
109 {c_basename}({self_type}{self_name}, PyObject *args)
110 """)
111 PARSER_PROTOTYPE_FASTCALL: Final[str] = libclinic.normalize_snippet("""
112 static PyObject *
113 {c_basename}({self_type}{self_name}, PyObject *const *args, Py_ssize_t nargs)
114 """)
115 PARSER_PROTOTYPE_FASTCALL_KEYWORDS: Final[str] = libclinic.normalize_snippet("""
116 static PyObject *
117 {c_basename}({self_type}{self_name}, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
118 """)
119 PARSER_PROTOTYPE_DEF_CLASS: Final[str] = libclinic.normalize_snippet("""
120 static PyObject *
121 {c_basename}({self_type}{self_name}, PyTypeObject *{defining_class_name}, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
122 """)
123 PARSER_PROTOTYPE_NOARGS: Final[str] = libclinic.normalize_snippet("""
124 static PyObject *
125 {c_basename}({self_type}{self_name}, PyObject *Py_UNUSED(ignored))
126 """)
127 PARSER_PROTOTYPE_GETTER: Final[str] = libclinic.normalize_snippet("""
128 static PyObject *
129 {c_basename}({self_type}{self_name}, void *Py_UNUSED(context))
130 """)
131 PARSER_PROTOTYPE_SETTER: Final[str] = libclinic.normalize_snippet("""
132 static int
133 {c_basename}({self_type}{self_name}, PyObject *value, void *Py_UNUSED(context))
134 """)
135 METH_O_PROTOTYPE: Final[str] = libclinic.normalize_snippet("""
136 static PyObject *
137 {c_basename}({impl_parameters})
138 """)
139 DOCSTRING_PROTOTYPE_VAR: Final[str] = libclinic.normalize_snippet("""
140 PyDoc_VAR({c_basename}__doc__);
141 """)
142 DOCSTRING_PROTOTYPE_STRVAR: Final[str] = libclinic.normalize_snippet("""
143 PyDoc_STRVAR({c_basename}__doc__,
144 {docstring});
145 """)
146 GETSET_DOCSTRING_PROTOTYPE_STRVAR: Final[str] = libclinic.normalize_snippet("""
147 PyDoc_STRVAR({getset_basename}__doc__,
148 {docstring});
149 #if defined({getset_basename}_DOCSTR)
150 # undef {getset_basename}_DOCSTR
151 #endif
152 #define {getset_basename}_DOCSTR {getset_basename}__doc__
153 """)
154 IMPL_DEFINITION_PROTOTYPE: Final[str] = libclinic.normalize_snippet("""
155 static {impl_return_type}
156 {c_basename}_impl({impl_parameters})
157 """)
158 METHODDEF_PROTOTYPE_DEFINE: Final[str] = libclinic.normalize_snippet(r"""
159 #define {methoddef_name} \
160 {{"{name}", {methoddef_cast}{c_basename}{methoddef_cast_end}, {methoddef_flags}, {c_basename}__doc__}},
161 """)
162 GETTERDEF_PROTOTYPE_DEFINE: Final[str] = libclinic.normalize_snippet(r"""
163 #if !defined({getset_basename}_DOCSTR)
164 # define {getset_basename}_DOCSTR NULL
165 #endif
166 #if defined({getset_name}_GETSETDEF)
167 # undef {getset_name}_GETSETDEF
168 # define {getset_name}_GETSETDEF {{"{name}", (getter){getset_basename}_get, (setter){getset_basename}_set, {getset_basename}_DOCSTR}},
169 #else
170 # define {getset_name}_GETSETDEF {{"{name}", (getter){getset_basename}_get, NULL, {getset_basename}_DOCSTR}},
171 #endif
172 """)
173 SETTERDEF_PROTOTYPE_DEFINE: Final[str] = libclinic.normalize_snippet(r"""
174 #if !defined({getset_basename}_DOCSTR)
175 # define {getset_basename}_DOCSTR NULL
176 #endif
177 #if defined({getset_name}_GETSETDEF)
178 # undef {getset_name}_GETSETDEF
179 # define {getset_name}_GETSETDEF {{"{name}", (getter){getset_basename}_get, (setter){getset_basename}_set, {getset_basename}_DOCSTR}},
180 #else
181 # define {getset_name}_GETSETDEF {{"{name}", NULL, (setter){getset_basename}_set, NULL}},
182 #endif
183 """)
184 METHODDEF_PROTOTYPE_IFNDEF: Final[str] = libclinic.normalize_snippet("""
185 #ifndef {methoddef_name}
186 #define {methoddef_name}
187 #endif /* !defined({methoddef_name}) */
188 """)
189
190
191 class ParseArgsCodeGen:
192 func: Function
193 codegen: CodeGen
194 limited_capi: bool = False
195
196 # Function parameters
197 parameters: list[Parameter]
198 converters: list[CConverter]
199
200 # Is 'defining_class' used for the first parameter?
201 requires_defining_class: bool
202
203 # Use METH_FASTCALL calling convention?
204 fastcall: bool
205
206 # Declaration of the return variable (ex: "int return_value;")
207 return_value_declaration: str
208
209 # Calling convention (ex: "METH_NOARGS")
210 flags: str
211
212 # Variables declarations
213 declarations: str
214
215 pos_only: int = 0
216 min_pos: int = 0
217 max_pos: int = 0
218 min_kw_only: int = 0
219 pseudo_args: int = 0
220 vararg: int | str = NO_VARARG
221
222 docstring_prototype: str
223 docstring_definition: str
224 impl_prototype: str | None
225 impl_definition: str
226 methoddef_define: str
227 parser_prototype: str
228 parser_definition: str
229 cpp_if: str
230 cpp_endif: str
231 methoddef_ifndef: str
232
233 parser_body_fields: tuple[str, ...]
234
235 def __init__(self, func: Function, codegen: CodeGen) -> None:
236 self.func = func
237 self.codegen = codegen
238
239 self.parameters = list(self.func.parameters.values())
240 first_param = self.parameters.pop(0)
241 if not isinstance(first_param.converter, self_converter):
242 raise ValueError("the first parameter must use self_converter")
243
244 self.requires_defining_class = False
245 if self.parameters and isinstance(self.parameters[0].converter, defining_class_converter):
246 self.requires_defining_class = True
247 del self.parameters[0]
248 self.converters = [p.converter for p in self.parameters]
249
250 if self.func.critical_section:
251 self.codegen.add_include('pycore_critical_section.h',
252 'Py_BEGIN_CRITICAL_SECTION()')
253 self.fastcall = not self.is_new_or_init()
254
255 self.pos_only = 0
256 self.min_pos = 0
257 self.max_pos = 0
258 self.min_kw_only = 0
259 self.pseudo_args = 0
260 for i, p in enumerate(self.parameters, 1):
261 if p.is_keyword_only():
262 assert not p.is_positional_only()
263 if not p.is_optional():
264 self.min_kw_only = i - self.max_pos - int(self.vararg != NO_VARARG)
265 elif p.is_vararg():
266 self.pseudo_args += 1
267 self.vararg = i - 1
268 else:
269 if self.vararg == NO_VARARG:
270 self.max_pos = i
271 if p.is_positional_only():
272 self.pos_only = i
273 if not p.is_optional():
274 self.min_pos = i
275
276 def is_new_or_init(self) -> bool:
277 return self.func.kind.new_or_init
278
279 def has_option_groups(self) -> bool:
280 return (bool(self.parameters
281 and (self.parameters[0].group or self.parameters[-1].group)))
282
283 def use_meth_o(self) -> bool:
284 return (len(self.parameters) == 1
285 and self.parameters[0].is_positional_only()
286 and not self.converters[0].is_optional()
287 and not self.requires_defining_class
288 and not self.is_new_or_init())
289
290 def use_simple_return(self) -> bool:
291 return (self.func.return_converter.type == 'PyObject *'
292 and not self.func.critical_section)
293
294 def select_prototypes(self) -> None:
295 self.docstring_prototype = ''
296 self.docstring_definition = ''
297 self.methoddef_define = METHODDEF_PROTOTYPE_DEFINE
298 self.return_value_declaration = "PyObject *return_value = NULL;"
299
300 if self.is_new_or_init() and not self.func.docstring:
301 pass
302 elif self.func.kind is GETTER:
303 self.methoddef_define = GETTERDEF_PROTOTYPE_DEFINE
304 if self.func.docstring:
305 self.docstring_definition = GETSET_DOCSTRING_PROTOTYPE_STRVAR
306 elif self.func.kind is SETTER:
307 if self.func.docstring:
308 fail("docstrings are only supported for @getter, not @setter")
309 self.return_value_declaration = "int {return_value};"
310 self.methoddef_define = SETTERDEF_PROTOTYPE_DEFINE
311 else:
312 self.docstring_prototype = DOCSTRING_PROTOTYPE_VAR
313 self.docstring_definition = DOCSTRING_PROTOTYPE_STRVAR
314
315 def init_limited_capi(self) -> None:
316 self.limited_capi = self.codegen.limited_capi
317 if self.limited_capi and (self.pseudo_args or
318 (any(p.is_optional() for p in self.parameters) and
319 any(p.is_keyword_only() and not p.is_optional() for p in self.parameters)) or
320 any(c.broken_limited_capi for c in self.converters)):
321 warn(f"Function {self.func.full_name} cannot use limited C API")
322 self.limited_capi = False
323
324 def parser_body(
325 self,
326 *fields: str,
327 declarations: str = ''
328 ) -> None:
329 lines = [self.parser_prototype]
330 self.parser_body_fields = fields
331
332 preamble = libclinic.normalize_snippet("""
333 {{
334 {return_value_declaration}
335 {parser_declarations}
336 {declarations}
337 {initializers}
338 """) + "\n"
339 finale = libclinic.normalize_snippet("""
340 {modifications}
341 {lock}
342 {return_value} = {c_basename}_impl({impl_arguments});
343 {unlock}
344 {return_conversion}
345 {post_parsing}
346
347 {exit_label}
348 {cleanup}
349 return return_value;
350 }}
351 """)
352 for field in preamble, *fields, finale:
353 lines.append(field)
354 code = libclinic.linear_format("\n".join(lines),
355 parser_declarations=self.declarations)
356 self.parser_definition = code
357
358 def parse_no_args(self) -> None:
359 parser_code: list[str] | None
360 simple_return = self.use_simple_return()
361 if self.func.kind is GETTER:
362 self.parser_prototype = PARSER_PROTOTYPE_GETTER
363 parser_code = []
364 elif self.func.kind is SETTER:
365 self.parser_prototype = PARSER_PROTOTYPE_SETTER
366 parser_code = []
367 elif not self.requires_defining_class:
368 # no self.parameters, METH_NOARGS
369 self.flags = "METH_NOARGS"
370 self.parser_prototype = PARSER_PROTOTYPE_NOARGS
371 parser_code = []
372 else:
373 assert self.fastcall
374
375 self.flags = "METH_METHOD|METH_FASTCALL|METH_KEYWORDS"
376 self.parser_prototype = PARSER_PROTOTYPE_DEF_CLASS
377 return_error = ('return NULL;' if simple_return
378 else 'goto exit;')
379 parser_code = [libclinic.normalize_snippet("""
380 if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {{
381 PyErr_SetString(PyExc_TypeError, "{name}() takes no arguments");
382 %s
383 }}
384 """ % return_error, indent=4)]
385
386 if simple_return:
387 self.parser_definition = '\n'.join([
388 self.parser_prototype,
389 '{{',
390 *parser_code,
391 ' return {c_basename}_impl({impl_arguments});',
392 '}}'])
393 else:
394 self.parser_body(*parser_code)
395
396 def parse_one_arg(self) -> None:
397 self.flags = "METH_O"
398
399 if (isinstance(self.converters[0], object_converter) and
400 self.converters[0].format_unit == 'O'):
401 meth_o_prototype = METH_O_PROTOTYPE
402
403 if self.use_simple_return():
404 # maps perfectly to METH_O, doesn't need a return converter.
405 # so we skip making a parse function
406 # and call directly into the impl function.
407 self.impl_prototype = ''
408 self.impl_definition = meth_o_prototype
409 else:
410 # SLIGHT HACK
411 # use impl_parameters for the parser here!
412 self.parser_prototype = meth_o_prototype
413 self.parser_body()
414
415 else:
416 argname = 'arg'
417 if self.parameters[0].name == argname:
418 argname += '_'
419 self.parser_prototype = libclinic.normalize_snippet("""
420 static PyObject *
421 {c_basename}({self_type}{self_name}, PyObject *%s)
422 """ % argname)
423
424 displayname = self.parameters[0].get_displayname(0)
425 parsearg: str | None
426 parsearg = self.converters[0].parse_arg(argname, displayname,
427 limited_capi=self.limited_capi)
428 if parsearg is None:
429 self.converters[0].use_converter()
430 parsearg = """
431 if (!PyArg_Parse(%s, "{format_units}:{name}", {parse_arguments})) {{
432 goto exit;
433 }}
434 """ % argname
435
436 parser_code = libclinic.normalize_snippet(parsearg, indent=4)
437 self.parser_body(parser_code)
438
439 def parse_option_groups(self) -> None:
440 # positional parameters with option groups
441 # (we have to generate lots of PyArg_ParseTuple calls
442 # in a big switch statement)
443
444 self.flags = "METH_VARARGS"
445 self.parser_prototype = PARSER_PROTOTYPE_VARARGS
446 parser_code = ' {option_group_parsing}'
447 self.parser_body(parser_code)
448
449 def parse_pos_only(self) -> None:
450 if self.fastcall:
451 # positional-only, but no option groups
452 # we only need one call to _PyArg_ParseStack
453
454 self.flags = "METH_FASTCALL"
455 self.parser_prototype = PARSER_PROTOTYPE_FASTCALL
456 nargs = 'nargs'
457 argname_fmt = 'args[%d]'
458 else:
459 # positional-only, but no option groups
460 # we only need one call to PyArg_ParseTuple
461
462 self.flags = "METH_VARARGS"
463 self.parser_prototype = PARSER_PROTOTYPE_VARARGS
464 if self.limited_capi:
465 nargs = 'PyTuple_Size(args)'
466 argname_fmt = 'PyTuple_GetItem(args, %d)'
467 else:
468 nargs = 'PyTuple_GET_SIZE(args)'
469 argname_fmt = 'PyTuple_GET_ITEM(args, %d)'
470
471 left_args = f"{nargs} - {self.max_pos}"
472 max_args = NO_VARARG if (self.vararg != NO_VARARG) else self.max_pos
473 if self.limited_capi:
474 parser_code = []
475 if nargs != 'nargs':
476 nargs_def = f'Py_ssize_t nargs = {nargs};'
477 parser_code.append(libclinic.normalize_snippet(nargs_def, indent=4))
478 nargs = 'nargs'
479 if self.min_pos == max_args:
480 pl = '' if self.min_pos == 1 else 's'
481 parser_code.append(libclinic.normalize_snippet(f"""
482 if ({nargs} != {self.min_pos}) {{{{
483 PyErr_Format(PyExc_TypeError, "{{name}} expected {self.min_pos} argument{pl}, got %zd", {nargs});
484 goto exit;
485 }}}}
486 """,
487 indent=4))
488 else:
489 if self.min_pos:
490 pl = '' if self.min_pos == 1 else 's'
491 parser_code.append(libclinic.normalize_snippet(f"""
492 if ({nargs} < {self.min_pos}) {{{{
493 PyErr_Format(PyExc_TypeError, "{{name}} expected at least {self.min_pos} argument{pl}, got %zd", {nargs});
494 goto exit;
495 }}}}
496 """,
497 indent=4))
498 if max_args != NO_VARARG:
499 pl = '' if max_args == 1 else 's'
500 parser_code.append(libclinic.normalize_snippet(f"""
501 if ({nargs} > {max_args}) {{{{
502 PyErr_Format(PyExc_TypeError, "{{name}} expected at most {max_args} argument{pl}, got %zd", {nargs});
503 goto exit;
504 }}}}
505 """,
506 indent=4))
507 else:
508 self.codegen.add_include('pycore_modsupport.h',
509 '_PyArg_CheckPositional()')
510 parser_code = [libclinic.normalize_snippet(f"""
511 if (!_PyArg_CheckPositional("{{name}}", {nargs}, {self.min_pos}, {max_args})) {{{{
512 goto exit;
513 }}}}
514 """, indent=4)]
515
516 has_optional = False
517 use_parser_code = True
518 for i, p in enumerate(self.parameters):
519 if p.is_vararg():
520 if self.fastcall:
521 parser_code.append(libclinic.normalize_snippet("""
522 %s = PyTuple_New(%s);
523 if (!%s) {{
524 goto exit;
525 }}
526 for (Py_ssize_t i = 0; i < %s; ++i) {{
527 PyTuple_SET_ITEM(%s, i, Py_NewRef(args[%d + i]));
528 }}
529 """ % (
530 p.converter.parser_name,
531 left_args,
532 p.converter.parser_name,
533 left_args,
534 p.converter.parser_name,
535 self.max_pos
536 ), indent=4))
537 else:
538 parser_code.append(libclinic.normalize_snippet("""
539 %s = PyTuple_GetSlice(%d, -1);
540 """ % (
541 p.converter.parser_name,
542 self.max_pos
543 ), indent=4))
544 continue
545
546 displayname = p.get_displayname(i+1)
547 argname = argname_fmt % i
548 parsearg: str | None
549 parsearg = p.converter.parse_arg(argname, displayname, limited_capi=self.limited_capi)
550 if parsearg is None:
551 use_parser_code = False
552 parser_code = []
553 break
554 if has_optional or p.is_optional():
555 has_optional = True
556 parser_code.append(libclinic.normalize_snippet("""
557 if (%s < %d) {{
558 goto skip_optional;
559 }}
560 """, indent=4) % (nargs, i + 1))
561 parser_code.append(libclinic.normalize_snippet(parsearg, indent=4))
562
563 if use_parser_code:
564 if has_optional:
565 parser_code.append("skip_optional:")
566 else:
567 for parameter in self.parameters:
568 parameter.converter.use_converter()
569
570 if self.limited_capi:
571 self.fastcall = False
572 if self.fastcall:
573 self.codegen.add_include('pycore_modsupport.h',
574 '_PyArg_ParseStack()')
575 parser_code = [libclinic.normalize_snippet("""
576 if (!_PyArg_ParseStack(args, nargs, "{format_units}:{name}",
577 {parse_arguments})) {{
578 goto exit;
579 }}
580 """, indent=4)]
581 else:
582 self.flags = "METH_VARARGS"
583 self.parser_prototype = PARSER_PROTOTYPE_VARARGS
584 parser_code = [libclinic.normalize_snippet("""
585 if (!PyArg_ParseTuple(args, "{format_units}:{name}",
586 {parse_arguments})) {{
587 goto exit;
588 }}
589 """, indent=4)]
590 self.parser_body(*parser_code)
591
592 def parse_general(self, clang: CLanguage) -> None:
593 parsearg: str | None
594 deprecated_positionals: dict[int, Parameter] = {}
595 deprecated_keywords: dict[int, Parameter] = {}
596 for i, p in enumerate(self.parameters):
597 if p.deprecated_positional:
598 deprecated_positionals[i] = p
599 if p.deprecated_keyword:
600 deprecated_keywords[i] = p
601
602 has_optional_kw = (
603 max(self.pos_only, self.min_pos) + self.min_kw_only
604 < len(self.converters) - int(self.vararg != NO_VARARG)
605 )
606
607 use_parser_code = True
608 if self.limited_capi:
609 parser_code = []
610 use_parser_code = False
611 self.fastcall = False
612 else:
613 if self.vararg == NO_VARARG:
614 self.codegen.add_include('pycore_modsupport.h',
615 '_PyArg_UnpackKeywords()')
616 args_declaration = "_PyArg_UnpackKeywords", "%s, %s, %s" % (
617 self.min_pos,
618 self.max_pos,
619 self.min_kw_only
620 )
621 nargs = "nargs"
622 else:
623 self.codegen.add_include('pycore_modsupport.h',
624 '_PyArg_UnpackKeywordsWithVararg()')
625 args_declaration = "_PyArg_UnpackKeywordsWithVararg", "%s, %s, %s, %s" % (
626 self.min_pos,
627 self.max_pos,
628 self.min_kw_only,
629 self.vararg
630 )
631 nargs = f"Py_MIN(nargs, {self.max_pos})" if self.max_pos else "0"
632
633 if self.fastcall:
634 self.flags = "METH_FASTCALL|METH_KEYWORDS"
635 self.parser_prototype = PARSER_PROTOTYPE_FASTCALL_KEYWORDS
636 argname_fmt = 'args[%d]'
637 self.declarations = declare_parser(self.func, codegen=self.codegen)
638 self.declarations += "\nPyObject *argsbuf[%s];" % len(self.converters)
639 if has_optional_kw:
640 self.declarations += "\nPy_ssize_t noptargs = %s + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - %d;" % (nargs, self.min_pos + self.min_kw_only)
641 parser_code = [libclinic.normalize_snippet("""
642 args = %s(args, nargs, NULL, kwnames, &_parser, %s, argsbuf);
643 if (!args) {{
644 goto exit;
645 }}
646 """ % args_declaration, indent=4)]
647 else:
648 # positional-or-keyword arguments
649 self.flags = "METH_VARARGS|METH_KEYWORDS"
650 self.parser_prototype = PARSER_PROTOTYPE_KEYWORD
651 argname_fmt = 'fastargs[%d]'
652 self.declarations = declare_parser(self.func, codegen=self.codegen)
653 self.declarations += "\nPyObject *argsbuf[%s];" % len(self.converters)
654 self.declarations += "\nPyObject * const *fastargs;"
655 self.declarations += "\nPy_ssize_t nargs = PyTuple_GET_SIZE(args);"
656 if has_optional_kw:
657 self.declarations += "\nPy_ssize_t noptargs = %s + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - %d;" % (nargs, self.min_pos + self.min_kw_only)
658 parser_code = [libclinic.normalize_snippet("""
659 fastargs = %s(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, %s, argsbuf);
660 if (!fastargs) {{
661 goto exit;
662 }}
663 """ % args_declaration, indent=4)]
664
665 if self.requires_defining_class:
666 self.flags = 'METH_METHOD|' + self.flags
667 self.parser_prototype = PARSER_PROTOTYPE_DEF_CLASS
668
669 if use_parser_code:
670 if deprecated_keywords:
671 code = clang.deprecate_keyword_use(self.func, deprecated_keywords,
672 argname_fmt,
673 codegen=self.codegen,
674 fastcall=self.fastcall)
675 parser_code.append(code)
676
677 add_label: str | None = None
678 for i, p in enumerate(self.parameters):
679 if isinstance(p.converter, defining_class_converter):
680 raise ValueError("defining_class should be the first "
681 "parameter (after clang)")
682 displayname = p.get_displayname(i+1)
683 parsearg = p.converter.parse_arg(argname_fmt % i, displayname, limited_capi=self.limited_capi)
684 if parsearg is None:
685 parser_code = []
686 use_parser_code = False
687 break
688 if add_label and (i == self.pos_only or i == self.max_pos):
689 parser_code.append("%s:" % add_label)
690 add_label = None
691 if not p.is_optional():
692 parser_code.append(libclinic.normalize_snippet(parsearg, indent=4))
693 elif i < self.pos_only:
694 add_label = 'skip_optional_posonly'
695 parser_code.append(libclinic.normalize_snippet("""
696 if (nargs < %d) {{
697 goto %s;
698 }}
699 """ % (i + 1, add_label), indent=4))
700 if has_optional_kw:
701 parser_code.append(libclinic.normalize_snippet("""
702 noptargs--;
703 """, indent=4))
704 parser_code.append(libclinic.normalize_snippet(parsearg, indent=4))
705 else:
706 if i < self.max_pos:
707 label = 'skip_optional_pos'
708 first_opt = max(self.min_pos, self.pos_only)
709 else:
710 label = 'skip_optional_kwonly'
711 first_opt = self.max_pos + self.min_kw_only
712 if self.vararg != NO_VARARG:
713 first_opt += 1
714 if i == first_opt:
715 add_label = label
716 parser_code.append(libclinic.normalize_snippet("""
717 if (!noptargs) {{
718 goto %s;
719 }}
720 """ % add_label, indent=4))
721 if i + 1 == len(self.parameters):
722 parser_code.append(libclinic.normalize_snippet(parsearg, indent=4))
723 else:
724 add_label = label
725 parser_code.append(libclinic.normalize_snippet("""
726 if (%s) {{
727 """ % (argname_fmt % i), indent=4))
728 parser_code.append(libclinic.normalize_snippet(parsearg, indent=8))
729 parser_code.append(libclinic.normalize_snippet("""
730 if (!--noptargs) {{
731 goto %s;
732 }}
733 }}
734 """ % add_label, indent=4))
735
736 if use_parser_code:
737 if add_label:
738 parser_code.append("%s:" % add_label)
739 else:
740 for parameter in self.parameters:
741 parameter.converter.use_converter()
742
743 self.declarations = declare_parser(self.func, codegen=self.codegen,
744 hasformat=True)
745 if self.limited_capi:
746 # positional-or-keyword arguments
747 assert not self.fastcall
748 self.flags = "METH_VARARGS|METH_KEYWORDS"
749 self.parser_prototype = PARSER_PROTOTYPE_KEYWORD
750 parser_code = [libclinic.normalize_snippet("""
751 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "{format_units}:{name}", _keywords,
752 {parse_arguments}))
753 goto exit;
754 """, indent=4)]
755 self.declarations = "static char *_keywords[] = {{{keywords_c} NULL}};"
756 if deprecated_positionals or deprecated_keywords:
757 self.declarations += "\nPy_ssize_t nargs = PyTuple_Size(args);"
758
759 elif self.fastcall:
760 self.codegen.add_include('pycore_modsupport.h',
761 '_PyArg_ParseStackAndKeywords()')
762 parser_code = [libclinic.normalize_snippet("""
763 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser{parse_arguments_comma}
764 {parse_arguments})) {{
765 goto exit;
766 }}
767 """, indent=4)]
768 else:
769 self.codegen.add_include('pycore_modsupport.h',
770 '_PyArg_ParseTupleAndKeywordsFast()')
771 parser_code = [libclinic.normalize_snippet("""
772 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
773 {parse_arguments})) {{
774 goto exit;
775 }}
776 """, indent=4)]
777 if deprecated_positionals or deprecated_keywords:
778 self.declarations += "\nPy_ssize_t nargs = PyTuple_GET_SIZE(args);"
779 if deprecated_keywords:
780 code = clang.deprecate_keyword_use(self.func, deprecated_keywords,
781 codegen=self.codegen,
782 fastcall=self.fastcall)
783 parser_code.append(code)
784
785 if deprecated_positionals:
786 code = clang.deprecate_positional_use(self.func, deprecated_positionals)
787 # Insert the deprecation code before parameter parsing.
788 parser_code.insert(0, code)
789
790 assert self.parser_prototype is not None
791 self.parser_body(*parser_code, declarations=self.declarations)
792
793 def copy_includes(self) -> None:
794 # Copy includes from parameters to Clinic after parse_arg()
795 # has been called above.
796 for converter in self.converters:
797 for include in converter.get_includes():
798 self.codegen.add_include(
799 include.filename,
800 include.reason,
801 condition=include.condition)
802
803 def handle_new_or_init(self) -> None:
804 self.methoddef_define = ''
805
806 if self.func.kind is METHOD_NEW:
807 self.parser_prototype = PARSER_PROTOTYPE_KEYWORD
808 else:
809 self.return_value_declaration = "int return_value = -1;"
810 self.parser_prototype = PARSER_PROTOTYPE_KEYWORD___INIT__
811
812 fields: list[str] = list(self.parser_body_fields)
813 parses_positional = 'METH_NOARGS' not in self.flags
814 parses_keywords = 'METH_KEYWORDS' in self.flags
815 if parses_keywords:
816 assert parses_positional
817
818 if self.requires_defining_class:
819 raise ValueError("Slot methods cannot access their defining class.")
820
821 if not parses_keywords:
822 self.declarations = '{base_type_ptr}'
823 self.codegen.add_include('pycore_modsupport.h',
824 '_PyArg_NoKeywords()')
825 fields.insert(0, libclinic.normalize_snippet("""
826 if ({self_type_check}!_PyArg_NoKeywords("{name}", kwargs)) {{
827 goto exit;
828 }}
829 """, indent=4))
830 if not parses_positional:
831 self.codegen.add_include('pycore_modsupport.h',
832 '_PyArg_NoPositional()')
833 fields.insert(0, libclinic.normalize_snippet("""
834 if ({self_type_check}!_PyArg_NoPositional("{name}", args)) {{
835 goto exit;
836 }}
837 """, indent=4))
838
839 self.parser_body(*fields, declarations=self.declarations)
840
841 def process_methoddef(self, clang: CLanguage) -> None:
842 methoddef_cast_end = ""
843 if self.flags in ('METH_NOARGS', 'METH_O', 'METH_VARARGS'):
844 methoddef_cast = "(PyCFunction)"
845 elif self.func.kind is GETTER:
846 methoddef_cast = "" # This should end up unused
847 elif self.limited_capi:
848 methoddef_cast = "(PyCFunction)(void(*)(void))"
849 else:
850 methoddef_cast = "_PyCFunction_CAST("
851 methoddef_cast_end = ")"
852
853 if self.func.methoddef_flags:
854 self.flags += '|' + self.func.methoddef_flags
855
856 self.methoddef_define = self.methoddef_define.replace('{methoddef_flags}', self.flags)
857 self.methoddef_define = self.methoddef_define.replace('{methoddef_cast}', methoddef_cast)
858 self.methoddef_define = self.methoddef_define.replace('{methoddef_cast_end}', methoddef_cast_end)
859
860 self.methoddef_ifndef = ''
861 conditional = clang.cpp.condition()
862 if not conditional:
863 self.cpp_if = self.cpp_endif = ''
864 else:
865 self.cpp_if = "#if " + conditional
866 self.cpp_endif = "#endif /* " + conditional + " */"
867
868 if self.methoddef_define and self.codegen.add_ifndef_symbol(self.func.full_name):
869 self.methoddef_ifndef = METHODDEF_PROTOTYPE_IFNDEF
870
871 def finalize(self, clang: CLanguage) -> None:
872 # add ';' to the end of self.parser_prototype and self.impl_prototype
873 # (they mustn't be None, but they could be an empty string.)
874 assert self.parser_prototype is not None
875 if self.parser_prototype:
876 assert not self.parser_prototype.endswith(';')
877 self.parser_prototype += ';'
878
879 if self.impl_prototype is None:
880 self.impl_prototype = self.impl_definition
881 if self.impl_prototype:
882 self.impl_prototype += ";"
883
884 self.parser_definition = self.parser_definition.replace("{return_value_declaration}", self.return_value_declaration)
885
886 compiler_warning = clang.compiler_deprecated_warning(self.func, self.parameters)
887 if compiler_warning:
888 self.parser_definition = compiler_warning + "\n\n" + self.parser_definition
889
890 def create_template_dict(self) -> dict[str, str]:
891 d = {
892 "docstring_prototype" : self.docstring_prototype,
893 "docstring_definition" : self.docstring_definition,
894 "impl_prototype" : self.impl_prototype,
895 "methoddef_define" : self.methoddef_define,
896 "parser_prototype" : self.parser_prototype,
897 "parser_definition" : self.parser_definition,
898 "impl_definition" : self.impl_definition,
899 "cpp_if" : self.cpp_if,
900 "cpp_endif" : self.cpp_endif,
901 "methoddef_ifndef" : self.methoddef_ifndef,
902 }
903
904 # make sure we didn't forget to assign something,
905 # and wrap each non-empty value in \n's
906 d2 = {}
907 for name, value in d.items():
908 assert value is not None, "got a None value for template " + repr(name)
909 if value:
910 value = '\n' + value + '\n'
911 d2[name] = value
912 return d2
913
914 def parse_args(self, clang: CLanguage) -> dict[str, str]:
915 self.select_prototypes()
916 self.init_limited_capi()
917
918 self.flags = ""
919 self.declarations = ""
920 self.parser_prototype = ""
921 self.parser_definition = ""
922 self.impl_prototype = None
923 self.impl_definition = IMPL_DEFINITION_PROTOTYPE
924
925 # parser_body_fields remembers the fields passed in to the
926 # previous call to parser_body. this is used for an awful hack.
927 self.parser_body_fields: tuple[str, ...] = ()
928
929 if not self.parameters:
930 self.parse_no_args()
931 elif self.use_meth_o():
932 self.parse_one_arg()
933 elif self.has_option_groups():
934 self.parse_option_groups()
935 elif (not self.requires_defining_class
936 and self.pos_only == len(self.parameters) - self.pseudo_args):
937 self.parse_pos_only()
938 else:
939 self.parse_general(clang)
940
941 self.copy_includes()
942 if self.is_new_or_init():
943 self.handle_new_or_init()
944 self.process_methoddef(clang)
945 self.finalize(clang)
946
947 return self.create_template_dict()