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 extract_ext import * 6 7Just about anything has a truth value in Python 8 9 >>> assert check_bool(None) 10 >>> extract_bool(None) 11 0 12 13 >>> assert check_bool(2) 14 >>> extract_bool(2) 15 1 16 17 >>> assert not check_bool('') 18 19Check that object manager types work properly. These are a different 20case because they wrap Python objects instead of being wrapped by them. 21 22 >>> assert not check_list(2) 23 >>> try: x = extract_list(2) 24 ... except TypeError as x: 25 ... if str(x) != 'Expecting an object of type list; got an object of type int instead': 26 ... print(x) 27 ... else: 28 ... print('expected an exception, got', x, 'instead') 29 30Can't extract a list from a tuple. Use list(x) to convert a sequence 31to a list: 32 33 >>> assert not check_list((1, 2, 3)) 34 >>> assert check_list([1, 2, 3]) 35 >>> extract_list([1, 2, 3]) 36 [1, 2, 3] 37 38Can get a char const* from a Python string: 39 40 >>> assert check_cstring('hello') 41 >>> extract_cstring('hello') 42 'hello' 43 44Can't get a char const* from a Python int: 45 46 >>> assert not check_cstring(1) 47 >>> try: x = extract_cstring(1) 48 ... except TypeError: pass 49 ... else: 50 ... print('expected an exception, got', x, 'instead') 51 52Extract an std::string (class) rvalue from a native Python type 53 54 >>> assert check_string('hello') 55 >>> extract_string('hello') 56 'hello' 57 58Constant references are not treated as rvalues for the purposes of 59extract: 60 61 >>> assert not check_string_cref('hello') 62 63We can extract lvalues where appropriate: 64 65 >>> x = X(42) 66 >>> check_X(x) 67 1 68 >>> extract_X(x) 69 X(42) 70 71 >>> check_X_ptr(x) 72 1 73 >>> extract_X_ptr(x) 74 X(42) 75 >>> extract_X_ref(x) 76 X(42) 77 78Demonstrate that double-extraction of an rvalue works, and all created 79copies of the object are destroyed: 80 81 >>> n = count_Xs() 82 >>> double_X(333) 83 666 84 >>> count_Xs() - n 85 0 86 87General check for cleanliness: 88 89 >>> del x 90 >>> count_Xs() 91 0 92''' 93 94def run(args = None): 95 import sys 96 import doctest 97 98 if args is not None: 99 sys.argv = args 100 return doctest.testmod(sys.modules.get(__name__)) 101 102if __name__ == '__main__': 103 print("running...") 104 import sys 105 status = run()[0] 106 if (status == 0): print("Done.") 107 sys.exit(status) 108