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

gh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_STR) (#125194)

Replace PyUnicode_New(0, 0), PyUnicode_FromString("")
and PyUnicode_FromStringAndSize("", 0)
with Py_GetConstant(Py_CONSTANT_EMPTY_STR).

files:
M Doc/includes/newtypes/custom2.c
M Doc/includes/newtypes/custom3.c
M Doc/includes/newtypes/custom4.c
M Modules/_ctypes/_ctypes.c
M Modules/_datetimemodule.c
M Modules/_elementtree.c
M Modules/_functoolsmodule.c
M Modules/_io/stringio.c
M Modules/_testcapi/datetime.c
M Modules/cjkcodecs/multibytecodec.c
M Modules/socketmodule.c
M Modules/unicodedata.c
M Objects/abstract.c
M Objects/exceptions.c
M Objects/stringlib/unicode_format.h
M Parser/pegen_errors.c
M Python/Python-tokenize.c
M Python/ceval.c
M Python/codecs.c
M Python/formatter_unicode.c
M Python/marshal.c
M Python/symtable.c

diff --git a/Doc/includes/newtypes/custom2.c b/Doc/includes/newtypes/custom2.c
index a0222b1795209b..768ce29fab9ff0 100644
--- a/Doc/includes/newtypes/custom2.c
+++ b/Doc/includes/newtypes/custom2.c
@@ -23,12 +23,12 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
     CustomObject *self;
     self = (CustomObject *) type->tp_alloc(type, 0);
     if (self != NULL) {
-        self->first = PyUnicode_FromString("");
+        self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (self->first == NULL) {
             Py_DECREF(self);
             return NULL;
         }
-        self->last = PyUnicode_FromString("");
+        self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (self->last == NULL) {
             Py_DECREF(self);
             return NULL;
diff --git a/Doc/includes/newtypes/custom3.c b/Doc/includes/newtypes/custom3.c
index 4aeebe0a7507d1..7d969adfa7c9cc 100644
--- a/Doc/includes/newtypes/custom3.c
+++ b/Doc/includes/newtypes/custom3.c
@@ -23,12 +23,12 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
     CustomObject *self;
     self = (CustomObject *) type->tp_alloc(type, 0);
     if (self != NULL) {
-        self->first = PyUnicode_FromString("");
+        self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (self->first == NULL) {
             Py_DECREF(self);
             return NULL;
         }
-        self->last = PyUnicode_FromString("");
+        self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (self->last == NULL) {
             Py_DECREF(self);
             return NULL;
diff --git a/Doc/includes/newtypes/custom4.c b/Doc/includes/newtypes/custom4.c
index 3998918f68301e..a7b8de44a57c90 100644
--- a/Doc/includes/newtypes/custom4.c
+++ b/Doc/includes/newtypes/custom4.c
@@ -39,12 +39,12 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
     CustomObject *self;
     self = (CustomObject *) type->tp_alloc(type, 0);
     if (self != NULL) {
-        self->first = PyUnicode_FromString("");
+        self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (self->first == NULL) {
             Py_DECREF(self);
             return NULL;
         }
-        self->last = PyUnicode_FromString("");
+        self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (self->last == NULL) {
             Py_DECREF(self);
             return NULL;
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 9453ed3250bc3b..8435ee4090b9e5 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -4756,7 +4756,7 @@ Array_subscript(PyObject *myself, PyObject *item)
             wchar_t *dest;
 
             if (slicelen <= 0)
-                return PyUnicode_New(0, 0);
+                return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
             if (step == 1) {
                 return PyUnicode_FromWideChar(ptr + start,
                                               slicelen);
@@ -5438,7 +5438,7 @@ Pointer_subscript(PyObject *myself, PyObject *item)
             wchar_t *dest;
 
             if (len <= 0)
-                return PyUnicode_New(0, 0);
+                return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
             if (step == 1) {
                 return PyUnicode_FromWideChar(ptr + start,
                                               len);
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 90527d2a3e0350..2ba46cddb4f558 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -2921,7 +2921,7 @@ delta_bool(PyDateTime_Delta *self)
 static PyObject *
 delta_repr(PyDateTime_Delta *self)
 {
-    PyObject *args = PyUnicode_FromString("");
+    PyObject *args = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
 
     if (args == NULL) {
         return NULL;
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index ec999582d2fb9d..e134e096e044b7 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -196,7 +196,7 @@ list_join(PyObject* list)
     PyObject* joiner;
     PyObject* result;
 
-    joiner = PyUnicode_FromStringAndSize("", 0);
+    joiner = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     if (!joiner)
         return NULL;
     result = PyUnicode_Join(joiner, list);
@@ -1317,7 +1317,7 @@ _elementtree_Element_findtext_impl(ElementObject *self, 
PyTypeObject *cls,
             PyObject* text = element_get_text((ElementObject*)item);
             if (text == Py_None) {
                 Py_DECREF(item);
-                return PyUnicode_New(0, 0);
+                return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
             }
             Py_XINCREF(text);
             Py_DECREF(item);
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 31cf7bcc09782c..4ab3adc0fe44cc 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -604,7 +604,7 @@ partial_repr(partialobject *pto)
         return PyUnicode_FromString("...");
     }
 
-    arglist = PyUnicode_FromString("");
+    arglist = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     if (arglist == NULL)
         goto done;
     /* Pack positional arguments */
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index 6d48bcb552b4bf..f558613dc6233c 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -353,7 +353,7 @@ _stringio_readline(stringio *self, Py_ssize_t limit)
 
     /* In case of overseek, return the empty string */
     if (self->pos >= self->string_size)
-        return PyUnicode_New(0, 0);
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
 
     start = self->buf + self->pos;
     if (limit < 0 || limit > self->string_size - self->pos)
diff --git a/Modules/_testcapi/datetime.c b/Modules/_testcapi/datetime.c
index f3d54215e04232..b800f9b8eb3473 100644
--- a/Modules/_testcapi/datetime.c
+++ b/Modules/_testcapi/datetime.c
@@ -129,7 +129,7 @@ static PyObject *
 get_timezones_offset_zero(PyObject *self, PyObject *args)
 {
     PyObject *offset = PyDelta_FromDSU(0, 0, 0);
-    PyObject *name = PyUnicode_FromString("");
+    PyObject *name = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     if (offset == NULL || name == NULL) {
         Py_XDECREF(offset);
         Py_XDECREF(name);
diff --git a/Modules/cjkcodecs/multibytecodec.c 
b/Modules/cjkcodecs/multibytecodec.c
index 373518673dd352..53135ae4aa7968 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -669,7 +669,7 @@ 
_multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self,
 
     if (datalen == 0) {
         ERROR_DECREF(errorcb);
-        return make_tuple(PyUnicode_New(0, 0), 0);
+        return make_tuple(Py_GetConstant(Py_CONSTANT_EMPTY_STR), 0);
     }
 
     _PyUnicodeWriter_Init(&buf.writer);
@@ -1434,7 +1434,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
     Py_ssize_t rsize;
 
     if (sizehint == 0)
-        return PyUnicode_New(0, 0);
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
 
     _PyUnicodeWriter_Init(&buf.writer);
     buf.excobj = NULL;
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index ded6f255aaddea..0829d2358129d2 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -5636,7 +5636,7 @@ socket_gethostname(PyObject *self, PyObject *unused)
         return PyErr_SetFromWindowsErr(0);
 
     if (size == 0)
-        return PyUnicode_New(0, 0);
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
 
     /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
        names */
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
index 333ffe68a454e4..60bde755d24574 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -413,7 +413,7 @@ unicodedata_UCD_decomposition_impl(PyObject *self, int chr)
     if (UCD_Check(self)) {
         const change_record *old = get_old_record(self, c);
         if (old->category_changed == 0)
-            return PyUnicode_FromString(""); /* unassigned */
+            return Py_GetConstant(Py_CONSTANT_EMPTY_STR); /* unassigned */
     }
 
     if (code < 0 || code >= 0x110000)
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 7cca81464cd112..f6647874d732f6 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -862,7 +862,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
 
     /* If no format_spec is provided, use an empty string */
     if (format_spec == NULL) {
-        empty = PyUnicode_New(0, 0);
+        empty = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         format_spec = empty;
     }
 
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index c685481b13a93a..6fbe0f197eaebf 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -154,7 +154,7 @@ BaseException_str(PyBaseExceptionObject *self)
 {
     switch (PyTuple_GET_SIZE(self->args)) {
     case 0:
-        return PyUnicode_FromString("");
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     case 1:
         return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
     default:
@@ -3001,7 +3001,7 @@ UnicodeEncodeError_str(PyObject *self)
 
     if (exc->object == NULL) {
         /* Not properly initialized. */
-        return PyUnicode_FromString("");
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     }
 
     /* Get reason and encoding as strings, which they might not be if
@@ -3123,7 +3123,7 @@ UnicodeDecodeError_str(PyObject *self)
 
     if (exc->object == NULL) {
         /* Not properly initialized. */
-        return PyUnicode_FromString("");
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     }
 
     /* Get reason and encoding as strings, which they might not be if
@@ -3224,7 +3224,7 @@ UnicodeTranslateError_str(PyObject *self)
 
     if (exc->object == NULL) {
         /* Not properly initialized. */
-        return PyUnicode_FromString("");
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     }
 
     /* Get reason as a string, which it might not be if it's been
diff --git a/Objects/stringlib/unicode_format.h 
b/Objects/stringlib/unicode_format.h
index 91c71ab5736c25..44b269ba8ceb55 100644
--- a/Objects/stringlib/unicode_format.h
+++ b/Objects/stringlib/unicode_format.h
@@ -73,7 +73,7 @@ Py_LOCAL_INLINE(PyObject *)
 SubString_new_object_or_empty(SubString *str)
 {
     if (str->str == NULL) {
-        return PyUnicode_New(0, 0);
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     }
     return SubString_new_object(str);
 }
@@ -531,7 +531,7 @@ render_field(PyObject *fieldobj, SubString *format_spec, 
_PyUnicodeWriter *write
                                                      format_spec->start,
                                                      format_spec->end);
         else
-            format_spec_object = PyUnicode_New(0, 0);
+            format_spec_object = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (format_spec_object == NULL)
             goto done;
 
diff --git a/Parser/pegen_errors.c b/Parser/pegen_errors.c
index e94a4923228d0f..6146f69912bfa3 100644
--- a/Parser/pegen_errors.c
+++ b/Parser/pegen_errors.c
@@ -276,7 +276,7 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t 
lineno)
         assert(p->tok->fp_interactive);
         // We can reach this point if the tokenizer buffers for interactive 
source have not been
         // initialized because we failed to decode the original source with 
the given locale.
-        return PyUnicode_FromStringAndSize("", 0);
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     }
 
     Py_ssize_t relative_lineno = p->starting_lineno ? lineno - 
p->starting_lineno + 1 : lineno;
@@ -359,7 +359,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject 
*errtype,
             error_line = get_error_line_from_tokenizer_buffers(p, lineno);
         }
         else {
-            error_line = PyUnicode_FromStringAndSize("", 0);
+            error_line = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         }
         if (!error_line) {
             goto error;
diff --git a/Python/Python-tokenize.c b/Python/Python-tokenize.c
index 34b4445be27f62..50ce83d18f6e73 100644
--- a/Python/Python-tokenize.c
+++ b/Python/Python-tokenize.c
@@ -263,7 +263,7 @@ tokenizeriter_next(tokenizeriterobject *it)
     }
     PyObject *str = NULL;
     if (token.start == NULL || token.end == NULL) {
-        str = PyUnicode_FromString("");
+        str = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     }
     else {
         str = PyUnicode_FromStringAndSize(token.start, token.end - 
token.start);
@@ -281,7 +281,7 @@ tokenizeriter_next(tokenizeriterobject *it)
     PyObject* line = NULL;
     int line_changed = 1;
     if (it->tok->tok_extra_tokens && is_trailing_token) {
-        line = PyUnicode_FromString("");
+        line = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     } else {
         Py_ssize_t size = it->tok->inp - line_start;
         if (size >= 1 && it->tok->implicit_newline) {
@@ -326,7 +326,7 @@ tokenizeriter_next(tokenizeriterobject *it)
         else if (type == NL) {
             if (it->tok->implicit_newline) {
                 Py_DECREF(str);
-                str = PyUnicode_FromString("");
+                str = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
             }
         }
 
diff --git a/Python/ceval.c b/Python/ceval.c
index ba5c70b25f0a89..f4e0add3034707 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1287,7 +1287,7 @@ too_many_positional(PyThreadState *tstate, PyCodeObject 
*co,
     }
     else {
         /* This will not fail. */
-        kwonly_sig = PyUnicode_FromString("");
+        kwonly_sig = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         assert(kwonly_sig != NULL);
     }
     _PyErr_Format(tstate, PyExc_TypeError,
diff --git a/Python/codecs.c b/Python/codecs.c
index 68dc232bb86163..2cb3875db35058 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -696,7 +696,7 @@ PyObject *PyCodec_IgnoreErrors(PyObject *exc)
         wrong_exception_type(exc);
         return NULL;
     }
-    return Py_BuildValue("(Nn)", PyUnicode_New(0, 0), end);
+    return Py_BuildValue("(Nn)", Py_GetConstant(Py_CONSTANT_EMPTY_STR), end);
 }
 
 
diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
index ebd67214f43042..16f711184990ac 100644
--- a/Python/formatter_unicode.c
+++ b/Python/formatter_unicode.c
@@ -740,7 +740,7 @@ get_locale_info(enum LocaleType type, LocaleInfo 
*locale_info)
         break;
     case LT_NO_LOCALE:
         locale_info->decimal_point = PyUnicode_FromOrdinal('.');
-        locale_info->thousands_sep = PyUnicode_New(0, 0);
+        locale_info->thousands_sep = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         if (!locale_info->decimal_point || !locale_info->thousands_sep)
             return -1;
         locale_info->grouping = no_grouping;
diff --git a/Python/marshal.c b/Python/marshal.c
index 3d127b4e331d0d..a280fbfd078f41 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -1226,7 +1226,7 @@ r_object(RFILE *p)
             v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
         }
         else {
-            v = PyUnicode_New(0, 0);
+            v = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
         }
         if (v == NULL)
             break;
diff --git a/Python/symtable.c b/Python/symtable.c
index 8bc9db6d7d6811..52be910c0b6a9b 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -354,7 +354,7 @@ static void _dump_symtable(PySTEntryObject* ste, PyObject* 
prefix)
 
 static void dump_symtable(PySTEntryObject* ste)
 {
-    PyObject *empty = PyUnicode_FromString("");
+    PyObject *empty = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     assert(empty != NULL);
     _dump_symtable(ste, empty);
     Py_DECREF(empty);

_______________________________________________
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