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

Skip to content
Open
Prev Previous commit
Next Next commit
Make replacement less complex
  • Loading branch information
marat committed Sep 26, 2025
commit d5f2014681d7d9d8eb2a692388ba9993d043e2a3
20 changes: 6 additions & 14 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,7 @@
PyObject *colonzreplacement = NULL; /* py string, replacement for %:z */
PyObject *Zreplacement = NULL; /* py string, replacement for %Z */
PyObject *freplacement = NULL; /* py string, replacement for %f */
PyObject *dash_replacement = NULL; /* py string, replacement for %- */

assert(object && format && timetuple);
assert(PyUnicode_Check(format));
Expand Down Expand Up @@ -1959,7 +1960,7 @@
i++;
/* A % has been seen and ch is the character after it. */
PyObject *replacement = NULL;
int need_decref_replacement = 0;

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Check warning on line 1963 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

unused variable ‘need_decref_replacement’ [-Wunused-variable]

Comment thread
mhsmith marked this conversation as resolved.
Outdated
if (ch == 'z') {
/* %z -> +HHMM */
Expand Down Expand Up @@ -2058,13 +2059,12 @@
Py_UCS4 next_ch = PyUnicode_READ_CHAR(format, i);
i++;

PyObject *tmp = make_dash_replacement(object, next_ch, timetuple);
if (tmp == NULL) {
Py_XDECREF(dash_replacement);
dash_replacement = make_dash_replacement(object, next_ch, timetuple);
if (dash_replacement == NULL) {
goto Error;
}

replacement = tmp;
need_decref_replacement = 1;
replacement = dash_replacement;
}
#endif
else {
Expand All @@ -2074,21 +2074,12 @@
assert(replacement != NULL);
assert(PyUnicode_Check(replacement));
if (PyUnicodeWriter_WriteSubstring(writer, format, start, end) < 0) {
if (need_decref_replacement) {
Py_DECREF(replacement);
}
goto Error;
}
start = i;
if (PyUnicodeWriter_WriteStr(writer, replacement) < 0) {
if (need_decref_replacement) {
Py_DECREF(replacement);
}
goto Error;
}
if (need_decref_replacement) {
Py_DECREF(replacement);
}
} /* end while() */

PyObject *newformat;
Expand All @@ -2115,6 +2106,7 @@
Py_XDECREF(colonzreplacement);
Py_XDECREF(Zreplacement);
Py_XDECREF(strftime);
Py_XDECREF(dash_replacement);
Comment thread
mhsmith marked this conversation as resolved.
return result;

Error:
Expand Down
Loading