Skip to content

Commit 6a39e96

Browse files
authored
gh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) (#125195)
Replace PyBytes_FromString("") and PyBytes_FromStringAndSize("", 0) with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
1 parent 3ee474f commit 6a39e96

File tree

6 files changed

+9
-9
lines changed

6 files changed

+9
-9
lines changed

Modules/_ctypes/_ctypes.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4732,7 +4732,7 @@ Array_subscript(PyObject *myself, PyObject *item)
47324732
char *dest;
47334733

47344734
if (slicelen <= 0)
4735-
return PyBytes_FromStringAndSize("", 0);
4735+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
47364736
if (step == 1) {
47374737
return PyBytes_FromStringAndSize(ptr + start,
47384738
slicelen);
@@ -5418,7 +5418,7 @@ Pointer_subscript(PyObject *myself, PyObject *item)
54185418
char *dest;
54195419

54205420
if (len <= 0)
5421-
return PyBytes_FromStringAndSize("", 0);
5421+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
54225422
if (step == 1) {
54235423
return PyBytes_FromStringAndSize(ptr + start,
54245424
len);

Modules/_io/textio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ _io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
559559
Py_DECREF(state);
560560
}
561561
else {
562-
buffer = PyBytes_FromString("");
562+
buffer = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
563563
flag = 0;
564564
}
565565
flag <<= 1;

Modules/mmapmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ mmap_read_line_method(mmap_object *self,
486486

487487
remaining = (self->pos < self->size) ? self->size - self->pos : 0;
488488
if (!remaining)
489-
return PyBytes_FromString("");
489+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
490490
start = self->data + self->pos;
491491

492492
if (safe_memchr(&eol, start, '\n', remaining) < 0) {
@@ -1274,7 +1274,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
12741274

12751275
CHECK_VALID(NULL);
12761276
if (slicelen <= 0)
1277-
return PyBytes_FromStringAndSize("", 0);
1277+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
12781278
else if (step == 1)
12791279
return _safe_PyBytes_FromStringAndSize(self->data + start, slicelen);
12801280
else {

Modules/zlibmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ newcompobject(PyTypeObject *type)
267267
self->eof = 0;
268268
self->is_initialised = 0;
269269
self->zdict = NULL;
270-
self->unused_data = PyBytes_FromStringAndSize("", 0);
270+
self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
271271
if (self->unused_data == NULL) {
272272
Py_DECREF(self);
273273
return NULL;
274274
}
275-
self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
275+
self->unconsumed_tail = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
276276
if (self->unconsumed_tail == NULL) {
277277
Py_DECREF(self);
278278
return NULL;

Objects/bytesobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ bytes_subscript(PyObject *op, PyObject* item)
16381638
&stop, step);
16391639

16401640
if (slicelength <= 0) {
1641-
return PyBytes_FromStringAndSize("", 0);
1641+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
16421642
}
16431643
else if (start == 0 && step == 1 &&
16441644
slicelength == PyBytes_GET_SIZE(self) &&

Parser/action_helpers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
15411541
}
15421542

15431543
if (bytes_found) {
1544-
PyObject* res = PyBytes_FromString("");
1544+
PyObject* res = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
15451545

15461546
/* Bytes literals never get a kind, but just for consistency
15471547
since they are represented as Constant nodes, we'll mirror

0 commit comments

Comments
 (0)