import source from 1.3.40
[external/swig.git] / Examples / test-suite / python / director_exception_runme.py
1 from director_exception import *
2
3 class MyException(Exception):
4         def __init__(self, a, b):
5                 self.msg = a + b
6
7 class MyFoo(Foo):
8         def ping(self):
9                 raise NotImplementedError, "MyFoo::ping() EXCEPTION"
10
11 class MyFoo2(Foo):
12         def ping(self):
13                 return True
14                 pass # error: should return a string
15
16 class MyFoo3(Foo):
17         def ping(self):
18                 raise MyException("foo", "bar")
19
20 # Check that the NotImplementedError raised by MyFoo.ping() is returned by
21 # MyFoo.pong().
22 ok = 0
23 a = MyFoo()
24 b = launder(a)
25 try:
26         b.pong()
27 except NotImplementedError, e:
28         if str(e) == "MyFoo::ping() EXCEPTION":
29                 ok = 1
30         else:
31                 print "Unexpected error message: %s" % str(e)
32 except:
33         pass
34 if not ok:
35         raise RuntimeError
36
37
38 # Check that the director returns the appropriate TypeError if the return type
39 # is wrong.
40 ok = 0
41 a = MyFoo2()
42 b = launder(a)
43 try:
44         b.pong()
45 except TypeError, e:
46         if str(e) == "Swig director type mismatch in output value of type 'std::string'":
47                 ok = 1
48         else:
49                 print "Unexpected error message: %s" % str(e)
50 if not ok:
51         raise RuntimeError
52
53
54 # Check that the director can return an exception which requires two arguments
55 # to the constructor, without mangling it.
56 ok = 0
57 a = MyFoo3()
58 b = launder(a)
59 try:
60         b.pong()
61 except MyException, e:
62         if e.msg == 'foobar':
63                 ok = 1
64         else:
65                 print "Unexpected error message: %s" % str(e)
66 if not ok:
67         raise RuntimeError
68
69 try:
70         raise Exception2()
71 except Exception2:
72         pass
73
74 try:
75         raise Exception1()
76 except Exception1:
77         pass