blob: 68a485a7d32371b9d48f38b8adb4356db05d805c [file] [log] [blame]
Wenzel Jakob57082212015-09-04 23:42:12 +02001#!/usr/bin/env python
2from __future__ import print_function
Ben Pritchard1b522462016-03-10 16:31:38 -05003from functools import partial
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004import sys
5sys.path.append('.')
6
7from example import Pet
8from example import Dog
Wenzel Jakob48548ea2016-01-17 22:36:44 +01009from example import Rabbit
Wenzel Jakob38bd7112015-07-05 20:05:44 +020010from example import dog_bark
11from example import pet_print
12
13polly = Pet('Polly', 'parrot')
14molly = Dog('Molly')
Wenzel Jakob48548ea2016-01-17 22:36:44 +010015roger = Rabbit('Rabbit')
16print(roger.name() + " is a " + roger.species())
17pet_print(roger)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020018print(polly.name() + " is a " + polly.species())
19pet_print(polly)
20print(molly.name() + " is a " + molly.species())
21pet_print(molly)
22dog_bark(molly)
23try:
24 dog_bark(polly)
25except Exception as e:
26 print('The following error is expected: ' + str(e))
27
28from example import test_callback1
29from example import test_callback2
30from example import test_callback3
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020031from example import test_callback4
Brad Harmon835fc062016-06-16 13:19:15 -050032from example import test_callback5
Wenzel Jakob19208fe2015-10-13 17:37:25 +020033from example import test_cleanup
Wenzel Jakob38bd7112015-07-05 20:05:44 +020034
35def func1():
36 print('Callback function 1 called!')
37
Wenzel Jakob7b8e0322015-08-28 17:49:15 +020038def func2(a, b, c, d):
39 print('Callback function 2 called : ' + str(a) + ", " + str(b) + ", " + str(c) + ", "+ str(d))
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020040 return d
Wenzel Jakob38bd7112015-07-05 20:05:44 +020041
Ben Pritchard1b522462016-03-10 16:31:38 -050042def func3(a):
43 print('Callback function 3 called : ' + str(a))
44
Wenzel Jakob38bd7112015-07-05 20:05:44 +020045print(test_callback1(func1))
46print(test_callback2(func2))
Ben Pritchard1b522462016-03-10 16:31:38 -050047print(test_callback1(partial(func2, "Hello", "from", "partial", "object")))
48print(test_callback1(partial(func3, "Partial object with one argument")))
Wenzel Jakob38bd7112015-07-05 20:05:44 +020049
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020050test_callback3(lambda i: i + 1)
51f = test_callback4()
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020052print("func(43) = %i" % f(43))
Brad Harmon835fc062016-06-16 13:19:15 -050053f = test_callback5()
54print("func(number=43) = %i" % f(number=43))
Wenzel Jakob19208fe2015-10-13 17:37:25 +020055
56test_cleanup()
Wenzel Jakob954b7932016-07-10 10:13:18 +020057
Jason Rhinelander3f589372016-08-07 13:05:26 -040058from example import payload_cstats
59cstats = payload_cstats()
60print("Payload instances not destroyed:", cstats.alive())
61print("Copy constructions:", cstats.copy_constructions)
62print("Move constructions:", cstats.move_constructions >= 1)
63
Wenzel Jakob954b7932016-07-10 10:13:18 +020064from example import dummy_function
65from example import dummy_function2
66from example import test_dummy_function
67from example import roundtrip
68
69test_dummy_function(dummy_function)
70test_dummy_function(roundtrip(dummy_function))
Wenzel Jakob8de04372016-08-18 11:18:12 +020071if roundtrip(None) is not None:
72 print("Problem!")
Wenzel Jakob954b7932016-07-10 10:13:18 +020073test_dummy_function(lambda x: x + 2)
74
75try:
76 test_dummy_function(dummy_function2)
77 print("Problem!")
78except Exception as e:
79 if 'Incompatible function arguments' in str(e):
80 print("All OK!")
81 else:
82 print("Problem!")
83
84try:
85 test_dummy_function(lambda x, y: x + y)
86 print("Problem!")
87except Exception as e:
Wenzel Jakob4e27f7b2016-07-10 11:01:35 +020088 if 'missing 1 required positional argument' in str(e) or \
89 'takes exactly 2 arguments' in str(e):
Wenzel Jakob954b7932016-07-10 10:13:18 +020090 print("All OK!")
91 else:
92 print("Problem!")
Dean Moldovaned23dda2016-08-04 01:40:40 +020093
94print(test_callback3.__doc__)
95print(test_callback4.__doc__)