[Python-Dev] Reference cycle on the module dict (globals()) (original) (raw)

Victor Stinner victor.stinner at gmail.com
Tue Jan 19 04:42:11 EST 2016


Hi,

While working on my FAT Python optimizer project, I found an annoying bug in my code. When at least one guard is created with a reference to the global namespace (globals(), the module dictionary), objects of the module are no more removed at exit.

Example:

import sys

class MessageAtExit: def del(self): print('del called')

display a message at exit, when message_at_exit is removed

message_at_exit = MessageAtExit()

create a reference cycle:

module -> module dict -> Guard -> module dict

guard = sys.Guard(globals())

(the code is adapted from a test of test_gc)

Apply attached patch to Python 3.6 to get the sys.Guard object. It's a minimalist object to keep a strong reference to an object.

I expected the garbage collector to break such (simple?) reference cycle.

The Guard object implements a traverse module, but it is never called.

Did I miss something obvious, or is it a known issue of the garbage collector on modules?

Victor -------------- next part -------------- A non-text attachment was scrubbed... Name: guard.patch Type: text/x-patch Size: 3804 bytes Desc: not available URL: <http://mail.python.org/pipermail/python-dev/attachments/20160119/e8ac9ca6/attachment-0001.bin>



More information about the Python-Dev mailing list