opaque.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. # Copyright Gottfried Ganßauge 2003..2006. Distributed under the Boost
  3. # Software License, Version 1.0. (See accompanying
  4. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. """
  6. >>> from opaque_ext import *
  7. Check for correct conversion
  8. >>> use(get())
  9. Check that None is converted to a NULL opaque pointer
  10. >>> useany(get())
  11. 1
  12. >>> useany(None)
  13. 0
  14. Check that we don't lose type information by converting NULL
  15. opaque pointers to None
  16. >>> assert getnull() is None
  17. >>> useany(getnull())
  18. 0
  19. >>> failuse(get())
  20. Traceback (most recent call last):
  21. ...
  22. RuntimeError: success
  23. Check that there is no conversion from integers ...
  24. >>> try: use(0)
  25. ... except TypeError: pass
  26. ... else: print('expected a TypeError')
  27. ... and from strings to opaque objects
  28. >>> try: use("")
  29. ... except TypeError: pass
  30. ... else: print('expected a TypeError')
  31. Now check the same for another opaque pointer type
  32. >>> use2(get2())
  33. >>> failuse2(get2())
  34. Traceback (most recent call last):
  35. ...
  36. RuntimeError: success
  37. >>> try: use2(0)
  38. ... except TypeError: pass
  39. ... else: print('expected a TypeError')
  40. >>> try: use2("")
  41. ... except TypeError: pass
  42. ... else: print('expected a TypeError')
  43. Check that opaque types are distinct
  44. >>> try: use(get2())
  45. ... except TypeError: pass
  46. ... else: print('expected a TypeError')
  47. >>> try: use2(get())
  48. ... except TypeError: pass
  49. ... else: print('expected a TypeError')
  50. This used to result in a segmentation violation
  51. >>> type(get()) != type (get2())
  52. 1
  53. """
  54. def run(args = None):
  55. import sys
  56. import doctest
  57. if args is not None:
  58. sys.argv = args
  59. return doctest.testmod(sys.modules.get(__name__))
  60. if __name__ == '__main__':
  61. print("running...")
  62. import sys
  63. status = run()[0]
  64. if (status == 0): print("Done.")
  65. sys.exit(status)