From 6c369f44aa0d36266ae2b6d8419d6fec7dfafc4d Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sat, 8 Jun 2024 11:40:10 +0000 Subject: [PATCH 1/2] Fix pure-python datetime.date.fromtimestamp --- Lib/_pydatetime.py | 2 ++ Lib/test/datetimetester.py | 6 ++++++ .../Library/2024-06-08-14-36-40.gh-issue-120268.MNpd1q.rst | 2 ++ 3 files changed, 10 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-06-08-14-36-40.gh-issue-120268.MNpd1q.rst diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index b7d569cc41740e..34ccb2da13d0f3 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -966,6 +966,8 @@ def __new__(cls, year, month=None, day=None): @classmethod def fromtimestamp(cls, t): "Construct a date from a POSIX timestamp (like time.time())." + if t is None: + raise TypeError("'NoneType' object cannot be interpreted as an integer") y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t) return cls(y, m, d) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index b80da5697ef865..d267a90f25a09c 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1336,6 +1336,12 @@ def test_insane_fromtimestamp(self): self.assertRaises(OverflowError, self.theclass.fromtimestamp, insane) + def test_fromtimestamp_with_none_arg(self): + # See gh-120268 for more details + print(self.theclass) + with self.assertRaises(TypeError): + self.theclass.fromtimestamp(None) + def test_today(self): import time diff --git a/Misc/NEWS.d/next/Library/2024-06-08-14-36-40.gh-issue-120268.MNpd1q.rst b/Misc/NEWS.d/next/Library/2024-06-08-14-36-40.gh-issue-120268.MNpd1q.rst new file mode 100644 index 00000000000000..d48d43cd047f7a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-06-08-14-36-40.gh-issue-120268.MNpd1q.rst @@ -0,0 +1,2 @@ +Prohibit passing ``None`` to pure-Python :meth:`datetime.date.fromtimestamp` +to achieve consistency with C-extension implementation. From 43827548f659fa760bf58f04edb521748e86ec7c Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sat, 8 Jun 2024 11:52:21 +0000 Subject: [PATCH 2/2] Remove accidentally added print --- Lib/test/datetimetester.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index d267a90f25a09c..28f75a803b4e04 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1338,7 +1338,6 @@ def test_insane_fromtimestamp(self): def test_fromtimestamp_with_none_arg(self): # See gh-120268 for more details - print(self.theclass) with self.assertRaises(TypeError): self.theclass.fromtimestamp(None)