Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'explicitly': 0.04; 'string.': 0.04; 'python3': 0.05; 'subject:bug': 0.05; 'things.': 0.05; 'bug.': 0.07; 'type,': 0.07; 'utf-8': 0.07; 'works.': 0.07; 'python': 0.09; 'encode': 0.09; 'encodes': 0.09; 'funny,': 0.09; 'imply': 0.09; 'str)': 0.09; 'structure,': 0.09; 'language': 0.14; 'component': 0.15; 'encoding': 0.15; '(bytes': 0.16; '(more': 0.16; 'arbitrarily': 0.16; 'bugs,': 0.16; 'decode': 0.16; 'does;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'somewhere.': 0.16; 'structure.': 0.16; 'transforming': 0.16; 'string': 0.17; 'wrote:': 0.17; 'bytes': 0.17; 'unicode': 0.17; '(or': 0.18; 'appropriate': 0.20; 'translate': 0.20; 'received:209.85.214.174': 0.21; 'script': 0.24; 'header:In-Reply- To:1': 0.25; 'bugs': 0.27; 'converting': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'all.': 0.28; 'dictionary': 0.29; 'fighting': 0.29; 'str': 0.29; 'unlikely': 0.29; 'convert': 0.29; 'objects': 0.29; "we're": 0.30; 'fri,': 0.30; 'sense': 0.31; 'getting': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'decisions': 0.35; 'described': 0.35; 'exist': 0.35; 'feed': 0.35; 'nov': 0.35; 'doing': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'should': 0.36; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'some': 0.38; 'description': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'kind': 0.61; "you'll": 0.62; 'perfect': 0.63; 'cast': 0.65; 'reverse': 0.65; 'encounters': 0.84; 'etc,': 0.84 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:to :content-type; bh=GnfAiE8Sf5kyMr85QyCfp46VvtjezD9wX+MYIKYgeRg=; b=WfdUzAf8m7tvoqZtT5JcgvNswy03LiTtF8p8hyxI8ntdQmOEMdOlP/36Fj+E0PADAX 95afGp/p0zwvJp4ZQt2jbDpepxZGgyif3U2kWj13ZCLvMV8yNCs/NwIDUQX+7VuzHsTp 5KxKEuqBhsGrphJxWorjjFNZCN1f3kL8h8mIGsEeamRv60vDHk3Hw4PkVGUqDTYQn3Y3 PCM2DmdYc92/KiCh8NHvunnyZ3zWQSR3jtGqhOQ9LMSCds+S4lK3Il4AwkexfLZtpl2U TUgmNCg3xJrxB57suvbMBVA3zdz9T6nvQeajrNPhlGDzhhtcNWLBIyNUkYCVHPSIytKO UA4w== MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 9 Nov 2012 23:22:04 +1100 Subject: Re: Python3.3 str() bug? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352463727 news.xs4all.nl 6902 [2001:888:2000:d::a6]:51115 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33018 On Fri, Nov 9, 2012 at 10:08 PM, Helmut Jarausch wrote: > For me it's not funny, at all. His description "funny" was in reference to the fact that you described this as a bug. This is a heavily-used mature language; bugs as fundamental as you imply are unlikely to exist (consequences of design decisions there will be, but not outright bugs, usually); extraordinary claims require extraordinary evidence. > Whenever Python3 encounters a bytestring it needs an encoding to convert it to > a string. If I feed a list of bytestrings or a list of list of bytestrings to > 'str' , etc, it should use the encoding for each bytestring component of the > given data structure. > > How can I convert a data strucure of arbitrarily complex nature, which contains > bytestrings somewhere, to a string? Okay, now we're getting somewhere. What you really should be doing is not transforming the whole structure, but explicitly transforming each part inside it. I recommend you stop fighting the language and start thinking about your data as either *bytes* or *characters* and using the appropriate data types (bytes or str) everywhere. You'll then find that it makes perfect sense to explicitly translate (en/decode) from one to another, but it doesn't make sense to encode a list in UTF-8 or decode a dictionary from Latin-1. > This problem has arisen while converting a working Python2 script to Python3.3. > Since Python2 doesn't have bytestrings it just works. Actually it does; it just calls them "str". And there's a Unicode string type, called "unicode", which is (more or less) the thing that Python 3 calls "str". You may be able to do some kind of recursive cast that, in one sweep of your data structure, encodes all str objects into bytes using a given encoding (or the reverse thereof). But I don't think this is the best way to do things. ChrisA