callbacks.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 callbacks_ext import *
  6. >>> def double(x):
  7. ... return x + x
  8. ...
  9. >>> apply_int_int(double, 42)
  10. 84
  11. >>> apply_void_int(double, 42)
  12. >>> def identity(x):
  13. ... return x
  14. Once we have array conversion support, this test will fail. Er,
  15. succeed<wink>:
  16. >>> try: apply_to_string_literal(identity)
  17. ... except ReferenceError: pass # expected
  18. ... else: print('expected an exception!')
  19. >>> try: apply_X_ref_handle(lambda ignored:X(42), None)
  20. ... except ReferenceError: pass # expected
  21. ... else: print('expected an exception!')
  22. >>> x = X(42)
  23. >>> x.y = X(7)
  24. >>> apply_X_ref_handle(lambda z:z.y, x).value()
  25. 7
  26. >>> x = apply_X_X(identity, X(42))
  27. >>> x.value()
  28. 42
  29. >>> x_count()
  30. 1
  31. >>> del x
  32. >>> x_count()
  33. 0
  34. >>> def increment(x):
  35. ... x.set(x.value() + 1)
  36. ...
  37. >>> x = X(42)
  38. >>> apply_void_X_ref(increment, x)
  39. >>> x.value()
  40. 43
  41. >>> apply_void_X_cref(increment, x)
  42. >>> x.value() # const-ness is not respected, sorry!
  43. 44
  44. >>> last_x = 1
  45. >>> def decrement(x):
  46. ... global last_x
  47. ... last_x = x
  48. ... if x is not None:
  49. ... x.set(x.value() - 1)
  50. >>> apply_void_X_ptr(decrement, x)
  51. >>> x.value()
  52. 43
  53. >>> last_x.value()
  54. 43
  55. >>> increment(last_x)
  56. >>> x.value()
  57. 44
  58. >>> last_x.value()
  59. 44
  60. >>> apply_void_X_ptr(decrement, None)
  61. >>> assert last_x is None
  62. >>> x.value()
  63. 44
  64. >>> last_x = 1
  65. >>> apply_void_X_deep_ptr(decrement, None)
  66. >>> assert last_x is None
  67. >>> x.value()
  68. 44
  69. >>> apply_void_X_deep_ptr(decrement, x)
  70. >>> x.value()
  71. 44
  72. >>> last_x.value()
  73. 43
  74. >>> y = apply_X_ref_handle(identity, x)
  75. >>> assert y.value() == x.value()
  76. >>> increment(x)
  77. >>> assert y.value() == x.value()
  78. >>> y = apply_X_ptr_handle_cref(identity, x)
  79. >>> assert y.value() == x.value()
  80. >>> increment(x)
  81. >>> assert y.value() == x.value()
  82. >>> y = apply_X_ptr_handle_cref(identity, None)
  83. >>> y
  84. >>> def new_x(ignored):
  85. ... return X(666)
  86. ...
  87. >>> try: apply_X_ref_handle(new_x, 1)
  88. ... except ReferenceError: pass
  89. ... else: print('no error')
  90. >>> try: apply_X_ptr_handle_cref(new_x, 1)
  91. ... except ReferenceError: pass
  92. ... else: print('no error')
  93. >>> try: apply_cstring_cstring(identity, 'hello')
  94. ... except ReferenceError: pass
  95. ... else: print('no error')
  96. >>> apply_char_char(identity, 'x')
  97. 'x'
  98. >>> apply_cstring_pyobject(identity, 'hello')
  99. 'hello'
  100. >>> apply_cstring_pyobject(identity, None)
  101. >>> apply_char_char(identity, 'x')
  102. 'x'
  103. >>> assert apply_to_own_type(identity) is type(identity)
  104. >>> assert apply_object_object(identity, identity) is identity
  105. '''
  106. def run(args = None):
  107. import sys
  108. import doctest
  109. if args is not None:
  110. sys.argv = args
  111. return doctest.testmod(sys.modules.get(__name__))
  112. if __name__ == '__main__':
  113. print("running...")
  114. import sys
  115. status = run()[0]
  116. if (status == 0): print("Done.")
  117. sys.exit(status)