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 7>>> from m2 import * 8 9 Prove that we get an appropriate error from trying to return a type 10 for which we have no registered to_python converter 11 12>>> def check_unregistered(f, msgprefix): 13... try: 14... f(1) 15... except TypeError as x: 16... if not str(x).startswith(msgprefix): 17... print(str(x)) 18... else: 19... print('expected a TypeError') 20... 21>>> check_unregistered(make_unregistered, 'No to_python (by-value) converter found for C++ type') 22>>> check_unregistered(make_unregistered2, 'No Python class registered for C++ class') 23 24>>> n = new_noddy() 25>>> s = new_simple() 26>>> unwrap_int(n) 2742 28>>> unwrap_int_ref(n) 2942 30>>> unwrap_int_const_ref(n) 3142 32>>> unwrap_simple(s) 33'hello, world' 34>>> unwrap_simple_ref(s) 35'hello, world' 36>>> unwrap_simple_const_ref(s) 37'hello, world' 38>>> unwrap_int(5) 395 40 41Can't get a non-const reference to a built-in integer object 42>>> try: 43... unwrap_int_ref(7) 44... except: pass 45... else: print('no exception') 46 47>>> unwrap_int_const_ref(9) 489 49 50>>> wrap_int(n) 5142 52 53try: wrap_int_ref(n) 54... except: pass 55... else: print('no exception') 56 57>>> wrap_int_const_ref(n) 5842 59 60>>> unwrap_simple_ref(wrap_simple(s)) 61'hello, world' 62 63>>> unwrap_simple_ref(wrap_simple_ref(s)) 64'hello, world' 65 66>>> unwrap_simple_ref(wrap_simple_const_ref(s)) 67'hello, world' 68 69>>> f(s) 7012 71 72>>> unwrap_simple(g(s)) 73'hello, world' 74 75>>> f(g(s)) 7612 77 78>>> f_mutable_ref(g(s)) 7912 80 81>>> f_const_ptr(g(s)) 8212 83 84>>> f_mutable_ptr(g(s)) 8512 86 87>>> f2(g(s)) 8812 89 90Create an extension class which wraps "complicated" (init1 and get_n) 91are a complicated constructor and member function, respectively. 92 93>>> c1 = complicated(s, 99) 94>>> c1.get_n() 9599 96>>> c2 = complicated(s) 97>>> c2.get_n() 980 99 100 a quick regression test for a bug where None could be converted 101 to the target of any member function. To see it, we need to 102 access the __dict__ directly, to bypass the type check supplied 103 by the Method property which wraps the method when accessed as an 104 attribute. 105 106>>> try: A.__dict__['name'](None) 107... except TypeError: pass 108... else: print('expected an exception!') 109 110 111>>> a = A() 112>>> b = B() 113>>> c = C() 114>>> d = D() 115 116 117>>> take_a(a).name() 118'A' 119 120>>> try: 121... take_b(a) 122... except: pass 123... else: print('no exception') 124 125>>> try: 126... take_c(a) 127... except: pass 128... else: print('no exception') 129 130>>> try: 131... take_d(a) 132... except: pass 133... else: print('no exception') 134 135------ 136>>> take_a(b).name() 137'A' 138 139>>> take_b(b).name() 140'B' 141 142>>> try: 143... take_c(b) 144... except: pass 145... else: print('no exception') 146 147>>> try: 148... take_d(b) 149... except: pass 150... else: print('no exception') 151 152------- 153>>> take_a(c).name() 154'A' 155 156>>> try: 157... take_b(c) 158... except: pass 159... else: print('no exception') 160 161>>> take_c(c).name() 162'C' 163 164>>> try: 165... take_d(c) 166... except: pass 167... else: print('no exception') 168 169------- 170>>> take_a(d).name() 171'A' 172>>> take_b(d).name() 173'B' 174>>> take_c(d).name() 175'C' 176>>> take_d(d).name() 177'D' 178 179>>> take_d_shared_ptr(d).name() 180'D' 181 182>>> d_as_a = d_factory() 183>>> dd = take_d(d_as_a) 184>>> dd.name() 185'D' 186>>> print(g.__doc__.splitlines()[1]) 187g( (Simple)arg1) -> Simple : 188 189""" 190 191def run(args = None): 192 193 import sys 194 import doctest 195 196 if args is not None: 197 sys.argv = args 198 199 return doctest.testmod(sys.modules.get(__name__)) 200 201if __name__ == '__main__': 202 print("running...") 203 import sys 204 status = run()[0] 205 if (status == 0): print("Done.") 206 sys.exit(status) 207