mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
[5.1.x] Fixed #35942 -- Fixed createsuperuser crash on Python 3.13+ when username is unavailable.
Thanks Mariusz Felisiak and Jacob Tyler Walls for reviews. Backport of c635decb00ac957daf81c08541cdc9cf46f6d86d from main.
This commit is contained in:
parent
08ac8c1b44
commit
4b262408aa
@ -115,10 +115,12 @@ def get_system_username():
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
result = getpass.getuser()
|
result = getpass.getuser()
|
||||||
except (ImportError, KeyError):
|
except (ImportError, KeyError, OSError):
|
||||||
# KeyError will be raised by os.getpwuid() (called by getuser())
|
# TODO: Drop ImportError and KeyError when dropping support for PY312.
|
||||||
# if there is no corresponding entry in the /etc/passwd file
|
# KeyError (Python <3.13) or OSError (Python 3.13+) will be raised by
|
||||||
# (a very restricted chroot environment, for example).
|
# os.getpwuid() (called by getuser()) if there is no corresponding
|
||||||
|
# entry in the /etc/passwd file (for example, in a very restricted
|
||||||
|
# chroot environment).
|
||||||
return ""
|
return ""
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -9,4 +9,5 @@ Django 5.1.4 fixes several bugs in 5.1.3.
|
|||||||
Bugfixes
|
Bugfixes
|
||||||
========
|
========
|
||||||
|
|
||||||
* ...
|
* Fixed a crash in ``createsuperuser`` on Python 3.13+ caused by an unhandled
|
||||||
|
``OSError`` when the username could not be determined (:ticket:`35942`).
|
||||||
|
@ -126,6 +126,13 @@ class GetDefaultUsernameTestCase(TestCase):
|
|||||||
def test_actual_implementation(self):
|
def test_actual_implementation(self):
|
||||||
self.assertIsInstance(management.get_system_username(), str)
|
self.assertIsInstance(management.get_system_username(), str)
|
||||||
|
|
||||||
|
def test_getuser_raises_exception(self):
|
||||||
|
# TODO: Drop ImportError and KeyError when dropping support for PY312.
|
||||||
|
for exc in (ImportError, KeyError, OSError):
|
||||||
|
with self.subTest(exc=str(exc)):
|
||||||
|
with mock.patch("getpass.getuser", side_effect=exc):
|
||||||
|
self.assertEqual(management.get_system_username(), "")
|
||||||
|
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
management.get_system_username = lambda: "joe"
|
management.get_system_username = lambda: "joe"
|
||||||
self.assertEqual(management.get_default_username(), "joe")
|
self.assertEqual(management.get_default_username(), "joe")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user