Skip to content

Commit 6c08f56

Browse files
committed
gh-129813, PEP 782: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES)
Replace PyBytes_FromStringAndSize(NULL, 0) with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES). Py_GetConstant() cannot fail.
1 parent adb4140 commit 6c08f56

File tree

5 files changed

+10
-19
lines changed

5 files changed

+10
-19
lines changed

Modules/_bz2module.c

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

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

Modules/_lzmamodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,10 +1263,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
12631263
self->needs_input = 1;
12641264
self->input_buffer = NULL;
12651265
self->input_buffer_size = 0;
1266-
Py_XSETREF(self->unused_data, PyBytes_FromStringAndSize(NULL, 0));
1267-
if (self->unused_data == NULL) {
1268-
goto error;
1269-
}
1266+
Py_XSETREF(self->unused_data, Py_GetConstant(Py_CONSTANT_EMPTY_BYTES));
12701267

12711268
switch (format) {
12721269
case FORMAT_AUTO:

Modules/zlibmodule.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
10091009
/* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
10101010
doing any work at all; just return an empty string. */
10111011
if (mode == Z_NO_FLUSH) {
1012-
return PyBytes_FromStringAndSize(NULL, 0);
1012+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
10131013
}
10141014

10151015
ENTER_ZLIB(self);
@@ -1764,11 +1764,7 @@ zlib__ZlibDecompressor_impl(PyTypeObject *type, int wbits, PyObject *zdict)
17641764
self->zst.zfree = PyZlib_Free;
17651765
self->zst.next_in = NULL;
17661766
self->zst.avail_in = 0;
1767-
self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
1768-
if (self->unused_data == NULL) {
1769-
Py_CLEAR(self);
1770-
return NULL;
1771-
}
1767+
self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
17721768
self->lock = PyThread_allocate_lock();
17731769
if (self->lock == NULL) {
17741770
Py_DECREF(self);

Objects/bytesobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,7 +2781,7 @@ bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
27812781
"errors without a string argument");
27822782
return NULL;
27832783
}
2784-
bytes = PyBytes_FromStringAndSize(NULL, 0);
2784+
bytes = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
27852785
}
27862786
else if (encoding != NULL) {
27872787
/* Encode via the codec registry */
@@ -3676,7 +3676,7 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str)
36763676
if (size == 0 && !writer->use_bytearray) {
36773677
Py_CLEAR(writer->buffer);
36783678
/* Get the empty byte string singleton */
3679-
result = PyBytes_FromStringAndSize(NULL, 0);
3679+
result = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
36803680
}
36813681
else if (writer->use_small_buffer) {
36823682
if (writer->use_bytearray) {

Objects/unicodeobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4913,7 +4913,7 @@ _PyUnicode_EncodeUTF7(PyObject *str,
49134913
len = PyUnicode_GET_LENGTH(str);
49144914

49154915
if (len == 0)
4916-
return PyBytes_FromStringAndSize(NULL, 0);
4916+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
49174917

49184918
/* It might be possible to tighten this worst case */
49194919
if (len > PY_SSIZE_T_MAX / 8)
@@ -6914,7 +6914,7 @@ PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
69146914

69156915
len = PyUnicode_GET_LENGTH(unicode);
69166916
if (len == 0) {
6917-
return PyBytes_FromStringAndSize(NULL, 0);
6917+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
69186918
}
69196919

69206920
kind = PyUnicode_KIND(unicode);
@@ -7364,7 +7364,7 @@ unicode_encode_ucs1(PyObject *unicode,
73647364
/* allocate enough for a simple encoding without
73657365
replacements, if we need more, we'll resize */
73667366
if (size == 0)
7367-
return PyBytes_FromStringAndSize(NULL, 0);
7367+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
73687368

73697369
_PyBytesWriter_Init(&writer);
73707370
str = _PyBytesWriter_Alloc(&writer, size);
@@ -8305,7 +8305,7 @@ encode_code_page(int code_page,
83058305
}
83068306

83078307
if (len == 0)
8308-
return PyBytes_FromStringAndSize(NULL, 0);
8308+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
83098309

83108310
offset = 0;
83118311
do

0 commit comments

Comments
 (0)