def _notify_stream_qt(kernel, stream): from IPython.external.qt_for_kernel import QtCore if _use_appnope() and kernel._darwin_app_nap: from appnope import nope_scope as context else: from contextlib import contextmanager @contextmanager def context(): yield def process_stream_events(): while stream.getsockopt(zmq.EVENTS) & zmq.POLLIN: with context(): kernel.do_one_iteration() fd = stream.getsockopt(zmq.FD) notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app) notifier.activated.connect(process_stream_events) # there may already be unprocessed events waiting. # these events will not wake zmq's edge-triggered FD # since edge-triggered notification only occurs on new i/o activity. # process all the waiting events immediately # so we start in a clean state ensuring that any new i/o events will notify. # schedule first call on the eventloop as soon as it's running, # so we don't block here processing events timer = QtCore.QTimer(kernel.app) timer.setSingleShot(True) timer.timeout.connect(process_stream_events) timer.start(0)
def inputhook(context): global _appref app = QtCore.QCoreApplication.instance() if not app: _appref = app = QtGui.QApplication([" "]) event_loop = QtCore.QEventLoop(app) if sys.platform == 'win32': # The QSocketNotifier method doesn't appear to work on Windows. # Use polling instead. timer = QtCore.QTimer() timer.timeout.connect(event_loop.quit) while not context.input_is_ready(): timer.start(50) # 50 ms event_loop.exec_() timer.stop() else: # On POSIX platforms, we can use a file descriptor to quit the event # loop when there is input ready to read. notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read) # connect the callback we care about before we turn it on notifier.activated.connect(event_loop.exit) notifier.setEnabled(True) # only start the event loop we are not already flipped if not context.input_is_ready(): event_loop.exec_()
def _notify_stream_qt(kernel, stream): from IPython.external.qt_for_kernel import QtCore def process_stream_events(): """fall back to main loop when there's a socket event""" # call flush to ensure that the stream doesn't lose events # due to our consuming of the edge-triggered FD # flush returns the number of events consumed. # if there were any, wake it up if stream.flush(limit=1): notifier.setEnabled(False) kernel.app.quit() fd = stream.getsockopt(zmq.FD) notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app) notifier.activated.connect(process_stream_events) # there may already be unprocessed events waiting. # these events will not wake zmq's edge-triggered FD # since edge-triggered notification only occurs on new i/o activity. # process all the waiting events immediately # so we start in a clean state ensuring that any new i/o events will notify. # schedule first call on the eventloop as soon as it's running, # so we don't block here processing events timer = QtCore.QTimer(kernel.app) timer.setSingleShot(True) timer.timeout.connect(process_stream_events) timer.start(0)
def inputhook(context): global _appref app = QtCore.QCoreApplication.instance() if not app: if sys.platform == 'linux': if not os.environ.get('DISPLAY') \ and not os.environ.get('WAYLAND_DISPLAY'): import warnings global _already_warned if not _already_warned: _already_warned = True warnings.warn( 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' 'not set or empty and Qt5 requires this environment ' 'variable. Deactivate Qt5 code.') return try: QtCore.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) except AttributeError: # Only for Qt>=5.6, <6. pass try: QtCore.QApplication.setHighDpiScaleFactorRoundingPolicy( QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) except AttributeError: # Only for Qt>=5.14. pass _appref = app = QtGui.QApplication([" "]) # "reclaim" IPython sys.excepthook after event loop starts # without this, it defaults back to BaseIPythonApplication.excepthook # and exceptions in the Qt event loop are rendered without traceback # formatting and look like "bug in IPython". QtCore.QTimer.singleShot(0, _reclaim_excepthook) event_loop = QtCore.QEventLoop(app) if sys.platform == 'win32': # The QSocketNotifier method doesn't appear to work on Windows. # Use polling instead. timer = QtCore.QTimer() timer.timeout.connect(event_loop.quit) while not context.input_is_ready(): timer.start(50) # 50 ms _exec(event_loop) timer.stop() else: # On POSIX platforms, we can use a file descriptor to quit the event # loop when there is input ready to read. notifier = QtCore.QSocketNotifier( context.fileno(), enum_helper("QtCore.QSocketNotifier.Type").Read) try: # connect the callback we care about before we turn it on notifier.activated.connect(lambda: event_loop.exit()) notifier.setEnabled(True) # only start the event loop we are not already flipped if not context.input_is_ready(): _exec(event_loop) finally: notifier.setEnabled(False)
def inputhook(context): app = QtCore.QCoreApplication.instance() if not app: return event_loop = QtCore.QEventLoop(app) notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read) notifier.setEnabled(True) notifier.activated.connect(event_loop.exit) event_loop.exec_()
def inputhook(context): global _appref app = QtCore.QCoreApplication.instance() if not app: if sys.platform == 'linux': if not os.environ.get('DISPLAY') \ and not os.environ.get('WAYLAND_DISPLAY'): import warnings global _already_warned if not _already_warned: _already_warned = True warnings.warn( 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' 'not set or empty and Qt5 requires this environment ' 'variable. Deactivate Qt5 code.') return QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) _appref = app = QtGui.QApplication([" "]) # "reclaim" IPython sys.excepthook after event loop starts # without this, it defaults back to BaseIPythonApplication.excepthook # and exceptions in the Qt event loop are rendered without traceback # formatting and look like "bug in IPython". QtCore.QTimer.singleShot(0, _reclaim_excepthook) event_loop = QtCore.QEventLoop(app) if sys.platform == 'win32': # The QSocketNotifier method doesn't appear to work on Windows. # Use polling instead. timer = QtCore.QTimer() timer.timeout.connect(event_loop.quit) while not context.input_is_ready(): timer.start(50) # 50 ms event_loop.exec_() timer.stop() else: # On POSIX platforms, we can use a file descriptor to quit the event # loop when there is input ready to read. notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read) try: # connect the callback we care about before we turn it on # lambda is necessary as PyQT inspect the function signature to know # what arguments to pass to. See https://github.com/ipython/ipython/pull/12355 notifier.activated.connect(lambda: event_loop.exit()) notifier.setEnabled(True) # only start the event loop we are not already flipped if not context.input_is_ready(): event_loop.exec_() finally: notifier.setEnabled(False)
def _notify_stream_qt(kernel, stream): from IPython.external.qt_for_kernel import QtCore if _on_os_x_10_9() and kernel._darwin_app_nap: from IPython.external.appnope import nope_scope as context else: from IPython.core.interactiveshell import NoOpContext as context def process_stream_events(): while stream.getsockopt(zmq.EVENTS) & zmq.POLLIN: with context(): kernel.do_one_iteration() fd = stream.getsockopt(zmq.FD) notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app) notifier.activated.connect(process_stream_events)
def inputhook(context): global _appref app = QtCore.QCoreApplication.instance() if not app: if sys.platform == 'linux': if not os.environ.get('DISPLAY') \ and not os.environ.get('WAYLAND_DISPLAY'): import warnings global _already_warned if not _already_warned: _already_warned = True warnings.warn( 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' 'not set or empty and Qt5 requires this environment ' 'variable. Deactivate Qt5 code.' ) return QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) _appref = app = QtGui.QApplication([" "]) event_loop = QtCore.QEventLoop(app) if sys.platform == 'win32': # The QSocketNotifier method doesn't appear to work on Windows. # Use polling instead. timer = QtCore.QTimer() timer.timeout.connect(event_loop.quit) while not context.input_is_ready(): timer.start(50) # 50 ms event_loop.exec_() timer.stop() else: # On POSIX platforms, we can use a file descriptor to quit the event # loop when there is input ready to read. notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read) try: # connect the callback we care about before we turn it on notifier.activated.connect(event_loop.exit) notifier.setEnabled(True) # only start the event loop we are not already flipped if not context.input_is_ready(): event_loop.exec_() finally: notifier.setEnabled(False)
def _notify_stream_qt(kernel, stream): from IPython.external.qt_for_kernel import QtCore if _use_appnope() and kernel._darwin_app_nap: from appnope import nope_scope as context else: from contextlib import contextmanager @contextmanager def context(): yield def process_stream_events(): while stream.getsockopt(zmq.EVENTS) & zmq.POLLIN: with context(): kernel.do_one_iteration() fd = stream.getsockopt(zmq.FD) notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app) notifier.activated.connect(process_stream_events)
def inputhook(context): app = QtCore.QCoreApplication.instance() if not app: return event_loop = QtCore.QEventLoop(app) if sys.platform == 'win32': # The QSocketNotifier method doesn't appear to work on Windows. # Use polling instead. timer = QtCore.QTimer() timer.timeout.connect(event_loop.quit) while not context.input_is_ready(): timer.start(50) # 50 ms event_loop.exec_() timer.stop() else: # On POSIX platforms, we can use a file descriptor to quit the event # loop when there is input ready to read. notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read) notifier.setEnabled(True) notifier.activated.connect(event_loop.exit) event_loop.exec_()