Skip to content

Commit b9a8ca0

Browse files
authored
pythongh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_STR) (python#125194)
Replace PyUnicode_New(0, 0), PyUnicode_FromString("") and PyUnicode_FromStringAndSize("", 0) with Py_GetConstant(Py_CONSTANT_EMPTY_STR).
1 parent 6a39e96 commit b9a8ca0

22 files changed

+35
-35
lines changed

Doc/includes/newtypes/custom2.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2323
CustomObject *self;
2424
self = (CustomObject *) type->tp_alloc(type, 0);
2525
if (self != NULL) {
26-
self->first = PyUnicode_FromString("");
26+
self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
2727
if (self->first == NULL) {
2828
Py_DECREF(self);
2929
return NULL;
3030
}
31-
self->last = PyUnicode_FromString("");
31+
self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
3232
if (self->last == NULL) {
3333
Py_DECREF(self);
3434
return NULL;

Doc/includes/newtypes/custom3.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2323
CustomObject *self;
2424
self = (CustomObject *) type->tp_alloc(type, 0);
2525
if (self != NULL) {
26-
self->first = PyUnicode_FromString("");
26+
self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
2727
if (self->first == NULL) {
2828
Py_DECREF(self);
2929
return NULL;
3030
}
31-
self->last = PyUnicode_FromString("");
31+
self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
3232
if (self->last == NULL) {
3333
Py_DECREF(self);
3434
return NULL;

Doc/includes/newtypes/custom4.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3939
CustomObject *self;
4040
self = (CustomObject *) type->tp_alloc(type, 0);
4141
if (self != NULL) {
42-
self->first = PyUnicode_FromString("");
42+
self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
4343
if (self->first == NULL) {
4444
Py_DECREF(self);
4545
return NULL;
4646
}
47-
self->last = PyUnicode_FromString("");
47+
self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
4848
if (self->last == NULL) {
4949
Py_DECREF(self);
5050
return NULL;

Modules/_ctypes/_ctypes.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4756,7 +4756,7 @@ Array_subscript(PyObject *myself, PyObject *item)
47564756
wchar_t *dest;
47574757

47584758
if (slicelen <= 0)
4759-
return PyUnicode_New(0, 0);
4759+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
47604760
if (step == 1) {
47614761
return PyUnicode_FromWideChar(ptr + start,
47624762
slicelen);
@@ -5438,7 +5438,7 @@ Pointer_subscript(PyObject *myself, PyObject *item)
54385438
wchar_t *dest;
54395439

54405440
if (len <= 0)
5441-
return PyUnicode_New(0, 0);
5441+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
54425442
if (step == 1) {
54435443
return PyUnicode_FromWideChar(ptr + start,
54445444
len);

Modules/_datetimemodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,7 @@ delta_bool(PyDateTime_Delta *self)
29212921
static PyObject *
29222922
delta_repr(PyDateTime_Delta *self)
29232923
{
2924-
PyObject *args = PyUnicode_FromString("");
2924+
PyObject *args = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
29252925

29262926
if (args == NULL) {
29272927
return NULL;

Modules/_elementtree.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ list_join(PyObject* list)
196196
PyObject* joiner;
197197
PyObject* result;
198198

199-
joiner = PyUnicode_FromStringAndSize("", 0);
199+
joiner = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
200200
if (!joiner)
201201
return NULL;
202202
result = PyUnicode_Join(joiner, list);
@@ -1317,7 +1317,7 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyTypeObject *cls,
13171317
PyObject* text = element_get_text((ElementObject*)item);
13181318
if (text == Py_None) {
13191319
Py_DECREF(item);
1320-
return PyUnicode_New(0, 0);
1320+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
13211321
}
13221322
Py_XINCREF(text);
13231323
Py_DECREF(item);

Modules/_functoolsmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ partial_repr(partialobject *pto)
604604
return PyUnicode_FromString("...");
605605
}
606606

607-
arglist = PyUnicode_FromString("");
607+
arglist = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
608608
if (arglist == NULL)
609609
goto done;
610610
/* Pack positional arguments */

Modules/_io/stringio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ _stringio_readline(stringio *self, Py_ssize_t limit)
353353

354354
/* In case of overseek, return the empty string */
355355
if (self->pos >= self->string_size)
356-
return PyUnicode_New(0, 0);
356+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
357357

358358
start = self->buf + self->pos;
359359
if (limit < 0 || limit > self->string_size - self->pos)

Modules/_testcapi/datetime.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static PyObject *
129129
get_timezones_offset_zero(PyObject *self, PyObject *args)
130130
{
131131
PyObject *offset = PyDelta_FromDSU(0, 0, 0);
132-
PyObject *name = PyUnicode_FromString("");
132+
PyObject *name = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
133133
if (offset == NULL || name == NULL) {
134134
Py_XDECREF(offset);
135135
Py_XDECREF(name);

Modules/cjkcodecs/multibytecodec.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ _multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self,
669669

670670
if (datalen == 0) {
671671
ERROR_DECREF(errorcb);
672-
return make_tuple(PyUnicode_New(0, 0), 0);
672+
return make_tuple(Py_GetConstant(Py_CONSTANT_EMPTY_STR), 0);
673673
}
674674

675675
_PyUnicodeWriter_Init(&buf.writer);
@@ -1434,7 +1434,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
14341434
Py_ssize_t rsize;
14351435

14361436
if (sizehint == 0)
1437-
return PyUnicode_New(0, 0);
1437+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
14381438

14391439
_PyUnicodeWriter_Init(&buf.writer);
14401440
buf.excobj = NULL;

Modules/socketmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5636,7 +5636,7 @@ socket_gethostname(PyObject *self, PyObject *unused)
56365636
return PyErr_SetFromWindowsErr(0);
56375637

56385638
if (size == 0)
5639-
return PyUnicode_New(0, 0);
5639+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
56405640

56415641
/* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
56425642
names */

Modules/unicodedata.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ unicodedata_UCD_decomposition_impl(PyObject *self, int chr)
413413
if (UCD_Check(self)) {
414414
const change_record *old = get_old_record(self, c);
415415
if (old->category_changed == 0)
416-
return PyUnicode_FromString(""); /* unassigned */
416+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR); /* unassigned */
417417
}
418418

419419
if (code < 0 || code >= 0x110000)

Objects/abstract.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
862862

863863
/* If no format_spec is provided, use an empty string */
864864
if (format_spec == NULL) {
865-
empty = PyUnicode_New(0, 0);
865+
empty = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
866866
format_spec = empty;
867867
}
868868

Objects/exceptions.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ BaseException_str(PyBaseExceptionObject *self)
154154
{
155155
switch (PyTuple_GET_SIZE(self->args)) {
156156
case 0:
157-
return PyUnicode_FromString("");
157+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
158158
case 1:
159159
return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
160160
default:
@@ -3001,7 +3001,7 @@ UnicodeEncodeError_str(PyObject *self)
30013001

30023002
if (exc->object == NULL) {
30033003
/* Not properly initialized. */
3004-
return PyUnicode_FromString("");
3004+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
30053005
}
30063006

30073007
/* Get reason and encoding as strings, which they might not be if
@@ -3123,7 +3123,7 @@ UnicodeDecodeError_str(PyObject *self)
31233123

31243124
if (exc->object == NULL) {
31253125
/* Not properly initialized. */
3126-
return PyUnicode_FromString("");
3126+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
31273127
}
31283128

31293129
/* Get reason and encoding as strings, which they might not be if
@@ -3224,7 +3224,7 @@ UnicodeTranslateError_str(PyObject *self)
32243224

32253225
if (exc->object == NULL) {
32263226
/* Not properly initialized. */
3227-
return PyUnicode_FromString("");
3227+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
32283228
}
32293229

32303230
/* Get reason as a string, which it might not be if it's been

Objects/stringlib/unicode_format.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Py_LOCAL_INLINE(PyObject *)
7373
SubString_new_object_or_empty(SubString *str)
7474
{
7575
if (str->str == NULL) {
76-
return PyUnicode_New(0, 0);
76+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
7777
}
7878
return SubString_new_object(str);
7979
}
@@ -531,7 +531,7 @@ render_field(PyObject *fieldobj, SubString *format_spec, _PyUnicodeWriter *write
531531
format_spec->start,
532532
format_spec->end);
533533
else
534-
format_spec_object = PyUnicode_New(0, 0);
534+
format_spec_object = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
535535
if (format_spec_object == NULL)
536536
goto done;
537537

Parser/pegen_errors.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
276276
assert(p->tok->fp_interactive);
277277
// We can reach this point if the tokenizer buffers for interactive source have not been
278278
// initialized because we failed to decode the original source with the given locale.
279-
return PyUnicode_FromStringAndSize("", 0);
279+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
280280
}
281281

282282
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,
359359
error_line = get_error_line_from_tokenizer_buffers(p, lineno);
360360
}
361361
else {
362-
error_line = PyUnicode_FromStringAndSize("", 0);
362+
error_line = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
363363
}
364364
if (!error_line) {
365365
goto error;

Python/Python-tokenize.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ tokenizeriter_next(tokenizeriterobject *it)
263263
}
264264
PyObject *str = NULL;
265265
if (token.start == NULL || token.end == NULL) {
266-
str = PyUnicode_FromString("");
266+
str = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
267267
}
268268
else {
269269
str = PyUnicode_FromStringAndSize(token.start, token.end - token.start);
@@ -281,7 +281,7 @@ tokenizeriter_next(tokenizeriterobject *it)
281281
PyObject* line = NULL;
282282
int line_changed = 1;
283283
if (it->tok->tok_extra_tokens && is_trailing_token) {
284-
line = PyUnicode_FromString("");
284+
line = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
285285
} else {
286286
Py_ssize_t size = it->tok->inp - line_start;
287287
if (size >= 1 && it->tok->implicit_newline) {
@@ -326,7 +326,7 @@ tokenizeriter_next(tokenizeriterobject *it)
326326
else if (type == NL) {
327327
if (it->tok->implicit_newline) {
328328
Py_DECREF(str);
329-
str = PyUnicode_FromString("");
329+
str = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
330330
}
331331
}
332332

Python/ceval.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ too_many_positional(PyThreadState *tstate, PyCodeObject *co,
12871287
}
12881288
else {
12891289
/* This will not fail. */
1290-
kwonly_sig = PyUnicode_FromString("");
1290+
kwonly_sig = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
12911291
assert(kwonly_sig != NULL);
12921292
}
12931293
_PyErr_Format(tstate, PyExc_TypeError,

Python/codecs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ PyObject *PyCodec_IgnoreErrors(PyObject *exc)
696696
wrong_exception_type(exc);
697697
return NULL;
698698
}
699-
return Py_BuildValue("(Nn)", PyUnicode_New(0, 0), end);
699+
return Py_BuildValue("(Nn)", Py_GetConstant(Py_CONSTANT_EMPTY_STR), end);
700700
}
701701

702702

Python/formatter_unicode.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
740740
break;
741741
case LT_NO_LOCALE:
742742
locale_info->decimal_point = PyUnicode_FromOrdinal('.');
743-
locale_info->thousands_sep = PyUnicode_New(0, 0);
743+
locale_info->thousands_sep = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
744744
if (!locale_info->decimal_point || !locale_info->thousands_sep)
745745
return -1;
746746
locale_info->grouping = no_grouping;

Python/marshal.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ r_object(RFILE *p)
12261226
v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
12271227
}
12281228
else {
1229-
v = PyUnicode_New(0, 0);
1229+
v = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
12301230
}
12311231
if (v == NULL)
12321232
break;

Python/symtable.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ static void _dump_symtable(PySTEntryObject* ste, PyObject* prefix)
354354

355355
static void dump_symtable(PySTEntryObject* ste)
356356
{
357-
PyObject *empty = PyUnicode_FromString("");
357+
PyObject *empty = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
358358
assert(empty != NULL);
359359
_dump_symtable(ste, empty);
360360
Py_DECREF(empty);

0 commit comments

Comments
 (0)