Skip to content

Commit e123f74

Browse files
[3.12] gh-120268: Prohibit passing None to _pydatetime.date.fromtimestamp (GH-120269) (GH-120283)
This makes the pure Python implementation consistent with the C implementation. (cherry picked from commit 34f5ae6) Co-authored-by: Kirill Podoprigora <[email protected]>
1 parent b884536 commit e123f74

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

Lib/_pydatetime.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ def __new__(cls, year, month=None, day=None):
970970
@classmethod
971971
def fromtimestamp(cls, t):
972972
"Construct a date from a POSIX timestamp (like time.time())."
973+
if t is None:
974+
raise TypeError("'NoneType' object cannot be interpreted as an integer")
973975
y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
974976
return cls(y, m, d)
975977

Lib/test/datetimetester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,11 @@ def test_insane_fromtimestamp(self):
13311331
self.assertRaises(OverflowError, self.theclass.fromtimestamp,
13321332
insane)
13331333

1334+
def test_fromtimestamp_with_none_arg(self):
1335+
# See gh-120268 for more details
1336+
with self.assertRaises(TypeError):
1337+
self.theclass.fromtimestamp(None)
1338+
13341339
def test_today(self):
13351340
import time
13361341

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prohibit passing ``None`` to pure-Python :meth:`datetime.date.fromtimestamp`
2+
to achieve consistency with C-extension implementation.

0 commit comments

Comments
 (0)