Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
*js text eol=lf
tests/staticfiles_tests/apps/test/static/test/*txt text eol=lf
tests/staticfiles_tests/project/documents/test/*txt text eol=lf
docs/releases/*.txt merge=union
# Make GitHub syntax-highlight .html files as Django templates
*.html linguist-language=django
13 changes: 13 additions & 0 deletions django/utils/autoreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ def trigger_reload(filename):

def restart_with_reloader():
new_environ = {**os.environ, DJANGO_AUTORELOAD_ENV: "true"}
orig = getattr(sys, "orig_argv", ())
if any(
(arg == "-u")
or (
arg.startswith("-")
and not arg.startswith(("--", "-X", "-W"))
and len(arg) > 2
and arg[1:].isalpha()
and "u" in arg
)
for arg in orig[1:]
):
new_environ.setdefault("PYTHONUNBUFFERED", "1")
args = get_child_arguments()
while True:
p = subprocess.run(args, env=new_environ, close_fds=False)
Expand Down
47 changes: 47 additions & 0 deletions tests/utils_tests/test_autoreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,53 @@ def test_python_m_django(self):
[self.executable, "-Wall", "-m", "django"] + argv[1:],
)

def test_propagates_unbuffered_from_parent(self):
for args in ("-u", "-Iuv"):
with self.subTest(args=args):
with mock.patch.dict(os.environ, {}, clear=True):
with tempfile.TemporaryDirectory() as d:
script = Path(d) / "manage.py"
script.touch()
mock_call = self.patch_autoreload([str(script), "runserver"])
with (
mock.patch("__main__.__spec__", None),
mock.patch.object(
autoreload.sys,
"orig_argv",
[self.executable, args, str(script), "runserver"],
),
):
autoreload.restart_with_reloader()
env = mock_call.call_args.kwargs["env"]
self.assertEqual(env.get("PYTHONUNBUFFERED"), "1")

def test_does_not_propagate_unbuffered_from_parent(self):
for args in (
"-Xdev",
"-Xfaulthandler",
"--user",
"-Wall",
"-Wdefault",
"-Wignore::UserWarning",
):
with self.subTest(args=args):
with mock.patch.dict(os.environ, {}, clear=True):
with tempfile.TemporaryDirectory() as d:
script = Path(d) / "manage.py"
script.touch()
mock_call = self.patch_autoreload([str(script), "runserver"])
with (
mock.patch("__main__.__spec__", None),
mock.patch.object(
autoreload.sys,
"orig_argv",
[self.executable, args, str(script), "runserver"],
),
):
autoreload.restart_with_reloader()
env = mock_call.call_args.kwargs["env"]
self.assertIsNone(env.get("PYTHONUNBUFFERED"))


class ReloaderTests(SimpleTestCase):
RELOADER_CLS = None
Expand Down
Loading