diff options
author | Christian Heimes <christian@python.org> | 2022-03-15 09:41:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-15 09:41:04 (GMT) |
commit | 2cf7f865f099db11cc6903b334d9c376610313e8 (patch) | |
tree | bd2f0b9fe770e4d68b34d97a2ade8aee6cb63144 | |
parent | d8066b420b888591f485d132e62979d07abfc3f4 (diff) | |
download | cpython-2cf7f865f099db11cc6903b334d9c376610313e8.zip cpython-2cf7f865f099db11cc6903b334d9c376610313e8.tar.gz cpython-2cf7f865f099db11cc6903b334d9c376610313e8.tar.bz2 |
bpo-46587: Skip tests if strftime does not support glibc extension (GH-31873)
Co-authored-by: Victor Stinner <vstinner@python.org>
-rw-r--r-- | Lib/test/datetimetester.py | 3 | ||||
-rw-r--r-- | Lib/test/support/__init__.py | 45 | ||||
-rw-r--r-- | Lib/test/test_support.py | 6 | ||||
-rw-r--r-- | Lib/test/test_time.py | 3 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Tests/2022-03-14-17-10-35.bpo-46587.ASDsJX.rst | 2 |
5 files changed, 38 insertions, 21 deletions
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index df3764d..e208a29 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -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 diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c7bee8b..01bb57e 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 1ce3c82..59e9f3a 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -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 diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index e3d75da..57011d1 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 0000000..ebd94ab --- /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. |