ยปCore Development>Code coverage>Doc/includes/mp_newtype.py

Python code coverage for Doc/includes/mp_newtype.py

#countcontent
1n/afrom multiprocessing import freeze_support
2n/afrom multiprocessing.managers import BaseManager, BaseProxy
3n/aimport operator
4n/a
5n/a##
6n/a
7n/aclass Foo:
8n/a def f(self):
9n/a print('you called Foo.f()')
10n/a def g(self):
11n/a print('you called Foo.g()')
12n/a def _h(self):
13n/a print('you called Foo._h()')
14n/a
15n/a# A simple generator function
16n/adef baz():
17n/a for i in range(10):
18n/a yield i*i
19n/a
20n/a# Proxy type for generator objects
21n/aclass GeneratorProxy(BaseProxy):
22n/a _exposed_ = ['__next__']
23n/a def __iter__(self):
24n/a return self
25n/a def __next__(self):
26n/a return self._callmethod('__next__')
27n/a
28n/a# Function to return the operator module
29n/adef get_operator_module():
30n/a return operator
31n/a
32n/a##
33n/a
34n/aclass MyManager(BaseManager):
35n/a pass
36n/a
37n/a# register the Foo class; make `f()` and `g()` accessible via proxy
38n/aMyManager.register('Foo1', Foo)
39n/a
40n/a# register the Foo class; make `g()` and `_h()` accessible via proxy
41n/aMyManager.register('Foo2', Foo, exposed=('g', '_h'))
42n/a
43n/a# register the generator function baz; use `GeneratorProxy` to make proxies
44n/aMyManager.register('baz', baz, proxytype=GeneratorProxy)
45n/a
46n/a# register get_operator_module(); make public functions accessible via proxy
47n/aMyManager.register('operator', get_operator_module)
48n/a
49n/a##
50n/a
51n/adef test():
52n/a manager = MyManager()
53n/a manager.start()
54n/a
55n/a print('-' * 20)
56n/a
57n/a f1 = manager.Foo1()
58n/a f1.f()
59n/a f1.g()
60n/a assert not hasattr(f1, '_h')
61n/a assert sorted(f1._exposed_) == sorted(['f', 'g'])
62n/a
63n/a print('-' * 20)
64n/a
65n/a f2 = manager.Foo2()
66n/a f2.g()
67n/a f2._h()
68n/a assert not hasattr(f2, 'f')
69n/a assert sorted(f2._exposed_) == sorted(['g', '_h'])
70n/a
71n/a print('-' * 20)
72n/a
73n/a it = manager.baz()
74n/a for i in it:
75n/a print('<%d>' % i, end=' ')
76n/a print()
77n/a
78n/a print('-' * 20)
79n/a
80n/a op = manager.operator()
81n/a print('op.add(23, 45) =', op.add(23, 45))
82n/a print('op.pow(2, 94) =', op.pow(2, 94))
83n/a print('op._exposed_ =', op._exposed_)
84n/a
85n/a##
86n/a
87n/aif __name__ == '__main__':
88n/a freeze_support()
89n/a test()