]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46587: Skip tests if strftime does not support glibc extension (GH-31873)
authorChristian Heimes <christian@python.org>
Tue, 15 Mar 2022 09:41:04 +0000 (11:41 +0200)
committerGitHub <noreply@github.com>
Tue, 15 Mar 2022 09:41:04 +0000 (10:41 +0100)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/datetimetester.py
Lib/test/support/__init__.py
Lib/test/test_support.py
Lib/test/test_time.py
Misc/NEWS.d/next/Tests/2022-03-14-17-10-35.bpo-46587.ASDsJX.rst [new file with mode: 0644]

index df3764d61b186932c93a19ef5856ad4e20a9b869..e208a29813eed3f29efb1a537cee20d9f673c8fb 100644 (file)
@@ -1676,7 +1676,8 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
                 # 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
index c7bee8be662cc0da467d7628fb627b3b9eebde3a..01bb57ec44f0c940744afd3505fa9c392e86372a 100644 (file)
@@ -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
index 1ce3c826d6b1bc8c3f170d4343ee776a2066a2c8..59e9f3a6c1c8da16f271dcfd3fcf3651b5b17872 100644 (file)
@@ -682,6 +682,12 @@ class TestSupport(unittest.TestCase):
         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
index e3d75da55cb3a610eb03455d0d0c68be7dfcee08..57011d158cd03772b3ea97e7cd83ac7a5c165f8b 100644 (file)
@@ -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 (file)
index 0000000..ebd94ab
--- /dev/null
@@ -0,0 +1,2 @@
+Skip tests if platform's ``strftime`` does not support non-portable glibc
+extensions.