summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsarahboyce <sarahvboyce95@gmail.com>2023-08-26 16:23:19 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-08-28 12:57:14 +0200
commitf6ed2c36ddf35bcc32c912123a4eb6fe0a6bfc6a (patch)
tree3fc99a6503da6d0c091b6040cf35796591812320
parent24f1a38b37c0af3a5ce0dd7b5392fe4e75d7e1dc (diff)
Fixed #34787 -- Fixed autoreloader crash when run from installed script on Windows.
-rw-r--r--django/utils/autoreload.py4
-rw-r--r--tests/utils_tests/test_autoreload.py11
2 files changed, 13 insertions, 2 deletions
diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
index 5b22aef2b1..e570f89300 100644
--- a/django/utils/autoreload.py
+++ b/django/utils/autoreload.py
@@ -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:]]
diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py
index e33276ba61..fd33506499 100644
--- a/tests/utils_tests/test_autoreload.py
+++ b/tests/utils_tests/test_autoreload.py
@@ -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", {})