Commit d4ad59e
committed
Clear the copy of the globs dict after running examples. This helps to
break cycles, which are a special problem when running generator tests
that provoke exceptions by invoking the .next() method of a named
generator-iterator: then the iterator is named in globs, and the
iterator's frame gets a tracekback object pointing back to globs, and
gc doesn't chase these types so the cycle leaks.
Also changed _run_examples() to make a copy of globs itself, so its
callers (direct and indirect) don't have to (and changed the callers
to stop making their own copies); *that* much is a change I've been
meaning to make for a long time (it's more robust the new way).
Here's a way to provoke the symptom without doctest; it leaks at a
prodigious rate; if the last two "source" lines are replaced with
g().next()
the iterator isn't named and then there's no leak:
source = """\
def g():
yield 1/0
k = g()
k.next()
"""
code = compile(source, "<source>", "exec")
def f(globs):
try:
exec code in globs
except ZeroDivisionError:
pass
while 1:
f(globals().copy())
After this change, running test_generators in an infinite loop still leaks,
but reduced from a flood to a trickle.1 parent 77f2d50 commit d4ad59e
1 file changed
Lines changed: 15 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
532 | | - | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
533 | 536 | | |
534 | 537 | | |
535 | 538 | | |
536 | 539 | | |
| 540 | + | |
537 | 541 | | |
538 | 542 | | |
539 | 543 | | |
540 | 544 | | |
541 | 545 | | |
542 | 546 | | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
543 | 554 | | |
544 | 555 | | |
545 | 556 | | |
546 | 557 | | |
547 | 558 | | |
548 | | - | |
| 559 | + | |
549 | 560 | | |
550 | 561 | | |
551 | 562 | | |
| |||
735 | 746 | | |
736 | 747 | | |
737 | 748 | | |
738 | | - | |
| 749 | + | |
739 | 750 | | |
740 | 751 | | |
741 | 752 | | |
| |||
773 | 784 | | |
774 | 785 | | |
775 | 786 | | |
776 | | - | |
777 | | - | |
| 787 | + | |
778 | 788 | | |
779 | 789 | | |
780 | 790 | | |
| |||
0 commit comments