[Python-checkins] bpo-37915: Fix comparison between tzinfo objects and timezone objects (GH-15390)

Miss Islington (bot) webhook-mailer at python.org
Fri Aug 23 04:48:46 EDT 2019


https://github.com/python/cpython/commit/5c77730300c0358d7bebd2bb39ea5d10222a3d9a
commit: 5c77730300c0358d7bebd2bb39ea5d10222a3d9a
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-08-23T01:48:40-07:00
summary:

bpo-37915: Fix comparison between tzinfo objects and timezone objects (GH-15390)


https://bugs.python.org/issue37915

Automerge-Triggered-By: @pablogsal
(cherry picked from commit 4be11c009abe88175fa164b45e4838e7267dfa97)

Co-authored-by: Pablo Galindo <Pablogsal at gmail.com>

files:
A Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst
M Lib/test/datetimetester.py
M Modules/_datetimemodule.c

diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index d0101c98bc76..58004870a00f 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -413,6 +413,11 @@ def test_offset_boundaries(self):
                 with self.assertRaises(ValueError):
                     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())
 
 #############################################################################
 # Base class for testing a particular aspect of timedelta, time, date and
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 000000000000..1dc9ea4b8cf8
--- /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 appeared when comparing instances of
+``datetime.timezone`` and ``datetime.tzinfo`` objects. Patch by Pablo
+Galindo.
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index beb1c3cfbca9..e6abfc2cb223 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -32,6 +32,7 @@
 #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)
 
 /*[clinic input]
 module datetime
@@ -3748,7 +3749,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);



More information about the Python-checkins mailing list