Skip to content

Commit 1b1796d

Browse files
authored
[3.7] bpo-37915: Fix comparison between tzinfo objects and timezone objects (GH-15390) (GH-15417)
https://bugs.python.org/issue37915 Automerge-Triggered-By: @pablogsal. (cherry picked from commit 4be11c0) Co-authored-by: Pablo Galindo <[email protected]>
1 parent d0da97d commit 1b1796d

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "datetime.h"
2121
#undef Py_BUILD_CORE
2222

23+
#define PyTimezone_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeZoneType)
24+
2325
/*[clinic input]
2426
module datetime
2527
class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType"
@@ -3648,7 +3650,7 @@ timezone_richcompare(PyDateTime_TimeZone *self,
36483650
{
36493651
if (op != Py_EQ && op != Py_NE)
36503652
Py_RETURN_NOTIMPLEMENTED;
3651-
if (!PyTZInfo_Check(other)) {
3653+
if (!PyTimezone_Check(other)) {
36523654
Py_RETURN_NOTIMPLEMENTED;
36533655
}
36543656
return delta_richcompare(self->offset, other->offset, op);

0 commit comments

Comments
 (0)