Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; '-*-': 0.07; 'sys': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'encoding:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '2.7': 0.14; '2.7.2': 0.16; 'cc:name:python list': 0.16; 'debian.': 0.16; 'declaration': 0.16; 'declared': 0.16; 'non-ascii': 0.16; 'skip:u 50': 0.16; 'syntaxerror:': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'url:peps': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'skip:" 30': 0.26; 'least': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'character': 0.29; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'occurs': 0.31; 'python2.7': 0.31; 'sep': 0.31; 'steven': 0.31; 'file': 0.32; 'url:python': 0.33; "i'd": 0.34; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; '8bit%:80': 0.36; 'url:org': 0.36; 'should': 0.36; 'january': 0.37; 'two': 0.37; 'skip:o 20': 0.38; '8bit%:86': 0.38; 'hat': 0.38; 'bad': 0.39; 'skip:p 20': 0.39; 'skip:u 10': 0.60; 'more': 0.64; 'different': 0.65; 'details': 0.65; '8bit%:40': 0.68; '8bit%:50': 0.68; 'prompt': 0.68; '8bit%:100': 0.72; 'subject:this': 0.83; 'oscar': 0.84; '2013,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=qFSN2ZRNfS4W5Vvir/jmn9s+eglY22gvICIQnmx8vac=; b=UB3mp19pBHD4p0TaR+qJeypqD6uskx7qfVvlPu4uaR9WdQjnwPOTY8EvXRecq5JNrZ kW6Fr3gKR3NVzXkV4Fd4H+VkOcJ1PShW/nA/OPb85sz/L/rBy1vosLsnph1J+kVIJ3+K jPD0XxP80rrU7YkOClt6u8Q8KYC7STAbLMFBBkFix7MuWvHU8TJcUceuSgDZO3863EZq sJMc0zaw3mc5Fu2u7QjD4FhBNXBnJT5e7q56XrXJkuC0GnxvffbPXvlttdQDYf9cSHso g/ptPwXm+h7be3XwBGDSSjkguXLKsLJ2LbXRIfRbwxa1kIO6SGY3kC3+0cDZ2cOHEtzV agpw== X-Received: by 10.66.251.42 with SMTP id zh10mr21897246pac.84.1390684532941; Sat, 25 Jan 2014 13:15:32 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <52e33f8d$0$29999$c3e8da3$5496439d@news.astraweb.com> References: <52e33f8d$0$29999$c3e8da3$5496439d@news.astraweb.com> From: Oscar Benjamin Date: Sat, 25 Jan 2014 21:15:12 +0000 Subject: Re: Trying to understand this moji-bake To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Python List 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390684541 news.xs4all.nl 2932 [2001:888:2000:d::a6]:33374 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64752 On 25 January 2014 04:37, Steven D'Aprano wrote: > > But using Python 2.7, I get a really bad case of moji-bake: > > [steve@ando ~]$ python2.7 -c "print u'=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0= =B6'" > =C3=83=C2=B1=C3=83=C2=B8=C3=8E=C2=BB=C3=8F=C3=90=C2=B9=C3=90=C2=B6 > > However, interactively it works fine: > > [steve@ando ~]$ python2.7 -E > Python 2.7.2 (default, May 18 2012, 18:25:10) > [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> print u'=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6' > =C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6 > > This occurs on at least two different machines, one using Centos and the > other Debian. Same for me. It's to do with using a u literal: $ python2.7 -c "print('=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6')" =C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6 $ python2.7 -c "print(u'=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6')" =C3=83=C2=B1=C3=83=C2=B8=C3=8E=C2=BB=C3=8F=E2=82=AC=C3=90=C2=B9=C3=90=C2=B6 $ python2.7 -c "print(repr('=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6'))" '\xc3\xb1\xc3\xb8\xce\xbb\xcf\x80\xd0\xb9\xd0\xb6' $ python2.7 -c "print(repr(u'=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6'))" u'\xc3\xb1\xc3\xb8\xce\xbb\xcf\x80\xd0\xb9\xd0\xb6' $ python2.7 Python 2.7.5+ (default, Sep 19 2013, 13:49:51) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> b=3D'\xc3\xb1\xc3\xb8\xce\xbb\xcf\x80\xd0\xb9\xd0\xb6' >>> print(b) =C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6 >>> s=3Du'\xc3\xb1\xc3\xb8\xce\xbb\xcf\x80\xd0\xb9\xd0\xb6' >>> print(s) =C3=83=C2=B1=C3=83=C2=B8=C3=8E=C2=BB=C3=8F=E2=82=AC=C3=90=C2=B9=C3=90=C2=B6 >>> print(s.encode('latin-1')) =C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6 >>> import sys >>> sys.getdefaultencoding() 'ascii' It works in the interactive prompt: >>> s =3D '=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6' >>> print(s) =C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6 >>> s =3D u'=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6' >>> print(s) =C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6 But the interactive prompt has an associated encoding: >>> import sys >>> sys.stdout.encoding 'UTF-8' If I put it into a utf-8 file with no encoding declared I get a SyntaxError= : $ cat tmp.py s =3D u'=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6' print(s) oscar@tonis-laptop:~$ python2.7 tmp.py File "tmp.py", line 1 SyntaxError: Non-ASCII character '\xc3' in file tmp.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details If I add the encoding declaration it works: oscar@tonis-laptop:~$ vim tmp.py oscar@tonis-laptop:~$ cat tmp.py # -*- coding: utf-8 -*- s =3D u'=C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6' print(s) oscar@tonis-laptop:~$ python2.7 tmp.py =C3=B1=C3=B8=CE=BB=CF=80=D0=B9=D0=B6 oscar@tonis-laptop:~$ So I'd say that your original example should be a SyntaxError with Python 2.7 but instead it implicitly uses latin-1. Oscar