Opened 11 years ago

Closed 11 years ago

#20231 closed Bug (fixed)

django.utils.text.smart_strip does not work with lazy strings (TypeError)

Reported by: Baptiste Mispelon Owned by: nobody
Component: Utilities Version: dev
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

>>> from django.utils.text import smart_split
>>> from django.utils.translation import ugettext_lazy as _
>>> smart_split(_('a b c d'))
<django.utils.functional.lazy.<locals>.__proxy__ object at 0x7f53e20b0e10>
>>> list(smart_split(_('a b c d')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./django/utils/functional.py", line 128, in __wrapper__
    raise TypeError("Lazy object returned unexpected type.")
TypeError: Lazy object returned unexpected type.

This is caused by this line: https://github.com/django/django/blob/master/django/utils/text.py#L359

I don't think the allow_lazy decorator can be applied to a generator function so I think the line should be removed altogether.

If we remove the decorator, then lazy strings are evaluated when the generator is called.
This is not exactly backwards-incompatible since this function never actually worked with lazy strings in the first place.

Change History (3)

comment:1 by Claude Paroz, 11 years ago

Triage Stage: UnreviewedAccepted

+1 to remove allow_lazy here. As soon as you begin to manipulate strings with splitting and such, the lazyness of the string has to be dropped anyway, in my opinion. The fact that the issue didn't emerge until now is also a good sign that the functionality is not so crucial.

comment:2 by Baptiste Mispelon, 11 years ago

Has patch: set

I've prepared a pull request for this ticket that include fixes for 3 other ones: https://github.com/django/django/pull/1007

comment:3 by Claude Paroz <claude@…>, 11 years ago

Resolution: fixed
Status: newclosed

In e7b9c11c3f87e235e643c340d38325fbd4257047:

Fixed #20231 -- Don't use allow_lazy on smart_split

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