https://github.com/python/cpython/commit/6a39e96ab8ebc1144f713988ac6fe439e4476488
commit: 6a39e96ab8ebc1144f713988ac6fe439e4476488
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2024-10-09T17:12:11+02:00
summary:

gh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) (#125195)

Replace PyBytes_FromString("") and PyBytes_FromStringAndSize("", 0)
with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).

files:
M Modules/_ctypes/_ctypes.c
M Modules/_io/textio.c
M Modules/mmapmodule.c
M Modules/zlibmodule.c
M Objects/bytesobject.c
M Parser/action_helpers.c

diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 951e6914ba67a4..9453ed3250bc3b 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -4732,7 +4732,7 @@ Array_subscript(PyObject *myself, PyObject *item)
             char *dest;
 
             if (slicelen <= 0)
-                return PyBytes_FromStringAndSize("", 0);
+                return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
             if (step == 1) {
                 return PyBytes_FromStringAndSize(ptr + start,
                                                  slicelen);
@@ -5418,7 +5418,7 @@ Pointer_subscript(PyObject *myself, PyObject *item)
             char *dest;
 
             if (len <= 0)
-                return PyBytes_FromStringAndSize("", 0);
+                return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
             if (step == 1) {
                 return PyBytes_FromStringAndSize(ptr + start,
                                                  len);
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 439e26c5271939..68d16361962412 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -559,7 +559,7 @@ 
_io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
         Py_DECREF(state);
     }
     else {
-        buffer = PyBytes_FromString("");
+        buffer = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
         flag = 0;
     }
     flag <<= 1;
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 99a85e9e49ad47..e1c26e19932664 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -486,7 +486,7 @@ mmap_read_line_method(mmap_object *self,
 
     remaining = (self->pos < self->size) ? self->size - self->pos : 0;
     if (!remaining)
-        return PyBytes_FromString("");
+        return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
     start = self->data + self->pos;
 
     if (safe_memchr(&eol, start, '\n', remaining) < 0) {
@@ -1274,7 +1274,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
 
         CHECK_VALID(NULL);
         if (slicelen <= 0)
-            return PyBytes_FromStringAndSize("", 0);
+            return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
         else if (step == 1)
             return _safe_PyBytes_FromStringAndSize(self->data + start, 
slicelen);
         else {
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index c5aaf22eeb2948..78dcce73cdaade 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -267,12 +267,12 @@ newcompobject(PyTypeObject *type)
     self->eof = 0;
     self->is_initialised = 0;
     self->zdict = NULL;
-    self->unused_data = PyBytes_FromStringAndSize("", 0);
+    self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
     if (self->unused_data == NULL) {
         Py_DECREF(self);
         return NULL;
     }
-    self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
+    self->unconsumed_tail = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
     if (self->unconsumed_tail == NULL) {
         Py_DECREF(self);
         return NULL;
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 58a4feed351707..bf58e55e100b3a 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -1638,7 +1638,7 @@ bytes_subscript(PyObject *op, PyObject* item)
                                             &stop, step);
 
         if (slicelength <= 0) {
-            return PyBytes_FromStringAndSize("", 0);
+            return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
         }
         else if (start == 0 && step == 1 &&
                  slicelength == PyBytes_GET_SIZE(self) &&
diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c
index 1972c606827cdb..24b817c6f8ff27 100644
--- a/Parser/action_helpers.c
+++ b/Parser/action_helpers.c
@@ -1541,7 +1541,7 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq 
*strings,
     }
 
     if (bytes_found) {
-        PyObject* res = PyBytes_FromString("");
+        PyObject* res = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
 
         /* Bytes literals never get a kind, but just for consistency
            since they are represented as Constant nodes, we'll mirror

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to