OLD | NEW |
1 from test.support import verbose, TestFailed | 1 from test.support import verbose, TestFailed |
2 import sys | 2 import sys |
3 import test.support as support | 3 import test.support as support |
4 import unittest | 4 import unittest |
5 | 5 |
6 maxsize = support.MAX_Py_ssize_t | 6 maxsize = support.MAX_Py_ssize_t |
7 | 7 |
8 # test string formatting operator (I am not sure if this is being tested | 8 # test string formatting operator (I am not sure if this is being tested |
9 # elsewhere but, surely, some of the given cases are *not* tested because | 9 # elsewhere but, surely, some of the given cases are *not* tested because |
10 # they crash python) | 10 # they crash python) |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 testformat("%x", 0x42, "42") | 209 testformat("%x", 0x42, "42") |
210 testformat("%x", -0x42, "-42") | 210 testformat("%x", -0x42, "-42") |
211 testformat("%x", 0x42, "42") | 211 testformat("%x", 0x42, "42") |
212 testformat("%x", -0x42, "-42") | 212 testformat("%x", -0x42, "-42") |
213 testformat("%x", float(0x42), "42") | 213 testformat("%x", float(0x42), "42") |
214 testformat("%o", 0o42, "42") | 214 testformat("%o", 0o42, "42") |
215 testformat("%o", -0o42, "-42") | 215 testformat("%o", -0o42, "-42") |
216 testformat("%o", 0o42, "42") | 216 testformat("%o", 0o42, "42") |
217 testformat("%o", -0o42, "-42") | 217 testformat("%o", -0o42, "-42") |
218 testformat("%o", float(0o42), "42") | 218 testformat("%o", float(0o42), "42") |
| 219 testformat("%r", "\u0370", "'\u0370'") |
| 220 testformat("%a", "\u0370", "'\\u0370'") |
219 # Test exception for unknown format characters | 221 # Test exception for unknown format characters |
220 if verbose: | 222 if verbose: |
221 print('Testing exceptions') | 223 print('Testing exceptions') |
222 def test_exc(formatstr, args, exception, excmsg): | 224 def test_exc(formatstr, args, exception, excmsg): |
223 try: | 225 try: |
224 testformat(formatstr, args) | 226 testformat(formatstr, args) |
225 except exception as exc: | 227 except exception as exc: |
226 if str(exc) == excmsg: | 228 if str(exc) == excmsg: |
227 if verbose: | 229 if verbose: |
228 print("yes") | 230 print("yes") |
229 else: | 231 else: |
230 if verbose: print('no') | 232 if verbose: print('no') |
231 print('Unexpected ', exception, ':', repr(str(exc))) | 233 print('Unexpected ', exception, ':', repr(str(exc))) |
232 except: | 234 except: |
233 if verbose: print('no') | 235 if verbose: print('no') |
234 print('Unexpected exception') | 236 print('Unexpected exception') |
235 raise | 237 raise |
236 else: | 238 else: |
237 raise TestFailed('did not get expected exception: %s' % excmsg) | 239 raise TestFailed('did not get expected exception: %s' % excmsg) |
238 test_exc('abc %a', 1, ValueError, | 240 test_exc('abc %b', 1, ValueError, |
239 "unsupported format character 'a' (0x61) at index 5") | 241 "unsupported format character 'b' (0x62) at index 5") |
240 #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, | 242 #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, |
241 # "unsupported format character '?' (0x3000) at index 5") | 243 # "unsupported format character '?' (0x3000) at index 5") |
242 test_exc('%d', '1', TypeError, "%d format: a number is required, not str
") | 244 test_exc('%d', '1', TypeError, "%d format: a number is required, not str
") |
243 test_exc('%g', '1', TypeError, "a float is required") | 245 test_exc('%g', '1', TypeError, "a float is required") |
244 test_exc('no format', '1', TypeError, | 246 test_exc('no format', '1', TypeError, |
245 "not all arguments converted during string formatting") | 247 "not all arguments converted during string formatting") |
246 test_exc('no format', '1', TypeError, | 248 test_exc('no format', '1', TypeError, |
247 "not all arguments converted during string formatting") | 249 "not all arguments converted during string formatting") |
248 | 250 |
249 if maxsize == 2**31-1: | 251 if maxsize == 2**31-1: |
250 # crashes 2.2.1 and earlier: | 252 # crashes 2.2.1 and earlier: |
251 try: | 253 try: |
252 "%*d"%(maxsize, -127) | 254 "%*d"%(maxsize, -127) |
253 except MemoryError: | 255 except MemoryError: |
254 pass | 256 pass |
255 else: | 257 else: |
256 raise TestFailed('"%*d"%(maxsize, -127) should fail') | 258 raise TestFailed('"%*d"%(maxsize, -127) should fail') |
257 | 259 |
258 def test_main(): | 260 def test_main(): |
259 support.run_unittest(FormatTest) | 261 support.run_unittest(FormatTest) |
260 | 262 |
261 | 263 |
262 if __name__ == "__main__": | 264 if __name__ == "__main__": |
263 unittest.main() | 265 unittest.main() |
OLD | NEW |