shared_ptr.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 shared_ptr_ext import *
  6. Test that shared_ptr<Derived> can be converted to shared_ptr<Base>
  7. >>> Y.store(YYY(42))
  8. >>> x = X(17)
  9. >>> null_x = null(x)
  10. >>> null_x # should be None
  11. >>> identity(null_x) # should also be None
  12. >>> a = New(1)
  13. >>> A.call_f(a)
  14. 1
  15. >>> New(0)
  16. >>> type(factory(3))
  17. <class 'shared_ptr_ext.Y'>
  18. >>> type(factory(42))
  19. <class 'shared_ptr_ext.YY'>
  20. >>> class P(Z):
  21. ... def v(self):
  22. ... return -Z.v(self);
  23. ... def __del__(self):
  24. ... print('bye')
  25. ...
  26. >>> p = P(12)
  27. >>> p.value()
  28. 12
  29. >>> p.v()
  30. -12
  31. >>> look(p)
  32. 12
  33. >>> try: modify(p)
  34. ... except TypeError: pass
  35. ... else: 'print(expected a TypeError)'
  36. >>> look(None)
  37. -1
  38. >>> store(p)
  39. >>> del p
  40. >>> Z.get().v()
  41. -12
  42. >>> Z.count()
  43. 1
  44. >>> Z.look_store()
  45. 12
  46. >>> Z.release()
  47. bye
  48. >>> Z.count()
  49. 0
  50. >>> z = Z(13)
  51. >>> z.value()
  52. 13
  53. >>> z.v()
  54. 13
  55. >>> try: modify(z)
  56. ... except TypeError: pass
  57. ... else: 'print(expected a TypeError)'
  58. >>> Z.get() # should be None
  59. >>> store(z)
  60. >>> assert Z.get() is z # show that deleter introspection works
  61. >>> del z
  62. >>> Z.get().value()
  63. 13
  64. >>> Z.count()
  65. 1
  66. >>> Z.look_store()
  67. 13
  68. >>> Z.release()
  69. >>> Z.count()
  70. 0
  71. >>> x = X(17)
  72. >>> x.value()
  73. 17
  74. >>> look(x)
  75. 17
  76. >>> try: modify(x)
  77. ... except TypeError: pass
  78. ... else: 'print(expected a TypeError)'
  79. >>> look(None)
  80. -1
  81. >>> store(x)
  82. >>> del x
  83. >>> X.count()
  84. 1
  85. >>> X.look_store()
  86. 17
  87. >>> X.release()
  88. >>> X.count()
  89. 0
  90. >>> y = Y(19)
  91. >>> y.value()
  92. 19
  93. >>> modify(y)
  94. >>> look(y)
  95. -1
  96. >>> store(Y(23))
  97. >>> Y.count()
  98. 1
  99. >>> Y.look_store()
  100. 23
  101. >>> Y.release()
  102. >>> Y.count()
  103. 0
  104. '''
  105. def run(args = None):
  106. import sys
  107. import doctest
  108. if args is not None:
  109. sys.argv = args
  110. return doctest.testmod(sys.modules.get(__name__))
  111. if __name__ == '__main__':
  112. print("running...")
  113. import sys
  114. status = run()[0]
  115. if (status == 0): print("Done.")
  116. sys.exit(status)