Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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; 'encoding': 0.05; 'explicitly': 0.05; 'subject:Python': 0.06; '-*-': 0.07; 'debug': 0.07; 'debugging': 0.07; 'defaults': 0.07; 'think,': 0.07; 'tries': 0.07; 'utf-8': 0.07; 'string': 0.09; 'coding:': 0.09; 'encode': 0.09; 'option,': 0.09; 'output,': 0.09; 'solution,': 0.09; 'strings.': 0.09; 'subject:set': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'mostly': 0.14; '__future__': 0.16; 'backslash': 0.16; 'byte,': 0.16; 'differs': 0.16; 'expected,': 0.16; 'foo()': 0.16; 'foo(object):': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'imo,': 0.16; 'limit,': 0.16; 'non-ascii': 0.16; 'quoted': 0.16; 'record,': 0.16; 'repr()': 0.16; 'set,': 0.16; 'str,': 0.16; 'subject: \n ': 0.16; 'traceback.': 0.16; 'unicode,': 0.16; 'unicode.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'trying': 0.19; 'code,': 0.22; 'input': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'unicode': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; 'switch': 0.26; 'second': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'function': 0.29; 'am,': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'breaking': 0.31; 'types.': 0.31; 'class': 0.32; 'probably': 0.32; 'not.': 0.33; 'actual': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'problem': 0.35; 'problem.': 0.35; 'objects': 0.35; 'requirement': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'building': 0.35; 'method': 0.36; 'should': 0.36; 'turn': 0.37; 'two': 0.37; 'being': 0.38; 'problems': 0.38; 'handle': 0.38; 'rather': 0.38; 'that,': 0.38; 'generating': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'skip:u 10': 0.60; 'length': 0.61; 'simply': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; '(that': 0.65; 'default': 0.69; 'behavior': 0.77; 'characters,': 0.84; 'effectively,': 0.84; "it'd": 0.84; 'presumably': 0.84; 'subject:being': 0.84; 'str.': 0.91; 'subject:skip:s 20': 0.91; 'to:none': 0.92; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=zBVUOw72A6y0bp+kLBFlKGwl1y3fSFaLAoKnag1PC1Q=; b=SjxRFkBmjmK/rEHNK4heP0qKC9mHWbJyKH1w87h6Lm6BcTg6Y/seiLT3uRUDlbTb45 rBULxHxrK3/QJxwlTqY7M3dlvCsacvyuztwC/H0SE9tpxUgY7a6TXJqjBeMSmMK2jAr5 22soA5OAMbfPFPFNPfKxF8NvGM9EXhbDSPLbsNb3WmkvdXWpub1aMLs4MEpTNTocJaiP BL86PJuSdzvsYN2NDadSZ2QcFAbzFZaOVVmiA3I4EzKEWfukwf/fqDFUOIaKFpdqI0Wn cOPO7dzlxEMTxSwRRBpLqncgWWJN/cahlHDhtAfF1gNMpaKmwtuL6Eivfos1oGERuHUH gwNA== MIME-Version: 1.0 X-Received: by 10.68.173.132 with SMTP id bk4mr13387219pbc.169.1386112831583; Tue, 03 Dec 2013 15:20:31 -0800 (PST) In-Reply-To: <3323513.NxQf30XKqz@xrated> References: <3323513.NxQf30XKqz@xrated> Date: Wed, 4 Dec 2013 10:20:31 +1100 Subject: Re: Python 2.7.5: Strange and differing behavior depending on sys.setdefaultencoding being set From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386112840 news.xs4all.nl 2901 [2001:888:2000:d::a6]:57817 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60969 On Wed, Dec 4, 2013 at 9:32 AM, Hans-Peter Jansen wrote: > I'm experiencing strange behavior with attached code, that differs depend= ing > on sys.setdefaultencoding being set or not. If it is set, the code works = as > expected, if not - what should be the usual case - the code fails with so= me > non-sensible traceback. Interesting. You're mixing str and unicode objects a lot here. The cleanest solution, IMO, would be to either switch to Python 3 or add this to the top of your code: from __future__ import unicode_literals Either way, you'll have all your quoted strings be Unicode, rather than byte, strings. Then take away the requirement that Unicode strings contain non-ASCII characters, and let everything go through that code branch. Looking at this line in reprstr(): s =3D "u'%s'" % s.replace("'", "\\'") Two potential problems with that. Firstly, the representation is flawed: a backslash in the input string won't be changed, so it's not a true repr; but if this is just for debugging output, that's not a big deal. Secondly, this code might produce either a str or a unicode, depending on the type of s. That may cause messes later; since you seem to be mostly working with the unicode type after that, it'd probably be simpler/safer to make that always return one: s =3D u"u'%s'" % s.replace("'", "\\'") But the actual problem, I think, is that repr() guarantees to return a str, and you're trying to return a unicode. Here's an illustration: # -*- coding: utf-8 -*- class Foo(object): def __repr__(self): return u'=C3=A4=C3=B6=C3=BC' foo =3D Foo() print(foo.__repr__()) print(repr(foo)) The first one succeeds, because building up that string isn't at all a problem. The second one then tries to turn the return value of __repr__ into a string using the default encoding - which defaults to 'ascii', hence the problem you're seeing. Solution 1: Switch to Python 3, in which this will work fine (because repr() in Py3 returns a Unicode string, since _everything_ is Unicode). Solution 2: Explicitly encode in frec, or at the end of Record.__repr__(): def __repr__(self): s =3D u'%s(\n%s\n)' % (self.__class__.__name__, frec(self.__dic= t__)) return s.encode("utf-8") (that could be a one-liner, but it's already pushing 80-chars, so if you have a length limit, breaking it helps) Solution 3: Don't use __repr__ here, but simply have your frec function intelligently handle Record types. Effectively, you have your own method of generating a debug description of a Record, which could then return a unicode instead of a str. I personally recommend switching to Python 3 :) But presumably that's not an option, or you'd already have considered it. ChrisA