@@ -70,12 +70,34 @@ def testcommon(formatstr, args, output=None, limit=None, overflowok=False):
70
70
testformat (b_format , b_args , b_output , limit , overflowok )
71
71
testformat (ba_format , b_args , ba_output , limit , overflowok )
72
72
73
+ def test_exc (formatstr , args , exception , excmsg ):
74
+ try :
75
+ testformat (formatstr , args )
76
+ except exception as exc :
77
+ if str (exc ) == excmsg :
78
+ if verbose :
79
+ print ("yes" )
80
+ else :
81
+ if verbose : print ('no' )
82
+ print ('Unexpected ' , exception , ':' , repr (str (exc )))
83
+ except :
84
+ if verbose : print ('no' )
85
+ print ('Unexpected exception' )
86
+ raise
87
+ else :
88
+ raise TestFailed ('did not get expected exception: %s' % excmsg )
89
+
90
+ def test_exc_common (formatstr , args , exception , excmsg ):
91
+ # test str and bytes
92
+ test_exc (formatstr , args , exception , excmsg )
93
+ test_exc (formatstr .encode ('ascii' ), args , exception , excmsg )
73
94
74
95
class FormatTest (unittest .TestCase ):
75
96
76
97
def test_common_format (self ):
77
98
# test the format identifiers that work the same across
78
99
# str, bytes, and bytearrays (integer, float, oct, hex)
100
+ testcommon ("%%" , (), "%" )
79
101
testcommon ("%.1d" , (1 ,), "1" )
80
102
testcommon ("%.*d" , (sys .maxsize ,1 ), overflowok = True ) # expect overflow
81
103
testcommon ("%.100d" , (1 ,), '00000000000000000000000000000000000000'
@@ -246,6 +268,20 @@ def test_common_format(self):
246
268
testcommon ('%g' , 1.1 , '1.1' )
247
269
testcommon ('%#g' , 1.1 , '1.10000' )
248
270
271
+ if verbose :
272
+ print ('Testing exceptions' )
273
+ test_exc_common ('%' , (), ValueError , "incomplete format" )
274
+ test_exc_common ('% %s' , 1 , ValueError ,
275
+ "unsupported format character '%' (0x25) at index 2" )
276
+ test_exc_common ('%d' , '1' , TypeError ,
277
+ "%d format: a number is required, not str" )
278
+ test_exc_common ('%d' , b'1' , TypeError ,
279
+ "%d format: a number is required, not bytes" )
280
+ test_exc_common ('%x' , '1' , TypeError ,
281
+ "%x format: an integer is required, not str" )
282
+ test_exc_common ('%x' , 3.14 , TypeError ,
283
+ "%x format: an integer is required, not float" )
284
+
249
285
def test_str_format (self ):
250
286
testformat ("%r" , "\u0378 " , "'\\ u0378'" ) # non printable
251
287
testformat ("%a" , "\u0378 " , "'\\ u0378'" ) # non printable
@@ -255,29 +291,10 @@ def test_str_format(self):
255
291
# Test exception for unknown format characters, etc.
256
292
if verbose :
257
293
print ('Testing exceptions' )
258
- def test_exc (formatstr , args , exception , excmsg ):
259
- try :
260
- testformat (formatstr , args )
261
- except exception as exc :
262
- if str (exc ) == excmsg :
263
- if verbose :
264
- print ("yes" )
265
- else :
266
- if verbose : print ('no' )
267
- print ('Unexpected ' , exception , ':' , repr (str (exc )))
268
- except :
269
- if verbose : print ('no' )
270
- print ('Unexpected exception' )
271
- raise
272
- else :
273
- raise TestFailed ('did not get expected exception: %s' % excmsg )
274
294
test_exc ('abc %b' , 1 , ValueError ,
275
295
"unsupported format character 'b' (0x62) at index 5" )
276
296
#test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
277
297
# "unsupported format character '?' (0x3000) at index 5")
278
- test_exc ('%d' , '1' , TypeError , "%d format: a number is required, not str" )
279
- test_exc ('%x' , '1' , TypeError , "%x format: an integer is required, not str" )
280
- test_exc ('%x' , 3.14 , TypeError , "%x format: an integer is required, not float" )
281
298
test_exc ('%g' , '1' , TypeError , "must be real number, not str" )
282
299
test_exc ('no format' , '1' , TypeError ,
283
300
"not all arguments converted during string formatting" )
@@ -334,28 +351,6 @@ def __bytes__(self):
334
351
# Test exception for unknown format characters, etc.
335
352
if verbose :
336
353
print ('Testing exceptions' )
337
- def test_exc (formatstr , args , exception , excmsg ):
338
- try :
339
- testformat (formatstr , args )
340
- except exception as exc :
341
- if str (exc ) == excmsg :
342
- if verbose :
343
- print ("yes" )
344
- else :
345
- if verbose : print ('no' )
346
- print ('Unexpected ' , exception , ':' , repr (str (exc )))
347
- except :
348
- if verbose : print ('no' )
349
- print ('Unexpected exception' )
350
- raise
351
- else :
352
- raise TestFailed ('did not get expected exception: %s' % excmsg )
353
- test_exc (b'%d' , '1' , TypeError ,
354
- "%d format: a number is required, not str" )
355
- test_exc (b'%d' , b'1' , TypeError ,
356
- "%d format: a number is required, not bytes" )
357
- test_exc (b'%x' , 3.14 , TypeError ,
358
- "%x format: an integer is required, not float" )
359
354
test_exc (b'%g' , '1' , TypeError , "float argument required, not str" )
360
355
test_exc (b'%g' , b'1' , TypeError , "float argument required, not bytes" )
361
356
test_exc (b'no format' , 7 , TypeError ,
0 commit comments