How to test for a cycle caused by saved exception?

Viewed 23

I'm talking about this problem: https://bugs.python.org/issue36820.

Small summary:

Saving an exception causes a cyclic reference, because the exception's data include a traceback containing the stack frame with the variable where the exception was saved.

try:
    1/0
except Exception as e:
    ee = e

The code is not broken, beacuse Python will eventually free the memory with its garbage collector. But the whole sitation can be avoided:

try:
    1/0
except Exception as e:
    ee = e
finally:
    ee = None

In the linked bpo-36820 there is a demonstration with a weak reference kept alive.

My question is if there exist a test that does not need to edit the function itself. Something like

  • run the tested function
  • check if a new cycle was created

Can the gc module do that?

0 Answers
Related