Skip to content

Commit b38cd94

Browse files
committed
Add strptime locale cache property tests
Parse localized names while switching from C to an available non-C locale and back. Build numeric fields independently, including years below 1000, and restore the caller locale after the test.
1 parent 22ab46e commit b38cd94

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎Lib/test/test_datetime_property.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import locale
23
import sys
34
import time
45
import unittest
@@ -252,6 +253,26 @@ class DateTimeTest(unittest.TestCase):
252253
datetime_module = datetime
253254
theclass = datetime.datetime
254255

256+
@support.run_with_locale("LC_TIME", "fr_FR.UTF-8", "fr_FR.utf8",
257+
"de_DE.UTF-8", "de_DE.utf8", "en_US.UTF-8",
258+
"en_US.utf8", "en_US")
259+
@hypothesis.settings(max_examples=25)
260+
@hypothesis.given(day=st.dates(), abbreviated=st.booleans())
261+
@hypothesis.example(day=datetime.date.min, abbreviated=False)
262+
@hypothesis.example(day=datetime.date(2000, 2, 29), abbreviated=True)
263+
@hypothesis.example(day=datetime.date(2024, 8, 31), abbreviated=False)
264+
def test_locale_name_cache(self, day: datetime.date, abbreviated: bool) -> None:
265+
selected_locale = locale.setlocale(locale.LC_TIME)
266+
names = "%b|%a" if abbreviated else "%B|%A"
267+
fmt = "%Y|%d|" + names
268+
for locale_name in ("C", selected_locale, "C"):
269+
with support.run_with_locale("LC_TIME", locale_name):
270+
# Only obtain the localized names from libc. Construct years
271+
# ourselves so that this tests the full supported year range.
272+
value = f"{day.year:04}|{day.day}|{day.strftime(names)}"
273+
actual = self.theclass.strptime(value, fmt)
274+
self.assertEqual(actual.isoformat(), f"{day.isoformat()}T00:00:00")
275+
255276
# Known failure: duplicate regex group names leak re.PatternError instead
256277
# of rejecting contradictory fields with the documented ValueError.
257278
@unittest.expectedFailure

0 commit comments

Comments
 (0)