Code

Ticket #16017: malformedlocale.diff

File malformedlocale.diff, 4.7 KB (added by charstring, 3 years ago)
Line 
1diff -r 8a52d5651cc2 django/contrib/auth/management/__init__.py
2--- a/django/contrib/auth/management/__init__.py        Thu Jun 09 20:01:28 2011 +0000
3+++ b/django/contrib/auth/management/__init__.py        Fri Jun 10 04:59:27 2011 +0200
4@@ -83,14 +83,23 @@
5     :returns: The username as a unicode string, or an empty string if the
6         username could not be determined.
7     """
8+    default_username = u''
9     try:
10-        return getpass.getuser().decode(locale.getdefaultlocale()[1])
11-    except (ImportError, KeyError, UnicodeDecodeError):
12-        # KeyError will be raised by os.getpwuid() (called by getuser())
13-        # if there is no corresponding entry in the /etc/passwd file
14-        # (a very restricted chroot environment, for example).
15-        # UnicodeDecodeError - preventive treatment for non-latin Windows.
16-        return u''
17+        default_locale = locale.getdefaultlocale()[1]
18+    except ValueError:
19+        # ValueError - unknown or broken locale like 'UTF-8'
20+        return default_username
21+
22+    if default_locale:
23+        try:
24+            return getpass.getuser().decode(default_locale)
25+        except (ImportError, KeyError, UnicodeDecodeError):
26+            # KeyError will be raised by os.getpwuid() (called by getuser())
27+            # if there is no corresponding entry in the /etc/passwd file
28+            # (a very restricted chroot environment, for example).
29+            # UnicodeDecodeError - preventive treatment for non-latin Windows.
30+            pass
31+    return default_username
32 
33 
34 def get_default_username(check_db=True):
35diff -r 8a52d5651cc2 django/contrib/auth/tests/basic.py
36--- a/django/contrib/auth/tests/basic.py        Thu Jun 09 20:01:28 2011 +0000
37+++ b/django/contrib/auth/tests/basic.py        Fri Jun 10 04:59:27 2011 +0200
38@@ -1,7 +1,11 @@
39+import locale
40+import os
41+from StringIO import StringIO
42+
43 from django.test import TestCase
44 from django.contrib.auth.models import User, AnonymousUser
45 from django.core.management import call_command
46-from StringIO import StringIO
47+from django.contrib.auth.management.commands import createsuperuser
48 
49 class BasicTestCase(TestCase):
50     def test_user(self):
51@@ -93,3 +97,70 @@
52         self.assertEqual(u.email, 'joe@somewhere.org')
53         self.assertFalse(u.has_usable_password())
54 
55+    def test_createsuperuser_nolocale(self):
56+        """ Check that createsuperuser does not break when no locale is set.
57+
58+        See ticket #16017.
59+        TODO: check if this needs to be skipped on non *NIX platforms
60+        """
61+
62+        orgenv = {}
63+        for envvar in (key for key in os.environ.keys() if
64+                key.startswith('LC_') or key in ('LANG', 'LANGUAGE')):
65+            orgenv[envvar] = os.environ[envvar]
66+            os.environ[envvar] = 'C'
67+
68+        new_io = StringIO()
69+        class mock_getpass: pass
70+        mock_getpass.getpass = staticmethod(lambda
71+                    p=None: "nopasswd")
72+        createsuperuser.getpass = mock_getpass
73+        try:
74+            # Call the command in this new environment
75+            call_command("createsuperuser",
76+                interactive=True,
77+                username="nolocale",
78+                email="nolocale@example.com",
79+                stdout=new_io
80+            )
81+        except:
82+            self.fail("createsuperuser failed when without locale information")
83+        finally:
84+            for envvar, val in orgenv.items():
85+                os.environ[envvar] = val
86+
87+        # If we were successful, a user would have been created
88+        u = User.objects.get(username="nolocale")
89+        self.assertEqual(u.email, 'nolocale@example.com')
90+
91+    def test_createsuperuser_unknownlocale(self):
92+        """ Check that createsuperuser does not break when an unknown locale
93+        is set. Seen on a Mac at the DjangoCon EU sprint 2011.
94+
95+        See ticket #16017.
96+        """
97+
98+        orgenv = {}
99+        for envvar in (key for key in os.environ.keys() if
100+                key.startswith('LC_') or key in ('LANG', 'LANGUAGE')):
101+            orgenv[envvar] = os.environ[envvar]
102+            os.environ[envvar] = 'UTF-8'
103+
104+        new_io = StringIO()
105+        try:
106+            # Call the command in this new environment
107+            call_command("createsuperuser",
108+                interactive=True,
109+                username="weirdlocale",
110+                email="weirdlocale@example.com",
111+                stdout=new_io
112+            )
113+        except:
114+            self.fail("createsuperuser failed when with unkown locale information")
115+        finally:
116+            for envvar, val in orgenv.items():
117+                os.environ[envvar] = val
118+
119+        # If we were successful, a user would have been created
120+        u = User.objects.get(username="weirdlocale")
121+        self.assertEqual(u.email, 'weirdlocale@example.com')