blob: 674174a16844fca6c5ceadf3294806b182a27725 [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
58from example import dummy_function
59from example import dummy_function2
60from example import test_dummy_function
61from example import roundtrip
62
63test_dummy_function(dummy_function)
64test_dummy_function(roundtrip(dummy_function))
65test_dummy_function(lambda x: x + 2)
66
67try:
68 test_dummy_function(dummy_function2)
69 print("Problem!")
70except Exception as e:
71 if 'Incompatible function arguments' in str(e):
72 print("All OK!")
73 else:
74 print("Problem!")
75
76try:
77 test_dummy_function(lambda x, y: x + y)
78 print("Problem!")
79except Exception as e:
Wenzel Jakob4e27f7b2016-07-10 11:01:35 +020080 if 'missing 1 required positional argument' in str(e) or \
81 'takes exactly 2 arguments' in str(e):
Wenzel Jakob954b7932016-07-10 10:13:18 +020082 print("All OK!")
83 else:
84 print("Problem!")