Skip to content

Commit

Permalink
bpo-37915: Fix comparison between tzinfo objects and timezone objects (
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal authored and websurfer5 committed Jul 20, 2020
1 parent f1c97a4 commit 6bb543d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/datetimetester.py
Expand Up @@ -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
Expand Down
@@ -0,0 +1,3 @@
Fix a segmentation fault that appeared when comparing instances of
``datetime.timezone`` and ``datetime.tzinfo`` objects. Patch by Pablo
Galindo.
3 changes: 2 additions & 1 deletion Modules/_datetimemodule.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6bb543d

Please sign in to comment.