Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0085a24

Browse files
committed
Closes #15973: fix a segmentation fault when comparing timezone objects.
1 parent fd296ff commit 0085a24

4 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/datetime.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,8 @@ def __getinitargs__(self):
18541854
return (self._offset, self._name)
18551855

18561856
def __eq__(self, other):
1857+
if type(other) != timezone:
1858+
return False
18571859
return self._offset == other._offset
18581860

18591861
def __hash__(self):

Lib/test/datetimetester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ def test_comparison(self):
235235
self.assertEqual(timezone(-5 * HOUR), timezone(-5 * HOUR, 'EST'))
236236
with self.assertRaises(TypeError): timezone(ZERO) < timezone(ZERO)
237237
self.assertIn(timezone(ZERO), {timezone(ZERO)})
238+
self.assertTrue(timezone(ZERO) != None)
239+
self.assertFalse(timezone(ZERO) == None)
238240

239241
def test_aware_datetime(self):
240242
# test that timezone instances can be used by datetime

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Library
3131
Extension Modules
3232
-----------------
3333

34+
- Issue #15973: Fix a segmentation fault when comparing datetime timezone
35+
objects.
36+
3437
- Issue #15977: Fix memory leak in Modules/_ssl.c when the function
3538
_set_npn_protocols() is called multiple times, thanks to Daniel Sommermann.
3639

Modules/_datetimemodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,6 +3215,12 @@ timezone_richcompare(PyDateTime_TimeZone *self,
32153215
{
32163216
if (op != Py_EQ && op != Py_NE)
32173217
Py_RETURN_NOTIMPLEMENTED;
3218+
if (Py_TYPE(other) != &PyDateTime_TimeZoneType) {
3219+
if (op == Py_EQ)
3220+
Py_RETURN_FALSE;
3221+
else
3222+
Py_RETURN_TRUE;
3223+
}
32183224
return delta_richcompare(self->offset, other->offset, op);
32193225
}
32203226

0 commit comments

Comments
 (0)