#20812 closed Bug (fixed)

django.utils.encoding.force_text() should call unicode() instead of __unicode__()

Reported by: lie.1296@… Owned by: apollo13
Component: Utilities 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 (last modified by timo)

Unlike the unicode() builtin function, the method __unicode__() does not validate its return type.

If people wrote a buggy __unicode__ which does not return str or unicode, this can lead to a situation where force_text() will return non-text, and so the bug may only appear much, much later when other parts of the application assumes they were getting text and often it will become very difficult to find how the non-string object came from.

Expected result:

force_text() should be switched to use unicode() so returning non-text from __unicode__() will immediately raise an error at the point where the error would have been obvious.

Change History (4)

comment:2 Changed 21 months ago by timo

  • Description modified (diff)

comment:3 Changed 21 months ago by mjtamlyn

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I think the current implementation may have something to do with the cross python 2/3 compatibility. We could get the same effect by calling six.text_type(obj.__unicode__()), but I'm not sure if that's the best approach.

comment:4 Changed 20 months ago by apollo13

  • Has patch set
  • Owner changed from nobody to apollo13
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.5 to master

comment:5 Changed 20 months ago by Florian Apolloner <florian@…>

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

In 2326dedde81c59950d2b1e0653a837e1d7019acd:

Fixed #20812 -- Error out if unicode/str doesn't return a text type.

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