Change the treatment of positions returned by PEP293

error handers in the Unicode codecs: Negative
positions are treated as being relative to the end of
the input and out of bounds positions result in an
IndexError.

Also update the PEP and include an explanation of
this in the documentation for codecs.register_error.

Fixes a small bug in iconv_codecs: if the position
from the callback is negative *add* it to the size
instead of substracting it.

From SF patch #677429.
This commit is contained in:
Walter Dörwald 2003-01-31 17:19:08 +00:00
parent f7f4517fae
commit 2e0b18af30
5 changed files with 122 additions and 41 deletions

View file

@ -728,9 +728,11 @@ int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler
if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos))
goto onError;
if (newpos<0)
newpos = 0;
else if (newpos>insize)
newpos = insize;
newpos = insize+newpos;
if (newpos<0 || newpos>insize) {
PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", newpos);
goto onError;
}
/* need more space? (at least enough for what we
have+the replacement+the rest of the string (starting
@ -2246,9 +2248,12 @@ static PyObject *unicode_encode_call_errorhandler(const char *errors,
return NULL;
}
if (*newpos<0)
*newpos = 0;
else if (*newpos>size)
*newpos = size;
*newpos = size+*newpos;
if (*newpos<0 || *newpos>size) {
PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos);
Py_DECREF(restuple);
return NULL;
}
Py_INCREF(resunicode);
Py_DECREF(restuple);
return resunicode;
@ -3084,9 +3089,12 @@ static PyObject *unicode_translate_call_errorhandler(const char *errors,
return NULL;
}
if (*newpos<0)
*newpos = 0;
else if (*newpos>size)
*newpos = size;
*newpos = size+*newpos;
if (*newpos<0 || *newpos>size) {
PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos);
Py_DECREF(restuple);
return NULL;
}
Py_INCREF(resunicode);
Py_DECREF(restuple);
return resunicode;