Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#16261 closed Bug (worksforme)

Model.__repr__() should never return unicode

Reported by: Luc Saffre Owned by: nobody
Component: Core (Other) Version: 1.3
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

Unlike stated in ticket #13831,
and according to
this post on slashdot,
it seems that __repr__() should never return unicode in Python 2.

The current version does:

    def __repr__(self):
        try:
            u = unicode(self)
        except (UnicodeEncodeError, UnicodeDecodeError):
            u = '[Bad Unicode data]'
        return smart_str(u'<%s: %s>' % (self.__class__.__name__, u))

My suggestion would be to change this to:

    def __repr__(self):
        try:
            u = unicode(self)
        except (UnicodeEncodeError, UnicodeDecodeError):
            u = '[Bad Unicode data]'
        return '<%s: %r>' % (self.__class__.__name__, u)

Change History (2)

comment:1 by Aymeric Augustin, 13 years ago

Component: UncategorizedCore (Other)
Resolution: worksforme
Status: newclosed
Type: UncategorizedBug

Since smart_str returns a bytestring version of its first argument, the current implementation of __repr__ always returns a bytestring.

So I don't understand why you suggest this change.

Also, it is backwards incompatible for people relying on a certain format of __repr__ in their tests.


On the general topic of __repr__ and unicode

The Python docs say that __repr__ should return a "string representation". Your argument is that "string representation" means "instance of str" and not "instance of basestr", but I'm not convinced.

This bug (http://bugs.python.org/issue5876) says that __repr__ can return unicode, which should be automatically converted as needed. Apparently, this feature was broken between 2.4 and 2.5.

Even if __repr__ actually returned unicode, I'm not sure we would change it.

comment:2 by Aymeric Augustin, 13 years ago

#13831 is not related: it discusses non-ascii characters in __repr__, not unicode.

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