Skip to content

Commit 64a61ca

Browse files
[3.13] gh-120268: Prohibit passing None to _pydatetime.date.fromtimestamp (GH-120269) (GH-120282)
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 f386cc9 commit 64a61ca

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
@@ -966,6 +966,8 @@ def __new__(cls, year, month=None, day=None):
966966
@classmethod
967967
def fromtimestamp(cls, t):
968968
"Construct a date from a POSIX timestamp (like time.time())."
969+
if t is None:
970+
raise TypeError("'NoneType' object cannot be interpreted as an integer")
969971
y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
970972
return cls(y, m, d)
971973

Lib/test/datetimetester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,11 @@ def test_insane_fromtimestamp(self):
13551355
self.assertRaises(OverflowError, self.theclass.fromtimestamp,
13561356
insane)
13571357

1358+
def test_fromtimestamp_with_none_arg(self):
1359+
# See gh-120268 for more details
1360+
with self.assertRaises(TypeError):
1361+
self.theclass.fromtimestamp(None)
1362+
13581363
def test_today(self):
13591364
import time
13601365

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)