Skip to content

Commit b864c26

Browse files
committed
Convert more functions
Replace PyBytes_FromStringAndSize(NULL, 0) with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
1 parent 000ba58 commit b864c26

14 files changed

Lines changed: 185 additions & 223 deletions

Modules/_bz2module.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
668668
self->bzs_avail_in_real = 0;
669669
self->input_buffer = NULL;
670670
self->input_buffer_size = 0;
671-
self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
672-
if (self->unused_data == NULL)
673-
goto error;
671+
self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
674672

675673
bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
676674
if (catch_bz2_error(bzerror))

Modules/_codecsmodule.c

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -201,52 +201,45 @@ _codecs_escape_encode_impl(PyObject *module, PyObject *data,
201201
const char *errors)
202202
/*[clinic end generated code: output=4af1d477834bab34 input=8f4b144799a94245]*/
203203
{
204-
Py_ssize_t size;
205-
Py_ssize_t newsize;
206-
PyObject *v;
207-
208-
size = PyBytes_GET_SIZE(data);
204+
Py_ssize_t size = PyBytes_GET_SIZE(data);
209205
if (size > PY_SSIZE_T_MAX / 4) {
210206
PyErr_SetString(PyExc_OverflowError,
211207
"string is too large to encode");
212208
return NULL;
213209
}
214-
newsize = 4*size;
215-
v = PyBytes_FromStringAndSize(NULL, newsize);
210+
Py_ssize_t alloc_size = 4*size;
216211

217-
if (v == NULL) {
212+
PyBytesWriter *writer = PyBytesWriter_Create(alloc_size);
213+
if (writer == NULL) {
218214
return NULL;
219215
}
220-
else {
221-
Py_ssize_t i;
222-
char c;
223-
char *p = PyBytes_AS_STRING(v);
224-
225-
for (i = 0; i < size; i++) {
226-
/* There's at least enough room for a hex escape */
227-
assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
228-
c = PyBytes_AS_STRING(data)[i];
229-
if (c == '\'' || c == '\\')
230-
*p++ = '\\', *p++ = c;
231-
else if (c == '\t')
232-
*p++ = '\\', *p++ = 't';
233-
else if (c == '\n')
234-
*p++ = '\\', *p++ = 'n';
235-
else if (c == '\r')
236-
*p++ = '\\', *p++ = 'r';
237-
else if (c < ' ' || c >= 0x7f) {
238-
*p++ = '\\';
239-
*p++ = 'x';
240-
*p++ = Py_hexdigits[(c & 0xf0) >> 4];
241-
*p++ = Py_hexdigits[c & 0xf];
242-
}
243-
else
244-
*p++ = c;
245-
}
246-
*p = '\0';
247-
if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
248-
return NULL;
216+
char *p = PyBytesWriter_GetData(writer);
217+
218+
for (Py_ssize_t i = 0; i < size; i++) {
219+
/* There's at least enough room for a hex escape */
220+
assert(alloc_size - (p - (char*)PyBytesWriter_GetData(writer)) >= 4);
221+
char c = PyBytes_AS_STRING(data)[i];
222+
if (c == '\'' || c == '\\')
223+
*p++ = '\\', *p++ = c;
224+
else if (c == '\t')
225+
*p++ = '\\', *p++ = 't';
226+
else if (c == '\n')
227+
*p++ = '\\', *p++ = 'n';
228+
else if (c == '\r')
229+
*p++ = '\\', *p++ = 'r';
230+
else if (c < ' ' || c >= 0x7f) {
231+
*p++ = '\\';
232+
*p++ = 'x';
233+
*p++ = Py_hexdigits[(c & 0xf0) >> 4];
234+
*p++ = Py_hexdigits[c & 0xf];
249235
}
236+
else
237+
*p++ = c;
238+
}
239+
240+
PyObject *v = PyBytesWriter_FinishWithPointer(writer, p);
241+
if (v == NULL) {
242+
return NULL;
250243
}
251244

252245
return codec_tuple(v, size);

Modules/_dbmmodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,7 @@ _dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key,
401401
return PyBytes_FromStringAndSize(val.dptr, val.dsize);
402402
}
403403
if (default_value == NULL) {
404-
default_value = PyBytes_FromStringAndSize(NULL, 0);
405-
if (default_value == NULL) {
406-
return NULL;
407-
}
404+
default_value = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
408405
val.dptr = NULL;
409406
val.dsize = 0;
410407
}

Modules/_hashopenssl.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -806,15 +806,15 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
806806
/*[clinic end generated code: output=ef9320c23280efad input=816a6537cea3d1db]*/
807807
{
808808
EVP_MD_CTX *temp_ctx;
809-
PyObject *retval = PyBytes_FromStringAndSize(NULL, length);
810809

811-
if (retval == NULL) {
810+
PyBytesWriter *writer = PyBytesWriter_Create(length);
811+
if (writer == NULL) {
812812
return NULL;
813813
}
814814

815815
temp_ctx = EVP_MD_CTX_new();
816816
if (temp_ctx == NULL) {
817-
Py_DECREF(retval);
817+
PyBytesWriter_Discard(writer);
818818
PyErr_NoMemory();
819819
return NULL;
820820
}
@@ -823,17 +823,17 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
823823
goto error;
824824
}
825825
if (!EVP_DigestFinalXOF(temp_ctx,
826-
(unsigned char*)PyBytes_AS_STRING(retval),
826+
(unsigned char*)PyBytesWriter_GetData(writer),
827827
length))
828828
{
829829
goto error;
830830
}
831831

832832
EVP_MD_CTX_free(temp_ctx);
833-
return retval;
833+
return PyBytesWriter_Finish(writer);
834834

835835
error:
836-
Py_DECREF(retval);
836+
PyBytesWriter_Discard(writer);
837837
EVP_MD_CTX_free(temp_ctx);
838838
notify_ssl_error_occurred();
839839
return NULL;

Modules/_lzmamodule.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,10 +1259,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
12591259
self->needs_input = 1;
12601260
self->input_buffer = NULL;
12611261
self->input_buffer_size = 0;
1262-
Py_XSETREF(self->unused_data, PyBytes_FromStringAndSize(NULL, 0));
1263-
if (self->unused_data == NULL) {
1264-
goto error;
1265-
}
1262+
Py_XSETREF(self->unused_data, Py_GetConstant(Py_CONSTANT_EMPTY_BYTES));
12661263

12671264
switch (format) {
12681265
case FORMAT_AUTO:
@@ -1441,28 +1438,28 @@ _lzma__encode_filter_properties_impl(PyObject *module, lzma_filter filter)
14411438
{
14421439
lzma_ret lzret;
14431440
uint32_t encoded_size;
1444-
PyObject *result = NULL;
1441+
PyBytesWriter *writer = NULL;
14451442
_lzma_state *state = get_lzma_state(module);
14461443
assert(state != NULL);
14471444

14481445
lzret = lzma_properties_size(&encoded_size, &filter);
14491446
if (catch_lzma_error(state, lzret))
14501447
goto error;
14511448

1452-
result = PyBytes_FromStringAndSize(NULL, encoded_size);
1453-
if (result == NULL)
1449+
writer = PyBytesWriter_Create(encoded_size);
1450+
if (writer == NULL) {
14541451
goto error;
1452+
}
14551453

1456-
lzret = lzma_properties_encode(
1457-
&filter, (uint8_t *)PyBytes_AS_STRING(result));
1454+
lzret = lzma_properties_encode(&filter, PyBytesWriter_GetData(writer));
14581455
if (catch_lzma_error(state, lzret)) {
14591456
goto error;
14601457
}
14611458

1462-
return result;
1459+
return PyBytesWriter_Finish(writer);
14631460

14641461
error:
1465-
Py_XDECREF(result);
1462+
PyBytesWriter_Discard(writer);
14661463
return NULL;
14671464
}
14681465

Modules/binascii.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,9 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
205205
/*[clinic end generated code: output=e027f8e0b0598742 input=7cafeaf73df63d1c]*/
206206
{
207207
const unsigned char *ascii_data;
208-
unsigned char *bin_data;
209208
int leftbits = 0;
210209
unsigned char this_ch;
211210
unsigned int leftchar = 0;
212-
PyObject *rv;
213211
Py_ssize_t ascii_len, bin_len;
214212
binascii_state *state;
215213

@@ -223,9 +221,11 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
223221
ascii_len--;
224222

225223
/* Allocate the buffer */
226-
if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
224+
PyBytesWriter *writer = PyBytesWriter_Create(bin_len);
225+
if (writer == NULL) {
227226
return NULL;
228-
bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
227+
}
228+
unsigned char *bin_data = PyBytesWriter_GetData(writer);
229229

230230
for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
231231
/* XXX is it really best to add NULs if there's no more data */
@@ -248,8 +248,7 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
248248
return NULL;
249249
}
250250
PyErr_SetString(state->Error, "Illegal char");
251-
Py_DECREF(rv);
252-
return NULL;
251+
goto error;
253252
}
254253
this_ch = (this_ch - ' ') & 077;
255254
}
@@ -280,11 +279,14 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
280279
return NULL;
281280
}
282281
PyErr_SetString(state->Error, "Trailing garbage");
283-
Py_DECREF(rv);
284-
return NULL;
282+
goto error;
285283
}
286284
}
287-
return rv;
285+
return PyBytesWriter_Finish(writer);
286+
287+
error:
288+
PyBytesWriter_Discard(writer);
289+
return NULL;
288290
}
289291

290292
/*[clinic input]
@@ -888,8 +890,6 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
888890
{
889891
const char* argbuf;
890892
Py_ssize_t arglen;
891-
PyObject *retval;
892-
char* retbuf;
893893
Py_ssize_t i, j;
894894
binascii_state *state;
895895

@@ -911,10 +911,11 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
911911
return NULL;
912912
}
913913

914-
retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
915-
if (!retval)
914+
PyBytesWriter *writer = PyBytesWriter_Create(arglen/2);
915+
if (writer == NULL) {
916916
return NULL;
917-
retbuf = PyBytes_AS_STRING(retval);
917+
}
918+
char *retbuf = PyBytesWriter_GetData(writer);
918919

919920
for (i=j=0; i < arglen; i += 2) {
920921
unsigned int top = _PyLong_DigitValue[Py_CHARMASK(argbuf[i])];
@@ -926,14 +927,14 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
926927
}
927928
PyErr_SetString(state->Error,
928929
"Non-hexadecimal digit found");
929-
goto finally;
930+
goto error;
930931
}
931932
retbuf[j++] = (top << 4) + bot;
932933
}
933-
return retval;
934+
return PyBytesWriter_Finish(writer);
934935

935-
finally:
936-
Py_DECREF(retval);
936+
error:
937+
PyBytesWriter_Discard(writer);
937938
return NULL;
938939
}
939940

Modules/mmapmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,14 @@ _safe_PyBytes_FromStringAndSize(char *start, size_t num_bytes) {
451451
}
452452
}
453453
else {
454-
PyObject *result = PyBytes_FromStringAndSize(NULL, num_bytes);
455-
if (result == NULL) {
454+
PyBytesWriter *writer = PyBytesWriter_Create(num_bytes);
455+
if (writer == NULL) {
456456
return NULL;
457457
}
458-
if (safe_memcpy(PyBytes_AS_STRING(result), start, num_bytes) < 0) {
459-
Py_CLEAR(result);
458+
if (safe_memcpy(PyBytesWriter_GetData(writer), start, num_bytes) < 0) {
459+
PyBytesWriter_Discard(writer);
460460
}
461-
return result;
461+
return PyBytesWriter_Finish(writer);
462462
}
463463
}
464464

0 commit comments

Comments
 (0)