example-callbacks.py 2.52 KB
Newer Older
1
2
#!/usr/bin/env python
from __future__ import print_function
3
from functools import partial
Wenzel Jakob's avatar
Wenzel Jakob committed
4
5
6
7
8
import sys
sys.path.append('.')

from example import Pet
from example import Dog
Wenzel Jakob's avatar
Wenzel Jakob committed
9
from example import Rabbit
Wenzel Jakob's avatar
Wenzel Jakob committed
10
11
12
13
14
from example import dog_bark
from example import pet_print

polly = Pet('Polly', 'parrot')
molly = Dog('Molly')
Wenzel Jakob's avatar
Wenzel Jakob committed
15
16
17
roger = Rabbit('Rabbit')
print(roger.name() + " is a " + roger.species())
pet_print(roger)
Wenzel Jakob's avatar
Wenzel Jakob committed
18
19
20
21
22
23
24
25
26
27
28
29
30
print(polly.name() + " is a " + polly.species())
pet_print(polly)
print(molly.name() + " is a " + molly.species())
pet_print(molly)
dog_bark(molly)
try:
    dog_bark(polly)
except Exception as e:
    print('The following error is expected: ' + str(e))

from example import test_callback1
from example import test_callback2
from example import test_callback3
31
from example import test_callback4
32
from example import test_callback5
33
from example import test_cleanup
Wenzel Jakob's avatar
Wenzel Jakob committed
34
35
36
37

def func1():
    print('Callback function 1 called!')

Wenzel Jakob's avatar
Wenzel Jakob committed
38
39
def func2(a, b, c, d):
    print('Callback function 2 called : ' + str(a) + ", " + str(b) + ", " + str(c) + ", "+ str(d))
40
    return d
Wenzel Jakob's avatar
Wenzel Jakob committed
41

42
43
44
def func3(a):
    print('Callback function 3 called : ' + str(a))

Wenzel Jakob's avatar
Wenzel Jakob committed
45
46
print(test_callback1(func1))
print(test_callback2(func2))
47
48
print(test_callback1(partial(func2, "Hello", "from", "partial", "object")))
print(test_callback1(partial(func3, "Partial object with one argument")))
Wenzel Jakob's avatar
Wenzel Jakob committed
49

50
51
test_callback3(lambda i: i + 1)
f = test_callback4()
52
print("func(43) = %i" % f(43))
53
54
f = test_callback5()
print("func(number=43) = %i" % f(number=43))
55
56

test_cleanup()
57

58
59
60
61
62
63
from example import payload_cstats
cstats = payload_cstats()
print("Payload instances not destroyed:", cstats.alive())
print("Copy constructions:", cstats.copy_constructions)
print("Move constructions:", cstats.move_constructions >= 1)

64
65
66
67
68
69
70
from example import dummy_function
from example import dummy_function2
from example import test_dummy_function
from example import roundtrip

test_dummy_function(dummy_function)
test_dummy_function(roundtrip(dummy_function))
71
72
if roundtrip(None) is not None:
    print("Problem!")
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
test_dummy_function(lambda x: x + 2)

try:
    test_dummy_function(dummy_function2)
    print("Problem!")
except Exception as e:
    if 'Incompatible function arguments' in str(e):
        print("All OK!")
    else:
        print("Problem!")

try:
    test_dummy_function(lambda x, y: x + y)
    print("Problem!")
except Exception as e:
Wenzel Jakob's avatar
Wenzel Jakob committed
88
89
    if 'missing 1 required positional argument' in str(e) or \
       'takes exactly 2 arguments' in str(e):
90
91
92
        print("All OK!")
    else:
        print("Problem!")
93
94
95

print(test_callback3.__doc__)
print(test_callback4.__doc__)