##// END OF EJS Templates
Backport PR #9976: Let IPython.lib.guisupport detect terminal-integrated event loops...
Backport PR #9976: Let IPython.lib.guisupport detect terminal-integrated event loops Closes gh-9974 This is a bit more invasive than most backported changes, but it fixes a regression in IPython 5. My thinking: - The `guisupport` APIs that worked before should continue working until/unless we deprecate them. - There should be a common way to check if an event loop is already running in both the terminal and an IPython kernel. - It should be possible to check for any event loop, not just Qt and Wx (which `guisupport` has checks for). My plan is to make a public attribute `shell.active_eventloop`, which is either None or a string naming the event loop which IPython will run when waiting for input. E.g. `qt` or `gtk3`. (Todo: should we also expose the event loop object in cases where there is one? Not sure if anything useful can be done with it). This PR adds that attribute for terminal IPython; if we agree on it I'll make a separate PR for ipykernel. The functions in guisupport then become a convenient shortcut for checking this, and we can decide whether to deprecate them in favour or something more uniform, or add similar convenience functions for other common event loops. Signed-off-by: Thomas Kluyver <thomas@kluyver.me.uk>

File last commit:

r22148:9bf4fac3
r23138:fb70b991
Show More
dir2.py
81 lines | 2.1 KiB | text/x-python | PythonLexer
# encoding: utf-8
"""A fancy version of Python's builtin :func:`dir` function.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import inspect
from .py3compat import string_types
def safe_hasattr(obj, attr):
"""In recent versions of Python, hasattr() only catches AttributeError.
This catches all errors.
"""
try:
getattr(obj, attr)
return True
except:
return False
def dir2(obj):
"""dir2(obj) -> list of strings
Extended version of the Python builtin dir(), which does a few extra
checks.
This version is guaranteed to return only a list of true strings, whereas
dir() returns anything that objects inject into themselves, even if they
are later not really valid for attribute access (many extension libraries
have such bugs).
"""
# Start building the attribute list via dir(), and then complete it
# with a few extra special-purpose calls.
try:
words = set(dir(obj))
except Exception:
# TypeError: dir(obj) does not return a list
words = set()
# filter out non-string attributes which may be stuffed by dir() calls
# and poor coding in third-party modules
words = [w for w in words if isinstance(w, string_types)]
return sorted(words)
def get_real_method(obj, name):
"""Like getattr, but with a few extra sanity checks:
- If obj is a class, ignore its methods
- Check if obj is a proxy that claims to have all attributes
- Catch attribute access failing with any exception
- Check that the attribute is a callable object
Returns the method or None.
"""
if inspect.isclass(obj):
return None
try:
canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
except Exception:
return None
if canary is not None:
# It claimed to have an attribute it should never have
return None
try:
m = getattr(obj, name, None)
except Exception:
return None
if callable(m):
return m
return None