Fixed #34787 -- Fixed autoreloader crash when run from installed script on Windows.

This commit is contained in:
sarahboyce 2023-08-26 16:23:19 +02:00 committed by Mariusz Felisiak
parent 24f1a38b37
commit f6ed2c36dd
2 changed files with 13 additions and 2 deletions

View File

@ -227,6 +227,7 @@ def get_child_arguments():
import __main__
py_script = Path(sys.argv[0])
exe_entrypoint = py_script.with_suffix(".exe")
args = [sys.executable] + ["-W%s" % o for o in sys.warnoptions]
if sys.implementation.name == "cpython":
@ -237,7 +238,7 @@ def get_child_arguments():
# __spec__ is set when the server was started with the `-m` option,
# see https://docs.python.org/3/reference/import.html#main-spec
# __spec__ may not exist, e.g. when running in a Conda env.
if getattr(__main__, "__spec__", None) is not None:
if getattr(__main__, "__spec__", None) is not None and not exe_entrypoint.exists():
spec = __main__.__spec__
if (spec.name == "__main__" or spec.name.endswith(".__main__")) and spec.parent:
name = spec.parent
@ -248,7 +249,6 @@ def get_child_arguments():
elif not py_script.exists():
# sys.argv[0] may not exist for several reasons on Windows.
# It may exist with a .exe extension or have a -script.py suffix.
exe_entrypoint = py_script.with_suffix(".exe")
if exe_entrypoint.exists():
# Should be executed directly, ignoring sys.executable.
return [exe_entrypoint, *sys.argv[1:]]

View File

@ -238,6 +238,17 @@ class TestChildArguments(SimpleTestCase):
autoreload.get_child_arguments(), [exe_path, "runserver"]
)
@mock.patch("sys.warnoptions", [])
@mock.patch.dict(sys.modules, {"__main__": django.__main__})
def test_use_exe_when_main_spec(self):
with tempfile.TemporaryDirectory() as tmpdir:
exe_path = Path(tmpdir) / "django-admin.exe"
exe_path.touch()
with mock.patch("sys.argv", [exe_path.with_suffix(""), "runserver"]):
self.assertEqual(
autoreload.get_child_arguments(), [exe_path, "runserver"]
)
@mock.patch("__main__.__spec__", None)
@mock.patch("sys.warnoptions", [])
@mock.patch("sys._xoptions", {})