Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.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; 'python,': 0.02; 'string.': 0.04; 'intermediate': 0.05; 'run-time': 0.05; 'say,': 0.05; 'socket': 0.05; 'things.': 0.05; 'ascii': 0.07; 'compile-time': 0.07; 'objects,': 0.07; 'subject:How': 0.09; 'python': 0.09; 'convenience': 0.09; 'encode': 0.09; 'file-like': 0.09; 'happens.': 0.09; 'internally': 0.09; 'mode,': 0.09; 'patches': 0.09; 'subject:()': 0.09; 'subject:string': 0.09; 'subject:using': 0.09; 'terry': 0.09; 'aug': 0.13; 'library': 0.15; 'encoding': 0.15; '127': 0.16; '3.3.': 0.16; 'codec': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'original:': 0.16; 'pairs': 0.16; 'reedy': 0.16; 'socket.': 0.16; 'subject:unicode': 0.16; 'subject:variable': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'byte': 0.17; 'bytes': 0.17; 'string,': 0.17; 'unicode': 0.17; 'memory': 0.18; '(or': 0.18; 'bit': 0.21; 'error.': 0.21; 'received:209.85.214.174': 0.21; 'object.': 0.22; 'needed.': 0.23; 'raise': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'creating': 0.26; 'common': 0.26; 'am,': 0.27; 'done.': 0.27; 'handling': 0.27; 'change,': 0.27; 'strongly': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'chris': 0.28; 'fine': 0.28; '(possibly': 0.29; 'declared': 0.29; 'strings,': 0.29; 'maybe': 0.29; 'writes': 0.30; 'error': 0.30; '(and': 0.32; 'file': 0.32; 'could': 0.32; 'goes': 0.33; 'point,': 0.33; 'raising': 0.33; 'safely': 0.33; 'to:addr:python-list': 0.33; 'everyone': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'clear': 0.35; 'open': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'characters': 0.36; 'data.': 0.36; 'possible': 0.37; 'optimization': 0.37; 'does': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'sure': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'further': 0.61; 'containing': 0.61; 'back': 0.62; 'relatively': 0.62; 'different': 0.63; 'making': 0.64; '20,': 0.65; 'obvious': 0.71; 'pike': 0.84; 'subject:value': 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=um+zS+AwXk2fyAq/9RU2kZd809Qbj/gHvNopItitvoY=; b=yf+AxYD0QAxwNWMd0Y/7+T/2o7bGQy3i1rUpda9x703cR34MGAZJZ0ZtG+/udeKF3W Ake2JV3OhuzFcqdYB7IZonu1vA20BKKflMMwNYafgTOh2qDRmsI1lerRrmdZ8/E5mpK+ dYygmskLyjmRj/2LQGuga38EuXoYStYa4JMcovf5Q9b637ED6HUbEdUkkyVVGTZsvUia tw3RaL2xKMtIywMtErwO5H2D+vOvTFAnqUlaVy4n0RK1N5sISXQX9EDJBSijvrbd6Hs8 bMEpga3a11Bsk0gu0/GxXPBUJKwG06SBm3syGrGyv8fAX4Psh6+iqzNbdNX62I8/OjY0 IphA== MIME-Version: 1.0 In-Reply-To: References: <308df2af-abe7-4043-b199-0a39f440e0ab@googlegroups.com> <502f8a2a$0$29978$c3e8da3$5496439d@news.astraweb.com> <7xehn4vyya.fsf@ruckus.brouhaha.com> <5030832d$0$29978$c3e8da3$5496439d@news.astraweb.com> <7x8vdbmho6.fsf@ruckus.brouhaha.com> Date: Mon, 20 Aug 2012 14:07:39 +1000 Subject: Re: How do I display unicode value stored in a string variable using ord() 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.12 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345435662 news.xs4all.nl 6864 [2001:888:2000:d::a6]:60890 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27457 On Mon, Aug 20, 2012 at 10:35 AM, Terry Reedy wrote: > On 8/19/2012 6:42 PM, Chris Angelico wrote: >> However, Python goes a bit further by making it VERY clear that this >> is a mere optimization, and that Unicode strings and bytes strings are >> completely different beasts. In Pike, it's possible to forget to >> encode something before (say) writing it to a socket. Everything works >> fine while you have only ASCII characters in the string, and then >> breaks when you have a >255 codepoint - or perhaps worse, when you >> have a 127 > Python writes strings to file objects, including open sockets, without > creating a bytes object -- IF the file is opened in text mode, which always > has an associated encoding, even if the default 'ascii'. From what you say, > this is what Pike is missing. In text mode, the library does the encoding, but an encoding still happens. > I am pretty sure that the obvious optimization has already been done. The > internal bytes of all-ascii text can safely be sent to a file with ascii (or > ascii-compatible) encoding without intermediate 'decoding'. I remember > several patches of that sort. If a string is internally ucs2 and the file is > declared usc2 or utf-16 encoding, then again, pairs of bytes can go directly > (possibly with a byte swap). Maybe it doesn't take any memory change, but there is a data type change. A Unicode string cannot be sent over the network; an encoding is needed. In Pike, I can take a string like "\x20AC" (or "\u20ac" or "\U000020ac", same thing) and manipulate it as a one-character string, but I cannot write it to a file or file-like object. I can, however, pass it through a codec (and there's string_to_utf8() for the convenience of the common case), and get back something like "\xe2\x82\xac", which is a three-byte string. The thing is, though, that this new string is of exactly the same data type as the original: 'string'. Which means that I could have a string containing Latin-1 but not ASCII characters, and Pike will happily write it to a socket without raising a compile-time or run-time error. Python, under the same circumstances, would either raise an error or quietly (and correctly) encode the data. But this is a relatively trivial point, in the scheme of things. Python has an excellent model now for handling Unicode strings, and I would STRONGLY recommend everyone to upgrade to 3.3. ChrisA