Skip to content

bpo-37915: Fix comparison between tzinfo objects and timezone objects #15390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a segmentation fault that appear when comparing instances of
``datetime.timezone`` and ``datetime.tzinfo`` objects. Patch by Pablo
Galindo.
4 changes: 3 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down