summaryrefslogtreecommitdiff
path: root/Examples/test-suite/python/director_exception_runme.py
blob: 5b715bae6cb9463062e346427fbc751faf9d4ba6 (plain)
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
86
87
88
89
90
91
92
from director_exception import *


class MyException(Exception):

    def __init__(self, a, b):
        self.msg = a + b


class MyFoo(Foo):

    def ping(self):
        raise NotImplementedError, "MyFoo::ping() EXCEPTION"


class MyFoo2(Foo):

    def ping(self):
        return True
        pass  # error: should return a string


class MyFoo3(Foo):

    def ping(self):
        raise MyException("foo", "bar")

class MyFoo4(Foo):

    def ping(self, *args):
        print(type("bad", "call")) # throws TypeError message: type() takes 1 or 3 arguments
        return "Foo4.ping"


# Check that the NotImplementedError raised by MyFoo.ping() is returned by
# MyFoo.pong().
a = MyFoo()
b = launder(a)
try:
    b.pong()
except NotImplementedError, e:
    if not str(e) == "MyFoo::ping() EXCEPTION":
        raise RuntimeError("Unexpected error message: %s" % str(e))
except TypeError:
    pass


# Check that the director returns the appropriate TypeError if the return type
# is wrong.
a = MyFoo2()
b = launder(a)
try:
    b.pong()
except TypeError, e:
    # fastdispatch mode adds on Additional Information to the exception message - just check the main exception message exists
    if not str(e).startswith("SWIG director type mismatch in output value of type 'std::string'"):
        raise RuntimeError("Unexpected error message: %s" % str(e))


# Check that the director can return an exception which requires two arguments
# to the constructor, without mangling it.
a = MyFoo3()
b = launder(a)
try:
    b.pong()
except MyException, e:
    if e.msg != "foobar":
        raise RuntimeError("Unexpected error message: %s" % str(e))


# Check that the director returns the appropriate TypeError thrown in a director method
a = MyFoo4()
b = launder(a)
try:
    b.pong()
except TypeError as e:
    if not str(e).startswith("type() takes 1 or 3 arguments"):
        raise RuntimeError("Unexpected error message: %s" % str(e))


# This is expected to fail with -builtin option
# Throwing builtin classes as exceptions not supported
if not is_python_builtin():
    try:
        raise Exception2()
    except Exception2:
        pass

    try:
        raise Exception1()
    except Exception1:
        pass