Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fe940c

Browse files
committed
Return the orginal string only if it's a real str or unicode
instance, otherwise make a copy.
1 parent 8a5e679 commit 0fe940c

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

Objects/stringobject.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,8 +2401,15 @@ string_zfill(PyStringObject *self, PyObject *args)
24012401
return NULL;
24022402

24032403
if (PyString_GET_SIZE(self) >= width) {
2404-
Py_INCREF(self);
2405-
return (PyObject*) self;
2404+
if (PyString_CheckExact(self)) {
2405+
Py_INCREF(self);
2406+
return (PyObject*) self;
2407+
}
2408+
else
2409+
return PyString_FromStringAndSize(
2410+
PyString_AS_STRING(self),
2411+
PyString_GET_SIZE(self)
2412+
);
24062413
}
24072414

24082415
fill = width - PyString_GET_SIZE(self);

Objects/unicodeobject.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4841,8 +4841,15 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
48414841
return NULL;
48424842

48434843
if (self->length >= width) {
4844-
Py_INCREF(self);
4845-
return (PyObject*) self;
4844+
if (PyUnicode_CheckExact(self)) {
4845+
Py_INCREF(self);
4846+
return (PyObject*) self;
4847+
}
4848+
else
4849+
return PyUnicode_FromUnicode(
4850+
PyUnicode_AS_UNICODE(self),
4851+
PyUnicode_GET_SIZE(self)
4852+
);
48464853
}
48474854

48484855
fill = width - self->length;

0 commit comments

Comments
 (0)