From 02e5909f7ae436dab8e4d13370670c467163b8aa Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 15 Feb 2013 15:44:27 +0100 Subject: [PATCH] Fixed #19807 -- Sanitized getpass input in createsuperuser Python 2 getpass on Windows doesn't accept unicode, even when containing only ascii chars. Thanks Semmel for the report and tests. --- .../contrib/auth/management/commands/createsuperuser.py | 2 +- django/contrib/auth/tests/basic.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index f462d95749..ac2835d6b3 100644 --- a/django/contrib/auth/management/commands/createsuperuser.py +++ b/django/contrib/auth/management/commands/createsuperuser.py @@ -122,7 +122,7 @@ class Command(BaseCommand): while password is None: if not password: password = getpass.getpass() - password2 = getpass.getpass('Password (again): ') + password2 = getpass.getpass(force_str('Password (again): ')) if password != password2: self.stderr.write("Error: Your passwords didn't match.") password = None diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py index 03af1fd7bb..3c7bcffc7d 100644 --- a/django/contrib/auth/tests/basic.py +++ b/django/contrib/auth/tests/basic.py @@ -13,7 +13,7 @@ from django.core.management import call_command from django.test import TestCase from django.test.utils import override_settings from django.utils.encoding import force_str -from django.utils.six import StringIO +from django.utils.six import binary_type, StringIO def mock_inputs(inputs): @@ -24,8 +24,11 @@ def mock_inputs(inputs): def inner(test_func): def wrapped(*args): class mock_getpass: - pass - mock_getpass.getpass = staticmethod(lambda p=None: inputs['password']) + @staticmethod + def getpass(prompt=b'Password: ', stream=None): + # getpass on Windows only supports prompt as bytestring (#19807) + assert isinstance(prompt, binary_type) + return inputs['password'] def mock_input(prompt): # prompt should be encoded in Python 2. This line will raise an