mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-104050: Annotate toplevel functions in clinic.py (#106435)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
17af98227f
commit
110b97c94c
1 changed files with 47 additions and 16 deletions
|
@ -575,7 +575,7 @@ def permute_optional_groups(left, required, right):
|
||||||
return tuple(accumulator)
|
return tuple(accumulator)
|
||||||
|
|
||||||
|
|
||||||
def strip_leading_and_trailing_blank_lines(s):
|
def strip_leading_and_trailing_blank_lines(s: str) -> str:
|
||||||
lines = s.rstrip().split('\n')
|
lines = s.rstrip().split('\n')
|
||||||
while lines:
|
while lines:
|
||||||
line = lines[0]
|
line = lines[0]
|
||||||
|
@ -585,7 +585,11 @@ def strip_leading_and_trailing_blank_lines(s):
|
||||||
return '\n'.join(lines)
|
return '\n'.join(lines)
|
||||||
|
|
||||||
@functools.lru_cache()
|
@functools.lru_cache()
|
||||||
def normalize_snippet(s, *, indent=0):
|
def normalize_snippet(
|
||||||
|
s: str,
|
||||||
|
*,
|
||||||
|
indent: int = 0
|
||||||
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Reformats s:
|
Reformats s:
|
||||||
* removes leading and trailing blank lines
|
* removes leading and trailing blank lines
|
||||||
|
@ -599,7 +603,11 @@ def normalize_snippet(s, *, indent=0):
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def declare_parser(f, *, hasformat=False):
|
def declare_parser(
|
||||||
|
f: Function,
|
||||||
|
*,
|
||||||
|
hasformat: bool = False
|
||||||
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Generates the code template for a static local PyArg_Parser variable,
|
Generates the code template for a static local PyArg_Parser variable,
|
||||||
with an initializer. For core code (incl. builtin modules) the
|
with an initializer. For core code (incl. builtin modules) the
|
||||||
|
@ -658,7 +666,10 @@ def declare_parser(f, *, hasformat=False):
|
||||||
return normalize_snippet(declarations)
|
return normalize_snippet(declarations)
|
||||||
|
|
||||||
|
|
||||||
def wrap_declarations(text, length=78):
|
def wrap_declarations(
|
||||||
|
text: str,
|
||||||
|
length: int = 78
|
||||||
|
) -> str:
|
||||||
"""
|
"""
|
||||||
A simple-minded text wrapper for C function declarations.
|
A simple-minded text wrapper for C function declarations.
|
||||||
|
|
||||||
|
@ -680,14 +691,14 @@ def wrap_declarations(text, length=78):
|
||||||
if not after_l_paren:
|
if not after_l_paren:
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
continue
|
continue
|
||||||
parameters, _, after_r_paren = after_l_paren.partition(')')
|
in_paren, _, after_r_paren = after_l_paren.partition(')')
|
||||||
if not _:
|
if not _:
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
continue
|
continue
|
||||||
if ',' not in parameters:
|
if ',' not in in_paren:
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
continue
|
continue
|
||||||
parameters = [x.strip() + ", " for x in parameters.split(',')]
|
parameters = [x.strip() + ", " for x in in_paren.split(',')]
|
||||||
prefix += "("
|
prefix += "("
|
||||||
if len(prefix) < length:
|
if len(prefix) < length:
|
||||||
spaces = " " * len(prefix)
|
spaces = " " * len(prefix)
|
||||||
|
@ -1589,7 +1600,12 @@ def OverrideStdioWith(stdout):
|
||||||
sys.stdout = saved_stdout
|
sys.stdout = saved_stdout
|
||||||
|
|
||||||
|
|
||||||
def create_regex(before, after, word=True, whole_line=True):
|
def create_regex(
|
||||||
|
before: str,
|
||||||
|
after: str,
|
||||||
|
word: bool = True,
|
||||||
|
whole_line: bool = True
|
||||||
|
) -> re.Pattern[str]:
|
||||||
"""Create an re object for matching marker lines."""
|
"""Create an re object for matching marker lines."""
|
||||||
group_re = r"\w+" if word else ".+"
|
group_re = r"\w+" if word else ".+"
|
||||||
pattern = r'{}({}){}'
|
pattern = r'{}({}){}'
|
||||||
|
@ -1985,7 +2001,7 @@ def file_changed(filename: str, new_contents: str) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def write_file(filename: str, new_contents: str):
|
def write_file(filename: str, new_contents: str) -> None:
|
||||||
# Atomic write using a temporary file and os.replace()
|
# Atomic write using a temporary file and os.replace()
|
||||||
filename_new = f"{filename}.new"
|
filename_new = f"{filename}.new"
|
||||||
with open(filename_new, "w", encoding="utf-8") as fp:
|
with open(filename_new, "w", encoding="utf-8") as fp:
|
||||||
|
@ -2602,7 +2618,10 @@ class LandMine:
|
||||||
fail("Stepped on a land mine, trying to access attribute " + repr(name) + ":\n" + self.__message__)
|
fail("Stepped on a land mine, trying to access attribute " + repr(name) + ":\n" + self.__message__)
|
||||||
|
|
||||||
|
|
||||||
def add_c_converter(f, name=None):
|
def add_c_converter(
|
||||||
|
f: type[CConverter],
|
||||||
|
name: str | None = None
|
||||||
|
) -> type[CConverter]:
|
||||||
if not name:
|
if not name:
|
||||||
name = f.__name__
|
name = f.__name__
|
||||||
if not name.endswith('_converter'):
|
if not name.endswith('_converter'):
|
||||||
|
@ -2620,7 +2639,10 @@ def add_default_legacy_c_converter(cls):
|
||||||
legacy_converters[cls.format_unit] = cls
|
legacy_converters[cls.format_unit] = cls
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
def add_legacy_c_converter(format_unit, **kwargs):
|
def add_legacy_c_converter(
|
||||||
|
format_unit: str,
|
||||||
|
**kwargs
|
||||||
|
) -> Callable[[ConverterType], ConverterType]:
|
||||||
"""
|
"""
|
||||||
Adds a legacy converter.
|
Adds a legacy converter.
|
||||||
"""
|
"""
|
||||||
|
@ -3887,7 +3909,9 @@ class Py_buffer_converter(CConverter):
|
||||||
return super().parse_arg(argname, displayname)
|
return super().parse_arg(argname, displayname)
|
||||||
|
|
||||||
|
|
||||||
def correct_name_for_self(f) -> tuple[str, str]:
|
def correct_name_for_self(
|
||||||
|
f: Function
|
||||||
|
) -> tuple[str, str]:
|
||||||
if f.kind in (CALLABLE, METHOD_INIT):
|
if f.kind in (CALLABLE, METHOD_INIT):
|
||||||
if f.cls:
|
if f.cls:
|
||||||
return "PyObject *", "self"
|
return "PyObject *", "self"
|
||||||
|
@ -3898,7 +3922,9 @@ def correct_name_for_self(f) -> tuple[str, str]:
|
||||||
return "PyTypeObject *", "type"
|
return "PyTypeObject *", "type"
|
||||||
raise RuntimeError("Unhandled type of function f: " + repr(f.kind))
|
raise RuntimeError("Unhandled type of function f: " + repr(f.kind))
|
||||||
|
|
||||||
def required_type_for_self_for_parser(f):
|
def required_type_for_self_for_parser(
|
||||||
|
f: Function
|
||||||
|
) -> str | None:
|
||||||
type, _ = correct_name_for_self(f)
|
type, _ = correct_name_for_self(f)
|
||||||
if f.kind in (METHOD_INIT, METHOD_NEW, STATIC_METHOD, CLASS_METHOD):
|
if f.kind in (METHOD_INIT, METHOD_NEW, STATIC_METHOD, CLASS_METHOD):
|
||||||
return type
|
return type
|
||||||
|
@ -4193,7 +4219,12 @@ class float_return_converter(double_return_converter):
|
||||||
cast = '(double)'
|
cast = '(double)'
|
||||||
|
|
||||||
|
|
||||||
def eval_ast_expr(node, globals, *, filename='-'):
|
def eval_ast_expr(
|
||||||
|
node: ast.expr,
|
||||||
|
globals: dict[str, Any],
|
||||||
|
*,
|
||||||
|
filename: str = '-'
|
||||||
|
) -> FunctionType:
|
||||||
"""
|
"""
|
||||||
Takes an ast.Expr node. Compiles and evaluates it.
|
Takes an ast.Expr node. Compiles and evaluates it.
|
||||||
Returns the result of the expression.
|
Returns the result of the expression.
|
||||||
|
@ -4205,8 +4236,8 @@ def eval_ast_expr(node, globals, *, filename='-'):
|
||||||
if isinstance(node, ast.Expr):
|
if isinstance(node, ast.Expr):
|
||||||
node = node.value
|
node = node.value
|
||||||
|
|
||||||
node = ast.Expression(node)
|
expr = ast.Expression(node)
|
||||||
co = compile(node, filename, 'eval')
|
co = compile(expr, filename, 'eval')
|
||||||
fn = FunctionType(co, globals)
|
fn = FunctionType(co, globals)
|
||||||
return fn()
|
return fn()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue