Ticket #16017: malformedlocale.diff
File malformedlocale.diff, 4.7 KB (added by , 13 years ago) |
---|
-
django/contrib/auth/management/__init__.py
diff -r 8a52d5651cc2 django/contrib/auth/management/__init__.py
a b 83 83 :returns: The username as a unicode string, or an empty string if the 84 84 username could not be determined. 85 85 """ 86 default_username = u'' 86 87 try: 87 return getpass.getuser().decode(locale.getdefaultlocale()[1]) 88 except (ImportError, KeyError, UnicodeDecodeError): 89 # KeyError will be raised by os.getpwuid() (called by getuser()) 90 # if there is no corresponding entry in the /etc/passwd file 91 # (a very restricted chroot environment, for example). 92 # UnicodeDecodeError - preventive treatment for non-latin Windows. 93 return u'' 88 default_locale = locale.getdefaultlocale()[1] 89 except ValueError: 90 # ValueError - unknown or broken locale like 'UTF-8' 91 return default_username 92 93 if default_locale: 94 try: 95 return getpass.getuser().decode(default_locale) 96 except (ImportError, KeyError, UnicodeDecodeError): 97 # KeyError will be raised by os.getpwuid() (called by getuser()) 98 # if there is no corresponding entry in the /etc/passwd file 99 # (a very restricted chroot environment, for example). 100 # UnicodeDecodeError - preventive treatment for non-latin Windows. 101 pass 102 return default_username 94 103 95 104 96 105 def get_default_username(check_db=True): -
django/contrib/auth/tests/basic.py
diff -r 8a52d5651cc2 django/contrib/auth/tests/basic.py
a b 1 import locale 2 import os 3 from StringIO import StringIO 4 1 5 from django.test import TestCase 2 6 from django.contrib.auth.models import User, AnonymousUser 3 7 from django.core.management import call_command 4 from StringIO import StringIO8 from django.contrib.auth.management.commands import createsuperuser 5 9 6 10 class BasicTestCase(TestCase): 7 11 def test_user(self): … … 93 97 self.assertEqual(u.email, 'joe@somewhere.org') 94 98 self.assertFalse(u.has_usable_password()) 95 99 100 def test_createsuperuser_nolocale(self): 101 """ Check that createsuperuser does not break when no locale is set. 102 103 See ticket #16017. 104 TODO: check if this needs to be skipped on non *NIX platforms 105 """ 106 107 orgenv = {} 108 for envvar in (key for key in os.environ.keys() if 109 key.startswith('LC_') or key in ('LANG', 'LANGUAGE')): 110 orgenv[envvar] = os.environ[envvar] 111 os.environ[envvar] = 'C' 112 113 new_io = StringIO() 114 class mock_getpass: pass 115 mock_getpass.getpass = staticmethod(lambda 116 p=None: "nopasswd") 117 createsuperuser.getpass = mock_getpass 118 try: 119 # Call the command in this new environment 120 call_command("createsuperuser", 121 interactive=True, 122 username="nolocale", 123 email="nolocale@example.com", 124 stdout=new_io 125 ) 126 except: 127 self.fail("createsuperuser failed when without locale information") 128 finally: 129 for envvar, val in orgenv.items(): 130 os.environ[envvar] = val 131 132 # If we were successful, a user would have been created 133 u = User.objects.get(username="nolocale") 134 self.assertEqual(u.email, 'nolocale@example.com') 135 136 def test_createsuperuser_unknownlocale(self): 137 """ Check that createsuperuser does not break when an unknown locale 138 is set. Seen on a Mac at the DjangoCon EU sprint 2011. 139 140 See ticket #16017. 141 """ 142 143 orgenv = {} 144 for envvar in (key for key in os.environ.keys() if 145 key.startswith('LC_') or key in ('LANG', 'LANGUAGE')): 146 orgenv[envvar] = os.environ[envvar] 147 os.environ[envvar] = 'UTF-8' 148 149 new_io = StringIO() 150 try: 151 # Call the command in this new environment 152 call_command("createsuperuser", 153 interactive=True, 154 username="weirdlocale", 155 email="weirdlocale@example.com", 156 stdout=new_io 157 ) 158 except: 159 self.fail("createsuperuser failed when with unkown locale information") 160 finally: 161 for envvar, val in orgenv.items(): 162 os.environ[envvar] = val 163 164 # If we were successful, a user would have been created 165 u = User.objects.get(username="weirdlocale") 166 self.assertEqual(u.email, 'weirdlocale@example.com')