[2.2.x] Fixed #25624 -- Fixed autoreload crash with jinja2.ModuleLoader.

Backport of 1e92407f83 from master.
This commit is contained in:
Tom Forbes 2019-01-19 00:16:33 +00:00 committed by Tim Graham
parent 2ca200a7c3
commit 728358c5cf
2 changed files with 10 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import sys
import threading import threading
import time import time
import traceback import traceback
import weakref
from collections import defaultdict from collections import defaultdict
from pathlib import Path from pathlib import Path
from types import ModuleType from types import ModuleType
@ -98,7 +99,7 @@ def iter_all_python_module_files():
# This ensures cached results are returned in the usual case that modules # This ensures cached results are returned in the usual case that modules
# aren't loaded on the fly. # aren't loaded on the fly.
modules_view = sorted(list(sys.modules.items()), key=lambda i: i[0]) modules_view = sorted(list(sys.modules.items()), key=lambda i: i[0])
modules = tuple(m[1] for m in modules_view) modules = tuple(m[1] for m in modules_view if not isinstance(m[1], weakref.ProxyTypes))
return iter_modules_and_files(modules, frozenset(_error_files)) return iter_modules_and_files(modules, frozenset(_error_files))

View File

@ -6,6 +6,7 @@ import sys
import tempfile import tempfile
import threading import threading
import time import time
import weakref
import zipfile import zipfile
from importlib import import_module from importlib import import_module
from pathlib import Path from pathlib import Path
@ -116,6 +117,13 @@ class TestIterModulesAndFiles(SimpleTestCase):
self.import_and_cleanup('test_compiled') self.import_and_cleanup('test_compiled')
self.assertFileFound(compiled_file) self.assertFileFound(compiled_file)
def test_weakref_in_sys_module(self):
"""iter_all_python_module_file() ignores weakref modules."""
time_proxy = weakref.proxy(time)
sys.modules['time_proxy'] = time_proxy
self.addCleanup(lambda: sys.modules.pop('time_proxy', None))
list(autoreload.iter_all_python_module_files()) # No crash.
class TestCommonRoots(SimpleTestCase): class TestCommonRoots(SimpleTestCase):
def test_common_roots(self): def test_common_roots(self):