Generates the code template for a static local PyArg_Parser variable, with an initializer. For core code (incl. builtin modules) the kwtuple field is also statically initialized. Otherwise it is initialized at runtime.
(
f: Function,
*,
hasformat: bool = False,
codegen: 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 p.is_positional_or_keyword() or p.is_keyword_only() |
| 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 | Py_hash_t ob_hash; |
| 69 | PyObject *ob_item[NUM_KEYWORDS]; |
| 70 | }} _kwtuple = {{ |
| 71 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 72 | .ob_hash = -1, |
| 73 | .ob_item = {{ {keywords_py} }}, |
| 74 | }}; |
no test coverage detected
searching dependent graphs…