Skip to content

Commit 14f935c

Browse files
cleanup: move zreplacement to own function
(as seen for the other replacements)
1 parent fd1c821 commit 14f935c

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

Modules/_datetimemodule.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,32 @@ format_utcoffset(char *buf, size_t buflen, const char *sep,
15061506
return 0;
15071507
}
15081508

1509+
static PyObject *
1510+
make_somezreplacement(PyObject *object, char *sep, PyObject *tzinfoarg)
1511+
{
1512+
char buf[100];
1513+
PyObject *tzinfo = get_tzinfo_member(object);
1514+
PyObject *replacement = PyBytes_FromStringAndSize(NULL, 0);
1515+
1516+
if (replacement == NULL)
1517+
return NULL;
1518+
if (tzinfo == Py_None || tzinfo == NULL)
1519+
return replacement;
1520+
1521+
Py_DECREF(replacement);
1522+
1523+
assert(tzinfoarg != NULL);
1524+
if (format_utcoffset(buf,
1525+
sizeof(buf),
1526+
sep,
1527+
tzinfo,
1528+
tzinfoarg) < 0)
1529+
return NULL;
1530+
1531+
replacement = PyBytes_FromStringAndSize(buf, strlen(buf));
1532+
return replacement;
1533+
}
1534+
15091535
static PyObject *
15101536
make_Zreplacement(PyObject *object, PyObject *tzinfoarg)
15111537
{
@@ -1644,26 +1670,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
16441670
replacement_p = &zreplacement;
16451671
}
16461672
if (*replacement_p == NULL) {
1647-
/* format utcoffset */
1648-
char buf[100];
1649-
PyObject *tzinfo = get_tzinfo_member(object);
1650-
*replacement_p = PyBytes_FromStringAndSize("", 0);
1651-
if (*replacement_p == NULL) goto Done;
1652-
if (tzinfo != Py_None && tzinfo != NULL) {
1653-
assert(tzinfoarg != NULL);
1654-
if (format_utcoffset(buf,
1655-
sizeof(buf),
1656-
sep,
1657-
tzinfo,
1658-
tzinfoarg) < 0)
1659-
goto Done;
1660-
Py_DECREF(*replacement_p);
1661-
*replacement_p =
1662-
PyBytes_FromStringAndSize(buf,
1663-
strlen(buf));
1664-
if (*replacement_p == NULL)
1665-
goto Done;
1666-
}
1673+
*replacement_p = make_somezreplacement(object,
1674+
sep, tzinfoarg);
1675+
if (*replacement_p == NULL)
1676+
goto Done;
16671677
}
16681678
assert(*replacement_p != NULL);
16691679
assert(PyBytes_Check(*replacement_p));

0 commit comments

Comments
 (0)