Sanitize function parameter names that are Python keywords.
PiperOrigin-RevId: 559436458 Change-Id: I56b8a595885f3f3d3ecb7a00db5f1bd70e5f95d9
This commit is contained in:
committed by
Copybara-Service
parent
1f5a9e8c70
commit
a2de04f5be
@@ -14,6 +14,7 @@
|
||||
# ==============================================================================
|
||||
"""Code generator for function_traits.h."""
|
||||
|
||||
import keyword
|
||||
from typing import Mapping, Sequence
|
||||
|
||||
from absl import app
|
||||
@@ -24,6 +25,12 @@ from introspect import functions
|
||||
FUNCTIONS: Mapping[str, ast_nodes.FunctionDecl] = functions.FUNCTIONS
|
||||
|
||||
|
||||
def _sanitize_keyword(s: str) -> str:
|
||||
if keyword.iskeyword(s):
|
||||
return s + '_'
|
||||
return s
|
||||
|
||||
|
||||
def main(argv: Sequence[str]) -> None:
|
||||
if len(argv) > 1:
|
||||
raise app.UsageError('Too many command-line arguments.')
|
||||
@@ -59,7 +66,9 @@ def main(argv: Sequence[str]) -> None:
|
||||
else:
|
||||
getfunc = f'::{func.name}'
|
||||
|
||||
param_names = ', '.join(f'"{p.name}"' for p in parameters)
|
||||
param_names = ', '.join(
|
||||
f'"{_sanitize_keyword(p.name)}"' for p in parameters
|
||||
)
|
||||
|
||||
struct_decls.append(f"""
|
||||
struct {func.name} {{
|
||||
|
||||
Reference in New Issue
Block a user