Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:text': 0.05; 'utf-8': 0.07; 'exception.': 0.09; 'lawrence': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'behaviour.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'objects.': 0.16; 'throw': 0.16; 'wrote:': 0.18; 'input': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'bytes': 0.24; 'cc:2**0': 0.24; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'figure': 0.32; 'skip:b 30': 0.33; 'objects': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'raising': 0.36; 'mapping': 0.38; 'does': 0.39; 'either': 0.39; 'talking': 0.65; 'mar': 0.68; '2015': 0.84; 'to:none': 0.92; 'responses': 0.93 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:cc :content-type; bh=5cVumT1Bt3hmbuy9kNdU/KSXeBn6j1xd17zDvjpWFIA=; b=0019lPD3LYyFLyJcx8tanFSgUvlzl0wBLuwo1C73YgKnlsqxeyuMwuDILo4NtygOlo EJGBXlGVFIHpyS+EVzZlDKR2/brrvVJ3qWRXYZwb8an+BdKf1tu1Unrf9O1ALkcLSuaZ MDlsoJdFwfOvw24rGHNUu6D1A3FUbT9rI16TOaNrsxRSkpRzBR0YM2QBzKYa7i6xw/og Uo3boPQg+arsk6peCqnJ/Vnxalw2rSiR9Gt/qKcO3RA4tj71p9uUt0fuQ2cfwib8VvSB Rrvo09ygtyaetr1v3DcYzTTcLewCxV1fO7x/DiQyW4Zwhw/8oU65hjrjpqLhYsEzlyKu bvQw== MIME-Version: 1.0 X-Received: by 10.42.51.68 with SMTP id d4mr16690416icg.26.1425747189500; Sat, 07 Mar 2015 08:53:09 -0800 (PST) In-Reply-To: References: <9169f3b1-2ac7-42a3-8033-584f84b88a1f@googlegroups.com> <7a75a23c-4678-4d7a-a2ec-9e8fff4c07f8@googlegroups.com> <132d5ce6-f672-4eec-99f9-1cc9e88b94f3@googlegroups.com> <619e4cb5-1c4c-449b-a5d7-951101b32b45@googlegroups.com> <54f862ca$0$13014$c3e8da3$5496439d@news.astraweb.com> <54fadc70$0$13004$c3e8da3$5496439d@news.astraweb.com> <87twxxxbvd.fsf@elektro.pacujo.net> <54fb1bf4$0$12993$c3e8da3$5496439d@news.astraweb.com> <87twxw4xlz.fsf@elektro.pacujo.net> <87k2ysydtk.fsf@elektro.pacujo.net> Date: Sun, 8 Mar 2015 03:53:09 +1100 Subject: Re: Newbie question about text encoding From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425747192 news.xs4all.nl 2848 [2001:888:2000:d::a6]:44373 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87107 On Sun, Mar 8, 2015 at 3:40 AM, Mark Lawrence wrote: >> Here's an example: >> >> b = b'\x80' >> >> Yes, it generates an exception. IOW, UTF-8 is not a bijective mapping >> from str objects to bytes objects. >> > > Python 2 might, Python 3 doesn't. He was talking about this line of code: b.decode('utf-8').encode('utf-8') == b With the above assignment, that does indeed throw an error - which is correct behaviour. Challenge: Figure out a byte-string input that will make this function return True. def is_utf8_broken(b): return b.decode('utf-8').encode('utf-8') != b Correct responses for this function are either False or raising an exception. ChrisA