Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34f5ae6

Browse files
authored
gh-120268: Prohibit passing None to _pydatetime.date.fromtimestamp (#120269)
This makes the pure Python implementation consistent with the C implementation.
1 parent 7c016de commit 34f5ae6

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

Lib/_pydatetime.py

+2
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

+5
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,11 @@ def test_insane_fromtimestamp(self):
13361336
self.assertRaises(OverflowError, self.theclass.fromtimestamp,
13371337
insane)
13381338

1339+
def test_fromtimestamp_with_none_arg(self):
1340+
# See gh-120268 for more details
1341+
with self.assertRaises(TypeError):
1342+
self.theclass.fromtimestamp(None)
1343+
13391344
def test_today(self):
13401345
import time
13411346

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)