Skip to content

Commit 5966fd6

Browse files
pablogsallisroach
authored andcommitted
bpo-37915: Fix comparison between tzinfo objects and timezone objects (pythonGH-15390)
https://bugs.python.org/issue37915 Automerge-Triggered-By: @pablogsal
1 parent 1207d64 commit 5966fd6

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/test/datetimetester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ def test_offset_boundaries(self):
413413
with self.assertRaises(ValueError):
414414
timezone(delta)
415415

416+
def test_comparison_with_tzinfo(self):
417+
# Constructing tzinfo objects directly should not be done by users
418+
# and serves only to check the bug described in bpo-37915
419+
self.assertNotEqual(timezone.utc, tzinfo())
420+
self.assertNotEqual(timezone(timedelta(hours=1)), tzinfo())
416421

417422
#############################################################################
418423
# Base class for testing a particular aspect of timedelta, time, date and
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a segmentation fault that appeared when comparing instances of
2+
``datetime.timezone`` and ``datetime.tzinfo`` objects. Patch by Pablo
3+
Galindo.

Modules/_datetimemodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
3333
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
3434

35+
#define PyTimezone_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeZoneType)
3536

3637
/*[clinic input]
3738
module datetime
@@ -3745,7 +3746,7 @@ timezone_richcompare(PyDateTime_TimeZone *self,
37453746
{
37463747
if (op != Py_EQ && op != Py_NE)
37473748
Py_RETURN_NOTIMPLEMENTED;
3748-
if (!PyTZInfo_Check(other)) {
3749+
if (!PyTimezone_Check(other)) {
37493750
Py_RETURN_NOTIMPLEMENTED;
37503751
}
37513752
return delta_richcompare(self->offset, other->offset, op);

0 commit comments

Comments
 (0)