diff options
| author | Tommy Allen <tommy@esdf.io> | 2024-11-26 15:15:00 -0500 |
|---|---|---|
| committer | Natalia <124304+nessita@users.noreply.github.com> | 2024-11-26 17:16:30 -0300 |
| commit | 4b262408aa6dadea9ca80c4e1ccc9a046fb07583 (patch) | |
| tree | 5e447b0c71dcd1610dd3eea5edf64a204a6ca7b8 | |
| parent | 08ac8c1b4416d1fba39f86dd328017ca74abce67 (diff) | |
[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.
| -rw-r--r-- | django/contrib/auth/management/__init__.py | 10 | ||||
| -rw-r--r-- | docs/releases/5.1.4.txt | 3 | ||||
| -rw-r--r-- | tests/auth_tests/test_management.py | 7 |
3 files changed, 15 insertions, 5 deletions
diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py index c40f2aa69d..a8639cb258 100644 --- a/django/contrib/auth/management/__init__.py +++ b/django/contrib/auth/management/__init__.py @@ -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 diff --git a/docs/releases/5.1.4.txt b/docs/releases/5.1.4.txt index bee40f243e..468bd46381 100644 --- a/docs/releases/5.1.4.txt +++ b/docs/releases/5.1.4.txt @@ -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`). diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index 5765c50034..0ef85a7299 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -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") |
