Skip to content

Commit

Permalink
pythongh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES)
Browse files Browse the repository at this point in the history
Replace PyBytes_FromStringAndSize(NULL, 0)
with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
  • Loading branch information
vstinner committed Oct 16, 2024
1 parent 37e533a commit e65dcc8
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
self->bzs_avail_in_real = 0;
self->input_buffer = NULL;
self->input_buffer_size = 0;
self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
if (self->unused_data == NULL)
goto error;

Expand Down
2 changes: 1 addition & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ make_somezreplacement(PyObject *object, char *sep, PyObject *tzinfoarg)
PyObject *tzinfo = get_tzinfo_member(object);

if (tzinfo == Py_None || tzinfo == NULL) {
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

assert(tzinfoarg != NULL);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ _dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key,
return PyBytes_FromStringAndSize(val.dptr, val.dsize);
}
if (default_value == NULL) {
default_value = PyBytes_FromStringAndSize(NULL, 0);
default_value = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
if (default_value == NULL) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ _io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
CHECK_CLOSED(self, "read of closed file")

if (n == 0)
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);

/* Return up to n bytes. If at least one byte is buffered, we
only return buffered bytes. Otherwise, we do one raw read. */
Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* tp_alloc initializes all the fields to zero. So we don't have to
initialize them here. */

self->buf = PyBytes_FromStringAndSize(NULL, 0);
self->buf = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
if (self->buf == NULL) {
Py_DECREF(self);
return PyErr_NoMemory();
Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
if (len == 0 && _buflen(self) == 0) {
/* when the result starts with ^Z we return an empty buffer */
PyMem_Free(buf);
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

if (len) {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
self->needs_input = 1;
self->input_buffer = NULL;
self->input_buffer_size = 0;
Py_XSETREF(self->unused_data, PyBytes_FromStringAndSize(NULL, 0));
Py_XSETREF(self->unused_data, Py_GetConstant(Py_CONSTANT_EMPTY_BYTES));
if (self->unused_data == NULL) {
goto error;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ blob_read_impl(pysqlite_Blob *self, int length)

assert(length >= 0);
if (length == 0) {
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

PyObject *buffer = read_multiple(self, length, self->offset);
Expand Down
2 changes: 1 addition & 1 deletion Modules/cjkcodecs/multibytecodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ multibytecodec_encode(const MultibyteCodec *codec,
datalen = PyUnicode_GET_LENGTH(text);

if (datalen == 0 && !(flags & MBENC_RESET))
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);

buf.excobj = NULL;
buf.outobj = NULL;
Expand Down
4 changes: 2 additions & 2 deletions Modules/zlibmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
/* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
doing any work at all; just return an empty string. */
if (mode == Z_NO_FLUSH) {
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

ENTER_ZLIB(self);
Expand Down Expand Up @@ -1739,7 +1739,7 @@ ZlibDecompressor__new__(PyTypeObject *cls,
self->zst.zfree = PyZlib_Free;
self->zst.next_in = NULL;
self->zst.avail_in = 0;
self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
if (self->unused_data == NULL) {
Py_CLEAR(self);
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2706,7 +2706,7 @@ bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
"errors without a string argument");
return NULL;
}
bytes = PyBytes_FromStringAndSize(NULL, 0);
bytes = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
else if (encoding != NULL) {
/* Encode via the codec registry */
Expand Down Expand Up @@ -3604,7 +3604,7 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str)
if (size == 0 && !writer->use_bytearray) {
Py_CLEAR(writer->buffer);
/* Get the empty byte string singleton */
result = PyBytes_FromStringAndSize(NULL, 0);
result = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
else if (writer->use_small_buffer) {
if (writer->use_bytearray) {
Expand Down
8 changes: 4 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4868,7 +4868,7 @@ _PyUnicode_EncodeUTF7(PyObject *str,
len = PyUnicode_GET_LENGTH(str);

if (len == 0)
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);

/* It might be possible to tighten this worst case */
if (len > PY_SSIZE_T_MAX / 8)
Expand Down Expand Up @@ -6636,7 +6636,7 @@ PyUnicode_AsUnicodeEscapeString(PyObject *unicode)

len = PyUnicode_GET_LENGTH(unicode);
if (len == 0) {
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

kind = PyUnicode_KIND(unicode);
Expand Down Expand Up @@ -7086,7 +7086,7 @@ unicode_encode_ucs1(PyObject *unicode,
/* allocate enough for a simple encoding without
replacements, if we need more, we'll resize */
if (size == 0)
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);

_PyBytesWriter_Init(&writer);
str = _PyBytesWriter_Alloc(&writer, size);
Expand Down Expand Up @@ -8031,7 +8031,7 @@ encode_code_page(int code_page,
}

if (len == 0)
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);

offset = 0;
do
Expand Down

0 comments on commit e65dcc8

Please sign in to comment.