[issue36784] __import__ with empty folder after importlib.invalidate_caches causes reference leak

Sebastian Koslowski report at bugs.python.org
Mon May 6 16:16:11 EDT 2019


Sebastian Koslowski <sebastian.koslowski at gmail.com> added the comment:

So, I dug into this here at the PyCon19 sprints and as far as I can see there is no actual leak.

What you are seeing in your code example is from the state, that is kept between successive run of your import. All the cases you reported as not leaking generate a fixed tempdir. However, if the tempdir is random (or at least differs between runs) two new modules are added to sys.modules and one entry is added to the path_importer_cache for each run. These are not cleared by invalidate_caches().

If you append the following lines to test_importlib_cache_tempdir() these objects (and the caches in them) get cleared and your test passes. 

    sys.modules.pop(basename + ".test2")
    sys.modules.pop(basename)
    sys.path_importer_cache.pop(path)

This can also be confirmed using sys.gettotalrefcount().

----------
nosy: +skoslowski

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36784>
_______________________________________


More information about the Python-bugs-list mailing list