]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129813, PEP 782: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) (#138830)
authorVictor Stinner <vstinner@python.org>
Sat, 13 Sep 2025 16:30:25 +0000 (18:30 +0200)
committerGitHub <noreply@github.com>
Sat, 13 Sep 2025 16:30:25 +0000 (18:30 +0200)
Replace PyBytes_FromStringAndSize(NULL, 0) with
Py_GetConstant(Py_CONSTANT_EMPTY_BYTES). Py_GetConstant() cannot
fail.

Modules/_bz2module.c
Modules/_dbmmodule.c
Modules/_lzmamodule.c
Modules/zlibmodule.c
Objects/bytesobject.c
Objects/unicodeobject.c

index 914172684158a14dc6e2c50da34112ecbed965f6..ea94f4321d74de1f2248e57e1247314a23616d62 100644 (file)
@@ -669,9 +669,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);
-    if (self->unused_data == NULL)
-        goto error;
+    self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
 
     bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
     if (catch_bz2_error(bzerror))
index 17aca2f00a13c0232e4878ebab6441857d276286..3fdcf22ffd56d0b638667bfa2d18ef62cf75643f 100644 (file)
@@ -450,10 +450,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);
-        if (default_value == NULL) {
-            return NULL;
-        }
+        default_value = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
         val.dptr = NULL;
         val.dsize = 0;
     }
index 0b0b1bc765bbc9e7538163eb303cbfd420368df0..9a49c8e539bf5be6931436cca4af7cf3bae15b5e 100644 (file)
@@ -1263,10 +1263,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));
-    if (self->unused_data == NULL) {
-        goto error;
-    }
+    Py_XSETREF(self->unused_data, Py_GetConstant(Py_CONSTANT_EMPTY_BYTES));
 
     switch (format) {
         case FORMAT_AUTO:
index 1ee14e31612860cef91e0fe8ee5c315fc823a3a4..a31a3a00a0a32f750ccf673cf8e27a6e961d6966 100644 (file)
@@ -1009,7 +1009,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);
@@ -1764,11 +1764,7 @@ zlib__ZlibDecompressor_impl(PyTypeObject *type, int wbits, PyObject *zdict)
     self->zst.zfree = PyZlib_Free;
     self->zst.next_in = NULL;
     self->zst.avail_in = 0;
-    self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
-    if (self->unused_data == NULL) {
-        Py_CLEAR(self);
-        return NULL;
-    }
+    self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
     self->lock = PyThread_allocate_lock();
     if (self->lock == NULL) {
         Py_DECREF(self);
index 728da6f1a24d6af40c1e0c1b3a6ecbad2253592a..0bfdd727c7690386ed4866ce98820fc00adce6fb 100644 (file)
@@ -2785,7 +2785,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 */
@@ -3680,7 +3680,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) {
index 4c88e4c1fdca2eb116dc61a7aa969bd6afd2f8a0..c8d2c68615e13e0023dcf59e82c282b0eb0f542a 100644 (file)
@@ -4913,7 +4913,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)
@@ -6914,7 +6914,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);
@@ -7364,7 +7364,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);
@@ -8305,7 +8305,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