mirror of
https://github.com/django/django.git
synced 2024-12-22 09:05:43 +00:00
Fixed #35942 -- Fixed createsuperuser crash on Python 3.13+ when username is unavailable.
Thanks Mariusz Felisiak and Jacob Tyler Walls for reviews.
This commit is contained in:
parent
2e190a48d6
commit
c635decb00
@ -115,10 +115,12 @@ def get_system_username():
|
||||
"""
|
||||
try:
|
||||
result = getpass.getuser()
|
||||
except (ImportError, KeyError):
|
||||
# KeyError will be raised by os.getpwuid() (called by getuser())
|
||||
# if there is no corresponding entry in the /etc/passwd file
|
||||
# (a very restricted chroot environment, for example).
|
||||
except (ImportError, KeyError, OSError):
|
||||
# TODO: Drop ImportError and KeyError when dropping support for PY312.
|
||||
# KeyError (Python <3.13) or OSError (Python 3.13+) will be raised by
|
||||
# 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 result
|
||||
|
||||
|
@ -9,4 +9,5 @@ Django 5.1.4 fixes several bugs in 5.1.3.
|
||||
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):
|
||||
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):
|
||||
management.get_system_username = lambda: "joe"
|
||||
self.assertEqual(management.get_default_username(), "joe")
|
||||
|
Loading…
Reference in New Issue
Block a user