[Python-checkins] bpo-46587: Skip tests if strftime does not support glibc extension (GH-31873)

tiran webhook-mailer at python.org
Tue Mar 15 05:41:16 EDT 2022


https://github.com/python/cpython/commit/2cf7f865f099db11cc6903b334d9c376610313e8
commit: 2cf7f865f099db11cc6903b334d9c376610313e8
branch: main
author: Christian Heimes <christian at python.org>
committer: tiran <christian at python.org>
date: 2022-03-15T10:41:04+01:00
summary:

bpo-46587: Skip tests if strftime does not support glibc extension (GH-31873)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Tests/2022-03-14-17-10-35.bpo-46587.ASDsJX.rst
M Lib/test/datetimetester.py
M Lib/test/support/__init__.py
M Lib/test/test_support.py
M Lib/test/test_time.py

diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index df3764d61b186..e208a29813eed 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -1676,7 +1676,8 @@ def test_strftime_y2k(self):
                 # Year 42 returns '42', not padded
                 self.assertEqual(d.strftime("%Y"), '%d' % y)
                 # '0042' is obtained anyway
-                self.assertEqual(d.strftime("%4Y"), '%04d' % y)
+                if support.has_strftime_extensions:
+                    self.assertEqual(d.strftime("%4Y"), '%04d' % y)
 
     def test_replace(self):
         cls = self.theclass
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index c7bee8be662cc..01bb57ec44f0c 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -520,6 +520,11 @@ def requires_subprocess():
     """Used for subprocess, os.spawn calls, fd inheritance"""
     return unittest.skipUnless(has_subprocess_support, "requires subprocess support")
 
+# Does strftime() support glibc extension like '%4Y'?
+try:
+    has_strftime_extensions = time.strftime("%4Y") != "%4Y"
+except ValueError:
+    has_strftime_extensions = False
 
 # Define the URL of a dedicated HTTP server for the network tests.
 # The URL must use clear-text HTTP: no redirection to encrypted HTTPS.
@@ -769,29 +774,29 @@ def check_sizeof(test, o, size):
 
 @contextlib.contextmanager
 def run_with_locale(catstr, *locales):
+    try:
+        import locale
+        category = getattr(locale, catstr)
+        orig_locale = locale.setlocale(category)
+    except AttributeError:
+        # if the test author gives us an invalid category string
+        raise
+    except:
+        # cannot retrieve original locale, so do nothing
+        locale = orig_locale = None
+    else:
+        for loc in locales:
             try:
-                import locale
-                category = getattr(locale, catstr)
-                orig_locale = locale.setlocale(category)
-            except AttributeError:
-                # if the test author gives us an invalid category string
-                raise
+                locale.setlocale(category, loc)
+                break
             except:
-                # cannot retrieve original locale, so do nothing
-                locale = orig_locale = None
-            else:
-                for loc in locales:
-                    try:
-                        locale.setlocale(category, loc)
-                        break
-                    except:
-                        pass
+                pass
 
-            try:
-                yield
-            finally:
-                if locale and orig_locale:
-                    locale.setlocale(category, orig_locale)
+    try:
+        yield
+    finally:
+        if locale and orig_locale:
+            locale.setlocale(category, orig_locale)
 
 #=======================================================================
 # Decorator for running a function in a specific timezone, correctly
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 1ce3c826d6b1b..59e9f3a6c1c8d 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -682,6 +682,12 @@ def test_print_warning(self):
         self.check_print_warning("a\nb",
                                  'Warning -- a\nWarning -- b\n')
 
+    def test_has_strftime_extensions(self):
+        if support.is_emscripten or support.is_wasi or sys.platform == "win32":
+            self.assertFalse(support.has_strftime_extensions)
+        else:
+            self.assertTrue(support.has_strftime_extensions)
+
     # XXX -follows a list of untested API
     # make_legacy_pyc
     # is_resource_enabled
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index e3d75da55cb3a..57011d158cd03 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -622,6 +622,9 @@ class _TestStrftimeYear:
     def yearstr(self, y):
         return time.strftime('%Y', (y,) + (0,) * 8)
 
+    @unittest.skipUnless(
+        support.has_strftime_extensions, "requires strftime extension"
+    )
     def test_4dyear(self):
         # Check that we can return the zero padded value.
         if self._format == '%04d':
diff --git a/Misc/NEWS.d/next/Tests/2022-03-14-17-10-35.bpo-46587.ASDsJX.rst b/Misc/NEWS.d/next/Tests/2022-03-14-17-10-35.bpo-46587.ASDsJX.rst
new file mode 100644
index 0000000000000..ebd94abe9ca4c
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2022-03-14-17-10-35.bpo-46587.ASDsJX.rst
@@ -0,0 +1,2 @@
+Skip tests if platform's ``strftime`` does not support non-portable glibc
+extensions.



More information about the Python-checkins mailing list