bpo-36025: Fix PyDate_FromTimestamp API (GH-11922)
In the process of converting the date.fromtimestamp function to use
argument clinic in GH-8535, the C API for PyDate_FromTimestamp was
inadvertently changed to expect a timestamp object rather than an
argument tuple.
This PR fixes this backwards-incompatible change by adding a new wrapper
function for the C API function that unwraps the argument tuple and
passes it to the underlying function.
This PR also adds tests for both PyDate_FromTimestamp and
PyDateTime_FromTimestamp to prevent any further regressions.
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 715f0ea..617bf9a 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -5942,6 +5942,41 @@
with self.subTest(arg=arg, exact=exact):
self.assertFalse(is_tzinfo(arg, exact))
+ def test_date_from_timestamp(self):
+ ts = datetime(1995, 4, 12).timestamp()
+
+ for macro in [0, 1]:
+ with self.subTest(macro=macro):
+ d = _testcapi.get_date_fromtimestamp(int(ts), macro)
+
+ self.assertEqual(d, date(1995, 4, 12))
+
+ def test_datetime_from_timestamp(self):
+ ts0 = datetime(1995, 4, 12).timestamp()
+ ts1 = datetime(1995, 4, 12, 12, 30).timestamp()
+
+ cases = [
+ ((1995, 4, 12), None, False),
+ ((1995, 4, 12), None, True),
+ ((1995, 4, 12), timezone(timedelta(hours=1)), True),
+ ((1995, 4, 12, 14, 30), None, False),
+ ((1995, 4, 12, 14, 30), None, True),
+ ((1995, 4, 12, 14, 30), timezone(timedelta(hours=1)), True),
+ ]
+
+ from_timestamp = _testcapi.get_datetime_fromtimestamp
+ for case in cases:
+ for macro in [0, 1]:
+ with self.subTest(case=case, macro=macro):
+ dtup, tzinfo, usetz = case
+ dt_orig = datetime(*dtup, tzinfo=tzinfo)
+ ts = int(dt_orig.timestamp())
+
+ dt_rt = from_timestamp(ts, tzinfo, usetz, macro)
+
+ self.assertEqual(dt_orig, dt_rt)
+
+
def load_tests(loader, standard_tests, pattern):
standard_tests.addTest(ZoneInfoCompleteTest())
return standard_tests