summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2024-04-03 12:19:49 (GMT)
committerGitHub <noreply@github.com>2024-04-03 12:19:49 (GMT)
commit33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6 (patch)
treee9ae52885bb8f2c56a07b6b20b4ebb177ccb4ed7 /Lib/test
parent595bb496b0504429cf01a76fd1ada718d9dd25ca (diff)
downloadcpython-33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6.zip
cpython-33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6.tar.gz
cpython-33ee5cb3e92ea8798e7f1a2f3a13b92b39cee6d6.tar.bz2
GH-70647: Deprecate strptime day of month parsing without a year present to avoid leap-year bugs (GH-117107)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/datetimetester.py13
-rw-r--r--Lib/test/test_time.py8
-rw-r--r--Lib/test/test_unittest/test_assertions.py10
3 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 31fc383..c772639 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -2793,6 +2793,19 @@ class TestDateTime(TestDate):
newdate = strptime(string, format)
self.assertEqual(newdate, target, msg=reason)
+ def test_strptime_leap_year(self):
+ # GH-70647: warns if parsing a format with a day and no year.
+ with self.assertRaises(ValueError):
+ # The existing behavior that GH-70647 seeks to change.
+ self.theclass.strptime('02-29', '%m-%d')
+ with self.assertWarnsRegex(DeprecationWarning,
+ r'.*day of month without a year.*'):
+ self.theclass.strptime('03-14.159265', '%m-%d.%f')
+ with self._assertNotWarns(DeprecationWarning):
+ self.theclass.strptime('20-03-14.159265', '%y-%m-%d.%f')
+ with self._assertNotWarns(DeprecationWarning):
+ self.theclass.strptime('02-29,2024', '%m-%d,%Y')
+
def test_more_timetuple(self):
# This tests fields beyond those tested by the TestDate.test_timetuple.
t = self.theclass(2004, 12, 31, 6, 22, 33)
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index fb234b7..293799f 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -277,6 +277,8 @@ class TimeTestCase(unittest.TestCase):
'j', 'm', 'M', 'p', 'S',
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
format = '%' + directive
+ if directive == 'd':
+ format += ',%Y' # Avoid GH-70647.
strf_output = time.strftime(format, tt)
try:
time.strptime(strf_output, format)
@@ -299,6 +301,12 @@ class TimeTestCase(unittest.TestCase):
time.strptime('19', '%Y %')
self.assertIs(e.exception.__suppress_context__, True)
+ def test_strptime_leap_year(self):
+ # GH-70647: warns if parsing a format with a day and no year.
+ with self.assertWarnsRegex(DeprecationWarning,
+ r'.*day of month without a year.*'):
+ time.strptime('02-07 18:28', '%m-%d %H:%M')
+
def test_asctime(self):
time.asctime(time.gmtime(self.t))
diff --git a/Lib/test/test_unittest/test_assertions.py b/Lib/test/test_unittest/test_assertions.py
index 5c1a28e..1dec947 100644
--- a/Lib/test/test_unittest/test_assertions.py
+++ b/Lib/test/test_unittest/test_assertions.py
@@ -386,6 +386,16 @@ class TestLongMessage(unittest.TestCase):
'^UserWarning not triggered$',
'^UserWarning not triggered : oops$'])
+ def test_assertNotWarns(self):
+ def warn_future():
+ warnings.warn('xyz', FutureWarning, stacklevel=2)
+ self.assertMessagesCM('_assertNotWarns', (FutureWarning,),
+ warn_future,
+ ['^FutureWarning triggered$',
+ '^oops$',
+ '^FutureWarning triggered$',
+ '^FutureWarning triggered : oops$'])
+
def testAssertWarnsRegex(self):
# test error not raised
self.assertMessagesCM('assertWarnsRegex', (UserWarning, 'unused regex'),