-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtest_stop_iteration.py
87 lines (56 loc) · 1.76 KB
/
test_stop_iteration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
test example file exposing mltiple issues with corutine exception passover in case of stopiteration
the stdlib contextmanager implementation explicitly catches
and reshapes in case a StopIteration was send in and is raised out
"""
from __future__ import annotations
from collections.abc import Iterator
from contextlib import contextmanager
import pluggy
def test_stop() -> None:
raise StopIteration()
hookspec = pluggy.HookspecMarker("myproject")
hookimpl = pluggy.HookimplMarker("myproject")
class MySpec:
"""A hook specification namespace."""
@hookspec
def myhook(self, arg1: int, arg2: int) -> int: # type: ignore[empty-body]
"""My special little hook that you can customize."""
class Plugin_1:
"""A hook implementation namespace."""
@hookimpl
def myhook(self, arg1: int, arg2: int) -> int:
print("inside Plugin_1.myhook()")
raise StopIteration()
class Plugin_2:
"""A 2nd hook implementation namespace."""
@hookimpl(wrapper=True)
def myhook(self) -> Iterator[None]:
return (yield)
def try_pluggy() -> None:
# create a manager and add the spec
pm = pluggy.PluginManager("myproject")
pm.add_hookspecs(MySpec)
# register plugins
pm.register(Plugin_1())
pm.register(Plugin_2())
# call our ``myhook`` hook
results = pm.hook.myhook(arg1=1, arg2=2)
print(results)
@contextmanager
def my_cm() -> Iterator[None]:
try:
yield
except Exception as e:
print(e)
raise StopIteration()
def inner() -> None:
with my_cm():
raise StopIteration()
def try_context() -> None:
inner()
mains = {"pluggy": try_pluggy, "context": try_context}
if __name__ == "__main__":
import sys
if len(sys.argv) == 2:
mains[sys.argv[1]]()