Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
erlend-aasland committed Aug 11, 2022
commit 568a49cec1fbe6930f778a1a9dae01d150be5cc7
27 changes: 11 additions & 16 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ def normalize_snippet(s, *, indent=0):


def declare_parser(f, *, hasformat=False):
"""
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.
"""
def keywords(f):
params = f.parameters.values()
kwds = [
Expand All @@ -544,18 +550,13 @@ def keywords(f):
]
return kwds

"""
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.
"""
if hasformat:
fname = ''
format_ = '.format = "{format_units}:{name}",'
else:
fname = '.fname = "{name}",'
format_ = ''

num_keywords = len(keywords(f))
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
if num_keywords == 0:
declarations = """
Expand All @@ -564,15 +565,7 @@ def keywords(f):
#else
# define KWTUPLE NULL
#endif

static const char * const _keywords[] = {{{keywords_c} NULL}};
static _PyArg_Parser _parser = {{
.keywords = _keywords,
%s
.kwtuple = KWTUPLE,
}};
#undef KWTUPLE
""" % (format_ or fname)
"""
else:
declarations = """
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
Expand All @@ -590,15 +583,17 @@ def keywords(f):
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
""" % (num_keywords, num_keywords)

declarations += """
static const char * const _keywords[] = {{{keywords_c} NULL}};
static _PyArg_Parser _parser = {{
.keywords = _keywords,
%s
.kwtuple = KWTUPLE,
}};
#undef KWTUPLE
""" % (num_keywords, num_keywords, format_ or fname)
""" % (format_ or fname)
return normalize_snippet(declarations)


Expand Down