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