@@ -2146,10 +2146,9 @@ def test_backdoor_resistance(self):
21462146 # mistake) pass a string directly, which (if it's the right length)
21472147 # will get treated like a pickle, and bypass the normal sanity
21482148 # checks in the constructor. This can create insane objects.
2149- # The constructor doesn't want to burn the time to validate all
2150- # fields, but does check the month field. This stops, e.g.,
2151- # datetime.datetime('1995-03-25') from yielding an insane object.
2152- base = b'1995-03-25'
2149+ # The constructor validates fields (year, month, day, time).
2150+ # This stops, e.g., datetime.datetime('1995-03-25') from yielding an insane object.
2151+ base = b'\x07 \xe8 \x01 \x01 \x00 \x00 \x00 \x00 \x00 \x00 '
21532152 if not issubclass (self .theclass , datetime ):
21542153 base = base [:4 ]
21552154 for month_byte in b'9' , b'\0 ' , b'\r ' , b'\xff ' :
@@ -2161,11 +2160,13 @@ def test_backdoor_resistance(self):
21612160 self .theclass (bytes ([1 ] * len (base )), 'EST' )
21622161
21632162 for ord_byte in range (1 , 13 ):
2164- # This shouldn't blow up because of the month byte alone. If
2165- # the implementation changes to do more-careful checking, it may
2166- # blow up because other fields are insane.
21672163 self .theclass (base [:2 ] + bytes ([ord_byte ]) + base [3 :])
21682164
2165+ # Arbitrary ASCII strings of matching length with out-of-range fields
2166+ # (e.g. '1995' or '1995-03-25') must be rejected as invalid states.
2167+ bad_ascii = b'1995-03-25' if issubclass (self .theclass , datetime ) else b'1995'
2168+ self .assertRaises (TypeError , self .theclass , bad_ascii )
2169+
21692170 def test_valuerror_messages (self ):
21702171 pattern = re .compile (
21712172 r"(year|month|day) must be in \d+\.\.\d+, not \d+"
0 commit comments