PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/test-suite/python/director_exception_runme.py

#
Python | 81 lines | 60 code | 11 blank | 10 comment | 9 complexity | a76a75ad4634f658a4f756ecfdac5671 MD5 | raw file
 1from director_exception import *
 2
 3class MyException(Exception):
 4	def __init__(self, a, b):
 5		self.msg = a + b
 6
 7class MyFoo(Foo):
 8	def ping(self):
 9		raise NotImplementedError, "MyFoo::ping() EXCEPTION"
10
11class MyFoo2(Foo):
12	def ping(self):
13		return True
14		pass # error: should return a string
15
16class 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().
22ok = 0
23a = MyFoo()
24b = launder(a)
25try:
26	b.pong()
27except NotImplementedError, e:
28	if str(e) == "MyFoo::ping() EXCEPTION":
29		ok = 1
30	else:
31		print "Unexpected error message: %s" % str(e)
32except:
33	pass
34if not ok:
35	raise RuntimeError
36
37
38# Check that the director returns the appropriate TypeError if the return type
39# is wrong.
40ok = 0
41a = MyFoo2()
42b = launder(a)
43try:
44	b.pong()
45except 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)
50if 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.
56ok = 0
57a = MyFoo3()
58b = launder(a)
59try:
60	b.pong()
61except MyException, e:
62	if e.msg == 'foobar':
63		ok = 1
64	else:
65		print "Unexpected error message: %s" % str(e)
66if not ok:
67	raise RuntimeError
68
69# This is expected to fail with -builtin option
70# Throwing builtin classes as exceptions not supported
71try:
72	raise Exception2()
73except Exception2:
74	pass
75
76# This is expected to fail with -builtin option
77# Throwing builtin classes as exceptions not supported
78try:
79	raise Exception1()
80except Exception1:
81	pass