1
0
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:
Tommy Allen 2024-11-26 15:15:00 -05:00 committed by GitHub
parent 2e190a48d6
commit c635decb00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 5 deletions

View File

@ -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

View File

@ -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`).

View File

@ -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")