Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55491

Re: API for custom Unicode error handlers

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'example:': 0.03; 'subject:error': 0.03; 'else:': 0.03; 'elif': 0.05; 'handler': 0.05; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'handlers': 0.16; 'handlers.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:API': 0.16; 'subject:Unicode': 0.16; 'url:gmane': 0.16; 'valueerror):': 0.16; 'module': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'replace': 0.24; 'skip:e 30': 0.24; 'unicode': 0.24; 'looks': 0.24; 'question': 0.24; '(see': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; "i'm": 0.30; "skip:' 10": 0.31; 'too.': 0.31; 'assert': 0.31; "d'aprano": 0.31; 'keyerror:': 0.31; 'question:': 0.31; 'steven': 0.31; 'them?': 0.31; 'url:python': 0.33; 'advice': 0.35; 'except': 0.35; 'skip:u 20': 0.35; 'url:org': 0.36; 'should': 0.36; '8bit%:86': 0.38; 'to:addr:python-list': 0.38; 'planning': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'name': 0.63; 'holding': 0.65; 'received:46': 0.66; 'containing': 0.69; '3.4': 0.84; 'url:comments': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Serhiy Storchaka <storchaka@gmail.com>
Subject Re: API for custom Unicode error handlers
Date Fri, 04 Oct 2013 22:35:47 +0300
References <524ec8fe$0$29984$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 46.211.180.53
User-Agent Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.0
In-Reply-To <524ec8fe$0$29984$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.730.1380916476.18130.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380916476 news.xs4all.nl 15885 [2001:888:2000:d::a6]:55372
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55491

Show key headers only | View raw


04.10.13 16:56, Steven D'Aprano написав(ла):
> I have some custom Unicode error handlers, and I'm looking for advice on
> the right API for dealing with them.
>
> I have a module containing custom Unicode error handlers. For example:
>
> # Python 3
> import unicodedata
> def namereplace_errors(exc):
>      c = exc.object[exc.start]
>      try:
>          name = unicodedata.name(c)
>      except (KeyError, ValueError):
>          n = ord(c)
>          if n <= 0xFFFF:
>              replace = "\\u%04x"
>          else:
>              assert n <= 0x10FFFF
>              replace = "\\U%08x"
>          replace = replace % n
>      else:
>          replace = "\\N{%s}" % name
>      return replace, exc.start + 1

I'm planning to built this error handler in 3.4 (see 
http://comments.gmane.org/gmane.comp.python.ideas/21296).

Actually Python implementation should looks like:

def namereplace_errors(exc):
     if not isinstance(exc, UnicodeEncodeError):
         raise exc
     replace = []
     for c in exc.object[exc.start:exc.end]:
         try:
             replace.append(r'\N{%s}' % unicodedata.name(c))
         except KeyError:
             n = ord(c)
             if n < 0x100:
                 replace.append(r'\x%02x' % n)
             elif n < 0x10000:
                 replace.append(r'\u%04x' % n)
             else:
                 replace.append(r'\U%08x' % n)
     return ''.join(replace), exc.end

> Now, my question:
>
> Should the module holding the error handlers automatically register them?

This question interesting me too.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

API for custom Unicode error handlers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-04 13:56 +0000
  Re: API for custom Unicode error handlers Chris Angelico <rosuav@gmail.com> - 2013-10-05 03:22 +1000
  Re: API for custom Unicode error handlers Ethan Furman <ethan@stoneleaf.us> - 2013-10-04 11:05 -0700
  Re: API for custom Unicode error handlers Serhiy Storchaka <storchaka@gmail.com> - 2013-10-04 22:08 +0300
  Re: API for custom Unicode error handlers Serhiy Storchaka <storchaka@gmail.com> - 2013-10-04 22:35 +0300
  Re: API for custom Unicode error handlers Terry Reedy <tjreedy@udel.edu> - 2013-10-04 18:44 -0400

csiph-web