| LEFT | RIGHT |
| 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 |
| 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 |
| 10 | 25 |
| 11 | 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: |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 157 |
| 143 def test_assert_with_tuple_arg(self): | 158 def test_assert_with_tuple_arg(self): |
| 144 try: | 159 try: |
| 145 assert False, (3,) | 160 assert False, (3,) |
| 146 except AssertionError as e: | 161 except AssertionError as e: |
| 147 self.assertEqual(str(e), "(3,)") | 162 self.assertEqual(str(e), "(3,)") |
| 148 | 163 |
| 149 | 164 |
| 150 | 165 |
| 151 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 |
| 152 def test_invalid_cause(self): | 184 def test_invalid_cause(self): |
| 153 try: | 185 try: |
| 154 raise IndexError from 5 | 186 raise IndexError from 5 |
| 155 except TypeError as e: | 187 except TypeError as e: |
| 156 self.assertIn("exception cause", str(e)) | 188 self.assertIn("exception cause", str(e)) |
| 157 else: | 189 else: |
| 158 self.fail("No exception raised") | 190 self.fail("No exception raised") |
| 159 | 191 |
| 160 def test_class_cause(self): | 192 def test_class_cause(self): |
| 161 try: | 193 try: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 181 | 213 |
| 182 try: | 214 try: |
| 183 raise IndexError from MyException | 215 raise IndexError from MyException |
| 184 except RuntimeError: | 216 except RuntimeError: |
| 185 pass | 217 pass |
| 186 else: | 218 else: |
| 187 self.fail("No exception raised") | 219 self.fail("No exception raised") |
| 188 | 220 |
| 189 | 221 |
| 190 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 |
| 191 def test_sets_traceback(self): | 261 def test_sets_traceback(self): |
| 192 try: | 262 try: |
| 193 raise IndexError() | 263 raise IndexError() |
| 194 except IndexError as e: | 264 except IndexError as e: |
| 195 self.assertIsInstance(e.__traceback__, types.TracebackType) | 265 self.assertIsInstance(e.__traceback__, types.TracebackType) |
| 196 else: | 266 else: |
| 197 self.fail("No exception raised") | 267 self.fail("No exception raised") |
| 198 | 268 |
| 199 def test_accepts_traceback(self): | 269 def test_accepts_traceback(self): |
| 200 tb = get_tb() | 270 tb = get_tb() |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 pass | 461 pass |
| 392 else: | 462 else: |
| 393 self.fail("No exception raised") | 463 self.fail("No exception raised") |
| 394 | 464 |
| 395 | 465 |
| 396 def test_main(): | 466 def test_main(): |
| 397 support.run_unittest(__name__) | 467 support.run_unittest(__name__) |
| 398 | 468 |
| 399 if __name__ == "__main__": | 469 if __name__ == "__main__": |
| 400 unittest.main() | 470 unittest.main() |
| LEFT | RIGHT |