Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!news2.euro.net!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; 'bug': 0.02; 'distinct': 0.04; 'handler': 0.04; 'string.': 0.04; 'method,': 0.05; 'bug.': 0.07; 'constructor': 0.07; 'does.': 0.07; 'received:verizon.net': 0.07; 'say.': 0.07; 'specifying': 0.07; 'terry': 0.07; 'python': 0.08; 'bug,': 0.09; 'bytes,': 0.09; 'encoding.': 0.09; 'given,': 0.09; 'imo.': 0.09; 'instance.': 0.09; 'raised,': 0.09; 'raised.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'exception': 0.12; 'library': 0.13; 'argument': 0.15; 'method.': 0.15; '8-bit': 0.16; 'already.': 0.16; 'base64,': 0.16; 'codec': 0.16; 'discrepancy': 0.16; "doesn't.": 0.16; 'encode': 0.16; 'finney': 0.16; 'input.': 0.16; 'mean,': 0.16; 'necessary;': 0.16; 'no-op,': 0.16; 'py3': 0.16; 'reedy': 0.16; 'simplified': 0.16; 'subject:unicode': 0.16; 'succeed,': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'any,': 0.18; 'bytes': 0.18; 'instance': 0.18; 'jan': 0.19; 'subject:not': 0.19; 'pointed': 0.21; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'etc,': 0.23; 'parameters.': 0.23; "shouldn't": 0.23; 'ignore': 0.24; 'string': 0.24; 'thanks.': 0.24; 'expect': 0.25; 'convert': 0.25; 'writes:': 0.25; 'function': 0.27; 'fact': 0.27; 'separate': 0.28; 'subject:" ': 0.28; 'second': 0.28; 'anyway.': 0.29; 'explicitly': 0.29; 'unicode': 0.29; 'pm,': 0.29; 'coding.': 0.30; 'exists,': 0.30; 'line:': 0.30; 'may,': 0.30; 'object.': 0.30; 'semantics': 0.30; 'str': 0.30; 'undocumented': 0.30; 'error': 0.30; 'specified': 0.31; 'url:library': 0.31; 'version': 0.32; 'thu,': 0.32; 'does': 0.32; 'modules': 0.32; 'objects': 0.32; 'there': 0.33; 'this.': 0.33; "can't": 0.33; 'header:User-Agent:1': 0.33; 'object': 0.33; 'header:X-Complaints- To:1': 0.34; 'character': 0.34; 'steven': 0.34; 'done': 0.34; 'calling': 0.34; 'doc': 0.34; 'latter': 0.34; 'smart': 0.34; 'to:addr:python-list': 0.35; '...': 0.35; 'apply': 0.35; 'url:python': 0.35; 'two': 0.36; 'received:org': 0.36; '(by': 0.37; 'encoding': 0.37; 'but': 0.37; 'using': 0.37; 'either': 0.37; 'happens': 0.38; 'event': 0.38; 'think': 0.38; 'should': 0.38; 'correctly': 0.39; 'url:org': 0.39; 'to:addr:python.org': 0.40; 'summary': 0.40; 'type': 0.61; 'john': 0.61; "you've": 0.61; 'supply': 0.61; 'matter': 0.62; 'kind': 0.62; 'ever': 0.64; 'believe': 0.65; 'stated': 0.70; '8bit%:46': 0.73; 'succeed': 0.73; 'skip:\xe2 10': 0.74; 'care,': 0.77; '(bytes)': 0.84; '(unicode)': 0.84; 'encoding,': 0.84; 'modes:': 0.84; 'url:functions': 0.84; 'end-user': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: "Decoding unicode is not supported" in unusual situation Date: Wed, 07 Mar 2012 19:03:41 -0500 References: <4f571b94$0$12037$742ec2ed@news.sonic.net> <8762egmzfp.fsf@benfinney.id.au> <4f5749bc$0$29989$c3e8da3$5496439d@news.astraweb.com> <4f57b63f$0$11986$742ec2ed@news.sonic.net> <871up4m69h.fsf@benfinney.id.au> <4f57eeac$0$29989$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4f57eeac$0$29989$c3e8da3$5496439d@news.astraweb.com> 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: 109 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331165057 news.xs4all.nl 6915 [2001:888:2000:d::a6]:37225 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21361 On 3/7/2012 6:26 PM, Steven D'Aprano wrote: > On Thu, 08 Mar 2012 08:48:58 +1100, Ben Finney wrote: > >> John Nagle writes: >> >>> The library bug, if any, is that you can't apply >>> >>> unicode(s, errors=3D'replace') >>> >>> to a Unicode string. TypeError("Decoding unicode is not supported") i= s >>> raised. However >>> >>> unicode(s) >>> >>> will accept Unicode input. >> >> I think that's a Python bug. If the latter succeeds as a no-op, the >> former should also succeed as a no-op. Neither should ever get any >> errors when =E2=80=98s=E2=80=99 is a =E2=80=98unicode=E2=80=99 object = already. > No. The semantics of the unicode function (technically: a type > constructor) are well-defined, and there are two distinct behaviours: > > unicode(obj) > > is analogous to str(obj), and it attempts to convert obj to a unicode > string by calling obj.__unicode__, if it exists, or __str__ if it > doesn't. No encoding or decoding is attempted in the event that obj is = a > unicode instance. > > unicode(obj, encoding, errors) > > is explicitly stated in the docs as decoding obj if EITHER of encoding = or > errors is given, AND that obj must be either an 8-bit string (bytes) or= a > buffer object. > > It is true that u''.decode() will succeed, in Python 2, but the fact th= at > unicode objects have a decode method at all is IMO a bug. It has also I believe that is because in Py 2, codecs and .encode/.decode were used=20 for same type recoding like base64, uu coding. That was simplified in=20 Py3 so that 'decoding' is bytes to string and 'encoding' is string to=20 bytes, and base64, etc, are only done in their separate modules and not=20 also duplicated in the codecs machinery. > been corrected in Python 3, where (unicode) str objects no longer have = a > decode method, and bytes objects no longer have an encode method. > > >>> The Python documentation >>> ("http://docs.python.org/library/functions.html#unicode") does not >>> mention this. > > Yes it does. It is is the SECOND sentence, immediately after the summar= y > line: > > unicode([object[, encoding[, errors]]]) > Return the Unicode string version of object using one of the > following modes: > > If encoding and/or errors are given, unicode() will decode the obj= ect > which can either be an 8-bit string or a character buffer using th= e > codec for encoding. ... > > > Admittedly, it doesn't *explicitly* state that TypeError will be raised= , > but what other exception kind would you expect when you supply an > argument of the wrong type? What you have correctly pointed out is that there is no discrepancy=20 between doc and behavior and hence no bug for the purpose of the=20 tracker. Thanks. >>> It is therefore necessary to check the type before >>> calling "unicode", or catch the undocumented TypeError exception >>> afterward. >> >> Yes, this check should not be necessary; calling the =E2=80=98unicode=E2= =80=99 >> constructor with an object that's already an instance of =E2=80=98unic= ode=E2=80=99 >> should just return the object as-is, IMO. It shouldn't matter that >> you've specified how decoding errors are to be handled, because in tha= t >> case no decoding happens anyway. > > I don't believe that it is the job of unicode() to Do What I Mean, but > only to Do What I Say. If I *explicitly* tell unicode() to decode the > argument (by specifying either the codec or the error handler or both) > then it should not double-guess me and ignore the extra parameters. > > End-user applications may, with care, try to be smart and DWIM, but > library functions should be dumb and should do what they are told. --=20 Terry Jan Reedy