1from director_exception import *
2
3
4class MyException(Exception):
5
6    def __init__(self, a, b):
7        self.msg = a + b
8
9
10class MyFoo(Foo):
11
12    def ping(self):
13        raise NotImplementedError, "MyFoo::ping() EXCEPTION"
14
15
16class MyFoo2(Foo):
17
18    def ping(self):
19        return True
20        pass  # error: should return a string
21
22
23class MyFoo3(Foo):
24
25    def ping(self):
26        raise MyException("foo", "bar")
27
28class MyFoo4(Foo):
29
30    def ping(self, *args):
31        print(type("bad", "call")) # throws TypeError message: type() takes 1 or 3 arguments
32        return "Foo4.ping"
33
34
35# Check that the NotImplementedError raised by MyFoo.ping() is returned by
36# MyFoo.pong().
37ok = 0
38a = MyFoo()
39b = launder(a)
40try:
41    b.pong()
42except NotImplementedError, e:
43    if str(e) == "MyFoo::ping() EXCEPTION":
44        ok = 1
45    else:
46        print "Unexpected error message: %s" % str(e)
47except:
48    pass
49if not ok:
50    raise RuntimeError
51
52
53# Check that the director returns the appropriate TypeError if the return type
54# is wrong.
55ok = 0
56a = MyFoo2()
57b = launder(a)
58try:
59    b.pong()
60except TypeError, e:
61    # fastdispatch mode adds on Additional Information to the exception message - just check the main exception message exists
62    if str(e).startswith("SWIG director type mismatch in output value of type 'std::string'"):
63        ok = 1
64    else:
65        print "Unexpected error message: %s" % str(e)
66if not ok:
67    raise RuntimeError
68
69
70# Check that the director can return an exception which requires two arguments
71# to the constructor, without mangling it.
72ok = 0
73a = MyFoo3()
74b = launder(a)
75try:
76    b.pong()
77except MyException, e:
78    if e.msg == "foobar":
79        ok = 1
80    else:
81        print "Unexpected error message: %s" % str(e)
82if not ok:
83    raise RuntimeError
84
85
86# Check that the director returns the appropriate TypeError thrown in a director method
87ok = 0
88a = MyFoo4()
89b = launder(a)
90try:
91    b.pong()
92except TypeError as e:
93    if str(e).startswith("type() takes 1 or 3 arguments"):
94        ok = 1
95    else:
96        print "Unexpected error message: %s" % str(e)
97if not ok:
98    raise RuntimeError
99
100
101# This is expected to fail with -builtin option
102# Throwing builtin classes as exceptions not supported
103if not is_python_builtin():
104    try:
105        raise Exception2()
106    except Exception2:
107        pass
108
109    try:
110        raise Exception1()
111    except Exception1:
112        pass
113