Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81221
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <francis.moro@gmail.com> |
| 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; 'example:': 0.03; 'encoding': 0.05; 'explicitly': 0.05; 'subsequent': 0.05; '21,': 0.07; 'explicit': 0.07; 'none,': 0.07; 'suddenly': 0.07; 'string': 0.09; 'encode': 0.09; 'exception,': 0.09; 'handful': 0.09; 'locale': 0.09; 'prefix': 0.09; 'strings.': 0.09; 'subject:2.7': 0.09; 'type,': 0.09; 'valueerror:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'language,': 0.12; 'suggest': 0.14; '2.7': 0.14; 'itself.': 0.14; '"from': 0.16; '__future__': 0.16; 'ascii,': 0.16; 'doing:': 0.16; 'ecosystem': 0.16; 'effect.': 0.16; 'encoding.': 0.16; 'exception:': 0.16; 'expecting': 0.16; 'iterable': 0.16; 'prefix,': 0.16; 'str()': 0.16; 'str,': 0.16; 'subject:unicode': 0.16; 'two.': 0.16; 'typeerror:': 0.16; 'unicode,': 0.16; 'unicode.': 0.16; 'unpack': 0.16; 'win,': 0.16; 'with;': 0.16; 'worst': 0.16; 'subject:python': 0.16; 'exception': 0.16; 'language': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'library': 0.18; 'module': 0.19; 'passing': 0.19; 'producing': 0.19; 'written': 0.21; 'seems': 0.21; '(the': 0.22; '>>>': 0.22; 'code,': 0.22; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'byte': 0.24; 'bytes': 0.24; 'exists': 0.24; 'library,': 0.24; 'skip:l 30': 0.24; 'string,': 0.24; 'unicode': 0.24; 'decide': 0.24; 'cc:2**0': 0.24; 'handling': 0.26; 'skip:" 30': 0.26; 'switch': 0.26; 'pass': 0.26; 'values': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; 'character': 0.29; 'points': 0.29; 'raise': 0.29; 'absolute': 0.30; 'easier': 0.31; '"",': 0.31; '>>>>': 0.31; 'yields': 0.31; 'file': 0.32; 'this.': 0.32; 'probably': 0.32; 'stuff': 0.32; 'another': 0.32; 'quite': 0.32; 'text': 0.33; '(most': 0.33; 'fri,': 0.33; 'programmers': 0.33; 'reader': 0.33; 'subject:time': 0.33; 'skip:_ 10': 0.34; 'subject: (': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'subject:one': 0.36; 'useful': 0.36; 'thanks': 0.36; "i'll": 0.36; 'hi,': 0.36; 'should': 0.36; 'wrong': 0.37; 'too': 0.37; 'two': 0.37; 'clear': 0.37; 'sometimes': 0.38; 'message-id:@gmail.com': 0.38; 'nov': 0.38; 'handle': 0.38; 'pm,': 0.38; 'that,': 0.38; 'recent': 0.39; 'expect': 0.39; 'places': 0.64; 'subject:more': 0.64; 'more': 0.64; 'different': 0.65; 'finally': 0.65; 'charset:windows-1252': 0.65; 'worth': 0.66; 'world': 0.66; 'here': 0.66; 'yes': 0.68; 'prime': 0.74; 'url:wordpress': 0.78; 'characters,': 0.84; 'clearer': 0.84; 'commenting': 0.84; 'fortunately,': 0.84; 'lose...': 0.84; 'that)': 0.84; 'trick,': 0.84; 'str.': 0.91; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:cc:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=Wzw0rQgKMrwJd5itcRtvHynIBTd6M3DX259Wfp5LnpQ=; b=bbLwE+gsvS7bgEVKSu3aTajjORP0NArBtbAxVoEA0/wcE5H7ngabZHrrtcmxfmolJ9 gN16uzVprFqxFf2tKXUhgaJark+aR4Zij5pCuj37xBefeHc4cDIxqi3JZjZfyaPg4ixq ugDX4pVfpI+Kuo5ZZJc1tXOCbVTyfJPJMKCYRbOhWCyZVDoO3zE97NcwOzPvC+wUZEY5 YFANVWvPEXI7df/3/A78mvkTiOV9PxE3MkUCRRghWeGFqDEoruy2GKzr06w7P4fkT+wV V0Zxt/bxgHK3pYELA//nwojUhEMCk0ov6zWmL8BlSnVFB+CiTOtEE9Kh24U8d3AtusbU DttQ== |
| X-Received | by 10.180.109.194 with SMTP id hu2mr26713613wib.24.1416586266246; Fri, 21 Nov 2014 08:11:06 -0800 (PST) |
| Date | Fri, 21 Nov 2014 17:11:02 +0100 |
| From | Francis Moreau <francis.moro@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 |
| MIME-Version | 1.0 |
| CC | "python-list@python.org" <python-list@python.org> |
| Subject | Re: python 2.7 and unicode (one more time) |
| References | <m4kcu8$ta8$1@ger.gmane.org> <CAPTjJmrAyv_4WkvHPtFE8ADutQRFjqy3pMsQwAeX34JoubxkTw@mail.gmail.com> <546DF738.7030609@gmail.com> <CAPTjJmrY=SSa1pfGqTqTFhJ7xo6Q9ToFHG=jjVzxwg=bCoOAHA@mail.gmail.com> |
| In-Reply-To | <CAPTjJmrY=SSa1pfGqTqTFhJ7xo6Q9ToFHG=jjVzxwg=bCoOAHA@mail.gmail.com> |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16109.1416586269.18130.python-list@python.org> (permalink) |
| Lines | 100 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1416586269 news.xs4all.nl 2915 [2001:888:2000:d::a6]:44601 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81221 |
Show key headers only | View raw
On 11/20/2014 04:15 PM, Chris Angelico wrote:
> On Fri, Nov 21, 2014 at 1:14 AM, Francis Moreau <francis.moro@gmail.com> wrote:
>> Hi,
>>
>> Thanks for the "from __future__ import unicode_literals" trick, it makes
>> that switch much less intrusive.
>>
>> However it seems that I will suddenly be trapped by all modules which
>> are not prepared to handle unicode. For example:
>>
>> >>> from __future__ import unicode_literals
>> >>> import locale
>> >>> locale.setlocale(locale.LC_ALL, 'fr_FR')
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "/usr/lib64/python2.7/locale.py", line 546, in setlocale
>> locale = normalize(_build_localename(locale))
>> File "/usr/lib64/python2.7/locale.py", line 453, in _build_localename
>> language, encoding = localetuple
>> ValueError: too many values to unpack
>>
>> Is the locale module an exception and in that case I'll fix it by doing:
>>
>> >>> locale.setlocale(locale.LC_ALL, b'fr_FR')
>>
>> or is a (big) part of the modules in python 2.7 still not ready for
>> unicode and in that case I have to decide which prefix (u or b) I should
>> manually add ?
>
> Sadly, there are quite a lot of parts of Python 2 that simply don't
> handle Unicode strings. But you can probably keep all of those down to
> just a handful of explicit b"whatever" strings; most places should
> accept unicode as well as str. What you're seeing here is a prime
> example of one of this author's points (caution, long post):
>
> http://unspecified.wordpress.com/2012/04/19/the-importance-of-language-level-abstract-unicode-strings/
>
> """The lesson of Python 3 is: give programmers a Unicode string type,
> *make it the default*, and encoding issues will /mostly/ go away."""
>
> There's a whole ecosystem to Python 2 - some in the standard library,
> heaps more in the rest of the world - and a lot of it was written on
> the assumption that a byte is a character is an octet. When you pass
> Unicode strings to functions written to expect byte strings, sometimes
> you win, and sometimes you lose... even with the standard library
> itself. But the Python 3 ecosystem has been written on the assumption
> that strings are Unicode. It's only a narrow set of programs
> ("boundary code", where you're moving text across networks and stuff
> like that) where the Python 2 model is easier to work with; and the
> recent Py3 releases have been progressively working to relieve that
> pain.
>
> The absolute worst case is a function which exists in Python 2 and 3,
> and requires a byte string in Py2 and a text string in Py3. Sadly,
> that may be exactly what locale.setlocale() is. For that, I would
> suggest explicitly passing stuff through str():
>
> locale.setlocale(locale.LC_ALL, str('fr_FR'))
>
> In Python 3, 'fr_FR' is already a str, so passing it through str()
> will have no significant effect. (Though it would be worth commenting
> that, to make it clear to a subsequent reader that this is Py2 compat
> code.) In Python 2 with unicode_literals active, 'fr_FR' is a unicode,
> so passing it through str() will encode it to ASCII, producing a byte
> string that setlocale should be happy with.
>
> By the way, the reason for the strange error message is clearer in
> Python 3, which chains in another exception:
>
>>>> locale.setlocale(locale.LC_ALL, b'fr_FR')
> Traceback (most recent call last):
> File "/usr/local/lib/python3.5/locale.py", line 498, in _build_localename
> language, encoding = localetuple
> ValueError: too many values to unpack (expected 2)
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/local/lib/python3.5/locale.py", line 594, in setlocale
> locale = normalize(_build_localename(locale))
> File "/usr/local/lib/python3.5/locale.py", line 507, in _build_localename
> raise TypeError('Locale must be None, a string, or an iterable of
> two strings -- language code, encoding.')
> TypeError: Locale must be None, a string, or an iterable of two
> strings -- language code, encoding.
>
> So when it gets the wrong type of string, it attempts to unpack it as
> an iterable; it yields five values (the five bytes or characters,
> depending on which way it's the wrong type of string), but it's
> expecting two. Fortunately, str() will deal with this. But make sure
> you don't have the b prefix, or str() in Py3 will give you quite a
> different result!
>
Yes I finally used str() since only setlocale() reported to have some
issues with unicode_literals active in my appliction.
Thanks Chris for your useful insight.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: python 2.7 and unicode (one more time) Francis Moreau <francis.moro@gmail.com> - 2014-11-21 17:11 +0100
csiph-web