Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
* 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'.
  • Loading branch information
vstinner committed Mar 22, 2024
1 parent 52da03c commit 3082360
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions Tools/clinic/clinic.py
Expand Up @@ -1988,7 +1988,7 @@ def parse_file(
libclinic.write_file(output, cooked)


def create_parser_namespace() -> dict[str, Any]:
def _create_parser_namespace() -> dict[str, Any]:
ns = dict(
CConverter=CConverter,
CReturnConverter=CReturnConverter,
Expand All @@ -2002,16 +2002,21 @@ def create_parser_namespace() -> dict[str, Any]:
for name, return_converter in return_converters.items():
ns[f'{name}_return_converter'] = return_converter
return ns
_BASE_PARSER_NAMESPACE = _create_parser_namespace()


def create_parser_namespace() -> dict[str, Any]:
return _BASE_PARSER_NAMESPACE.copy()


class PythonParser:
def __init__(self, clinic: Clinic) -> None:
self.namespace = create_parser_namespace()
pass

def parse(self, block: Block) -> None:
ns = dict(self.namespace)
namespace = create_parser_namespace()
with contextlib.redirect_stdout(io.StringIO()) as s:
exec(block.input, ns)
exec(block.input, namespace)
block.output = s.getvalue()


Expand Down Expand Up @@ -3477,8 +3482,9 @@ def eval_ast_expr(
node = node.value

expr = ast.Expression(node)
namespace = create_parser_namespace()
co = compile(expr, filename, 'eval')
fn = FunctionType(co, globals)
fn = FunctionType(co, namespace)
return fn()


Expand Down Expand Up @@ -5001,18 +5007,16 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
parser.error(
"can't specify --converters and a filename at the same time"
)
converter_list: list[tuple[str, str, ConverterType]] = []
return_converter_list: list[tuple[str, str, ReturnConverterType]] = []
converter_list: list[tuple[str, Any]] = []
return_converter_list: list[tuple[str, Any]] = []

for name, converter in converters.items():
converter_list.append((
f'{name}_converter',
name,
converter,
))
for name, return_converter in return_converters.items():
return_converter_list.append((
f'{name}_return_converter',
name,
return_converter
))
Expand All @@ -5031,9 +5035,9 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
):
print(title + ":")
longest = -1
for name, short_name, converter in ids:
longest = max(longest, len(short_name))
for name, short_name, cls in sorted(ids, key=lambda x: x[1].lower()):
for name, converter in ids:
longest = max(longest, len(name))
for name, cls in sorted(ids, key=lambda x: x[1].lower()):
callable = getattr(cls, attribute, None)
if not callable:
continue
Expand All @@ -5046,7 +5050,7 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
else:
s = parameter_name
parameters.append(s)
print(' {}({})'.format(short_name, ', '.join(parameters)))
print(' {}({})'.format(name, ', '.join(parameters)))
print()
print("All converters also accept (c_default=None, py_default=None, annotation=None).")
print("All return converters also accept (py_default=None).")
Expand Down

0 comments on commit 3082360

Please sign in to comment.