Skip to content

[mypyc] Make generated generator helper method internal #19268

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 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion mypyc/codegen/emitclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def generate_finalize_for_class(
def generate_methods_table(cl: ClassIR, name: str, emitter: Emitter) -> None:
emitter.emit_line(f"static PyMethodDef {name}[] = {{")
for fn in cl.methods.values():
if fn.decl.is_prop_setter or fn.decl.is_prop_getter:
if fn.decl.is_prop_setter or fn.decl.is_prop_getter or fn.internal:
continue
emitter.emit_line(f'{{"{fn.name}",')
emitter.emit_line(f" (PyCFunction){PREFIX}{fn.cname(emitter.names)},")
Expand Down
4 changes: 2 additions & 2 deletions mypyc/codegen/emitmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def generate_function_declaration(fn: FuncIR, emitter: Emitter) -> None:
emitter.context.declarations[emitter.native_function_name(fn.decl)] = HeaderDeclaration(
f"{native_function_header(fn.decl, emitter)};", needs_export=True
)
if fn.name != TOP_LEVEL_NAME:
if fn.name != TOP_LEVEL_NAME and not fn.internal:
if is_fastcall_supported(fn, emitter.capi_version):
emitter.context.declarations[PREFIX + fn.cname(emitter.names)] = HeaderDeclaration(
f"{wrapper_function_header(fn, emitter.names)};"
Expand Down Expand Up @@ -571,7 +571,7 @@ def generate_c_for_modules(self) -> list[tuple[str, str]]:
for fn in module.functions:
emitter.emit_line()
generate_native_function(fn, emitter, self.source_paths[module_name], module_name)
if fn.name != TOP_LEVEL_NAME:
if fn.name != TOP_LEVEL_NAME and not fn.internal:
emitter.emit_line()
if is_fastcall_supported(fn, emitter.capi_version):
generate_wrapper_function(
Expand Down
1 change: 1 addition & 0 deletions mypyc/codegen/emitwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def wrapper_function_header(fn: FuncIR, names: NameGenerator) -> str:

See comment above for a summary of the arguments.
"""
assert not fn.internal
return (
"PyObject *{prefix}{name}("
"PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames)"
Expand Down
10 changes: 10 additions & 0 deletions mypyc/ir/func_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(
is_prop_setter: bool = False,
is_prop_getter: bool = False,
implicit: bool = False,
internal: bool = False,
) -> None:
self.name = name
self.class_name = class_name
Expand All @@ -160,6 +161,9 @@ def __init__(
# Currently only supported for property getters/setters
self.implicit = implicit

# If True, only direct C level calls are supported (no wrapper function)
self.internal = internal

# This is optional because this will be set to the line number when the corresponding
# FuncIR is created
self._line: int | None = None
Expand Down Expand Up @@ -204,6 +208,7 @@ def serialize(self) -> JsonDict:
"is_prop_setter": self.is_prop_setter,
"is_prop_getter": self.is_prop_getter,
"implicit": self.implicit,
"internal": self.internal,
}

# TODO: move this to FuncIR?
Expand All @@ -226,6 +231,7 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> FuncDecl:
data["is_prop_setter"],
data["is_prop_getter"],
data["implicit"],
data["internal"],
)


Expand Down Expand Up @@ -287,6 +293,10 @@ def fullname(self) -> str:
def id(self) -> str:
return self.decl.id

@property
def internal(self) -> bool:
return self.decl.internal

def cname(self, names: NameGenerator) -> str:
return self.decl.cname(names)

Expand Down
6 changes: 5 additions & 1 deletion mypyc/irbuild/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ def add_helper_to_generator_class(
sig.ret_type,
)
helper_fn_decl = FuncDecl(
"__mypyc_generator_helper__", fn_info.generator_class.ir.name, builder.module_name, sig
"__mypyc_generator_helper__",
fn_info.generator_class.ir.name,
builder.module_name,
sig,
internal=True,
)
helper_fn_ir = FuncIR(
helper_fn_decl, arg_regs, blocks, fn_info.fitem.line, traceback_name=fn_info.fitem.name
Expand Down