Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Jan 2013 11:58:59 GMT
From:      Gunther Stengl <gunther.stengl@googlemail.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   ports/175178: www/py27-django: python manage.py syncdb fails if no locale is set
Message-ID:  <201301101158.r0ABwxul055583@red.freebsd.org>
Resent-Message-ID: <201301101200.r0AC00j9007402@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         175178
>Category:       ports
>Synopsis:       www/py27-django: python manage.py syncdb fails if no locale is set
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-ports-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jan 10 12:00:00 UTC 2013
>Closed-Date:
>Last-Modified:
>Originator:     Gunther Stengl
>Release:        9.1-RELEASE i386
>Organization:
>Environment:
FreeBSD fbsd.local.lan 9.1-RELEASE FreeBSD 9.1-RELEASE #0 r243826: Tue Dec  4 06:55:39 UTC 2012     root@obrian.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
Django (py27-django-1.4.3) fails to create the db-superuser when doing a "python mange.py syncdb" AND "LC_ALL" is unset.

"LC_ALL=" causes the locale.getdefaultlocale() to return "(None, None)" which is not a String, as needed by decode().

If LC_ALL is set correctly, the scripts runs as expected.
>How-To-Repeat:
unset LC_ALL
python manage.py syncdb
--> Script bails out after "You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes"

See attached logfile at "Problem situation"
>Fix:
e.g. (bash): export LC_ALL=de_DE.UTF-8


Corrections for django, manage.py:
Check if locale.getdefaultlocale() returns something!=None.

Fallback to just using "getpass.getuser()" or issuing a warning ("Set your locale correctly") and terminating gracefully.

See attached logfile at "Testing and solution"

Patch attached with submission follows:

Problem situation
=================

Script started on Thu Jan 10 12:21:15 2013

[gunhed@fbsd ~/bin/test]$ django-admin.py startproject testproj
[gunhed@fbsd ~/bin/test]$ cd testproj
[gunhed@fbsd ~/bin/test/testproj]$ ls -l
total 8
-rwxr-xr-x  1 gunhed  gunhed  251 Jan 10 12:21 manage.py
drwxr-xr-x  2 gunhed  gunhed  512 Jan 10 12:21 testproj
[gunhed@fbsd ~/bin/test/testproj]$ cd testproj/
[gunhed@fbsd ~/bin/test/testproj/testproj]$ cp settings.py settings.py.orig
[gunhed@fbsd ~/bin/test/testproj/testproj]$ vi settings.py
[gunhed@fbsd ~/bin/test/testproj/testproj]$ diff settings.py.orig settings.py
14,15c14,15
<         'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
<         'NAME': '',                      # Or path to database file if using sqlite3.
---
>         'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
>         'NAME': 'test.sqlite3',                      # Or path to database file if using sqlite3.
[gunhed@fbsd ~/bin/test/testproj/testproj]$ cd ..
[gunhed@fbsd ~/bin/test/testproj]$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 110, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/sql.py", line 189, in emit_post_sync_signal
    interactive=interactive, db=db)
  File "/usr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 73, in create_superuser
    call_command("createsuperuser", interactive=True, database=db)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 150, in call_command
    return klass.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 70, in handle
    default_username = get_default_username()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 105, in get_default_username
    default_username = get_system_username()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 85, in get_system_username
    return getpass.getuser().decode(locale.getdefaultlocale()[1])
TypeError: decode() argument 1 must be string, not None
[gunhed@fbsd ~/bin/test/testproj]$ ipython
Python 2.7.3 (default, Jan  8 2013, 14:31:16) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import os

In [2]: import getpass

In [3]: import locale

In [4]: getpass.getuser()
Out[4]: 'gunhed'

In [5]: getpass.getuser().decode(locale.getdefaultlocale()[1])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-654a16de3ed2> in <module>()
----> 1 getpass.getuser().decode(locale.getdefaultlocale()[1])

TypeError: decode() argument 1 must be string, not None

In [6]: locale.getdefaultlocale()
Out[6]: (None, None)

In [7]: 
Do you really want to exit ([y]/n)? 
[gunhed@fbsd ~/bin/test/testproj]$ exit

Script done on Thu Jan 10 12:27:10 2013







Testing and solution
====================


Proving assumption
------------------

Script started on Thu Jan 10 12:32:36 2013

[gunhed@fbsd ~]$ locale
LANG=
LC_CTYPE="C"
LC_COLLATE="C"
LC_TIME="C"
LC_NUMERIC="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_ALL=
[gunhed@fbsd ~]$ ipython
Python 2.7.3 (default, Jan  8 2013, 14:31:16) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.

%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import getpass 

In [2]: import locale

In [3]: getpass.getuser()
Out[3]: 'gunhed'

In [4]: locale.getdefaultlocale()
Out[4]: (None, None)

In [5]: # so, the following must fail ...

In [6]: getpass.getuser().decode(locale.getdefaultlocale()[1])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-654a16de3ed2> in <module>()
----> 1 getpass.getuser().decode(locale.getdefaultlocale()[1])

TypeError: decode() argument 1 must be string, not None

In [7]: exit



Solution proposal
-----------------

[gunhed@fbsd ~]$ export LC_ALL=de_DE.UTF-8
[gunhed@fbsd ~]$ locale
LANG=
LC_CTYPE="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_ALL=de_DE.UTF-8
[gunhed@fbsd ~]$ ipython
Python 2.7.3 (default, Jan  8 2013, 14:31:16) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import getpass

In [2]: import locale

In [3]: getpass.getuser()
Out[3]: 'gunhed'

In [4]: locale.getdefaultlocale()
Out[4]: ('de_DE', 'UTF-8')

In [5]: # now let's see what happens ...

In [6]: getpass.getuser().decode(locale.getdefaultlocale()[1])
Out[6]: u'gunhed'

In [7]: exit
[gunhed@fbsd ~]$ exit

Script done on Thu Jan 10 12:34:57 2013



>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201301101158.r0ABwxul055583>