[KoCo-CVS] [Commit] cjkcodecs/src multibytecodec_compat.h multibytecodec.c
Brought to you by:
perky
From: Hye-Shik C. <pe...@us...> - 2003-06-06 05:57:55
|
perky 03/06/05 22:57:54 Modified: src multibytecodec.c Added: src multibytecodec_compat.h Log: Add Python-2.2 compatibility Revision Changes Path 1.15 +44 -4 cjkcodecs/src/multibytecodec.c Index: multibytecodec.c =================================================================== RCS file: /cvsroot/koco/cjkcodecs/src/multibytecodec.c,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- multibytecodec.c 5 Jun 2003 09:56:22 -0000 1.14 +++ multibytecodec.c 6 Jun 2003 05:57:53 -0000 1.15 @@ -26,11 +26,12 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: multibytecodec.c,v 1.14 2003/06/05 09:56:22 perky Exp $ + * $Id: multibytecodec.c,v 1.15 2003/06/06 05:57:53 perky Exp $ */ #include "Python.h" #include "multibytecodec.h" +#include "multibytecodec_compat.h" typedef struct { const Py_UNICODE *inbuf, *inbuf_top, *inbuf_end; @@ -113,8 +114,15 @@ return ERROR_IGNORE; else if (strcmp(errors, "replace") == 0) return ERROR_REPLACE; - else + else { +#ifndef NO_ERROR_CALLBACKS return PyCodec_LookupError(errors); +#else + PyErr_Format(PyExc_LookupError, "unknown error handler name '%s'", + errors); + return NULL; +#endif + } } static int @@ -169,10 +177,13 @@ MultibyteEncodeBuffer *buf, PyObject *errors, int e) { +#ifndef NO_ERROR_CALLBACKS PyObject *retobj = NULL, *retstr = NULL, *argsobj, *tobj; + int retstrsize, newpos; +#endif const char *reason; size_t esize; - int retstrsize, newpos, start, end; + int start, end; if (e > 0) { reason = "illegal multibyte sequence"; @@ -224,6 +235,18 @@ start = (int)(buf->inbuf - buf->inbuf_top); end = start + esize; +#ifdef NO_ERROR_CALLBACKS + if (esize == 1) + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't encode byte '\\u%04x' in position %d: %s", + codec->encoding, *buf->inbuf, start, reason); + else + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't encode bytes in position %d-%d: %s", + codec->encoding, start, end, reason); +errorexit: + return -1; +#else /* use cached exception object if available */ if (buf->excobj == NULL) { buf->excobj = PyUnicodeEncodeError_Create(codec->encoding, @@ -290,6 +313,7 @@ Py_XDECREF(retobj); Py_XDECREF(retstr); return -1; +#endif } static int @@ -298,10 +322,13 @@ MultibyteDecodeBuffer *buf, PyObject *errors, int e) { +#ifndef NO_ERROR_CALLBACKS PyObject *argsobj, *retobj = NULL, *retuni = NULL; + int retunisize, newpos; +#endif const char *reason; size_t esize; - int start, end, retunisize, newpos; + int start, end; if (e > 0) { reason = "illegal multibyte sequence"; @@ -336,6 +363,18 @@ start = (int)(buf->inbuf - buf->inbuf_top); end = start + esize; +#ifdef NO_ERROR_CALLBACKS + if (esize == 1) + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't decode byte 0x%02x in position %d: %s", + codec->encoding, *buf->inbuf, start, reason); + else + PyErr_Format(PyExc_UnicodeError, + "'%s' codec can't decode bytes in position %d-%d: %s", + codec->encoding, start, end, reason); +errorexit: + return -1; +#else /* use cached exception object if available */ if (buf->excobj == NULL) { buf->excobj = PyUnicodeDecodeError_Create(codec->encoding, @@ -396,6 +435,7 @@ errorexit: Py_XDECREF(retobj); return -1; +#endif } static PyObject * 1.1 cjkcodecs/src/multibytecodec_compat.h Index: multibytecodec_compat.h =================================================================== /* * multibytecodec_compat.h: 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: multibytecodec_compat.h,v 1.1 2003/06/06 05:57:53 perky Exp $ */ /* We don't support 2.0 and older */ #if PY_VERSION_HEX < 0x02010000 # error "multibytecodec requires python 2.1 or compatible" #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 are only for Python 2.3 and over */ #if PY_VERSION_HEX < 0x02030000 # define NO_ERROR_CALLBACKS 1 #endif /* Python 2.1 doesn't have sth */ #if PY_VERSION_HEX < 0x02020000 # define Py_USING_UNICODE 1 # define Py_UNICODE_SIZE 2 # define NO_PYSTRING_FROMFORMAT 1 # define OLD_STYLE_TYPE 1 # define METH_NOARGS METH_VARARGS #endif /* * ex: ts=8 sts=4 et */ |