object.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. from __future__ import print_function
  5. '''
  6. >>> from object_ext import *
  7. >>> type(ref_to_noncopyable())
  8. <class 'object_ext.NotCopyable'>
  9. >>> def print1(x):
  10. ... print(x)
  11. >>> call_object_3(print1)
  12. 3
  13. >>> message()
  14. 'hello, world!'
  15. >>> number()
  16. 42
  17. >>> test('hi')
  18. 1
  19. >>> test(None)
  20. 0
  21. >>> test_not('hi')
  22. 0
  23. >>> test_not(0)
  24. 1
  25. Attributes
  26. >>> class X: pass
  27. ...
  28. >>> x = X()
  29. >>> try: obj_getattr(x, 'foo')
  30. ... except AttributeError: pass
  31. ... else: print('expected an exception')
  32. >>> try: obj_objgetattr(x, 'objfoo')
  33. ... except AttributeError: pass
  34. ... else: print('expected an exception')
  35. >>> obj_setattr(x, 'foo', 1)
  36. >>> x.foo
  37. 1
  38. >>> obj_objsetattr(x, 'objfoo', 1)
  39. >>> try:obj_objsetattr(x, 1)
  40. ... except TypeError: pass
  41. ... else: print('expected an exception')
  42. >>> x.objfoo
  43. 1
  44. >>> obj_getattr(x, 'foo')
  45. 1
  46. >>> obj_objgetattr(x, 'objfoo')
  47. 1
  48. >>> try:obj_objgetattr(x, 1)
  49. ... except TypeError: pass
  50. ... else: print('expected an exception')
  51. >>> obj_const_getattr(x, 'foo')
  52. 1
  53. >>> obj_const_objgetattr(x, 'objfoo')
  54. 1
  55. >>> obj_setattr42(x, 'foo')
  56. >>> x.foo
  57. 42
  58. >>> obj_objsetattr42(x, 'objfoo')
  59. >>> x.objfoo
  60. 42
  61. >>> obj_moveattr(x, 'foo', 'bar')
  62. >>> x.bar
  63. 42
  64. >>> obj_objmoveattr(x, 'objfoo', 'objbar')
  65. >>> x.objbar
  66. 42
  67. >>> test_attr(x, 'foo')
  68. 1
  69. >>> test_objattr(x, 'objfoo')
  70. 1
  71. >>> test_not_attr(x, 'foo')
  72. 0
  73. >>> test_not_objattr(x, 'objfoo')
  74. 0
  75. >>> x.foo = None
  76. >>> test_attr(x, 'foo')
  77. 0
  78. >>> x.objfoo = None
  79. >>> test_objattr(x, 'objfoo')
  80. 0
  81. >>> test_not_attr(x, 'foo')
  82. 1
  83. >>> test_not_objattr(x, 'objfoo')
  84. 1
  85. >>> obj_delattr(x, 'foo')
  86. >>> obj_objdelattr(x, 'objfoo')
  87. >>> try:obj_delattr(x, 'foo')
  88. ... except AttributeError: pass
  89. ... else: print('expected an exception')
  90. >>> try:obj_objdelattr(x, 'objfoo')
  91. ... except AttributeError: pass
  92. ... else: print('expected an exception')
  93. Items
  94. >>> d = {}
  95. >>> obj_setitem(d, 'foo', 1)
  96. >>> d['foo']
  97. 1
  98. >>> obj_getitem(d, 'foo')
  99. 1
  100. >>> obj_const_getitem(d, 'foo')
  101. 1
  102. >>> obj_setitem42(d, 'foo')
  103. >>> obj_getitem(d, 'foo')
  104. 42
  105. >>> d['foo']
  106. 42
  107. >>> obj_moveitem(d, 'foo', 'bar')
  108. >>> d['bar']
  109. 42
  110. >>> obj_moveitem2(d, 'bar', d, 'baz')
  111. >>> d['baz']
  112. 42
  113. >>> test_item(d, 'foo')
  114. 1
  115. >>> test_not_item(d, 'foo')
  116. 0
  117. >>> d['foo'] = None
  118. >>> test_item(d, 'foo')
  119. 0
  120. >>> test_not_item(d, 'foo')
  121. 1
  122. Slices
  123. >>> assert check_string_slice()
  124. Operators
  125. >>> def print_args(*args, **kwds):
  126. ... print(args, kwds)
  127. >>> test_call(print_args, (0, 1, 2, 3), {'a':'A'})
  128. (0, 1, 2, 3) {'a': 'A'}
  129. >>> assert check_binary_operators()
  130. >>> class X: pass
  131. ...
  132. >>> assert check_inplace(list(range(3)), X())
  133. Now make sure that object is actually managing reference counts
  134. >>> import weakref
  135. >>> class Z: pass
  136. ...
  137. >>> z = Z()
  138. >>> def death(r): print('death')
  139. ...
  140. >>> r = weakref.ref(z, death)
  141. >>> z.foo = 1
  142. >>> obj_getattr(z, 'foo')
  143. 1
  144. >>> del z
  145. death
  146. '''
  147. def run(args = None):
  148. import sys
  149. import doctest
  150. if args is not None:
  151. sys.argv = args
  152. return doctest.testmod(sys.modules.get(__name__))
  153. if __name__ == '__main__':
  154. print("running...")
  155. import sys
  156. status = run()[0]
  157. if (status == 0): print("Done.")
  158. sys.exit(status)