Show More
@@ -1,54 +1,68 b'' | |||
|
1 | 1 | import sys |
|
2 | 2 | import os |
|
3 | 3 | from IPython.external.qt_for_kernel import QtCore, QtGui |
|
4 | from IPython import get_ipython | |
|
4 | 5 | |
|
5 | 6 | # If we create a QApplication, keep a reference to it so that it doesn't get |
|
6 | 7 | # garbage collected. |
|
7 | 8 | _appref = None |
|
8 | 9 | _already_warned = False |
|
9 | 10 | |
|
10 | 11 | |
|
12 | def _reclaim_excepthook(): | |
|
13 | shell = get_ipython() | |
|
14 | if shell is not None: | |
|
15 | sys.excepthook = shell.excepthook | |
|
16 | ||
|
17 | ||
|
11 | 18 | def inputhook(context): |
|
12 | 19 | global _appref |
|
13 | 20 | app = QtCore.QCoreApplication.instance() |
|
14 | 21 | if not app: |
|
15 | 22 | if sys.platform == 'linux': |
|
16 | 23 | if not os.environ.get('DISPLAY') \ |
|
17 | 24 | and not os.environ.get('WAYLAND_DISPLAY'): |
|
18 | 25 | import warnings |
|
19 | 26 | global _already_warned |
|
20 | 27 | if not _already_warned: |
|
21 | 28 | _already_warned = True |
|
22 | 29 | warnings.warn( |
|
23 | 30 | 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' |
|
24 | 31 | 'not set or empty and Qt5 requires this environment ' |
|
25 | 32 | 'variable. Deactivate Qt5 code.' |
|
26 | 33 | ) |
|
27 | 34 | return |
|
28 | 35 | QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) |
|
29 | 36 | _appref = app = QtGui.QApplication([" "]) |
|
37 | ||
|
38 | # "reclaim" IPython sys.excepthook after event loop starts | |
|
39 | # without this, it defaults back to BaseIPythonApplication.excepthook | |
|
40 | # and exceptions in the Qt event loop are rendered without traceback | |
|
41 | # formatting and look like "bug in IPython". | |
|
42 | QtCore.QTimer.singleShot(0, _reclaim_excepthook) | |
|
43 | ||
|
30 | 44 | event_loop = QtCore.QEventLoop(app) |
|
31 | 45 | |
|
32 | 46 | if sys.platform == 'win32': |
|
33 | 47 | # The QSocketNotifier method doesn't appear to work on Windows. |
|
34 | 48 | # Use polling instead. |
|
35 | 49 | timer = QtCore.QTimer() |
|
36 | 50 | timer.timeout.connect(event_loop.quit) |
|
37 | 51 | while not context.input_is_ready(): |
|
38 | 52 | timer.start(50) # 50 ms |
|
39 | 53 | event_loop.exec_() |
|
40 | 54 | timer.stop() |
|
41 | 55 | else: |
|
42 | 56 | # On POSIX platforms, we can use a file descriptor to quit the event |
|
43 | 57 | # loop when there is input ready to read. |
|
44 | 58 | notifier = QtCore.QSocketNotifier(context.fileno(), |
|
45 | 59 | QtCore.QSocketNotifier.Read) |
|
46 | 60 | try: |
|
47 | 61 | # connect the callback we care about before we turn it on |
|
48 | 62 | notifier.activated.connect(lambda: event_loop.exit()) |
|
49 | 63 | notifier.setEnabled(True) |
|
50 | 64 | # only start the event loop we are not already flipped |
|
51 | 65 | if not context.input_is_ready(): |
|
52 | 66 | event_loop.exec_() |
|
53 | 67 | finally: |
|
54 | 68 | notifier.setEnabled(False) |
General Comments 0
You need to be logged in to leave comments.
Login now