Skip to content

Commit 827b055

Browse files
committed
Change PyUnicode_EncodeCharmap() to return bytes objects
(which simplifies the implementation a little, because bytes objects are resizable in place).
1 parent aef90f4 commit 827b055

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

Objects/unicodeobject.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,44 +3541,45 @@ static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping)
35413541
}
35423542

35433543
static int
3544-
charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize)
3544+
charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize)
35453545
{
3546-
Py_ssize_t outsize = PyString_GET_SIZE(*outobj);
3546+
Py_ssize_t outsize = PyBytes_GET_SIZE( outobj);
35473547
/* exponentially overallocate to minimize reallocations */
35483548
if (requiredsize < 2*outsize)
35493549
requiredsize = 2*outsize;
3550-
if (_PyString_Resize(outobj, requiredsize)) {
3551-
return 0;
3550+
if (PyBytes_Resize(outobj, requiredsize)) {
3551+
Py_DECREF(outobj);
3552+
return -1;
35523553
}
3553-
return 1;
3554+
return 0;
35543555
}
35553556

35563557
typedef enum charmapencode_result {
35573558
enc_SUCCESS, enc_FAILED, enc_EXCEPTION
35583559
}charmapencode_result;
35593560
/* lookup the character, put the result in the output string and adjust
3560-
various state variables. Reallocate the output string if not enough
3561+
various state variables. Resize the output bytes object if not enough
35613562
space is available. Return a new reference to the object that
35623563
was put in the output buffer, or Py_None, if the mapping was undefined
35633564
(in which case no character was written) or NULL, if a
35643565
reallocation error occurred. The caller must decref the result */
35653566
static
35663567
charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping,
3567-
PyObject **outobj, Py_ssize_t *outpos)
3568+
PyObject *outobj, Py_ssize_t *outpos)
35683569
{
35693570
PyObject *rep;
35703571
char *outstart;
3571-
Py_ssize_t outsize = PyString_GET_SIZE(*outobj);
3572+
Py_ssize_t outsize = PyBytes_GET_SIZE(outobj);
35723573

35733574
if (mapping->ob_type == &EncodingMapType) {
35743575
int res = encoding_map_lookup(c, mapping);
35753576
Py_ssize_t requiredsize = *outpos+1;
35763577
if (res == -1)
35773578
return enc_FAILED;
35783579
if (outsize<requiredsize)
3579-
if (!charmapencode_resize(outobj, outpos, requiredsize))
3580+
if (charmapencode_resize(outobj, outpos, requiredsize))
35803581
return enc_EXCEPTION;
3581-
outstart = PyString_AS_STRING(*outobj);
3582+
outstart = PyBytes_AS_STRING(outobj);
35823583
outstart[(*outpos)++] = (char)res;
35833584
return enc_SUCCESS;
35843585
}
@@ -3593,23 +3594,23 @@ charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping,
35933594
if (PyInt_Check(rep)) {
35943595
Py_ssize_t requiredsize = *outpos+1;
35953596
if (outsize<requiredsize)
3596-
if (!charmapencode_resize(outobj, outpos, requiredsize)) {
3597+
if (charmapencode_resize(outobj, outpos, requiredsize)) {
35973598
Py_DECREF(rep);
35983599
return enc_EXCEPTION;
35993600
}
3600-
outstart = PyString_AS_STRING(*outobj);
3601+
outstart = PyBytes_AS_STRING(outobj);
36013602
outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep);
36023603
}
36033604
else {
36043605
const char *repchars = PyString_AS_STRING(rep);
36053606
Py_ssize_t repsize = PyString_GET_SIZE(rep);
36063607
Py_ssize_t requiredsize = *outpos+repsize;
36073608
if (outsize<requiredsize)
3608-
if (!charmapencode_resize(outobj, outpos, requiredsize)) {
3609+
if (charmapencode_resize(outobj, outpos, requiredsize)) {
36093610
Py_DECREF(rep);
36103611
return enc_EXCEPTION;
36113612
}
3612-
outstart = PyString_AS_STRING(*outobj);
3613+
outstart = PyBytes_AS_STRING(outobj);
36133614
memcpy(outstart + *outpos, repchars, repsize);
36143615
*outpos += repsize;
36153616
}
@@ -3625,7 +3626,7 @@ int charmap_encoding_error(
36253626
const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping,
36263627
PyObject **exceptionObject,
36273628
int *known_errorHandler, PyObject **errorHandler, const char *errors,
3628-
PyObject **res, Py_ssize_t *respos)
3629+
PyObject *res, Py_ssize_t *respos)
36293630
{
36303631
PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
36313632
Py_ssize_t repsize;
@@ -3760,22 +3761,22 @@ PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p,
37603761

37613762
/* allocate enough for a simple encoding without
37623763
replacements, if we need more, we'll resize */
3763-
res = PyString_FromStringAndSize(NULL, size);
3764+
res = PyBytes_FromStringAndSize(NULL, size);
37643765
if (res == NULL)
37653766
goto onError;
37663767
if (size == 0)
37673768
return res;
37683769

37693770
while (inpos<size) {
37703771
/* try to encode it */
3771-
charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos);
3772+
charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos);
37723773
if (x==enc_EXCEPTION) /* error */
37733774
goto onError;
37743775
if (x==enc_FAILED) { /* unencodable character */
37753776
if (charmap_encoding_error(p, size, &inpos, mapping,
37763777
&exc,
37773778
&known_errorHandler, &errorHandler, errors,
3778-
&res, &respos)) {
3779+
res, &respos)) {
37793780
goto onError;
37803781
}
37813782
}
@@ -3785,8 +3786,8 @@ PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p,
37853786
}
37863787

37873788
/* Resize if we allocated to much */
3788-
if (respos<PyString_GET_SIZE(res)) {
3789-
if (_PyString_Resize(&res, respos))
3789+
if (respos<PyBytes_GET_SIZE(res)) {
3790+
if (PyBytes_Resize(res, respos))
37903791
goto onError;
37913792
}
37923793
Py_XDECREF(exc);

0 commit comments

Comments
 (0)