@@ -303,6 +303,8 @@ def test_tzname(self):
303
303
self .assertEqual ('UTC+09:30' , timezone (9.5 * HOUR ).tzname (None ))
304
304
self .assertEqual ('UTC-00:01' , timezone (timedelta (minutes = - 1 )).tzname (None ))
305
305
self .assertEqual ('XYZ' , timezone (- 5 * HOUR , 'XYZ' ).tzname (None ))
306
+ # bpo-34482: Check that surrogates are handled properly.
307
+ self .assertEqual ('\ud800 ' , timezone (ZERO , '\ud800 ' ).tzname (None ))
306
308
307
309
# Sub-minute offsets:
308
310
self .assertEqual ('UTC+01:06:40' , timezone (timedelta (0 , 4000 )).tzname (None ))
@@ -1308,6 +1310,12 @@ def test_strftime(self):
1308
1310
except ValueError :
1309
1311
pass
1310
1312
1313
+ # bpo-34482: Check that surrogates don't cause a crash.
1314
+ try :
1315
+ t .strftime ('%y\ud800 %m' )
1316
+ except UnicodeEncodeError :
1317
+ pass
1318
+
1311
1319
#check that this standard extension works
1312
1320
t .strftime ("%f" )
1313
1321
@@ -1747,6 +1755,9 @@ def test_isoformat(self):
1747
1755
self .assertEqual (t .isoformat ('T' ), "0001-02-03T04:05:01.000123" )
1748
1756
self .assertEqual (t .isoformat (' ' ), "0001-02-03 04:05:01.000123" )
1749
1757
self .assertEqual (t .isoformat ('\x00 ' ), "0001-02-03\x00 04:05:01.000123" )
1758
+ # bpo-34482: Check that surrogates are handled properly.
1759
+ self .assertEqual (t .isoformat ('\ud800 ' ),
1760
+ "0001-02-03\ud800 04:05:01.000123" )
1750
1761
self .assertEqual (t .isoformat (timespec = 'hours' ), "0001-02-03T04" )
1751
1762
self .assertEqual (t .isoformat (timespec = 'minutes' ), "0001-02-03T04:05" )
1752
1763
self .assertEqual (t .isoformat (timespec = 'seconds' ), "0001-02-03T04:05:01" )
@@ -1755,6 +1766,8 @@ def test_isoformat(self):
1755
1766
self .assertEqual (t .isoformat (timespec = 'auto' ), "0001-02-03T04:05:01.000123" )
1756
1767
self .assertEqual (t .isoformat (sep = ' ' , timespec = 'minutes' ), "0001-02-03 04:05" )
1757
1768
self .assertRaises (ValueError , t .isoformat , timespec = 'foo' )
1769
+ # bpo-34482: Check that surrogates are handled properly.
1770
+ self .assertRaises (ValueError , t .isoformat , timespec = '\ud800 ' )
1758
1771
# str is ISO format with the separator forced to a blank.
1759
1772
self .assertEqual (str (t ), "0001-02-03 04:05:01.000123" )
1760
1773
@@ -2286,6 +2299,19 @@ def test_strptime(self):
2286
2299
self .assertIs (type (expected ), self .theclass )
2287
2300
self .assertIs (type (got ), self .theclass )
2288
2301
2302
+ # bpo-34482: Check that surrogates are handled properly.
2303
+ inputs = [
2304
+ ('2004-12-01\ud800 13:02:47.197' , '%Y-%m-%d\ud800 %H:%M:%S.%f' ),
2305
+ ('2004\ud800 12-01 13:02:47.197' , '%Y\ud800 %m-%d %H:%M:%S.%f' ),
2306
+ ('2004-12-01 13:02\ud800 47.197' , '%Y-%m-%d %H:%M\ud800 %S.%f' ),
2307
+ ]
2308
+ for string , format in inputs :
2309
+ with self .subTest (string = string , format = format ):
2310
+ expected = _strptime ._strptime_datetime (self .theclass , string ,
2311
+ format )
2312
+ got = self .theclass .strptime (string , format )
2313
+ self .assertEqual (expected , got )
2314
+
2289
2315
strptime = self .theclass .strptime
2290
2316
self .assertEqual (strptime ("+0002" , "%z" ).utcoffset (), 2 * MINUTE )
2291
2317
self .assertEqual (strptime ("-0002" , "%z" ).utcoffset (), - 2 * MINUTE )
@@ -2353,6 +2379,12 @@ def test_more_strftime(self):
2353
2379
t = t .replace (tzinfo = tz )
2354
2380
self .assertEqual (t .strftime ("%z" ), "-0200" + z )
2355
2381
2382
+ # bpo-34482: Check that surrogates don't cause a crash.
2383
+ try :
2384
+ t .strftime ('%y\ud800 %m %H\ud800 %M' )
2385
+ except UnicodeEncodeError :
2386
+ pass
2387
+
2356
2388
def test_extract (self ):
2357
2389
dt = self .theclass (2002 , 3 , 4 , 18 , 45 , 3 , 1234 )
2358
2390
self .assertEqual (dt .date (), date (2002 , 3 , 4 ))
@@ -2878,6 +2910,8 @@ def test_isoformat(self):
2878
2910
self .assertEqual (t .isoformat (timespec = 'microseconds' ), "12:34:56.123456" )
2879
2911
self .assertEqual (t .isoformat (timespec = 'auto' ), "12:34:56.123456" )
2880
2912
self .assertRaises (ValueError , t .isoformat , timespec = 'monkey' )
2913
+ # bpo-34482: Check that surrogates are handled properly.
2914
+ self .assertRaises (ValueError , t .isoformat , timespec = '\ud800 ' )
2881
2915
2882
2916
t = self .theclass (hour = 12 , minute = 34 , second = 56 , microsecond = 999500 )
2883
2917
self .assertEqual (t .isoformat (timespec = 'milliseconds' ), "12:34:56.999" )
@@ -2928,6 +2962,12 @@ def test_strftime(self):
2928
2962
# A naive object replaces %z and %Z with empty strings.
2929
2963
self .assertEqual (t .strftime ("'%z' '%Z'" ), "'' ''" )
2930
2964
2965
+ # bpo-34482: Check that surrogates don't cause a crash.
2966
+ try :
2967
+ t .strftime ('%H\ud800 %M' )
2968
+ except UnicodeEncodeError :
2969
+ pass
2970
+
2931
2971
def test_format (self ):
2932
2972
t = self .theclass (1 , 2 , 3 , 4 )
2933
2973
self .assertEqual (t .__format__ ('' ), str (t ))
0 commit comments