From b77759e01e81d961467e7f50ef5cb0ea72371721 Mon Sep 17 00:00:00 2001 From: marat Date: Thu, 19 Feb 2026 22:44:52 +0300 Subject: [PATCH 1/2] Unify behavior of unsupported %-format specifiers --- Lib/_pydatetime.py | 10 ++++++++++ Lib/test/datetimetester.py | 6 ++++++ Modules/_datetimemodule.c | 14 ++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index b6d68f2372850a..97f359d7866aa2 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -284,6 +284,16 @@ def _wrap_strftime(object, format, timetuple): push('{:04}'.format(year)) if ch == 'F': push('-{:02}-{:02}'.format(*timetuple[1:3])) + elif ch == '-': + if i < n: + next_ch = format[i] + i += 1 + if next_ch not in 'dmHIMSjUWVy': + push('%%-' + next_ch) + else: + push('%-' + next_ch) + else: + push('%-') else: push('%') push(ch) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 97eec618932aa5..2e9cbbc01ee7b1 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -15,6 +15,7 @@ import textwrap import unittest import warnings +import platform from array import array @@ -1589,6 +1590,11 @@ def test_strftime(self): self.assertEqual(t.strftime(""), "") # SF bug #761337 self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784 + # unsupported %-format specifiers are passed through unchanged. + self.assertEqual(t.strftime("%-1"), "%-1") + self.assertEqual(t.strftime("%--"), "%--") + self.assertEqual(t.strftime("%-#"), "%-#") + self.assertRaises(TypeError, t.strftime) # needs an arg self.assertRaises(TypeError, t.strftime, "one", "two") # too many args self.assertRaises(TypeError, t.strftime, 42) # arg wrong type diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 8f64e572bd6086..f5ad660202d68a 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2003,6 +2003,20 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, } continue; } + else if (ch == '-' && i < flen) { + Py_UCS4 next_ch = PyUnicode_READ_CHAR(format, i); + i++; + + if (strchr("dmHIMSjUWVy", (int)next_ch) == NULL) { + replacement = PyUnicode_FromFormat("%%%%-%c", (char)next_ch); + if (replacement == NULL) { + goto Error; + } + } + else { + continue; + } + } else { /* percent followed by something else */ continue; From 4b09c8e3f8c19f25d028900657110eb6d4b5048c Mon Sep 17 00:00:00 2001 From: marat Date: Thu, 19 Feb 2026 23:01:18 +0300 Subject: [PATCH 2/2] Remove platform from tests --- Lib/test/datetimetester.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 2e9cbbc01ee7b1..1edec59d71764c 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -15,7 +15,6 @@ import textwrap import unittest import warnings -import platform from array import array