[py3] Fixed #18805 -- ported createsuperuser.

Thanks sunsesh at gmail.com for the report.
This commit is contained in:
Aymeric Augustin 2012-08-20 22:24:48 +02:00
parent 610746f6cb
commit 54899d810d
2 changed files with 22 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import unicodedata
from django.contrib.auth import models as auth_app
from django.db.models import get_models, signals
from django.contrib.auth.models import User
from django.utils import six
from django.utils.six.moves import input
@ -84,17 +85,23 @@ def get_system_username():
:returns: The username as a unicode string, or an empty string if the
username could not be determined.
"""
default_locale = locale.getdefaultlocale()[1]
if default_locale:
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).
return ''
if not six.PY3:
default_locale = locale.getdefaultlocale()[1]
if not default_locale:
return ''
try:
return getpass.getuser().decode(default_locale)
except (ImportError, KeyError, UnicodeDecodeError):
# 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).
result = result.decode(default_locale)
except UnicodeDecodeError:
# UnicodeDecodeError - preventive treatment for non-latin Windows.
pass
return ''
return ''
return result
def get_default_username(check_db=True):

View File

@ -4,16 +4,20 @@ from django.contrib.auth import models, management
from django.contrib.auth.management.commands import changepassword
from django.core.management.base import CommandError
from django.test import TestCase
from django.utils import six
from django.utils.six import StringIO
class GetDefaultUsernameTestCase(TestCase):
def setUp(self):
self._getpass_getuser = management.get_system_username
self.old_get_system_username = management.get_system_username
def tearDown(self):
management.get_system_username = self._getpass_getuser
management.get_system_username = self.old_get_system_username
def test_actual_implementation(self):
self.assertIsInstance(management.get_system_username(), six.text_type)
def test_simple(self):
management.get_system_username = lambda: 'joe'