Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'builtin': 0.07; 'interpreted': 0.07; 'valueerror:': 0.07; 'works.': 0.07; 'be:': 0.09; 'buffer,': 0.09; 'here?': 0.09; 'normally,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'typeerror:': 0.09; 'def': 0.10; ':-)': 0.13; 'applies': 0.15; 'button?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'roy': 0.16; 'subject:unicode': 0.16; 'string': 0.17; 'wrote:': 0.17; 'fix': 0.17; 'integer': 0.17; 'string,': 0.17; 'unicode': 0.17; 'jan': 0.18; 'obviously': 0.18; '>>>': 0.18; 'supposed': 0.21; '"",': 0.22; 'latter': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; '(most': 0.27; 'am,': 0.27; 'functions.': 0.27; 'object,': 0.27; 'header:X-Complaints-To:1': 0.28; 'noticed': 0.28; 'post': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'that.': 0.30; 'url:python': 0.32; 'file': 0.32; 'not.': 0.32; 'print': 0.32; 'traceback': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'problem,': 0.35; 'received:org': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'behind': 0.38; 'object': 0.38; 'some': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'matter': 0.61; 'subject:, ': 0.61; 'moments': 0.65; 'sit': 0.65; 'smith': 0.71; 'special': 0.73; 'article': 0.78; 'received:fios.verizon.net': 0.84; 'subject:!)': 0.84; 'url:functions': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: __unicode__() works, unicode() blows up. (Never mind!) Date: Sun, 04 Nov 2012 13:10:55 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352052676 news.xs4all.nl 6978 [2001:888:2000:d::a6]:57423 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32739 On 11/4/2012 8:41 AM, Roy Smith wrote: > In article , > Roy Smith wrote: > >>>>> print u.__unicode__() >> None >> >>>>> print unicode(u) >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: coercing to Unicode: need string or buffer, NoneType found >> >> What's going on here? I thought >> (http://docs.python.org/2/library/functions.html#unicode) the latter two >> calls should be identical, but obviously they're not. > > Why is it, that no matter how long you stare at a problem, the answer > comes to you moments after you hit the Post button? :-) > > The problem is that __unicode__() is supposed to return a Unicode > object, and unicode() enforces that. The fix is to change: > > def __unicode__(self): > return self.username > > to be: > > def __unicode__(self): > return unicode(self.username) > > This never got noticed before because normally, self.username already is > a unicode string, so it just works. The same principle applies to some of the other special methods that sit behind builtin functions. >>> class C: def __len__(self): return '42' # whoops >>> len(C()) Traceback (most recent call last): File "", line 1, in len(C()) TypeError: 'str' object cannot be interpreted as an integer >>> class C: def __len__(self): return -42 # whoops again >>> len(C()) Traceback (most recent call last): File "", line 1, in len(C()) ValueError: __len__() should return >= 0 -- Terry Jan Reedy