Skip to content

Commit

Permalink
[3.13] gh-120268: Prohibit passing None to ``_pydatetime.date.fro…
Browse files Browse the repository at this point in the history
…mtimestamp`` (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]>
  • Loading branch information
miss-islington and Eclips4 authored Jun 11, 2024
1 parent f386cc9 commit 64a61ca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,11 @@ 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
with self.assertRaises(TypeError):
self.theclass.fromtimestamp(None)

def test_today(self):
import time

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Prohibit passing ``None`` to pure-Python :meth:`datetime.date.fromtimestamp`
to achieve consistency with C-extension implementation.

0 comments on commit 64a61ca

Please sign in to comment.