Fixed #32669 -- Fixed detection when started non-django modules which aren't packages with "python -m" in autoreloader.

This commit is contained in:
Moriyoshi Koizumi 2021-04-25 11:52:18 +09:00 committed by Mariusz Felisiak
parent 5a8e8f80bb
commit 9e4780deda
3 changed files with 17 additions and 3 deletions

View File

@ -223,8 +223,13 @@ def get_child_arguments():
# __spec__ is set when the server was started with the `-m` option, # __spec__ is set when the server was started with the `-m` option,
# see https://docs.python.org/3/reference/import.html#main-spec # see https://docs.python.org/3/reference/import.html#main-spec
# __spec__ may not exist, e.g. when running in a Conda env. # __spec__ may not exist, e.g. when running in a Conda env.
if getattr(__main__, '__spec__', None) is not None and __main__.__spec__.parent: if getattr(__main__, '__spec__', None) is not None:
args += ['-m', __main__.__spec__.parent] spec = __main__.__spec__
if (spec.name == '__main__' or spec.name.endswith('.__main__')) and spec.parent:
name = spec.parent
else:
name = spec.name
args += ['-m', name]
args += sys.argv[1:] args += sys.argv[1:]
elif not py_script.exists(): elif not py_script.exists():
# sys.argv[0] may not exist for several reasons on Windows. # sys.argv[0] may not exist for several reasons on Windows.

View File

@ -23,7 +23,7 @@ from django.test.utils import extend_sys_path
from django.utils import autoreload from django.utils import autoreload
from django.utils.autoreload import WatchmanUnavailable from django.utils.autoreload import WatchmanUnavailable
from .test_module import __main__ as test_main from .test_module import __main__ as test_main, main_module as test_main_module
from .utils import on_macos_with_hfs from .utils import on_macos_with_hfs
@ -182,6 +182,15 @@ class TestChildArguments(SimpleTestCase):
[sys.executable, '-m', 'utils_tests.test_module', 'runserver'], [sys.executable, '-m', 'utils_tests.test_module', 'runserver'],
) )
@mock.patch.dict(sys.modules, {'__main__': test_main_module})
@mock.patch('sys.argv', [test_main.__file__, 'runserver'])
@mock.patch('sys.warnoptions', [])
def test_run_as_non_django_module_non_package(self):
self.assertEqual(
autoreload.get_child_arguments(),
[sys.executable, '-m', 'utils_tests.test_module.main_module', 'runserver'],
)
@mock.patch('sys.argv', [__file__, 'runserver']) @mock.patch('sys.argv', [__file__, 'runserver'])
@mock.patch('sys.warnoptions', ['error']) @mock.patch('sys.warnoptions', ['error'])
def test_warnoptions(self): def test_warnoptions(self):