Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.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; 'encoding': 0.05; 'reject': 0.07; 'strict': 0.07; 'subject:code': 0.07; 'utf-8': 0.07; 'string': 0.09; 'encode': 0.09; 'indicates': 0.09; 'url:unicode': 0.09; 'python': 0.11; 'bug': 0.12; '16-bit': 0.16; 'bullet': 0.16; 'codec': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'handling,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'pairs': 0.16; 'pairs,': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'sequence.': 0.16; 'subject:UTF': 0.16; 'sure.': 0.16; 'surrogate': 0.16; 'units,': 0.16; 'wrote:': 0.18; "python's": 0.19; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'own.': 0.24; 'unicode': 0.24; '(see': 0.26; 'handling': 0.26; 'second': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'character': 0.29; 'raise': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; '"",': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'writes:': 0.31; 'file': 0.32; '(most': 0.33; 'table': 0.34; 'could': 0.34; "can't": 0.35; 'received:84': 0.35; 'but': 0.35; 'sequence': 0.36; 'url:org': 0.36; 'should': 0.36; 'error.': 0.37; 'example,': 0.37; 'unit': 0.37; 'too': 0.37; 'two': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'consists': 0.60; 'units': 0.60; 'first': 0.61; 'url:0': 0.67; 'header:Reply-To:1': 0.67; 'url:pdf': 0.68; 'invalid': 0.68; 'reply-to:no real name:2**0': 0.71; 'isolated': 0.84; 'lone': 0.84; 'reply- to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=PIY2p5aC c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=LqG8EI-UCiYA:10 a=--1n041xzFwA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=hDd6L7yn1Q0A:10 a=kZ7UWmmPAAAA:8 a=te1EGT4yAAAA:8 a=GFQ2S68NnonzV-S-6toA:9 a=wPNLvfGTeEIA:10 a=pyH5b1fOeEsA:10 X-AUTH: mrabarnett:2500 Date: Tue, 08 Oct 2013 18:00:58 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Encoding of surrogate code points to UTF-8 References: <52540e03$0$29984$c3e8da3$5496439d@news.astraweb.com> <86mwmjlraq.fsf@gmail.com> In-Reply-To: <86mwmjlraq.fsf@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 1381251660 news.xs4all.nl 15931 [2001:888:2000:d::a6]:38863 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56431 On 08/10/2013 16:23, Pete Forman wrote: > Steven D'Aprano writes: > >> I think this is a bug in Python's UTF-8 handling, but I'm not sure. > [snip] >> py> s = '\ud800\udc01' >> py> s.encode('utf-8') >> Traceback (most recent call last): >> File "", line 1, in >> UnicodeEncodeError: 'utf-8' codec can't encode character '\ud800' in >> position 0: surrogates not allowed >> >> >> Have I misunderstood? I think that Python is being too strict about >> rejecting surrogate code points. It should only reject lone surrogates, >> or invalid pairs, not valid pairs. Have I misunderstood the Unicode FAQs, >> or is this a bug in Python's handling of UTF-8? > > http://www.unicode.org/versions/Unicode6.2.0/ch03.pdf > > D75 Surrogate pair: A representation for a single abstract character > that consists of a sequence of two 16-bit code units, where the first > value of the pair is a high-surrogate code unit and the second value > is a low-surrogate code unit. > > * Surrogate pairs are used only in UTF-16. (See Section 3.9, Unicode > EncodingForms.) > > * Isolated surrogate code units have no interpretation on their own. > Certain other isolated code units in other encoding forms also have no > interpretation on their own. For example, the isolated byte [\x80] has > no interpretation in UTF-8; it can be used only as part of a multibyte > sequence. (See Table 3-7). It could be argued that this line by itself > should raise an error. > > > That first bullet indicates that it is indeed illegal to use surrogate > pairs in UTF-8 or UTF-32. > The only time you should get a surrogate pair in a Unicode string is in a narrow build, which doesn't exist in Python 3.3 and later.