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, 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) |
---|
27 | 42 |
---|
28 | >>> unwrap_int_ref(n) |
---|
29 | 42 |
---|
30 | >>> unwrap_int_const_ref(n) |
---|
31 | 42 |
---|
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) |
---|
39 | 5 |
---|
40 | |
---|
41 | Can'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) |
---|
48 | 9 |
---|
49 | |
---|
50 | >>> wrap_int(n) |
---|
51 | 42 |
---|
52 | |
---|
53 | try: wrap_int_ref(n) |
---|
54 | ... except: pass |
---|
55 | ... else: print 'no exception' |
---|
56 | |
---|
57 | >>> wrap_int_const_ref(n) |
---|
58 | 42 |
---|
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) |
---|
70 | 12 |
---|
71 | |
---|
72 | >>> unwrap_simple(g(s)) |
---|
73 | 'hello, world' |
---|
74 | |
---|
75 | >>> f(g(s)) |
---|
76 | 12 |
---|
77 | |
---|
78 | >>> f_mutable_ref(g(s)) |
---|
79 | 12 |
---|
80 | |
---|
81 | >>> f_const_ptr(g(s)) |
---|
82 | 12 |
---|
83 | |
---|
84 | >>> f_mutable_ptr(g(s)) |
---|
85 | 12 |
---|
86 | |
---|
87 | >>> f2(g(s)) |
---|
88 | 12 |
---|
89 | |
---|
90 | Create an extension class which wraps "complicated" (init1 and get_n) |
---|
91 | are a complicated constructor and member function, respectively. |
---|
92 | |
---|
93 | >>> c1 = complicated(s, 99) |
---|
94 | >>> c1.get_n() |
---|
95 | 99 |
---|
96 | >>> c2 = complicated(s) |
---|
97 | >>> c2.get_n() |
---|
98 | 0 |
---|
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 | |
---|
187 | """ |
---|
188 | |
---|
189 | def run(args = None): |
---|
190 | |
---|
191 | import sys |
---|
192 | import doctest |
---|
193 | |
---|
194 | if args is not None: |
---|
195 | sys.argv = args |
---|
196 | |
---|
197 | return doctest.testmod(sys.modules.get(__name__)) |
---|
198 | |
---|
199 | if __name__ == '__main__': |
---|
200 | print "running..." |
---|
201 | import sys |
---|
202 | status = run()[0] |
---|
203 | if (status == 0): print "Done." |
---|
204 | sys.exit(status) |
---|