Skip to content

Commit

Permalink
flush keepalives on operator assignment statements (python#13144)
Browse files Browse the repository at this point in the history
This PR makes mypyc flush keepalives on operator assignment statements, to prevent accessing undefined variables in the generated C code.

Fixes mypyc/mypyc#941.
  • Loading branch information
zsol authored and hauntsaninja committed Jul 16, 2022
1 parent 9f99400 commit 42e355f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions mypyc/irbuild/statement.py
Expand Up @@ -136,6 +136,7 @@ def transform_operator_assignment_stmt(builder: IRBuilder, stmt: OperatorAssignm
# usually operator assignments are done in-place
# but when target doesn't support that we need to manually assign
builder.assign(target, res, res.line)
builder.flush_keep_alives()


def transform_import(builder: IRBuilder, node: Import) -> None:
Expand Down
12 changes: 12 additions & 0 deletions mypyc/test-data/run-generators.test
Expand Up @@ -595,3 +595,15 @@ class C:
self.foo.flag = True
yield
self.foo.flag = False

[case testGeneratorEarlyReturnWithBorrows]
from typing import Iterator
class Bar:
bar = 0
class Foo:
bar = Bar()
def f(self) -> Iterator[int]:
if self:
self.bar.bar += 1
return
yield 0

0 comments on commit 42e355f

Please sign in to comment.