Code

Opened 18 months ago

Closed 17 months ago

Last modified 17 months ago

#19272 closed Bug (fixed)

gettext_lazy returns "unexpected type"

Reported by: tyrion Owned by: nobody
Component: Translations Version: master
Severity: Release blocker Keywords:
Cc: regression Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

>>> from django.utils.translation import gettext_lazy
>>> gettext_lazy("test").upper()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/tyrion/.virtualenvs/djangobug/src/django/django/utils/functional.py", line 121, in __wrapper__
    raise TypeError("Lazy object returned unexpected type.")
TypeError: Lazy object returned unexpected type.
>>>

This is because gettext returns an unicode string, but gettext_lazy is defined as:

gettext_lazy = lazy(gettext, str)
>>> from django.utils.translation import gettext
>>> from django.utils.functional import lazy
>>> gettext("test")
u'test'
>>> gettext_lazy = lazy(gettext, unicode)
>>> gettext_lazy("test")
<django.utils.functional.__proxy__ object at 0x975cfec>
>>> _.upper()
u'TEST'

Attachments (1)

19272-1.diff (1.3 KB) - added by claudep 17 months ago.
Preserve bytestrings in gettext[_lazy] calls

Download all attachments as: .zip

Change History (7)

comment:1 Changed 17 months ago by claudep

  • Cc regression added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Severity changed from Normal to Release blocker
  • Triage Stage changed from Unreviewed to Accepted

I'm not a fan of non-unicode gettext calls, so my suggestion would be to replace it by ugettext_lazy. However this is a regression and gettext_lazy should not transform the message to unicode on Python 2.

Changed 17 months ago by claudep

Preserve bytestrings in gettext[_lazy] calls

comment:2 Changed 17 months ago by claudep

  • Has patch set

comment:3 Changed 17 months ago by aaugustin

  • Triage Stage changed from Accepted to Ready for checkin

The patch looks good to me. (I didn't apply it or run the tests.)

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

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

In 550ddc66b496473c8ee282c7ab6be5885a359d75:

Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.

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

In ebafba50a418d4a444a067a3d35cea7f98a20158:

[1.5.x] Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.
Backport of 550ddc66b from master.

comment:6 Changed 17 months ago by Preston Holmes <preston@…>

In 5566caeea85069d0560d95071d8a4592a91f6957:

Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.