Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#24741 closed Bug (invalid)

UnicodeDecodeError in gettext via migrations autodetector

Reported by: Nicola Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If there are no migrations to apply I get this error:

Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/commands/migrate.py", line 207, in handle
    changes = autodetector.changes(graph=executor.loader.graph)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/autodetector.py", line 186, in _detect_changes
    self.generate_altered_fields()
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/autodetector.py", line 850, in generate_altered_fields
    if old_field_dec != new_field_dec:
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/functional.py", line 165, in __eq__
    return self.__cast() == other
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/functional.py", line 153, in __cast
    return self.__text_cast()
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/functional.py", line 141, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/translation/__init__.py", line 84, in ugettext
    return _trans.ugettext(message)
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/translation/trans_real.py", line 327, in ugettext
    return do_translate(message, 'ugettext')
  File "/home/nicola/django18/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/utils/translation/trans_real.py", line 307, in do_translate
    result = getattr(translation_object, translation_function)(eol_message)
  File "/usr/lib64/python2.7/gettext.py", line 220, in ugettext
    return unicode(message)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 3: ordinal not in range(128)

Change History (5)

comment:1 Changed 7 years ago by Tim Graham

Resolution: needsinfo
Status: newclosed

It's unclear if this is a bug in your project or in Django. Could you please a sample project that reproduces the error and reopen the ticket? Thanks.

comment:2 Changed 7 years ago by Tim Graham

Summary: migration errorUnicodeDecodeError in gettext via migrations autodetector

comment:3 Changed 7 years ago by Nicola

I think you can reproduce with a models like this

# -*- coding: utf-8 -*-
from django.db import models
#from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext as _

IMAGE_ROTATION = (
                        (0,_("None")),
                        (90,_("90 °")),
                        (180,_("180 °")),
                        (270,_("270 °")),
        )

class TestModel(models.Model):
        rotations = models.SmallIntegerField(choices=IMAGE_ROTATION,default=0)

makemigration fails, in my project migrations was done with 1.7 version

comment:4 Changed 7 years ago by Nicola

and is solved using something like this:

IMAGE_ROTATION = (
                        (0,_("None")),
                        (90,_(u"90 °")),
                        (180,_(u"180 °")),
                        (270,_(u"270 °")),
        )

comment:5 Changed 7 years ago by Simon Charette

Resolution: needsinfoinvalid

Hi drakkan,

As the name of the ugettext and ugettext_lazy method indicate they expect a unicode parameter to be provided.

When you call ugettext() with a utf-8 encoded byte string the method crash as expected and works correctly when you pass unicode strings (u prefixed strings).

I would suggest you make sure to define your translation strings as unicode by either prefixing them with the u prefix or using from __future__ import unicode_literals. If you must retrieve a translated string from a byte strings use the gettext and gettext_lazy method instead. Just keep in mind that Django expects unicode to be provided internally when dealing with localization.

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