Opened 19 months ago

Closed 18 months ago

Last modified 18 months ago

#21358 closed Bug (fixed)

runserver command fails to run with a non-english locale

Reported by: svartalf Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

runserver management command writes current datetime to stdout while starting. With a non-english locale it fails for python2.7.3:

svartalf@wonderland:~/projects/irk [django-1.6-upgrade]$ .env/bin/python manage.py runserver
Validating models...

0 errors found
Unhandled exception in thread started by <function wrapper at 0x1ec10c8>
Traceback (most recent call last):
  File "/home/svartalf/projects/irk/.env/lib/python2.7/site-packages/django/utils/autoreload.py", line 93, in wrapper
    fn(*args, **kwargs)
  File "/home/svartalf/projects/irk/.env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
    "quit_command": quit_command,
  File "/home/svartalf/projects/irk/.env/lib/python2.7/site-packages/django/core/management/base.py", line 65, in write
    if ending and not msg.endswith(ending):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

I've found that msg variable contains that text with type <type 'str'>:

Октябрь 31, 2013 - 14:30:02
Django version 1.6c1, using settings 'settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C

And it comes from that line of code:
https://github.com/django/django/blob/2ca00faa913754cd5860f6e1f23c8da2529c691a/django/core/management/commands/runserver.py#L105

I wrapped it with a django.utils.text.smart_text and works. Here is a link to my fork commit: https://github.com/svartalf/django/commit/76c058de1bab989c296403e8199aa5fe7f0f83f9

I can pull request it, or maybe, datetime format can be changed to something locale-independent.

Attachments (1)

21358.diff (1.5 KB) - added by claudep 19 months ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 19 months ago by claudep

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Could you test by simply adding that import at the top of the file:

from __future__ import unicode_literals

comment:2 Changed 19 months ago by svartalf

Nope, I still get that error:

svartalf@wonderland:~/projects/irk [django-1.6-upgrade]$ .env/bin/python manage.py runserver
Validating models...

0 errors found
Unhandled exception in thread started by <function wrapper at 0x2cf6140>
Traceback (most recent call last):
  File "/home/svartalf/projects/irk/.env/lib/python2.7/site-packages/django/utils/autoreload.py", line 93, in wrapper
    fn(*args, **kwargs)
  File "/home/svartalf/projects/irk/.env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
    "quit_command": quit_command,
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

Changed 19 months ago by claudep

comment:3 Changed 19 months ago by claudep

Ah yes, strftime is always returning a bytestring on Python 2, even when the formatting string is unicode. So my patch above workarounds that.
Of course, your patch is shorter and counts on the fact that on Python 2, if there are any unicode parameters in a text substitution, the resulting string is always unicode. But in any case, your patch should use force_text, smart_text is only useful when we may have a lazy string as parameter.

comment:4 Changed 18 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 2080bce695d480dd5fd851fdada221df923aa290:

Fixed #21358 -- Allowed runserver on non-English locales

Thanks svartalf for the report.

comment:5 Changed 18 months ago by Claude Paroz <claude@…>

In c8b4ac814ce7d71d352046c03115abba6d9489f5:

[1.6.x] Fixed #21358 -- Allowed runserver on non-English locales

Thanks svartalf for the report.
Backport of 2080bce69 from master.

Note: See TracTickets for help on using tickets.
Back to Top