@@ -5133,6 +5133,68 @@ def test_pickling(self):
51335133 self .assertEqual (derived .tzname (), 'cookie' )
51345134 self .assertEqual (orig .__reduce__ (), orig .__reduce_ex__ (2 ))
51355135
5136+ def test_fromutc_subclass_new_returns_non_datetime (self ):
5137+ call_count = 0
5138+
5139+ class EvilDatetime (self .theclass ):
5140+ def __new__ (cls , * args , ** kwargs ):
5141+ nonlocal call_count
5142+ call_count += 1
5143+ if call_count > 1 :
5144+ return bytearray (b'\x00 ' * 200 )
5145+ return super ().__new__ (cls , * args , ** kwargs )
5146+
5147+ class SimpleTZ (tzinfo ):
5148+ def utcoffset (self , dt ): return timedelta (hours = 1 )
5149+ def dst (self , dt ): return timedelta (hours = 1 )
5150+ def tzname (self , dt ): return "Test"
5151+
5152+ tz = SimpleTZ ()
5153+ dt = EvilDatetime (2000 , 1 , 1 , 12 , 0 , 0 , tzinfo = tz )
5154+ with self .assertRaises (TypeError ):
5155+ tz .fromutc (dt )
5156+
5157+ def test_fromutc_subclass_new_returns_non_datetime_with_delta (self ):
5158+ call_count = 0
5159+
5160+ class EvilDatetime (self .theclass ):
5161+ def __new__ (cls , * args , ** kwargs ):
5162+ nonlocal call_count
5163+ call_count += 1
5164+ if call_count > 1 :
5165+ return bytearray (b'\x00 ' * 200 )
5166+ return super ().__new__ (cls , * args , ** kwargs )
5167+ class SimpleTZ (tzinfo ):
5168+ def utcoffset (self , dt ): return timedelta (hours = 2 )
5169+ def dst (self , dt ): return timedelta (hours = 1 )
5170+ def tzname (self , dt ): return "Test"
5171+
5172+ tz = SimpleTZ ()
5173+ dt = EvilDatetime (2000 , 1 , 1 , 12 , 0 , 0 , tzinfo = tz )
5174+ with self .assertRaises (TypeError ):
5175+ tz .fromutc (dt )
5176+
5177+ def test_utctimetuple_subclass_new_returns_non_datetime (self ):
5178+ call_count = 0
5179+
5180+ class EvilDatetime (self .theclass ):
5181+ def __new__ (cls , * args , ** kwargs ):
5182+ nonlocal call_count
5183+ call_count += 1
5184+ if call_count > 1 :
5185+ return bytearray (b'\x00 ' * 200 )
5186+ return super ().__new__ (cls , * args , ** kwargs )
5187+
5188+ class SimpleTZ (tzinfo ):
5189+ def utcoffset (self , dt ): return timedelta (hours = 5 )
5190+ def dst (self , dt ): return timedelta (0 )
5191+ def tzname (self , dt ): return "Test"
5192+
5193+ tz = SimpleTZ ()
5194+ dt = EvilDatetime (2000 , 6 , 15 , 12 , 0 , 0 , tzinfo = tz )
5195+ with self .assertRaises (TypeError ):
5196+ dt .utctimetuple ()
5197+
51365198 def test_compat_unpickle (self ):
51375199 tests = [
51385200 b'cdatetime\n datetime\n '
0 commit comments