| OLD | NEW |
| 1 # Copyright 2007 Google, Inc. All Rights Reserved. | 1 # Copyright 2007 Google, Inc. All Rights Reserved. |
| 2 # Licensed to PSF under a Contributor Agreement. | 2 # Licensed to PSF under a Contributor Agreement. |
| 3 | 3 |
| 4 """Tests for the raise statement.""" | 4 """Tests for the raise statement.""" |
| 5 | 5 |
| 6 from test import support | 6 from test import support, script_helper |
| 7 import re |
| 7 import sys | 8 import sys |
| 8 import types | 9 import types |
| 9 import unittest | 10 import unittest |
| 10 | 11 |
| 11 | 12 |
| 13 try: |
| 14 from resource import setrlimit, RLIMIT_CORE, error as resource_error |
| 15 except ImportError: |
| 16 prepare_subprocess = None |
| 17 else: |
| 18 def prepare_subprocess(): |
| 19 # don't create core file |
| 20 try: |
| 21 setrlimit(RLIMIT_CORE, (0, 0)) |
| 22 except (ValueError, resource_error): |
| 23 pass |
| 24 |
| 25 |
| 26 |
| 12 def get_tb(): | 27 def get_tb(): |
| 13 try: | 28 try: |
| 14 raise OSError() | 29 raise OSError() |
| 15 except: | 30 except: |
| 16 return sys.exc_info()[2] | 31 return sys.exc_info()[2] |
| 17 | 32 |
| 18 | 33 |
| 19 class Context: | 34 class Context: |
| 20 def __enter__(self): | 35 def __enter__(self): |
| 21 return self | 36 return self |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 84 |
| 70 def test_nested_reraise(self): | 85 def test_nested_reraise(self): |
| 71 def nested_reraise(): | 86 def nested_reraise(): |
| 72 raise | 87 raise |
| 73 def reraise(): | 88 def reraise(): |
| 74 try: | 89 try: |
| 75 raise TypeError("foo") | 90 raise TypeError("foo") |
| 76 except: | 91 except: |
| 77 nested_reraise() | 92 nested_reraise() |
| 78 self.assertRaises(TypeError, reraise) | 93 self.assertRaises(TypeError, reraise) |
| 94 |
| 95 def test_raise_from_None(self): |
| 96 try: |
| 97 try: |
| 98 raise TypeError("foo") |
| 99 except: |
| 100 raise ValueError() from None |
| 101 except ValueError as e: |
| 102 self.assertTrue(isinstance(e.__context__, TypeError)) |
| 103 self.assertIsNone(e.__cause__) |
| 79 | 104 |
| 80 def test_with_reraise1(self): | 105 def test_with_reraise1(self): |
| 81 def reraise(): | 106 def reraise(): |
| 82 try: | 107 try: |
| 83 raise TypeError("foo") | 108 raise TypeError("foo") |
| 84 except: | 109 except: |
| 85 with Context(): | 110 with Context(): |
| 86 pass | 111 pass |
| 87 raise | 112 raise |
| 88 self.assertRaises(TypeError, reraise) | 113 self.assertRaises(TypeError, reraise) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 157 |
| 133 def test_assert_with_tuple_arg(self): | 158 def test_assert_with_tuple_arg(self): |
| 134 try: | 159 try: |
| 135 assert False, (3,) | 160 assert False, (3,) |
| 136 except AssertionError as e: | 161 except AssertionError as e: |
| 137 self.assertEqual(str(e), "(3,)") | 162 self.assertEqual(str(e), "(3,)") |
| 138 | 163 |
| 139 | 164 |
| 140 | 165 |
| 141 class TestCause(unittest.TestCase): | 166 class TestCause(unittest.TestCase): |
| 167 |
| 168 def testCauseSyntax(self): |
| 169 try: |
| 170 try: |
| 171 try: |
| 172 raise TypeError |
| 173 except Exception: |
| 174 raise ValueError from None |
| 175 except ValueError as exc: |
| 176 self.assertIsNone(exc.__cause__) |
| 177 raise exc from Ellipsis |
| 178 except ValueError as exc: |
| 179 e = exc |
| 180 |
| 181 self.assertIs(e.__cause__, Ellipsis) |
| 182 self.assertIsInstance(e.__context__, TypeError) |
| 183 |
| 142 def test_invalid_cause(self): | 184 def test_invalid_cause(self): |
| 143 try: | 185 try: |
| 144 raise IndexError from 5 | 186 raise IndexError from 5 |
| 145 except TypeError as e: | 187 except TypeError as e: |
| 146 self.assertIn("exception cause", str(e)) | 188 self.assertIn("exception cause", str(e)) |
| 147 else: | 189 else: |
| 148 self.fail("No exception raised") | 190 self.fail("No exception raised") |
| 149 | 191 |
| 150 def test_class_cause(self): | 192 def test_class_cause(self): |
| 151 try: | 193 try: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 171 | 213 |
| 172 try: | 214 try: |
| 173 raise IndexError from MyException | 215 raise IndexError from MyException |
| 174 except RuntimeError: | 216 except RuntimeError: |
| 175 pass | 217 pass |
| 176 else: | 218 else: |
| 177 self.fail("No exception raised") | 219 self.fail("No exception raised") |
| 178 | 220 |
| 179 | 221 |
| 180 class TestTraceback(unittest.TestCase): | 222 class TestTraceback(unittest.TestCase): |
| 223 |
| 224 def get_output(self, code, filename=None): |
| 225 """ |
| 226 Run the specified code in Python (in a new child process) and read the |
| 227 output from the standard error or from a file (if filename is set). |
| 228 Return the output lines as a list. |
| 229 """ |
| 230 options = {} |
| 231 if prepare_subprocess: |
| 232 options['preexec_fn'] = prepare_subprocess |
| 233 process = script_helper.spawn_python('-c', code, **options) |
| 234 stdout, stderr = process.communicate() |
| 235 exitcode = process.wait() |
| 236 output = support.strip_python_stderr(stdout) |
| 237 output = output.decode('ascii', 'backslashreplace') |
| 238 if filename: |
| 239 self.assertEqual(output, '') |
| 240 with open(filename, "rb") as fp: |
| 241 output = fp.read() |
| 242 output = output.decode('ascii', 'backslashreplace') |
| 243 output = re.sub('Current thread 0x[0-9a-f]+', |
| 244 'Current thread XXX', |
| 245 output) |
| 246 return output.splitlines(), exitcode |
| 247 |
| 248 def test_traceback_verbiage(self): |
| 249 code = """ |
| 250 try: |
| 251 raise ValueError |
| 252 except: |
| 253 raise NameError from None |
| 254 """ |
| 255 text, exitcode = self.get_output(code) |
| 256 self.assertEqual(len(text), 3) |
| 257 self.assertTrue(text[0].startswith('Traceback')) |
| 258 self.assertTrue(text[1].startswith(' File ')) |
| 259 self.assertTrue(text[2].startswith('NameError')) |
| 260 |
| 181 def test_sets_traceback(self): | 261 def test_sets_traceback(self): |
| 182 try: | 262 try: |
| 183 raise IndexError() | 263 raise IndexError() |
| 184 except IndexError as e: | 264 except IndexError as e: |
| 185 self.assertIsInstance(e.__traceback__, types.TracebackType) | 265 self.assertIsInstance(e.__traceback__, types.TracebackType) |
| 186 else: | 266 else: |
| 187 self.fail("No exception raised") | 267 self.fail("No exception raised") |
| 188 | 268 |
| 189 def test_accepts_traceback(self): | 269 def test_accepts_traceback(self): |
| 190 tb = get_tb() | 270 tb = get_tb() |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 pass | 461 pass |
| 382 else: | 462 else: |
| 383 self.fail("No exception raised") | 463 self.fail("No exception raised") |
| 384 | 464 |
| 385 | 465 |
| 386 def test_main(): | 466 def test_main(): |
| 387 support.run_unittest(__name__) | 467 support.run_unittest(__name__) |
| 388 | 468 |
| 389 if __name__ == "__main__": | 469 if __name__ == "__main__": |
| 390 unittest.main() | 470 unittest.main() |
| OLD | NEW |