unloading a module created with imp.new_module

Patrick Stinson patrickkidd at gmail.com
Mon Nov 24 02:27:16 EST 2014


How does the __del__ method have a reference to the module’s globals dict? because it references the print function?

Crazy. Is there any other way to comfort when a module is being deleted beside defining a class object with a printing dtor?


> On Nov 23, 2014, at 4:27 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> 
> 
> On Nov 23, 2014 4:10 AM, "Patrick Stinson" <patrickkidd at gmail.com <mailto:patrickkidd at gmail.com>> wrote:
> > m = types.ModuleType('mine')
> > exec(s, m.__dict__)
> > print('deleting...')
> > m = None
> > print('done')
> >
> > and the output is:
> >
> > deleting...
> > done
> > __del__
> >
> > I the “__del__" to come between “deleting…” and “done”. This is not being run from the interactive interpreter by via a .py file.
> 
> This suggests the presence of a reference cycle, since the object is deleted afterward by the garbage collector. One cycle here is that the __del__ function has a reference to the module's globals dict, which has a reference to the class instance, which has a reference to the class, which has a reference back to the function. There may be other cycles here as well, but it may also be that the module object itself isn't part of them.
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141123/9c2ea96f/attachment.html>


More information about the Python-list mailing list