Content-Length: 378935 | pFad | http://github.com/python/mypy/pull/19008/commits/476ebe1e858dc9f20dd5949ef251456d57776fa1

4C [mypyc] Refactor IR building for generator functions by JukkaL · Pull Request #19008 · python/mypy · GitHub
Skip to content

[mypyc] Refactor IR building for generator functions #19008

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 17 commits into from
May 2, 2025
Prev Previous commit
Next Next commit
Refactor
  • Loading branch information
JukkaL committed May 1, 2025
commit 476ebe1e858dc9f20dd5949ef251456d57776fa1
4 changes: 2 additions & 2 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ def flatten_classes(self, arg: RefExpr | TupleExpr) -> list[ClassIR] | None:
return None
return res

def enter(self, fn_info: FuncInfo | str = "") -> None:
def enter(self, fn_info: FuncInfo | str = "", *, ret_type: RType = none_rprimitive) -> None:
if isinstance(fn_info, str):
fn_info = FuncInfo(name=fn_info)
self.builder = LowLevelIRBuilder(self.errors, self.options)
Expand All @@ -1179,7 +1179,7 @@ def enter(self, fn_info: FuncInfo | str = "") -> None:
self.runtime_args.append([])
self.fn_info = fn_info
self.fn_infos.append(self.fn_info)
self.ret_types.append(none_rprimitive)
self.ret_types.append(ret_type)
if fn_info.is_generator:
self.nonlocal_control.append(GeneratorNonlocalControl())
else:
Expand Down
32 changes: 17 additions & 15 deletions mypyc/irbuild/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,20 @@ def c() -> None:
func_name = singledispatch_main_func_name(name)
else:
func_name = name
builder.enter(
FuncInfo(
fitem=fitem,
name=func_name,
class_name=class_name,
namespace=gen_func_ns(builder),
is_nested=is_nested,
contains_nested=contains_nested,
is_decorated=is_decorated,
in_non_ext=in_non_ext,
add_nested_funcs_to_env=add_nested_funcs_to_env,
)

fn_info = FuncInfo(
fitem=fitem,
name=func_name,
class_name=class_name,
namespace=gen_func_ns(builder),
is_nested=is_nested,
contains_nested=contains_nested,
is_decorated=is_decorated,
in_non_ext=in_non_ext,
add_nested_funcs_to_env=add_nested_funcs_to_env,
)
is_generator = builder.fn_info.is_generator
is_generator = fn_info.is_generator
builder.enter(fn_info, ret_type=sig.ret_type if not is_generator else object_rprimitive)

# Functions that contain nested functions need an environment class to store variables that
# are free in their nested functions. Generator functions need an environment class to
Expand All @@ -266,6 +266,10 @@ def c() -> None:
builder,
lambda args, blocks, fn_info: gen_func_ir(builder, args, blocks, sig, fn_info, cdef, is_singledispatch))

# Re-enter the FuncItem and visit the body of the function this time.
builder.enter(fn_info, ret_type=sig.ret_type)
setup_env_for_generator_class(builder)

load_outer_envs(builder, builder.fn_info.generator_class)
top_level = builder.top_level_fn_info()
if (
Expand All @@ -283,8 +287,6 @@ def c() -> None:
if contains_nested:
finalize_env_class(builder)

builder.ret_types[-1] = sig.ret_type

add_vars_to_env(builder)

builder.accept(fitem.body)
Expand Down
4 changes: 0 additions & 4 deletions mypyc/irbuild/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def gen_generator_func(
args, _, blocks, ret_type, fn_info = builder.leave()
func_ir, func_reg = gen_func_ir(args, blocks, fn_info)

# Re-enter the FuncItem and visit the body of the function this time.
builder.enter(fn_info)
setup_env_for_generator_class(builder)

return func_ir, func_reg


Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/mypy/pull/19008/commits/476ebe1e858dc9f20dd5949ef251456d57776fa1

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy