[KoCo-CVS] [Commit] KoreanCodecs/korean error_callback.py
Brought to you by:
perky
From: Hye-Shik C. <pe...@us...> - 2003-01-13 07:25:27
|
perky 03/01/12 23:25:26 Added: korean error_callback.py Log: Add PEP293 emulation codes. Revision Changes Path 1.1 KoreanCodecs/korean/error_callback.py Index: error_callback.py =================================================================== # # This file is part of KoreanCodecs. # # Copyright(C) 2002-2003 Hye-Shik Chang <pe...@Fr...>. # # KoreanCodecs is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # KoreanCodecs is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with KoreanCodecs; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # $Id: error_callback.py,v 1.1 2003/01/13 07:25:26 perky Exp $ # try: from __builtin__ import UnicodeEncodeError, UnicodeDecodeError from codecs import lookup_error except: # implementations from PEP293 for ancient systems class UnicodeEncodeError(UnicodeError): def __init__(self, encoding, object, start, end, reason): UnicodeError.__init__(self, "encoding '%s' can't encode characters " + "in positions %d-%d: %s" % (encoding, start, end-1, reason)) self.encoding = encoding self.object = object self.start = start self.end = end self.reason = reason class UnicodeDecodeError(UnicodeError): def __init__(self, encoding, object, start, end, reason): UnicodeError.__init__(self, "encoding '%s' can't decode characters " + "in positions %d-%d: %s" % (encoding, start, end-1, reason)) self.encoding = encoding self.object = object self.start = start self.end = end self.reason = reason def errcb_strict(exc): raise exc def errcb_ignore(exc): if isinstance(exc, UnicodeError): return (u"", exc.end) else: raise TypeError("can't handle %s" % exc.__name__) def errcb_replace(exc): if isinstance(exc, UnicodeEncodeError): return ((exc.end-exc.start)*u"?", exc.end) elif isinstance(exc, UnicodeDecodeError): return (u"\ufffd", exc.end) else: raise TypeError("can't handle %s" % exc.__name__) error_callbacks = { 'strict': errcb_strict, 'ignore': errcb_ignore, 'replace': errcb_replace, } def lookup_error(name): cb = error_callbacks.get(name) if cb: return cb else: raise LookupError, "unknown error handler name '%s'" % name # ex: ts=8 sts=4 et |