| 1 |
import sys |
| 2 |
import os |
| 3 |
from IPython.external.qt_for_kernel import QtCore, QtGui, enum_helper |
| 4 |
from IPython import get_ipython |
| 5 |
|
| 6 |
# If we create a QApplication, QEventLoop, or a QTimer, keep a reference to them |
| 7 |
# so that they don't get garbage collected or leak memory when created multiple times. |
| 8 |
_appref = None |
| 9 |
_eventloop = None |
| 10 |
_timer = None |
| 11 |
_already_warned = False |
| 12 |
|
| 13 |
|
| 14 |
def _exec(obj): |
| 15 |
# exec on PyQt6, exec_ elsewhere. |
| 16 |
obj.exec() if hasattr(obj, "exec") else obj.exec_() |
| 17 |
|
| 18 |
|
| 19 |
def _reclaim_excepthook(): |
| 20 |
shell = get_ipython() |
| 21 |
if shell is not None: |
| 22 |
sys.excepthook = shell.excepthook |
| 23 |
|
| 24 |
|
| 25 |
def inputhook(context): |
| 26 |
global _appref, _eventloop, _timer |
| 27 |
app = QtCore.QCoreApplication.instance() |
| 28 |
if not app: |
| 29 |
if sys.platform == 'linux': |
| 30 |
if not os.environ.get('DISPLAY') \ |
| 31 |
and not os.environ.get('WAYLAND_DISPLAY'): |
| 32 |
import warnings |
| 33 |
global _already_warned |
| 34 |
if not _already_warned: |
| 35 |
_already_warned = True |
| 36 |
warnings.warn( |
| 37 |
'The DISPLAY or WAYLAND_DISPLAY environment variable is ' |
| 38 |
'not set or empty and Qt5 requires this environment ' |
| 39 |
'variable. Deactivate Qt5 code.' |
| 40 |
) |
| 41 |
return |
| 42 |
try: |
| 43 |
QtCore.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) |
| 44 |
except AttributeError: # Only for Qt>=5.6, <6. |
| 45 |
pass |
| 46 |
try: |
| 47 |
QtCore.QApplication.setHighDpiScaleFactorRoundingPolicy( |
| 48 |
QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough |
| 49 |
) |
| 50 |
except AttributeError: # Only for Qt>=5.14. |
| 51 |
pass |
| 52 |
_appref = app = QtGui.QApplication([" "]) |
| 53 |
|
| 54 |
# "reclaim" IPython sys.excepthook after event loop starts |
| 55 |
# without this, it defaults back to BaseIPythonApplication.excepthook |
| 56 |
# and exceptions in the Qt event loop are rendered without traceback |
| 57 |
# formatting and look like "bug in IPython". |
| 58 |
QtCore.QTimer.singleShot(0, _reclaim_excepthook) |
| 59 |
|
| 60 |
if _eventloop is None: |
| 61 |
_eventloop = QtCore.QEventLoop(app) |
| 62 |
|
| 63 |
if sys.platform == 'win32': |
| 64 |
# The QSocketNotifier method doesn't appear to work on Windows. |
| 65 |
# Use polling instead. |
| 66 |
if _timer is None: |
| 67 |
_timer = QtCore.QTimer() |
| 68 |
_timer.timeout.connect(_eventloop.quit) |
| 69 |
while not context.input_is_ready(): |
| 70 |
# NOTE: run the event loop, and after 10 ms, call `quit` to exit it. |
| 71 |
_timer.start(10) # 10 ms |
| 72 |
_exec(_eventloop) |
| 73 |
_timer.stop() |
| 74 |
else: |
| 75 |
# On POSIX platforms, we can use a file descriptor to quit the event |
| 76 |
# loop when there is input ready to read. |
| 77 |
notifier = QtCore.QSocketNotifier( |
| 78 |
context.fileno(), enum_helper("QtCore.QSocketNotifier.Type").Read |
| 79 |
) |
| 80 |
try: |
| 81 |
# connect the callback we care about before we turn it on |
| 82 |
# lambda is necessary as PyQT inspect the function signature to know |
| 83 |
# what arguments to pass to. See https://github.com/ipython/ipython/pull/12355 |
| 84 |
notifier.activated.connect(lambda: _eventloop.exit()) |
| 85 |
notifier.setEnabled(True) |
| 86 |
# only start the event loop we are not already flipped |
| 87 |
if not context.input_is_ready(): |
| 88 |
_exec(_eventloop) |
| 89 |
finally: |
| 90 |
notifier.setEnabled(False) |