Skip to content

Commit 2cf7f86

Browse files
tiranvstinner
andauthored
bpo-46587: Skip tests if strftime does not support glibc extension (GH-31873)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent d8066b4 commit 2cf7f86

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

Lib/test/datetimetester.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,8 @@ def test_strftime_y2k(self):
16761676
# Year 42 returns '42', not padded
16771677
self.assertEqual(d.strftime("%Y"), '%d' % y)
16781678
# '0042' is obtained anyway
1679-
self.assertEqual(d.strftime("%4Y"), '%04d' % y)
1679+
if support.has_strftime_extensions:
1680+
self.assertEqual(d.strftime("%4Y"), '%04d' % y)
16801681

16811682
def test_replace(self):
16821683
cls = self.theclass

Lib/test/support/__init__.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@ def requires_subprocess():
520520
"""Used for subprocess, os.spawn calls, fd inheritance"""
521521
return unittest.skipUnless(has_subprocess_support, "requires subprocess support")
522522

523+
# Does strftime() support glibc extension like '%4Y'?
524+
try:
525+
has_strftime_extensions = time.strftime("%4Y") != "%4Y"
526+
except ValueError:
527+
has_strftime_extensions = False
523528

524529
# Define the URL of a dedicated HTTP server for the network tests.
525530
# The URL must use clear-text HTTP: no redirection to encrypted HTTPS.
@@ -769,29 +774,29 @@ def check_sizeof(test, o, size):
769774

770775
@contextlib.contextmanager
771776
def run_with_locale(catstr, *locales):
777+
try:
778+
import locale
779+
category = getattr(locale, catstr)
780+
orig_locale = locale.setlocale(category)
781+
except AttributeError:
782+
# if the test author gives us an invalid category string
783+
raise
784+
except:
785+
# cannot retrieve original locale, so do nothing
786+
locale = orig_locale = None
787+
else:
788+
for loc in locales:
772789
try:
773-
import locale
774-
category = getattr(locale, catstr)
775-
orig_locale = locale.setlocale(category)
776-
except AttributeError:
777-
# if the test author gives us an invalid category string
778-
raise
790+
locale.setlocale(category, loc)
791+
break
779792
except:
780-
# cannot retrieve original locale, so do nothing
781-
locale = orig_locale = None
782-
else:
783-
for loc in locales:
784-
try:
785-
locale.setlocale(category, loc)
786-
break
787-
except:
788-
pass
793+
pass
789794

790-
try:
791-
yield
792-
finally:
793-
if locale and orig_locale:
794-
locale.setlocale(category, orig_locale)
795+
try:
796+
yield
797+
finally:
798+
if locale and orig_locale:
799+
locale.setlocale(category, orig_locale)
795800

796801
#=======================================================================
797802
# Decorator for running a function in a specific timezone, correctly

Lib/test/test_support.py

+6
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ def test_print_warning(self):
682682
self.check_print_warning("a\nb",
683683
'Warning -- a\nWarning -- b\n')
684684

685+
def test_has_strftime_extensions(self):
686+
if support.is_emscripten or support.is_wasi or sys.platform == "win32":
687+
self.assertFalse(support.has_strftime_extensions)
688+
else:
689+
self.assertTrue(support.has_strftime_extensions)
690+
685691
# XXX -follows a list of untested API
686692
# make_legacy_pyc
687693
# is_resource_enabled

Lib/test/test_time.py

+3
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,9 @@ class _TestStrftimeYear:
622622
def yearstr(self, y):
623623
return time.strftime('%Y', (y,) + (0,) * 8)
624624

625+
@unittest.skipUnless(
626+
support.has_strftime_extensions, "requires strftime extension"
627+
)
625628
def test_4dyear(self):
626629
# Check that we can return the zero padded value.
627630
if self._format == '%04d':
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Skip tests if platform's ``strftime`` does not support non-portable glibc
2+
extensions.

0 commit comments

Comments
 (0)