New submission from Karthikeyan Singaravelan <tir.kar...@gmail.com>:
I originally hit upon this with issue36777 where I have used support.script_helper.make_script which calls importlib.invalidate_caches and then trying to use __import__ on an empty folder causes reference leak. I tried 3.8 to 3.5 and it exists on each version. A sample script as below. I tried using try finally instead of DirsOnSysPath in doubt and it still causes leak. I couldn't find any issues on search and let me know if I am using something in an incorrect manner. import importlib import unittest import os, sys import os.path from test import support def test_importlib_cache(): with support.temp_dir() as path: dirname, basename = os.path.split(path) os.mkdir(os.path.join(path, 'test2')) importlib.invalidate_caches() with support.DirsOnSysPath(dirname): __import__("{basename}.test2".format(basename=basename)) class Tests(unittest.TestCase): def test_bug(self): for _ in range(10): test_importlib_cache() ➜ cpython git:(master) ✗ ./python.exe -m test -R 3:3 test_import_bug Run tests sequentially 0:00:00 load avg: 1.56 [1/1] test_import_bug beginning 6 repetitions 123456 ...... test_import_bug leaked [980, 980, 980] references, sum=2940 test_import_bug leaked [370, 370, 370] memory blocks, sum=1110 test_import_bug failed == Tests result: FAILURE == 1 test failed: test_import_bug Total duration: 1 sec 529 ms Tests result: FAILURE I also tried __import__('test1.test2') instead of __import__("{basename}.test2".format(basename=basename)) and the program doesn't cause reference leak. Moving importlib.invalidate_caches() above support.temp_dir() also causes leak so I guess it's not something to do with temporary directories that are cleaned up after tests. ➜ cpython git:(master) ✗ mkdir -p test1/test2 ➜ cpython git:(master) ✗ ./python.exe -m test -R 3:3 test_import_bug Run tests sequentially 0:00:00 load avg: 1.97 [1/1] test_import_bug beginning 6 repetitions 123456 ...... test_import_bug passed == Tests result: SUCCESS == 1 test OK. Total duration: 557 ms Tests result: SUCCESS ---------- components: Library (Lib) messages: 341338 nosy: brett.cannon, eric.snow, ncoghlan, xtreak priority: normal severity: normal status: open title: __import__ with empty folder after importlib.invalidate_caches causes reference leak type: behavior versions: Python 3.7, Python 3.8 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue36784> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com