]> granicus.if.org Git - python/commitdiff
bpo-37915: Fix comparison between tzinfo objects and timezone objects (GH-15390)
authorPablo Galindo <Pablogsal@gmail.com>
Thu, 22 Aug 2019 19:24:25 +0000 (20:24 +0100)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 22 Aug 2019 19:24:25 +0000 (12:24 -0700)
https://bugs.python.org/issue37915

Automerge-Triggered-By: @pablogsal
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2019-08-22-16-13-27.bpo-37915.xyoZI5.rst [new file with mode: 0644]
Modules/_datetimemodule.c

index d0101c98bc76d89d21262587d14b4672395aade6..58004870a00f17096eec9d86701d4da865cb3632 100644 (file)
@@ -413,6 +413,11 @@ class TestTimeZone(unittest.TestCase):
                 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 (file)
index 0000000..1dc9ea4
--- /dev/null
@@ -0,0 +1,3 @@
+Fix a segmentation fault that appeared when comparing instances of
+``datetime.timezone`` and ``datetime.tzinfo`` objects. Patch by Pablo
+Galindo.
index 6d28b3e511c2cab77e567329154568e20331286c..56eaccdf1723a2edaa2a242c9e5e0dda3bda2f31 100644 (file)
@@ -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
@@ -3745,7 +3746,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);