gh-120268: Prohibit passing `None to _pydatetime.date.fromtimestamp` (#120269)

This makes the pure Python implementation consistent with the C implementation.
This commit is contained in:
Kirill Podoprigora 2024-06-08 23:45:57 +03:00 committed by GitHub
parent 7c016deae6
commit 34f5ae69fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 0 deletions

View file

@ -966,6 +966,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)

View file

@ -1336,6 +1336,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

View file

@ -0,0 +1,2 @@
Prohibit passing ``None`` to pure-Python :meth:`datetime.date.fromtimestamp`
to achieve consistency with C-extension implementation.