[KoCo-CVS] [Commit] iconvcodec _iconv_codec_compat.h _iconv_codec.c
Brought to you by:
perky
From: Hye-Shik C. <pe...@us...> - 2003-04-20 19:45:21
|
perky 03/04/20 12:45:19 Modified: . _iconv_codec.c Added: . _iconv_codec_compat.h Log: Raise UnicodeError for 2.2 and under Revision Changes Path 1.2 +43 -6 iconvcodec/_iconv_codec.c Index: _iconv_codec.c =================================================================== RCS file: /cvsroot/koco/iconvcodec/_iconv_codec.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- _iconv_codec.c 20 Apr 2003 18:17:57 -0000 1.1 +++ _iconv_codec.c 20 Apr 2003 19:45:18 -0000 1.2 @@ -24,11 +24,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _iconv_codec.c,v 1.1 2003/04/20 18:17:57 perky Exp $ + * $Id: _iconv_codec.c,v 1.2 2003/04/20 19:45:18 perky Exp $ */ #include "Python.h" #include <iconv.h> +#include "_iconv_codec_compat.h" #if Py_USING_UNICODE # if Py_UNICODE_SIZE == 2 @@ -202,7 +203,11 @@ else if (strcmp(errors, "replace") == 0) return ERROR_REPLACE; else +#ifdef HAVE_ERROR_CALLBACKS return PyCodec_LookupError(errors); +#else + return NULL; +#endif } #define UTF8NEXTCHAR(p) \ @@ -263,10 +268,13 @@ iconv_t ic, IconvEncoderBuffer *buf, PyObject *errors, int esize) { - PyObject *argsobj, *retobj = NULL, *tobj; - PyObject *retstr = NULL; +#ifdef HAVE_ERROR_CALLBACKS + PyObject *retobj = NULL, *retstr = NULL; + PyObject *argsobj, *tobj; + int retstrsize, newpos; +#endif const char *reason; - int retstrsize, newpos, start, end; + int start, end; if (errno == E2BIG) { RESERVE_ENCODEBUFFER(buf, -1); @@ -345,6 +353,18 @@ start = (int)(buf->inbuf - buf->inbuf_top); end = start + esize; +#ifndef HAVE_ERROR_CALLBACKS + if (esize == 1) + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't encode byte '\\u%04x' in position %d: %s", + self->encoding, *buf->inbuf, start, reason); + else + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't encode bytes in position %d-%d: %s", + self->encoding, start, end, reason); +errorexit: + return -1; +#else /* use cached exception object if available */ if (buf->excobj == NULL) { buf->excobj = PyUnicodeEncodeError_Create(self->encoding, @@ -427,6 +447,7 @@ Py_XDECREF(retobj); Py_XDECREF(retstr); return -1; +#endif } static int @@ -788,9 +809,12 @@ iconv_t ic, IconvDecoderBuffer *buf, PyObject *errors, int eno, int esize) { +#ifdef HAVE_ERROR_CALLBACKS + PyObject *argsobj, *retuni = NULL, *retobj = NULL; + int retunisize, newpos; +#endif const char *reason; - PyObject *argsobj, *retobj = NULL, *retuni = NULL; - int retunisize, newpos, start, end; + int start, end; if (PyErr_Occurred()) return -1; /* treat as an internal error */ @@ -827,6 +851,18 @@ start = (int)(buf->inbuf - buf->inbuf_top); end = start + esize; +#ifndef HAVE_ERROR_CALLBACKS + if (esize == 1) + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't decode byte 0x%02x in position %d: %s", + self->encoding, *buf->inbuf, start, reason); + else + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't decode bytes in position %d-%d: %s", + self->encoding, start, end, reason); +errorexit: + return -1; +#else /* use cached exception object if available */ if (buf->excobj == NULL) { buf->excobj = PyUnicodeDecodeError_Create( @@ -890,6 +926,7 @@ errorexit: Py_XDECREF(retobj); return -1; +#endif } static size_t 1.1 iconvcodec/_iconv_codec_compat.h Index: _iconv_codec_compat.h =================================================================== /* * _iconv_codec_compat.h: Iconv Codec Compatiblility Support * * Copyright (C) 2003 Hye-Shik Chang. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: _iconv_codec_compat.h,v 1.1 2003/04/20 19:45:19 perky Exp $ */ /* Versions that doesn't support Unicode */ #if PY_VERSION_HEX < 0x02000000 # error "Your Python version doesn't have a unicode support" #endif /* PyDoc_VAR: For ~ Python 2.2 */ #if PY_VERSION_HEX < 0x02030000 # define PyDoc_VAR(name) static char name[] # define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) # define PyDoc_STR(str) str #endif /* PEP293 Codec Error Callbacks: For Python 2.3 and over */ #if PY_VERSION_HEX >= 0x02030000 # define HAVE_ERROR_CALLBACKS 1 #endif /* * ex: ts=8 sts=4 et */ |