diff -r 8a52d5651cc2 django/contrib/auth/management/__init__.py
--- a/django/contrib/auth/management/__init__.py	Thu Jun 09 20:01:28 2011 +0000
+++ b/django/contrib/auth/management/__init__.py	Fri Jun 10 04:59:27 2011 +0200
@@ -83,14 +83,23 @@
     :returns: The username as a unicode string, or an empty string if the
         username could not be determined.
     """
+    default_username = u''
     try:
-        return getpass.getuser().decode(locale.getdefaultlocale()[1])
-    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).
-        # UnicodeDecodeError - preventive treatment for non-latin Windows.
-        return u''
+        default_locale = locale.getdefaultlocale()[1]
+    except ValueError:
+        # ValueError - unknown or broken locale like 'UTF-8'
+        return default_username
+
+    if default_locale:
+        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).
+            # UnicodeDecodeError - preventive treatment for non-latin Windows.
+            pass
+    return default_username
 
 
 def get_default_username(check_db=True):
diff -r 8a52d5651cc2 django/contrib/auth/tests/basic.py
--- a/django/contrib/auth/tests/basic.py	Thu Jun 09 20:01:28 2011 +0000
+++ b/django/contrib/auth/tests/basic.py	Fri Jun 10 04:59:27 2011 +0200
@@ -1,7 +1,11 @@
+import locale
+import os
+from StringIO import StringIO
+
 from django.test import TestCase
 from django.contrib.auth.models import User, AnonymousUser
 from django.core.management import call_command
-from StringIO import StringIO
+from django.contrib.auth.management.commands import createsuperuser
 
 class BasicTestCase(TestCase):
     def test_user(self):
@@ -93,3 +97,70 @@
         self.assertEqual(u.email, 'joe@somewhere.org')
         self.assertFalse(u.has_usable_password())
 
+    def test_createsuperuser_nolocale(self):
+        """ Check that createsuperuser does not break when no locale is set.
+
+        See ticket #16017.
+        TODO: check if this needs to be skipped on non *NIX platforms
+        """
+
+        orgenv = {}
+        for envvar in (key for key in os.environ.keys() if
+                key.startswith('LC_') or key in ('LANG', 'LANGUAGE')):
+            orgenv[envvar] = os.environ[envvar]
+            os.environ[envvar] = 'C'
+
+        new_io = StringIO()
+        class mock_getpass: pass
+        mock_getpass.getpass = staticmethod(lambda
+                    p=None: "nopasswd")
+        createsuperuser.getpass = mock_getpass
+        try:
+            # Call the command in this new environment
+            call_command("createsuperuser",
+                interactive=True,
+                username="nolocale",
+                email="nolocale@example.com",
+                stdout=new_io
+            )
+        except:
+            self.fail("createsuperuser failed when without locale information")
+        finally:
+            for envvar, val in orgenv.items():
+                os.environ[envvar] = val
+
+        # If we were successful, a user would have been created
+        u = User.objects.get(username="nolocale")
+        self.assertEqual(u.email, 'nolocale@example.com')
+
+    def test_createsuperuser_unknownlocale(self):
+        """ Check that createsuperuser does not break when an unknown locale
+        is set. Seen on a Mac at the DjangoCon EU sprint 2011.
+
+        See ticket #16017.
+        """
+
+        orgenv = {}
+        for envvar in (key for key in os.environ.keys() if
+                key.startswith('LC_') or key in ('LANG', 'LANGUAGE')):
+            orgenv[envvar] = os.environ[envvar]
+            os.environ[envvar] = 'UTF-8'
+
+        new_io = StringIO()
+        try:
+            # Call the command in this new environment
+            call_command("createsuperuser",
+                interactive=True,
+                username="weirdlocale",
+                email="weirdlocale@example.com",
+                stdout=new_io
+            )
+        except:
+            self.fail("createsuperuser failed when with unkown locale information")
+        finally:
+            for envvar, val in orgenv.items():
+                os.environ[envvar] = val
+
+        # If we were successful, a user would have been created
+        u = User.objects.get(username="weirdlocale")
+        self.assertEqual(u.email, 'weirdlocale@example.com')
