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
|
diff --git a/src/twisted/internet/test/test_asyncioreactor.py b/src/twisted/internet/test/test_asyncioreactor.py
index 2f7bad930d6..bbbb8c834ca 100644
--- a/src/twisted/internet/test/test_asyncioreactor.py
+++ b/src/twisted/internet/test/test_asyncioreactor.py
@@ -12,8 +12,8 @@
DefaultEventLoopPolicy,
Future,
SelectorEventLoop,
- get_event_loop,
get_event_loop_policy,
+ get_running_loop,
set_event_loop,
set_event_loop_policy,
)
@@ -74,14 +74,26 @@ def newLoop(self, policy: AbstractEventLoopPolicy) -> AbstractEventLoop:
Make a new asyncio loop from a policy for use with a reactor, and add
appropriate cleanup to restore any global state.
"""
- existingLoop = get_event_loop()
+ try:
+ existingLoop = get_running_loop()
+ except RuntimeError: # pragma: no branch
+ # For most runs, we should not have any existing loop,
+ # since the tests should leave a clean reactor.
+ # For some cases, like GTK tests,
+ # there might be a running reactor.
+ # To revert the state found at the start of the test
+ # we keep a reference and restore it later.
+ existingLoop = None
existingPolicy = get_event_loop_policy()
result = policy.new_event_loop()
@self.addCleanup
def cleanUp():
result.close()
- set_event_loop(existingLoop)
+ if existingLoop is not None: # pragma: no cover
+ # Revert the loop found at the start of the test.
+ # See https://github.com/twisted/twisted/pull/11706
+ set_event_loop(existingLoop)
set_event_loop_policy(existingPolicy)
return result
|