diff --git a/IPython/external/appnope/__init__.py b/IPython/external/appnope/__init__.py deleted file mode 100644 index e1f1e68..0000000 --- a/IPython/external/appnope/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ - -try: - from appnope import * -except ImportError: - __version__ = '0.0.5' - import sys - import platform - from distutils.version import LooseVersion as V - - if sys.platform != "darwin" or V(platform.mac_ver()[0]) < V("10.9"): - from ._dummy import * - else: - from ._nope import * - - del sys, platform, V diff --git a/IPython/external/appnope/_dummy.py b/IPython/external/appnope/_dummy.py deleted file mode 100644 index a55ec5b..0000000 --- a/IPython/external/appnope/_dummy.py +++ /dev/null @@ -1,30 +0,0 @@ -#----------------------------------------------------------------------------- -# Copyright (C) 2013 Min RK -# -# Distributed under the terms of the 2-clause BSD License. -#----------------------------------------------------------------------------- - -from contextlib import contextmanager - -def beginActivityWithOptions(options, reason=""): - return - -def endActivity(activity): - return - -def nope(): - return - -def nap(): - return - - -@contextmanager -def nope_scope( - options=0, - reason="Because Reasons" - ): - yield - -def napping_allowed(): - return True \ No newline at end of file diff --git a/IPython/external/appnope/_nope.py b/IPython/external/appnope/_nope.py deleted file mode 100644 index 70a3493..0000000 --- a/IPython/external/appnope/_nope.py +++ /dev/null @@ -1,126 +0,0 @@ -#----------------------------------------------------------------------------- -# Copyright (C) 2013 Min RK -# -# Distributed under the terms of the 2-clause BSD License. -#----------------------------------------------------------------------------- - -from contextlib import contextmanager - -import ctypes -import ctypes.util - -objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc')) - -void_p = ctypes.c_void_p -ull = ctypes.c_uint64 - -objc.objc_getClass.restype = void_p -objc.sel_registerName.restype = void_p -objc.objc_msgSend.restype = void_p -objc.objc_msgSend.argtypes = [void_p, void_p] - -msg = objc.objc_msgSend - -def _utf8(s): - """ensure utf8 bytes""" - if not isinstance(s, bytes): - s = s.encode('utf8') - return s - -def n(name): - """create a selector name (for methods)""" - return objc.sel_registerName(_utf8(name)) - -def C(classname): - """get an ObjC Class by name""" - return objc.objc_getClass(_utf8(classname)) - -# constants from Foundation - -NSActivityIdleDisplaySleepDisabled = (1 << 40) -NSActivityIdleSystemSleepDisabled = (1 << 20) -NSActivitySuddenTerminationDisabled = (1 << 14) -NSActivityAutomaticTerminationDisabled = (1 << 15) -NSActivityUserInitiated = (0x00FFFFFF | NSActivityIdleSystemSleepDisabled) -NSActivityUserInitiatedAllowingIdleSystemSleep = (NSActivityUserInitiated & ~NSActivityIdleSystemSleepDisabled) -NSActivityBackground = 0x000000FF -NSActivityLatencyCritical = 0xFF00000000 - -def beginActivityWithOptions(options, reason=""): - """Wrapper for: - - [ [ NSProcessInfo processInfo] - beginActivityWithOptions: (uint64)options - reason: (str)reason - ] - """ - NSProcessInfo = C('NSProcessInfo') - NSString = C('NSString') - - reason = msg(NSString, n("stringWithUTF8String:"), _utf8(reason)) - info = msg(NSProcessInfo, n('processInfo')) - activity = msg(info, - n('beginActivityWithOptions:reason:'), - ull(options), - void_p(reason) - ) - return activity - -def endActivity(activity): - """end a process activity assertion""" - NSProcessInfo = C('NSProcessInfo') - info = msg(NSProcessInfo, n('processInfo')) - msg(info, n("endActivity:"), void_p(activity)) - -_theactivity = None - -def nope(): - """disable App Nap by setting NSActivityUserInitiatedAllowingIdleSystemSleep""" - global _theactivity - _theactivity = beginActivityWithOptions( - NSActivityUserInitiatedAllowingIdleSystemSleep, - "Because Reasons" - ) - -def nap(): - """end the caffeinated state started by `nope`""" - global _theactivity - if _theactivity is not None: - endActivity(_theactivity) - _theactivity = None - -def napping_allowed(): - """is napping allowed?""" - return _theactivity is None - -@contextmanager -def nope_scope( - options=NSActivityUserInitiatedAllowingIdleSystemSleep, - reason="Because Reasons" - ): - """context manager for beginActivityWithOptions. - - Within this context, App Nap will be disabled. - """ - activity = beginActivityWithOptions(options, reason) - try: - yield - finally: - endActivity(activity) - -__all__ = [ - "NSActivityIdleDisplaySleepDisabled", - "NSActivityIdleSystemSleepDisabled", - "NSActivitySuddenTerminationDisabled", - "NSActivityAutomaticTerminationDisabled", - "NSActivityUserInitiated", - "NSActivityUserInitiatedAllowingIdleSystemSleep", - "NSActivityBackground", - "NSActivityLatencyCritical", - "beginActivityWithOptions", - "endActivity", - "nope", - "nap", - "napping_allowed", - "nope_scope", -] diff --git a/IPython/kernel/zmq/eventloops.py b/IPython/kernel/zmq/eventloops.py index 33eea0e..309413e 100644 --- a/IPython/kernel/zmq/eventloops.py +++ b/IPython/kernel/zmq/eventloops.py @@ -11,19 +11,14 @@ import zmq from IPython.config.application import Application from IPython.utils import io - - -def _on_os_x_10_9(): - import platform - from distutils.version import LooseVersion as V - return sys.platform == 'darwin' and V(platform.mac_ver()[0]) >= V('10.9') +from IPython.lib.inputhook import _use_appnope 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 + if _use_appnope() and kernel._darwin_app_nap: + from appnope import nope_scope as context else: from IPython.core.interactiveshell import NoOpContext as context @@ -93,10 +88,10 @@ def loop_wx(kernel): import wx from IPython.lib.guisupport import start_event_loop_wx - if _on_os_x_10_9() and kernel._darwin_app_nap: + if _use_appnope() and kernel._darwin_app_nap: # we don't hook up App Nap contexts for Wx, # just disable it outright. - from IPython.external.appnope import nope + from appnope import nope nope() doi = kernel.do_one_iteration diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index a3d741b..baeed8f 100644 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -3,16 +3,8 @@ Inputhook management for GUI event loop integration. """ -#----------------------------------------------------------------------------- -# Copyright (C) 2008-2011 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. try: import ctypes @@ -21,6 +13,7 @@ except ImportError: except SystemError: # IronPython issue, 2/8/2014 ctypes = None import os +import platform import sys from distutils.version import LooseVersion as V @@ -57,8 +50,14 @@ def _stdin_ready_nt(): def _stdin_ready_other(): """Return True, assuming there's something to read on stdin.""" - return True # + return True + +def _use_appnope(): + """Should we use appnope for dealing with OS X app nap? + Checks if we are on OS X 10.9 or greater. + """ + return sys.platform == 'darwin' and V(platform.mac_ver()[0]) >= V('10.9') def _ignore_CTRL_C_posix(): """Ignore CTRL+C (SIGINT).""" @@ -317,9 +316,10 @@ class WxInputHook(InputHookBase): raise ValueError("requires wxPython >= 2.8, but you have %s" % wx.__version__) from IPython.lib.inputhookwx import inputhook_wx - from IPython.external.appnope import nope self.manager.set_inputhook(inputhook_wx) - nope() + if _use_appnope(): + from appnope import nope + nope() import wx if app is None: @@ -334,8 +334,9 @@ class WxInputHook(InputHookBase): This restores appnapp on OS X """ - from IPython.external.appnope import nap - nap() + if _use_appnope(): + from appnope import nap + nap() @inputhook_manager.register('qt', 'qt4') class Qt4InputHook(InputHookBase): @@ -362,10 +363,11 @@ class Qt4InputHook(InputHookBase): app = QtGui.QApplication(sys.argv) """ from IPython.lib.inputhookqt4 import create_inputhook_qt4 - from IPython.external.appnope import nope - app, inputhook_qt4 = create_inputhook_qt4(self.manager, app) + app, inputhook_qt4 = create_inputhook_qt4(self, app) self.manager.set_inputhook(inputhook_qt4) - nope() + if _use_appnope(): + from appnope import nope + nope() return app @@ -374,8 +376,9 @@ class Qt4InputHook(InputHookBase): This restores appnapp on OS X """ - from IPython.external.appnope import nap - nap() + if _use_appnope(): + from appnope import nap + nap() @inputhook_manager.register('qt5') diff --git a/setup.py b/setup.py index 54638ed..abf1f1a 100755 --- a/setup.py +++ b/setup.py @@ -275,6 +275,7 @@ install_requires = [ # add readline if sys.platform == 'darwin': + install_requires.append('appnope') if 'bdist_wheel' in sys.argv[1:] or not setupext.check_for_readline(): install_requires.append('gnureadline') elif sys.platform.startswith('win'): diff --git a/setupbase.py b/setupbase.py index 4fe4666..6d3762a 100644 --- a/setupbase.py +++ b/setupbase.py @@ -670,7 +670,7 @@ def get_bdist_wheel(): if found: lis.pop(idx) - for pkg in ("gnureadline", "pyreadline", "mock", "terminado"): + for pkg in ("gnureadline", "pyreadline", "mock", "appnope", "terminado"): _remove_startswith(requires, pkg) requires.append("gnureadline; sys.platform == 'darwin' and platform.python_implementation == 'CPython'") requires.append("terminado (>=0.3.3); extra == 'notebook' and sys.platform != 'win32'") @@ -678,6 +678,7 @@ def get_bdist_wheel(): requires.append("pyreadline (>=2.0); extra == 'terminal' and sys.platform == 'win32' and platform.python_implementation == 'CPython'") requires.append("pyreadline (>=2.0); extra == 'all' and sys.platform == 'win32' and platform.python_implementation == 'CPython'") requires.append("mock; extra == 'test' and python_version < '3.3'") + requires.append("appnope; sys.platform == 'darwin'") for r in requires: pkg_info['Requires-Dist'] = r write_pkg_info(metadata_path, pkg_info)