diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-05-26 11:19:47 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-05-26 11:20:05 +0200 |
| commit | 246a31a843dc7ea7b49d6a4491cb035776224194 (patch) | |
| tree | 88b5cb2a1034c2a82d9a775c80323c95aaf8fbb6 | |
| parent | 4ba4c07e4e3154d20513957c3ea968277d4db0c2 (diff) | |
[3.2.x] Fixed #32783 -- Fixed crash of autoreloader when __main__ module doesn't have __spec__ attribute.
Regression in ec6d2531c59466924b645f314ac33f54470d7ac3.
Thanks JonathanNickelson for the report.
Backport of 12b19a1d76e1a6f80923c8358290d605dacd65d4 from main
| -rw-r--r-- | django/utils/autoreload.py | 3 | ||||
| -rw-r--r-- | docs/releases/3.2.4.txt | 3 | ||||
| -rw-r--r-- | tests/utils_tests/test_autoreload.py | 11 |
3 files changed, 16 insertions, 1 deletions
diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py index faa3252c71..9d29654f69 100644 --- a/django/utils/autoreload.py +++ b/django/utils/autoreload.py @@ -222,7 +222,8 @@ def get_child_arguments(): args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] # __spec__ is set when the server was started with the `-m` option, # see https://docs.python.org/3/reference/import.html#main-spec - if __main__.__spec__ is not None and __main__.__spec__.parent: + # __spec__ may not exist, e.g. when running in a Conda env. + if getattr(__main__, '__spec__', None) is not None and __main__.__spec__.parent: args += ['-m', __main__.__spec__.parent] args += sys.argv[1:] elif not py_script.exists(): diff --git a/docs/releases/3.2.4.txt b/docs/releases/3.2.4.txt index 63e5096334..9941bd7513 100644 --- a/docs/releases/3.2.4.txt +++ b/docs/releases/3.2.4.txt @@ -25,3 +25,6 @@ Bugfixes * Fixed a bug in Django 3.2 that would trigger the auto-reloader for template changes when directory paths were specified with strings (:ticket:`32744`). + +* Fixed a regression in Django 3.2 that caused a crash of auto-reloader with + ``AttributeError``, e.g. inside a ``Conda`` environment (:ticket:`32783`). diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py index a43d7c2fde..12034819ee 100644 --- a/tests/utils_tests/test_autoreload.py +++ b/tests/utils_tests/test_autoreload.py @@ -213,6 +213,17 @@ class TestChildArguments(SimpleTestCase): with self.assertRaisesMessage(RuntimeError, msg): autoreload.get_child_arguments() + @mock.patch('sys.argv', [__file__, 'runserver']) + @mock.patch('sys.warnoptions', []) + def test_module_no_spec(self): + module = types.ModuleType('test_module') + del module.__spec__ + with mock.patch.dict(sys.modules, {'__main__': module}): + self.assertEqual( + autoreload.get_child_arguments(), + [sys.executable, __file__, 'runserver'] + ) + class TestUtilities(SimpleTestCase): def test_is_django_module(self): |
