@@ -5133,16 +5133,17 @@ 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-
5136+ def _make_evil_datetime_class (self ):
51395137 class EvilDatetime (self .theclass ):
5138+ sabotage = False
51405139 def __new__ (cls , * args , ** kwargs ):
5141- nonlocal call_count
5142- call_count += 1
5143- if call_count > 1 :
5140+ if cls .sabotage :
51445141 return bytearray (b'\x00 ' * 200 )
51455142 return super ().__new__ (cls , * args , ** kwargs )
5143+ return EvilDatetime
5144+
5145+ def test_fromutc_subclass_new_returns_non_datetime (self ):
5146+ EvilDatetime = self ._make_evil_datetime_class ()
51465147
51475148 class SimpleTZ (tzinfo ):
51485149 def utcoffset (self , dt ): return timedelta (hours = 1 )
@@ -5151,39 +5152,26 @@ def tzname(self, dt): return "Test"
51515152
51525153 tz = SimpleTZ ()
51535154 dt = EvilDatetime (2000 , 1 , 1 , 12 , 0 , 0 , tzinfo = tz )
5155+ EvilDatetime .sabotage = True
51545156 with self .assertRaises (TypeError ):
51555157 tz .fromutc (dt )
51565158
51575159 def test_fromutc_subclass_new_returns_non_datetime_with_delta (self ):
5158- call_count = 0
5160+ EvilDatetime = self . _make_evil_datetime_class ()
51595161
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 )
51675162 class SimpleTZ (tzinfo ):
51685163 def utcoffset (self , dt ): return timedelta (hours = 2 )
51695164 def dst (self , dt ): return timedelta (hours = 1 )
51705165 def tzname (self , dt ): return "Test"
51715166
51725167 tz = SimpleTZ ()
51735168 dt = EvilDatetime (2000 , 1 , 1 , 12 , 0 , 0 , tzinfo = tz )
5169+ EvilDatetime .sabotage = True
51745170 with self .assertRaises (TypeError ):
51755171 tz .fromutc (dt )
51765172
51775173 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 )
5174+ EvilDatetime = self ._make_evil_datetime_class ()
51875175
51885176 class SimpleTZ (tzinfo ):
51895177 def utcoffset (self , dt ): return timedelta (hours = 5 )
@@ -5192,6 +5180,7 @@ def tzname(self, dt): return "Test"
51925180
51935181 tz = SimpleTZ ()
51945182 dt = EvilDatetime (2000 , 6 , 15 , 12 , 0 , 0 , tzinfo = tz )
5183+ EvilDatetime .sabotage = True
51955184 with self .assertRaises (TypeError ):
51965185 dt .utctimetuple ()
51975186
0 commit comments