File: test_exceptions_jy.py

package info (click to toggle)
jython 2.7.2%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,676 kB
  • sloc: python: 640,908; java: 306,458; xml: 1,984; sh: 522; ansic: 126; makefile: 76
file content (85 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Misc. exception related tests

Made for Jython.
"""
import sys
import unittest
from javatests import StackOverflowErrorTest
from test import test_support


class C:
    def __str__(self):
        raise Exception("E")
    def __repr__(self):
        raise Exception("S")

class ExceptionsTestCase(unittest.TestCase):

    def test_keyerror_str(self):
        self.assertEquals(str(KeyError()), '')
        # Is actually repr(args[0])
        self.assertEquals(str(KeyError('')), "''")
        self.assertEquals(str(KeyError('', '')), "('', '')")

    #From bugtests/test076.py
    def test_raise_no_arg(self):
        r = None
        try:
            try:
                raise RuntimeError("dummy")
            except RuntimeError:
                raise
        except RuntimeError, e:
            r = str(e)

        self.assertEquals(r, "dummy")

    def testBugFix1149372(self):
        try:
            c = C()
            str(c)
        except Exception, e:
            assert e.args[0] == "E"
            return
        unittest.fail("if __str__ raises an exception, re-raise")

    def test_wrap_StackOverflowError(self):
        with self.assertRaises(RuntimeError) as cm:
            StackOverflowErrorTest.throwStackOverflowError()
        self.assertEqual(
            cm.exception.message,
            "maximum recursion depth exceeded (Java StackOverflowError)")

        with self.assertRaises(RuntimeError) as cm:
            StackOverflowErrorTest.causeStackOverflowError()
        self.assertEqual(
            cm.exception.message,
            "maximum recursion depth exceeded (Java StackOverflowError)")

    def test_unicode_args(self):
        e = RuntimeError(u"Drink \u2615")  # coffee emoji
        # Can take the repr of any object
        self.assertEqual(repr(e), "RuntimeError(u'Drink \u2615',)")
        # Cannot of course turn a non-ascii Unicode object into a str, even if it's an exception object
        with self.assertRaises(UnicodeEncodeError) as cm:
            str(e)
        self.assertEqual(
            str(cm.exception),
            "'ascii' codec can't encode character u'\u2615' in position 6: ordinal not in range(128)")
        # But the exception hook, via Py#displayException, does not fail when attempting to __str__ the exception args
        with test_support.captured_stderr() as s:
            sys.excepthook(RuntimeError, u"Drink \u2615", None)
        # At minimum, it tells us what kind of exception it was
        self.assertEqual(s.getvalue()[:12], "RuntimeError")
        # It is fine with ascii values, of course
        with test_support.captured_stderr() as s:
            sys.excepthook(RuntimeError, u"Drink java", None)
        self.assertEqual(s.getvalue(), "RuntimeError: Drink java\n")


def test_main():
    test_support.run_unittest(ExceptionsTestCase)

if __name__ == '__main__':
    test_main()