diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2011-11-02 11:45:42 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2011-11-02 11:45:42 (GMT) |
commit | 9e8166843c223dbb9813462b95d39995702d4e5a (patch) | |
tree | 24034d8d11637e4300c1c055fac6d5fc5a967452 /Objects | |
parent | 2fbc1852099bd806c161fc93d15820d41e1d9793 (diff) | |
download | cpython-9e8166843c223dbb9813462b95d39995702d4e5a.zip cpython-9e8166843c223dbb9813462b95d39995702d4e5a.tar.gz cpython-9e8166843c223dbb9813462b95d39995702d4e5a.tar.bz2 |
Introduce PyObject* API for raising encode errors.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 7147f04..0a33ece 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -257,6 +257,12 @@ raise_encode_exception(PyObject **exceptionObject, const Py_UNICODE *unicode, Py_ssize_t size, Py_ssize_t startpos, Py_ssize_t endpos, const char *reason); +static void +raise_encode_exception_obj(PyObject **exceptionObject, + const char *encoding, + PyObject *unicode, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason); /* Same for linebreaks */ static unsigned char ascii_linebreak[] = { @@ -4786,9 +4792,9 @@ _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) for(k=0; k<repsize; k++) { c = prep[k]; if (0x80 <= c) { - raise_encode_exception(&exc, "utf-8", - PyUnicode_AS_UNICODE(unicode), - size, i-1, i, + raise_encode_exception_obj(&exc, "utf-8", + (PyObject*)unicode, + i-1, i, "surrogates not allowed"); goto error; } @@ -6434,6 +6440,33 @@ make_encode_exception(PyObject **exceptionObject, } } +/* This is ultimately going t replace above function. */ +static void +make_encode_exception_obj(PyObject **exceptionObject, + const char *encoding, + PyObject *unicode, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason) +{ + if (*exceptionObject == NULL) { + *exceptionObject = PyObject_CallFunction( + PyExc_UnicodeEncodeError, "sUnns", + encoding, unicode, startpos, endpos, reason); + } + else { + if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) + goto onError; + if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) + goto onError; + if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) + goto onError; + return; + onError: + Py_DECREF(*exceptionObject); + *exceptionObject = NULL; + } +} + /* raises a UnicodeEncodeError */ static void raise_encode_exception(PyObject **exceptionObject, @@ -6447,6 +6480,19 @@ raise_encode_exception(PyObject **exceptionObject, if (*exceptionObject != NULL) PyCodec_StrictErrors(*exceptionObject); } +/* This is ultimately going to replace above function. */ +static void +raise_encode_exception_obj(PyObject **exceptionObject, + const char *encoding, + PyObject *unicode, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason) +{ + make_encode_exception_obj(exceptionObject, + encoding, unicode, startpos, endpos, reason); + if (*exceptionObject != NULL) + PyCodec_StrictErrors(*exceptionObject); +} /* error handling callback helper: build arguments, call the callback and check the arguments, |