##// 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:

r13353:0ca701d5
r23138:fb70b991
Show More
macro.py
57 lines | 1.8 KiB | text/x-python | PythonLexer
"""Support for interactive macros in IPython"""
#*****************************************************************************
# Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#*****************************************************************************
import re
from IPython.utils import py3compat
from IPython.utils.encoding import DEFAULT_ENCODING
coding_declaration = re.compile(r"#\s*coding[:=]\s*([-\w.]+)")
class Macro(object):
"""Simple class to store the value of macros as strings.
Macro is just a callable that executes a string of IPython
input when called.
"""
def __init__(self,code):
"""store the macro value, as a single string which can be executed"""
lines = []
enc = None
for line in code.splitlines():
coding_match = coding_declaration.match(line)
if coding_match:
enc = coding_match.group(1)
else:
lines.append(line)
code = "\n".join(lines)
if isinstance(code, bytes):
code = code.decode(enc or DEFAULT_ENCODING)
self.value = code + '\n'
def __str__(self):
return py3compat.unicode_to_str(self.value)
def __unicode__(self):
return self.value
def __repr__(self):
return 'IPython.macro.Macro(%s)' % repr(self.value)
def __getstate__(self):
""" needed for safe pickling via %store """
return {'value': self.value}
def __add__(self, other):
if isinstance(other, Macro):
return Macro(self.value + other.value)
elif isinstance(other, py3compat.string_types):
return Macro(self.value + other)
raise TypeError