From 827b055ffe8060ac229cda8d75eb24176cc697c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20D=C3=B6rwald?= Date: Sat, 12 May 2007 13:23:53 +0000 Subject: Change PyUnicode_EncodeCharmap() to return bytes objects (which simplifies the implementation a little, because bytes objects are resizable in place). --- Objects/unicodeobject.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index df84eb1..7e455a5 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3541,34 +3541,35 @@ static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) } static int -charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) +charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) { - Py_ssize_t outsize = PyString_GET_SIZE(*outobj); + Py_ssize_t outsize = PyBytes_GET_SIZE( outobj); /* exponentially overallocate to minimize reallocations */ if (requiredsize < 2*outsize) requiredsize = 2*outsize; - if (_PyString_Resize(outobj, requiredsize)) { - return 0; + if (PyBytes_Resize(outobj, requiredsize)) { + Py_DECREF(outobj); + return -1; } - return 1; + return 0; } typedef enum charmapencode_result { enc_SUCCESS, enc_FAILED, enc_EXCEPTION }charmapencode_result; /* lookup the character, put the result in the output string and adjust - various state variables. Reallocate the output string if not enough + various state variables. Resize the output bytes object if not enough space is available. Return a new reference to the object that was put in the output buffer, or Py_None, if the mapping was undefined (in which case no character was written) or NULL, if a reallocation error occurred. The caller must decref the result */ static charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, - PyObject **outobj, Py_ssize_t *outpos) + PyObject *outobj, Py_ssize_t *outpos) { PyObject *rep; char *outstart; - Py_ssize_t outsize = PyString_GET_SIZE(*outobj); + Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); if (mapping->ob_type == &EncodingMapType) { int res = encoding_map_lookup(c, mapping); @@ -3576,9 +3577,9 @@ charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, if (res == -1) return enc_FAILED; if (outsize