From 278d66b94bb443e1c1581e014fee2593a33f338c Mon Sep 17 00:00:00 2001 From: Yusuke Miyazaki Date: Wed, 25 Oct 2017 16:29:05 +0900 Subject: [PATCH] Fixed #28501 -- Fixed "python -m django runserver" crash. --- django/utils/autoreload.py | 9 ++++++++- tests/utils_tests/test_autoreload.py | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py index 2784a89aeb..d3124e8751 100644 --- a/django/utils/autoreload.py +++ b/django/utils/autoreload.py @@ -280,8 +280,15 @@ def reloader_thread(): def restart_with_reloader(): + import django.__main__ while True: - args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv + args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + if sys.argv[0] == django.__main__.__file__: + # The server was started with `python -m django runserver`. + args += ['-m', 'django'] + args += sys.argv[1:] + else: + args += sys.argv new_environ = os.environ.copy() new_environ["RUN_MAIN"] = 'true' exit_code = subprocess.call(args, env=new_environ) diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py index da375c0f32..486d62cd18 100644 --- a/tests/utils_tests/test_autoreload.py +++ b/tests/utils_tests/test_autoreload.py @@ -277,3 +277,12 @@ class RestartWithReloaderTests(SimpleTestCase): autoreload.restart_with_reloader() self.assertEqual(mock_call.call_count, 1) self.assertEqual(mock_call.call_args[0][0], [self.executable, '-Wall'] + argv) + + def test_python_m_django(self): + main = '/usr/lib/pythonX.Y/site-packages/django/__main__.py' + argv = [main, 'runserver'] + mock_call = self.patch_autoreload(argv) + with mock.patch('django.__main__.__file__', main): + autoreload.restart_with_reloader() + self.assertEqual(mock_call.call_count, 1) + self.assertEqual(mock_call.call_args[0][0], [self.executable, '-Wall', '-m', 'django'] + argv[1:])