Debian Codegraph @

tests/test_eventloop.py

raw
1 """Test eventloop integration"""
2
3 import asyncio
4 import os
5 import sys
6 import threading
7 import time
8
9 import pytest
10
11 from ipykernel.eventloops import (
12 enable_gui,
13 loop_asyncio,
14 loop_cocoa,
15 loop_tk,
16 )
17
18 from .utils import flush_channels, start_new_kernel
19
20 KC = KM = None
21
22 qt_guis_avail = []
23
24 gui_to_module = {"qt6": "PySide6", "qt5": "PyQt5"}
25
26
27 def _get_qt_vers():
28 """If any version of Qt is available, this will populate `guis_avail` with 'qt' and 'qtx'. Due
29 to the import mechanism, we can't import multiple versions of Qt in one session."""
30 for gui in ["qt6", "qt5"]:
31 print(f"Trying {gui}")
32 try:
33 __import__(gui_to_module[gui])
34 qt_guis_avail.append(gui)
35 if "QT_API" in os.environ:
36 del os.environ["QT_API"]
37 except ImportError:
38 pass # that version of Qt isn't available.
39
40
41 _get_qt_vers()
42
43
44 @pytest.fixture(autouse=True)
45 def _setup_env():
46 """start the global kernel (if it isn't running) and return its client"""
47 global KM, KC
48 KM, KC = start_new_kernel()
49 flush_channels(KC)
50 yield
51 assert KM is not None
52 assert KC is not None
53 KC.stop_channels()
54 KM.shutdown_kernel(now=True)
55
56
57 windows_skip = pytest.mark.skipif(os.name == "nt", reason="causing failures on windows")
58
59 # some part of this module seems to hang when run with xvfb
60 pytestmark = pytest.mark.skipif(
61 sys.platform == "linux" and bool(os.getenv("CI")), reason="hangs on linux CI"
62 )
63
64
65 @windows_skip
66 @pytest.mark.skipif(sys.platform == "darwin", reason="hangs on macos")
67 def test_tk_loop(kernel):
68 def do_thing():
69 time.sleep(1)
70 try:
71 kernel.app_wrapper.app.quit()
72 # guard for tk failing to start (if there is no display)
73 except AttributeError:
74 pass
75
76 t = threading.Thread(target=do_thing)
77 t.start()
78 # guard for tk failing to start (if there is no display)
79 try:
80 loop_tk(kernel)
81 except Exception:
82 pass
83 t.join()
84
85
86 @windows_skip
87 def test_asyncio_loop(kernel):
88 def do_thing():
89 loop.call_soon(loop.stop)
90
91 loop = asyncio.get_event_loop()
92 loop.call_soon(do_thing)
93 loop_asyncio(kernel)
94
95
96 @windows_skip
97 def test_enable_gui(kernel):
98 enable_gui("tk", kernel)
99
100
101 @pytest.mark.skipif(sys.platform != "darwin", reason="MacOS-only")
102 def test_cocoa_loop(kernel):
103 loop_cocoa(kernel)
104
105
106 @pytest.mark.parametrize("gui", qt_guis_avail)
107 def test_qt_enable_gui(gui, kernel, capsys):
108 if os.getenv("GITHUB_ACTIONS", None) == "true" and gui == "qt5":
109 pytest.skip("Qt5 and GitHub action crash CPython")
110 if gui == "qt6" and sys.version_info < (3, 10):
111 pytest.skip(
112 "qt6 fails on 3.9 with AttributeError: module 'PySide6.QtPrintSupport' has no attribute 'QApplication'"
113 )
114 if sys.platform == "linux" and gui == "qt6" and os.getenv("GITHUB_ACTIONS", None) == "true":
115 pytest.skip("qt6 fails on github CI with missing libEGL.so.1")
116 enable_gui(gui, kernel)
117
118 # We store the `QApplication` instance in the kernel.
119 assert hasattr(kernel, "app")
120
121 # And the `QEventLoop` is added to `app`:`
122 assert hasattr(kernel.app, "qt_event_loop")
123
124 # Don't create another app even if `gui` is the same.
125 app = kernel.app
126 enable_gui(gui, kernel)
127 assert app == kernel.app
128
129 # Event loop integration can be turned off.
130 enable_gui(None, kernel)
131 assert not hasattr(kernel, "app")
132
133 # But now we're stuck with this version of Qt for good; can't switch.
134 for not_gui in ["qt6", "qt5"]:
135 if not_gui not in qt_guis_avail:
136 break
137
138 enable_gui(not_gui, kernel)
139 captured = capsys.readouterr()
140 assert captured.out == f"Cannot switch Qt versions for this session; you must use {gui}.\n"
141
142 # Check 'qt' gui, which means "the best available"
143 enable_gui(None, kernel)
144 enable_gui("qt", kernel)
145 assert gui_to_module[gui] in str(kernel.app)