newtest.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 m1 import *
  6. >>> from m2 import *
  7. Prove that we get an appropriate error from trying to return a type
  8. for which we have no registered to_python converter
  9. >>> def check_unregistered(f, msgprefix):
  10. ... try:
  11. ... f(1)
  12. ... except TypeError as x:
  13. ... if not str(x).startswith(msgprefix):
  14. ... print(str(x))
  15. ... else:
  16. ... print('expected a TypeError')
  17. ...
  18. >>> check_unregistered(make_unregistered, 'No to_python (by-value) converter found for C++ type')
  19. >>> check_unregistered(make_unregistered2, 'No Python class registered for C++ class')
  20. >>> n = new_noddy()
  21. >>> s = new_simple()
  22. >>> unwrap_int(n)
  23. 42
  24. >>> unwrap_int_ref(n)
  25. 42
  26. >>> unwrap_int_const_ref(n)
  27. 42
  28. >>> unwrap_simple(s)
  29. 'hello, world'
  30. >>> unwrap_simple_ref(s)
  31. 'hello, world'
  32. >>> unwrap_simple_const_ref(s)
  33. 'hello, world'
  34. >>> unwrap_int(5)
  35. 5
  36. Can't get a non-const reference to a built-in integer object
  37. >>> try:
  38. ... unwrap_int_ref(7)
  39. ... except: pass
  40. ... else: print('no exception')
  41. >>> unwrap_int_const_ref(9)
  42. 9
  43. >>> wrap_int(n)
  44. 42
  45. try: wrap_int_ref(n)
  46. ... except: pass
  47. ... else: print('no exception')
  48. >>> wrap_int_const_ref(n)
  49. 42
  50. >>> unwrap_simple_ref(wrap_simple(s))
  51. 'hello, world'
  52. >>> unwrap_simple_ref(wrap_simple_ref(s))
  53. 'hello, world'
  54. >>> unwrap_simple_ref(wrap_simple_const_ref(s))
  55. 'hello, world'
  56. >>> f(s)
  57. 12
  58. >>> unwrap_simple(g(s))
  59. 'hello, world'
  60. >>> f(g(s))
  61. 12
  62. >>> f_mutable_ref(g(s))
  63. 12
  64. >>> f_const_ptr(g(s))
  65. 12
  66. >>> f_mutable_ptr(g(s))
  67. 12
  68. >>> f2(g(s))
  69. 12
  70. Create an extension class which wraps "complicated" (init1 and get_n)
  71. are a complicated constructor and member function, respectively.
  72. >>> c1 = complicated(s, 99)
  73. >>> c1.get_n()
  74. 99
  75. >>> c2 = complicated(s)
  76. >>> c2.get_n()
  77. 0
  78. a quick regression test for a bug where None could be converted
  79. to the target of any member function. To see it, we need to
  80. access the __dict__ directly, to bypass the type check supplied
  81. by the Method property which wraps the method when accessed as an
  82. attribute.
  83. >>> try: A.__dict__['name'](None)
  84. ... except TypeError: pass
  85. ... else: print('expected an exception!')
  86. >>> a = A()
  87. >>> b = B()
  88. >>> c = C()
  89. >>> d = D()
  90. >>> take_a(a).name()
  91. 'A'
  92. >>> try:
  93. ... take_b(a)
  94. ... except: pass
  95. ... else: print('no exception')
  96. >>> try:
  97. ... take_c(a)
  98. ... except: pass
  99. ... else: print('no exception')
  100. >>> try:
  101. ... take_d(a)
  102. ... except: pass
  103. ... else: print('no exception')
  104. ------
  105. >>> take_a(b).name()
  106. 'A'
  107. >>> take_b(b).name()
  108. 'B'
  109. >>> try:
  110. ... take_c(b)
  111. ... except: pass
  112. ... else: print('no exception')
  113. >>> try:
  114. ... take_d(b)
  115. ... except: pass
  116. ... else: print('no exception')
  117. -------
  118. >>> take_a(c).name()
  119. 'A'
  120. >>> try:
  121. ... take_b(c)
  122. ... except: pass
  123. ... else: print('no exception')
  124. >>> take_c(c).name()
  125. 'C'
  126. >>> try:
  127. ... take_d(c)
  128. ... except: pass
  129. ... else: print('no exception')
  130. -------
  131. >>> take_a(d).name()
  132. 'A'
  133. >>> take_b(d).name()
  134. 'B'
  135. >>> take_c(d).name()
  136. 'C'
  137. >>> take_d(d).name()
  138. 'D'
  139. >>> take_d_shared_ptr(d).name()
  140. 'D'
  141. >>> d_as_a = d_factory()
  142. >>> dd = take_d(d_as_a)
  143. >>> dd.name()
  144. 'D'
  145. >>> print(g.__doc__.splitlines()[1])
  146. g( (Simple)arg1) -> Simple :
  147. """
  148. def run(args = None):
  149. import sys
  150. import doctest
  151. if args is not None:
  152. sys.argv = args
  153. return doctest.testmod(sys.modules.get(__name__))
  154. if __name__ == '__main__':
  155. print("running...")
  156. import sys
  157. status = run()[0]
  158. if (status == 0): print("Done.")
  159. sys.exit(status)