From 7920615acc919c84421f9262dd3d4adfdf53e412 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 22 Aug 2019 16:12:19 +0100 Subject: [PATCH 1/5] bpo-37915: Fix comparison between tzinfo objects and timezone objects --- Lib/test/datetimetester.py | 3 +++ Modules/_datetimemodule.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index d0101c98bc76d8..18b2e767bad948 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -413,6 +413,9 @@ def test_offset_boundaries(self): with self.assertRaises(ValueError): timezone(delta) + def test_comparison_with_tzinfo(self): + self.assertNotEqual(timezone.utc, tzinfo()) + self.assertNotEqual(timezone(timedelta(hours=1)), tzinfo()) ############################################################################# # Base class for testing a particular aspect of timedelta, time, date and diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 6d28b3e511c2ca..f4338bc8dc387b 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -32,6 +32,8 @@ #define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType) #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType) +#define PyTimezone_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeZoneType) +#define PyTimezone_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeZoneType) /*[clinic input] module datetime @@ -3745,7 +3747,7 @@ timezone_richcompare(PyDateTime_TimeZone *self, { if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED; - if (!PyTZInfo_Check(other)) { + if (!PyTimezone_Check(other)) { Py_RETURN_NOTIMPLEMENTED; } return delta_richcompare(self->offset, other->offset, op); From 9eac363141da83e2a61e3525251d98533370268f Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 22 Aug 2019 16:13:33 +0100 Subject: [PATCH 2/5] Add NEWS entry --- .../next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst diff --git a/Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst b/Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst new file mode 100644 index 00000000000000..546eed2934cd8a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst @@ -0,0 +1,3 @@ +Fix a segmentation fault that appear when comparing instances of +``datetime.timezone`` and ``datetime.tzinfo`` objects. Patch by Pablo +Galindo. From 0ea14fc4825b307da48db5d3b51ee0ee298ce870 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 22 Aug 2019 17:01:30 +0100 Subject: [PATCH 3/5] Update Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst Co-Authored-By: Paul Ganssle --- .../next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst b/Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst index 546eed2934cd8a..1dc9ea4b8cf8c7 100644 --- a/Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst +++ b/Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst @@ -1,3 +1,3 @@ -Fix a segmentation fault that appear when comparing instances of +Fix a segmentation fault that appeared when comparing instances of ``datetime.timezone`` and ``datetime.tzinfo`` objects. Patch by Pablo Galindo. From 6d4043e085a8fae53bda0d6b9be04f603141f9c5 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 22 Aug 2019 17:06:23 +0100 Subject: [PATCH 4/5] Add comment refering to bpo-37915 --- Lib/test/datetimetester.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 18b2e767bad948..58004870a00f17 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -414,6 +414,8 @@ def test_offset_boundaries(self): timezone(delta) def test_comparison_with_tzinfo(self): + # Constructing tzinfo objects directly should not be done by users + # and serves only to check the bug described in bpo-37915 self.assertNotEqual(timezone.utc, tzinfo()) self.assertNotEqual(timezone(timedelta(hours=1)), tzinfo()) From 83a280aa1acc5e46e29dba4d0b1da0fc5ebdb031 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 22 Aug 2019 17:09:02 +0100 Subject: [PATCH 5/5] Add only the non exact version of the check --- Modules/_datetimemodule.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index f4338bc8dc387b..56eaccdf1723a2 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -33,7 +33,6 @@ #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType) #define PyTimezone_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeZoneType) -#define PyTimezone_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeZoneType) /*[clinic input] module datetime