]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-120268: Prohibit passing ``None`` to ``_pydatetime.date.fromtimestamp`...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 11 Jun 2024 09:54:20 +0000 (11:54 +0200)
committerGitHub <noreply@github.com>
Tue, 11 Jun 2024 09:54:20 +0000 (12:54 +0300)
This makes the pure Python implementation consistent with the C implementation.
(cherry picked from commit 34f5ae69fe9ab0f5b23311d5c396d0cbb5902913)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Lib/_pydatetime.py
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2024-06-08-14-36-40.gh-issue-120268.MNpd1q.rst [new file with mode: 0644]

index cd0ea900bfb9b443919d253ec9940fb52d6b4779..ad6292e1e412c44b47bbc32219960073fac47181 100644 (file)
@@ -970,6 +970,8 @@ class date:
     @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)
 
index 404894ac36b91e72b9ae9e7235470eeff3e0cc41..0528e0701fa982ce4a07ff4d2b10f4a975f8d1a1 100644 (file)
@@ -1331,6 +1331,11 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
             self.assertRaises(OverflowError, self.theclass.fromtimestamp,
                               insane)
 
+    def test_fromtimestamp_with_none_arg(self):
+        # See gh-120268 for more details
+        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 (file)
index 0000000..d48d43c
--- /dev/null
@@ -0,0 +1,2 @@
+Prohibit passing ``None`` to pure-Python :meth:`datetime.date.fromtimestamp`
+to achieve consistency with C-extension implementation.