auto_ptr.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. '''
  5. >>> from auto_ptr_ext import *
  6. >>> x = X(42)
  7. >>> x.value()
  8. 42
  9. >>> look(x), look(x)
  10. (42, 42)
  11. >>> maybe_steal(x, 0)
  12. 42
  13. >>> look(x)
  14. 42
  15. >>> maybe_steal(x, 1)
  16. 42
  17. >>> broken_auto_ptr and -1 or look(x)
  18. -1
  19. >>> x = X(69)
  20. >>> steal(x)
  21. 69
  22. >>> broken_auto_ptr and -1 or look(x)
  23. -1
  24. >>> if not broken_auto_ptr:
  25. ... try: x.value()
  26. ... except TypeError: pass
  27. ... else: print('expected a TypeError exception')
  28. >>> x = make()
  29. >>> look(x)
  30. 77
  31. >>> z = callback(lambda z: z)
  32. >>> z.value()
  33. 77
  34. >>> extract(x).value()
  35. 77
  36. #
  37. # Test derived to base conversions
  38. #
  39. >>> y = Y(42)
  40. >>> y.value()
  41. 42
  42. >>> try: maybe_steal(y, 0)
  43. ... except TypeError: pass
  44. ... else: print('expected a TypeError exception')
  45. >>> y.value()
  46. 42
  47. >>> broken_auto_ptr and 42 or steal(y)
  48. 42
  49. >>> if not broken_auto_ptr:
  50. ... try: y.value()
  51. ... except TypeError: pass
  52. ... else: print('expected a TypeError exception')
  53. >>> print(look.__doc__.splitlines()[1])
  54. look( (X)arg1) -> int :
  55. >>> print(steal.__doc__.splitlines()[1])
  56. steal( (X)arg1) -> int :
  57. >>> print(maybe_steal.__doc__.splitlines()[1])
  58. maybe_steal( (X)arg1, (bool)arg2) -> int :
  59. >>> print(make.__doc__.splitlines()[1])
  60. make() -> X :
  61. >>> print(callback.__doc__.splitlines()[1])
  62. callback( (object)arg1) -> X :
  63. >>> print(extract.__doc__.splitlines()[1])
  64. extract( (object)arg1) -> X :
  65. '''
  66. def run(args = None):
  67. import sys
  68. import doctest
  69. if args is not None:
  70. sys.argv = args
  71. return doctest.testmod(sys.modules.get(__name__))
  72. if __name__ == '__main__':
  73. print("running...")
  74. import sys
  75. status = run()[0]
  76. if (status == 0): print("Done.")
  77. sys.exit(status)