Add generator close regression test

This commit is contained in:
kigland 2026-06-08 16:03:54 +08:00 committed by Ram Rachum
parent d5257380b2
commit 19f39a00a4

View file

@ -5,9 +5,11 @@ f_lasti can be -1, and co_code[-1] would read the wrong opcode and misreport a
normal return as "Call ended by exception".
"""
import io
import opcode
from unittest.mock import Mock
import pysnooper
from pysnooper.tracer import call_ended_by_exception
@ -39,3 +41,22 @@ def test_last_opcode_not_return_is_exception():
def test_non_return_event_is_never_exception():
co_code = bytes([opcode.opmap["NOP"], 0])
assert call_ended_by_exception(_frame(co_code, 0), "call", None) is False
def test_unstarted_generator_close_is_not_an_exception():
string_io = io.StringIO()
def gen(n):
for i in range(n):
yield i
@pysnooper.snoop(string_io, depth=2, color=False)
def main():
g = gen(5)
g.close()
return 42
assert main() == 42
output = string_io.getvalue()
assert "Call ended by exception" not in output
assert "Return value:.. 42" in output