Skip to content

Commit 5cc3575

Browse files
committed
gh-158252: Use Unicode whitespace for strptime aliases
Use the same whitespace predicate as Python for %n, %t, and trimming AM/PM indicators. Cover ASCII separators before adjacent numeric fields in the alias formats.
1 parent c4c4e20 commit 5cc3575

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎Lib/test/test_strptime.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def test_numeric_aliases(self):
5454
):
5555
self.assert_matches_python(text, fmt)
5656

57+
def test_whitespace_aliases_before_adjacent_fields(self):
58+
for separator in ('\x1c', '\x1d', '\x1e', '\x1f'):
59+
for whitespace in ('%n', '%t'):
60+
for field in ('%d', '%e'):
61+
self.assert_matches_python(f'2024{separator} 112',
62+
f'%Y{whitespace}{field}%m')
63+
for field in ('%H', '%k', '%I', '%l'):
64+
self.assert_matches_python(f'34{separator} 123',
65+
f'%M{whitespace}{field}%S')
66+
5767
def test_offsets_and_zone_names(self):
5868
offsets = ('', 'Z', '+0530', '-03:30', '+01:02:03', '-000001.000001',
5969
'+01:02:03.456789', '+000000.5', '-00:00:00.5',

‎Python/_strptime.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ strptime_name(const unsigned char *data, Py_ssize_t length, Py_ssize_t pos,
6666
}
6767
char *start = name;
6868
if (directive == 'p') {
69-
while (size && Py_ISSPACE(start[size - 1])) {
69+
while (size && Py_UNICODE_ISSPACE(start[size - 1])) {
7070
size--;
7171
}
72-
while (size && Py_ISSPACE(*start)) {
72+
while (size && Py_UNICODE_ISSPACE(*start)) {
7373
start++;
7474
size--;
7575
}
@@ -331,7 +331,7 @@ _PyTime_Strptime(PyObject *string, PyObject *format, _PyTime_StrptimeFields *fie
331331
continue;
332332
}
333333
if (c == 'n' || c == 't') {
334-
while (pos < length && Py_ISSPACE(data[pos])) {
334+
while (pos < length && Py_UNICODE_ISSPACE(data[pos])) {
335335
pos++;
336336
}
337337
continue;

0 commit comments

Comments
 (0)