8000 gh-113317: Change how Argument Clinic lists converters by vstinner · Pull Request #116853 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-113317: Change how Argument Clinic lists converters #116853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 27, 2024
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
Fix create_parser_namespace()
* Add NoneType.
* Don't call create_parser_namespace() to startup, but only populate
  the cache at the first call.
* Remove eval_ast_expr() 'globals' argument.
  • Loading branch information
vstinner committed Mar 22, 2024
commit 44d6b5ad0ecf4340273d23d12098df675584a1f1
17 changes: 9 additions & 8 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1988,25 +1988,28 @@ def parse_file(
libclinic.write_file(output, cooked)


def _create_parser_namespace() -> dict[str, Any]:
def create_parser_namespace() -> dict[str, Any]:
global _BASE_PARSER_NAMESPACE
if _BASE_PARSER_NAMESPACE is not None:
return _BASE_PARSER_NAMESPACE.copy()

ns = dict(
CConverter=CConverter,
CReturnConverter=CReturnConverter,
buffer=buffer,
robuffer=robuffer,
rwbuffer=rwbuffer,
unspecified=unspecified,
NoneType=NoneType,
)
for name, converter in converters.items():
ns[f'{name}_converter'] = converter
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]:
_BASE_PARSER_NAMESPACE = ns
return _BASE_PARSER_NAMESPACE.copy()
_BASE_PARSER_NAMESPACE = None


class PythonParser:
Expand Down Expand Up @@ -3465,7 +3468,6 @@ class float_return_converter(double_return_converter):

def eval_ast_expr(
node: ast.expr,
globals: dict[str, Any],
*,
filename: str = '-'
) -> Any:
Expand Down Expand Up @@ -4486,12 +4488,11 @@ def parse_converter(
case ast.Name(name):
return name, False, {}
case ast.Call(func=ast.Name(name)):
symbols = globals()
kwargs: ConverterArgs = {}
for node in annotation.keywords:
if not isinstance(node.arg, str):
fail("Cannot use a kwarg splat in a function-call annotation")
kwargs[node.arg] = eval_ast_expr(node.value, symbols)
kwargs[node.arg] = eval_ast_expr(node.value)
return name, False, kwargs
case _:
fail(
Expand Down
0