From bdc1de21992bfbc58e719442d3fb990894b6b713 Mon Sep 17 00:00:00 2001 From: Tom Forbes Date: Wed, 26 Jun 2019 05:44:10 +0100 Subject: [PATCH] [2.2.x] Fixed #30588 -- Fixed crash of autoreloader when __main__ module doesn't have __file__ attribute. Backport of 8454f6dea49dddb821bfcb7569ea222bb487dfd1 from master --- django/utils/autoreload.py | 4 +++- docs/releases/2.2.3.txt | 3 +++ tests/utils_tests/test_autoreload.py | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py index c959cd04ca5..2fdc2f3f83b 100644 --- a/django/utils/autoreload.py +++ b/django/utils/autoreload.py @@ -117,7 +117,9 @@ def iter_modules_and_files(modules, extra_files): # __main__ (usually manage.py) doesn't always have a __spec__ set. # Handle this by falling back to using __file__, resolved below. # See https://docs.python.org/reference/import.html#main-spec - sys_file_paths.append(module.__file__) + # __file__ may not exists, e.g. when running ipdb debugger. + if hasattr(module, '__file__'): + sys_file_paths.append(module.__file__) continue if getattr(module, '__spec__', None) is None: continue diff --git a/docs/releases/2.2.3.txt b/docs/releases/2.2.3.txt index a42e57c779a..08c56013494 100644 --- a/docs/releases/2.2.3.txt +++ b/docs/releases/2.2.3.txt @@ -12,3 +12,6 @@ Bugfixes * Fixed a regression in Django 2.2 where :class:`~django.db.models.Avg`, :class:`~django.db.models.StdDev`, and :class:`~django.db.models.Variance` crash with ``filter`` argument (:ticket:`30542`). + +* Fixed a regression in Django 2.2.2 where auto-reloader crashes with + ``AttributeError``, e.g. when using ``ipdb`` (:ticket:`30588`). diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py index 56b03d40a61..c7308ca53ad 100644 --- a/tests/utils_tests/test_autoreload.py +++ b/tests/utils_tests/test_autoreload.py @@ -134,6 +134,10 @@ class TestIterModulesAndFiles(SimpleTestCase): main_module = sys.modules['__main__'] self.assertFileFound(Path(main_module.__file__)) + def test_main_module_without_file_is_not_resolved(self): + fake_main = types.ModuleType('__main__') + self.assertEqual(autoreload.iter_modules_and_files((fake_main,), frozenset()), frozenset()) + class TestCommonRoots(SimpleTestCase): def test_common_roots(self):