diff options
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r-- | Lib/test/test_exceptions.py | 108 |
1 files changed, 91 insertions, 17 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 28dcb24..1ad7f97 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -8,7 +8,8 @@ import weakref import errno from test.support import (TESTFN, captured_output, check_impl_detail, - cpython_only, gc_collect, run_unittest, unlink) + cpython_only, gc_collect, run_unittest, no_tracing, + unlink) class NaiveException(Exception): def __init__(self, x): @@ -55,8 +56,8 @@ class ExceptionTests(unittest.TestCase): fp.close() unlink(TESTFN) - self.raise_catch(IOError, "IOError") - self.assertRaises(IOError, open, 'this file does not exist', 'r') + self.raise_catch(OSError, "OSError") + self.assertRaises(OSError, open, 'this file does not exist', 'r') self.raise_catch(ImportError, "ImportError") self.assertRaises(ImportError, __import__, "undefined_module") @@ -201,11 +202,35 @@ class ExceptionTests(unittest.TestCase): except NameError: pass else: - self.assertEqual(str(WindowsError(1001)), "1001") - self.assertEqual(str(WindowsError(1001, "message")), - "[Error 1001] message") - self.assertEqual(WindowsError(1001, "message").errno, 22) - self.assertEqual(WindowsError(1001, "message").winerror, 1001) + self.assertIs(WindowsError, OSError) + self.assertEqual(str(OSError(1001)), "1001") + self.assertEqual(str(OSError(1001, "message")), + "[Errno 1001] message") + # POSIX errno (9 aka EBADF) is untranslated + w = OSError(9, 'foo', 'bar') + self.assertEqual(w.errno, 9) + self.assertEqual(w.winerror, None) + self.assertEqual(str(w), "[Errno 9] foo: 'bar'") + # ERROR_PATH_NOT_FOUND (win error 3) becomes ENOENT (2) + w = OSError(0, 'foo', 'bar', 3) + self.assertEqual(w.errno, 2) + self.assertEqual(w.winerror, 3) + self.assertEqual(w.strerror, 'foo') + self.assertEqual(w.filename, 'bar') + self.assertEqual(str(w), "[WinError 3] foo: 'bar'") + # Unknown win error becomes EINVAL (22) + w = OSError(0, 'foo', None, 1001) + self.assertEqual(w.errno, 22) + self.assertEqual(w.winerror, 1001) + self.assertEqual(w.strerror, 'foo') + self.assertEqual(w.filename, None) + self.assertEqual(str(w), "[WinError 1001] foo") + # Non-numeric "errno" + w = OSError('bar', 'foo') + self.assertEqual(w.errno, 'bar') + self.assertEqual(w.winerror, None) + self.assertEqual(w.strerror, 'foo') + self.assertEqual(w.filename, None) def testAttributes(self): # test that exception attributes are happy @@ -287,11 +312,12 @@ class ExceptionTests(unittest.TestCase): {'args': ('foo',), 'x': 'foo'}), ] try: + # More tests are in test_WindowsError exceptionList.append( (WindowsError, (1, 'strErrorStr', 'filenameStr'), {'args' : (1, 'strErrorStr'), - 'strerror' : 'strErrorStr', 'winerror' : 1, - 'errno' : 22, 'filename' : 'filenameStr'}) + 'strerror' : 'strErrorStr', 'winerror' : None, + 'errno' : 1, 'filename' : 'filenameStr'}) ) except NameError: pass @@ -376,19 +402,37 @@ class ExceptionTests(unittest.TestCase): def testChainingAttrs(self): e = Exception() - self.assertEqual(e.__context__, None) - self.assertEqual(e.__cause__, None) + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) e = TypeError() - self.assertEqual(e.__context__, None) - self.assertEqual(e.__cause__, None) + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) class MyException(EnvironmentError): pass e = MyException() - self.assertEqual(e.__context__, None) - self.assertEqual(e.__cause__, None) + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) + + def testChainingDescriptors(self): + try: + raise Exception() + except Exception as exc: + e = exc + + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) + self.assertFalse(e.__suppress_context__) + + e.__context__ = NameError() + e.__cause__ = None + self.assertIsInstance(e.__context__, NameError) + self.assertIsNone(e.__cause__) + self.assertTrue(e.__suppress_context__) + e.__suppress_context__ = False + self.assertFalse(e.__suppress_context__) def testKeywordArgs(self): # test that builtin exception don't take keyword args, @@ -403,6 +447,7 @@ class ExceptionTests(unittest.TestCase): x = DerivedException(fancy_arg=42) self.assertEqual(x.fancy_arg, 42) + @no_tracing def testInfiniteRecursion(self): def f(): return f() @@ -742,6 +787,7 @@ class ExceptionTests(unittest.TestCase): u.start = 1000 self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997") + @no_tracing def test_badisinstance(self): # Bug #2542: if issubclass(e, MyException) raises an exception, # it should be ignored @@ -852,6 +898,7 @@ class ExceptionTests(unittest.TestCase): self.fail("MemoryError not raised") self.assertEqual(wr(), None) + @no_tracing def test_recursion_error_cleanup(self): # Same test as above, but with "recursion exceeded" errors class C: @@ -878,8 +925,35 @@ class ExceptionTests(unittest.TestCase): self.assertEqual(cm.exception.errno, errno.ENOTDIR, cm.exception) +class ImportErrorTests(unittest.TestCase): + + def test_attributes(self): + # Setting 'name' and 'path' should not be a problem. + exc = ImportError('test') + self.assertIsNone(exc.name) + self.assertIsNone(exc.path) + + exc = ImportError('test', name='somemodule') + self.assertEqual(exc.name, 'somemodule') + self.assertIsNone(exc.path) + + exc = ImportError('test', path='somepath') + self.assertEqual(exc.path, 'somepath') + self.assertIsNone(exc.name) + + exc = ImportError('test', path='somepath', name='somename') + self.assertEqual(exc.name, 'somename') + self.assertEqual(exc.path, 'somepath') + + def test_non_str_argument(self): + # Issue #15778 + arg = b'abc' + exc = ImportError(arg) + self.assertEqual(str(arg), str(exc)) + + def test_main(): - run_unittest(ExceptionTests) + run_unittest(ExceptionTests, ImportErrorTests) if __name__ == '__main__': unittest.main() |