PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Python | 81 lines | 60 code | 11 blank | 10 comment | 19 complexity | a76a75ad4634f658a4f756ecfdac5671 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. from director_exception import *
  2. class MyException(Exception):
  3. def __init__(self, a, b):
  4. self.msg = a + b
  5. class MyFoo(Foo):
  6. def ping(self):
  7. raise NotImplementedError, "MyFoo::ping() EXCEPTION"
  8. class MyFoo2(Foo):
  9. def ping(self):
  10. return True
  11. pass # error: should return a string
  12. class MyFoo3(Foo):
  13. def ping(self):
  14. raise MyException("foo", "bar")
  15. # Check that the NotImplementedError raised by MyFoo.ping() is returned by
  16. # MyFoo.pong().
  17. ok = 0
  18. a = MyFoo()
  19. b = launder(a)
  20. try:
  21. b.pong()
  22. except NotImplementedError, e:
  23. if str(e) == "MyFoo::ping() EXCEPTION":
  24. ok = 1
  25. else:
  26. print "Unexpected error message: %s" % str(e)
  27. except:
  28. pass
  29. if not ok:
  30. raise RuntimeError
  31. # Check that the director returns the appropriate TypeError if the return type
  32. # is wrong.
  33. ok = 0
  34. a = MyFoo2()
  35. b = launder(a)
  36. try:
  37. b.pong()
  38. except TypeError, e:
  39. if str(e) == "SWIG director type mismatch in output value of type 'std::string'":
  40. ok = 1
  41. else:
  42. print "Unexpected error message: %s" % str(e)
  43. if not ok:
  44. raise RuntimeError
  45. # Check that the director can return an exception which requires two arguments
  46. # to the constructor, without mangling it.
  47. ok = 0
  48. a = MyFoo3()
  49. b = launder(a)
  50. try:
  51. b.pong()
  52. except MyException, e:
  53. if e.msg == 'foobar':
  54. ok = 1
  55. else:
  56. print "Unexpected error message: %s" % str(e)
  57. if not ok:
  58. raise RuntimeError
  59. # This is expected to fail with -builtin option
  60. # Throwing builtin classes as exceptions not supported
  61. try:
  62. raise Exception2()
  63. except Exception2:
  64. pass
  65. # This is expected to fail with -builtin option
  66. # Throwing builtin classes as exceptions not supported
  67. try:
  68. raise Exception1()
  69. except Exception1:
  70. pass