Ticket #32669: 32669-autoreload-fix.patch.diff

File 32669-autoreload-fix.patch.diff, 2.3 KB (added by Moriyoshi Koizumi, 4 years ago)

Patch

  • django/utils/autoreload.py

    diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
    index 3847252632..47a8f0e5d1 100644
    a b def get_child_arguments():  
    222222    args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions]
    223223    # __spec__ is set when the server was started with the `-m` option,
    224224    # see https://docs.python.org/3/reference/import.html#main-spec
    225     if __main__.__spec__ is not None and __main__.__spec__.parent:
    226         args += ['-m', __main__.__spec__.parent]
    227         args += sys.argv[1:]
     225    if __main__.__spec__ is not None:
     226        modspec = __main__.__spec__
     227        if modspec.name.split(".")[-1] == "__main__" and modspec.parent:
     228            args += ['-m', modspec.parent]
     229            args += sys.argv[1:]
     230        else:
     231            args += ['-m', modspec.name]
     232            args += sys.argv[1:]
     233
    228234    elif not py_script.exists():
    229235        # sys.argv[0] may not exist for several reasons on Windows.
    230236        # It may exist with a .exe extension or have a -script.py suffix.
  • tests/utils_tests/test_autoreload.py

    diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py
    index 3cb901af7d..14a546e55b 100644
    a b from django.utils import autoreload  
    2424from django.utils.autoreload import WatchmanUnavailable
    2525
    2626from .test_module import __main__ as test_main
     27from .test_module import main_module as test_main_module
    2728from .utils import on_macos_with_hfs
    2829
    2930
    class TestChildArguments(SimpleTestCase):  
    182183            [sys.executable, '-m', 'utils_tests.test_module', 'runserver'],
    183184        )
    184185
     186    @mock.patch.dict(sys.modules, {'__main__': test_main_module})
     187    @mock.patch('sys.argv', [test_main.__file__, 'runserver'])
     188    @mock.patch('sys.warnoptions', [])
     189    def test_run_as_non_django_module(self):
     190        self.assertEqual(
     191            autoreload.get_child_arguments(),
     192            [sys.executable, '-m', 'utils_tests.test_module.main_module', 'runserver'],
     193        )
     194
    185195    @mock.patch('sys.argv', [__file__, 'runserver'])
    186196    @mock.patch('sys.warnoptions', ['error'])
    187197    def test_warnoptions(self):
Back to Top