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) 4from __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) 143 15>>> message() 16'hello, world!' 17>>> number() 1842 19 20>>> test('hi') 211 22>>> test(None) 230 24>>> test_not('hi') 250 26>>> test_not(0) 271 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 441 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 501 51>>> obj_getattr(x, 'foo') 521 53>>> obj_objgetattr(x, 'objfoo') 541 55>>> try:obj_objgetattr(x, 1) 56... except TypeError: pass 57... else: print('expected an exception') 58>>> obj_const_getattr(x, 'foo') 591 60>>> obj_const_objgetattr(x, 'objfoo') 611 62>>> obj_setattr42(x, 'foo') 63>>> x.foo 6442 65>>> obj_objsetattr42(x, 'objfoo') 66>>> x.objfoo 6742 68>>> obj_moveattr(x, 'foo', 'bar') 69>>> x.bar 7042 71>>> obj_objmoveattr(x, 'objfoo', 'objbar') 72>>> x.objbar 7342 74>>> test_attr(x, 'foo') 751 76>>> test_objattr(x, 'objfoo') 771 78>>> test_not_attr(x, 'foo') 790 80>>> test_not_objattr(x, 'objfoo') 810 82>>> x.foo = None 83>>> test_attr(x, 'foo') 840 85>>> x.objfoo = None 86>>> test_objattr(x, 'objfoo') 870 88>>> test_not_attr(x, 'foo') 891 90>>> test_not_objattr(x, 'objfoo') 911 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'] 1061 107>>> obj_getitem(d, 'foo') 1081 109>>> obj_const_getitem(d, 'foo') 1101 111>>> obj_setitem42(d, 'foo') 112>>> obj_getitem(d, 'foo') 11342 114>>> d['foo'] 11542 116>>> obj_moveitem(d, 'foo', 'bar') 117>>> d['bar'] 11842 119>>> obj_moveitem2(d, 'bar', d, 'baz') 120>>> d['baz'] 12142 122>>> test_item(d, 'foo') 1231 124>>> test_not_item(d, 'foo') 1250 126>>> d['foo'] = None 127>>> test_item(d, 'foo') 1280 129>>> test_not_item(d, 'foo') 1301 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') 1621 163>>> del z 164death 165''' 166 167def 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 175if __name__ == '__main__': 176 print("running...") 177 import sys 178 status = run()[0] 179 if (status == 0): print("Done.") 180 sys.exit(status) 181