Skip to content

Commit 49a86eb

Browse files
committed
Update
* Use also create_parser_namespace() in eval_ast_expr(). * Only createa the parser namespace once: add _BASE_PARSER_NAMESPACE. * run_clinic(): remove the first tuple item in converter_list and return_converter_list. Rename the new first tuple item from 'short_name' to 'name'.
1 parent 52da03c commit 49a86eb

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Tools/clinic/clinic.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ def parse_file(
19881988
libclinic.write_file(output, cooked)
19891989

19901990

1991-
def create_parser_namespace() -> dict[str, Any]:
1991+
def _create_parser_namespace() -> dict[str, Any]:
19921992
ns = dict(
19931993
CConverter=CConverter,
19941994
CReturnConverter=CReturnConverter,
@@ -2002,16 +2002,20 @@ def create_parser_namespace() -> dict[str, Any]:
20022002
for name, return_converter in return_converters.items():
20032003
ns[f'{name}_return_converter'] = return_converter
20042004
return ns
2005+
_BASE_PARSER_NAMESPACE = _create_parser_namespace()
2006+
2007+
2008+
def create_parser_namespace() -> dict[str, Any]:
2009+
return _BASE_PARSER_NAMESPACE.copy()
20052010

20062011

20072012
class PythonParser:
20082013
def __init__(self, clinic: Clinic) -> None:
2009-
self.namespace = create_parser_namespace()
20102014

20112015
def parse(self, block: Block) -> None:
2012-
ns = dict(self.namespace)
2016+
namespace = create_parser_namespace()
20132017
with contextlib.redirect_stdout(io.StringIO()) as s:
2014-
exec(block.input, ns)
2018+
exec(block.input, namespace)
20152019
block.output = s.getvalue()
20162020

20172021

@@ -3477,8 +3481,9 @@ def eval_ast_expr(
34773481
node = node.value
34783482

34793483
expr = ast.Expression(node)
3484+
namespace = create_parser_namespace()
34803485
co = compile(expr, filename, 'eval')
3481-
fn = FunctionType(co, globals)
3486+
fn = FunctionType(co, namespace)
34823487
return fn()
34833488

34843489

@@ -5006,13 +5011,11 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
50065011

50075012
for name, converter in converters.items():
50085013
converter_list.append((
5009-
f'{name}_converter',
50105014
name,
50115015
converter,
50125016
))
50135017
for name, return_converter in return_converters.items():
50145018
return_converter_list.append((
5015-
f'{name}_return_converter',
50165019
name,
50175020
return_converter
50185021
))
@@ -5031,9 +5034,9 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
50315034
):
50325035
print(title + ":")
50335036
longest = -1
5034-
for name, short_name, converter in ids:
5035-
longest = max(longest, len(short_name))
5036-
for name, short_name, cls in sorted(ids, key=lambda x: x[1].lower()):
5037+
for name, converter in ids:
5038+
longest = max(longest, len(name))
5039+
for name, cls in sorted(ids, key=lambda x: x[1].lower()):
50375040
callable = getattr(cls, attribute, None)
50385041
if not callable:
50395042
continue
@@ -5046,7 +5049,7 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
50465049
else:
50475050
s = parameter_name
50485051
parameters.append(s)
5049-
print(' {}({})'.format(short_name, ', '.join(parameters)))
5052+
print(' {}({})'.format(name, ', '.join(parameters)))
50505053
print()
50515054
print("All converters also accept (c_default=None, py_default=None, annotation=None).")
50525055
print("All return converters also accept (py_default=None).")

0 commit comments

Comments
 (0)