summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorPaul Ganssle <pganssle@users.noreply.github.com>2019-04-27 19:39:40 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2019-04-27 19:39:40 (GMT)
commit4d8c8c0ad6163c24136d3419eb04f310b31f7e64 (patch)
tree445d3de23677da75d5c1d3346a45d799b4874b48 /Lib
parent5c403b203510549a3f89d138d3265c5cc0cc12af (diff)
downloadcpython-4d8c8c0ad6163c24136d3419eb04f310b31f7e64.zip
cpython-4d8c8c0ad6163c24136d3419eb04f310b31f7e64.tar.gz
cpython-4d8c8c0ad6163c24136d3419eb04f310b31f7e64.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/datetimetester.py35
1 files changed, 35 insertions, 0 deletions
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 @@ class CapiTest(unittest.TestCase):
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