Skip to content

Commit

Permalink
remove old IPython compat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Nov 4, 2024
1 parent a9dc695 commit c7265e5
Showing 1 changed file with 3 additions and 188 deletions.
191 changes: 3 additions & 188 deletions lib/python/pyflyby/_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ def _get_or_create_ipython_terminal_app():
pass
else:
return TerminalIPythonApp.instance()
# The following has been tested on IPython 0.10.
if hasattr(IPython, "ipapi"):
return _IPython010TerminalApplication.instance()
raise RuntimeError(
"Couldn't get TerminalIPythonApp class. "
"Is your IPython version too old (or too new)? "
Expand Down Expand Up @@ -122,51 +119,6 @@ def _app_is_initialized(app):



class _IPython010TerminalApplication(object):
"""
Shim class that mimics IPython 0.11+ application classes, for use in
IPython 0.10.
"""

# IPython.ipapi.launch_instance() => IPython.Shell.start() creates an
# instance of "IPShell". IPShell has an attribute named "IP" which is an
# "InteractiveShell".

_instance = None

@classmethod
def instance(cls):
if cls._instance is not None:
self = cls._instance
self.init_shell()
return self
import IPython
if not hasattr(IPython, "ipapi"):
raise RuntimeError("Inappropriate version of IPython %r"
% (IPython.__version__,))
self = cls._instance = cls()
self.init_shell()
return self

def init_shell(self):
import IPython
ipapi = IPython.ipapi.get() # IPApi instance
if ipapi is not None:
self.shell = ipapi.IP # InteractiveShell instance
else:
self.shell = None

def initialize(self, argv=None):
import IPython
logger.debug("Creating IPython 0.10 session")
self._session = IPython.ipapi.make_session() # IPShell instance
self.init_shell()
assert self._session is not None

def start(self):
self._session.mainloop()



class _DummyIPythonEmbeddedApp(object):
"""
Expand Down Expand Up @@ -412,30 +364,7 @@ def install_in_ipython_config_file():
else:
_install_in_ipython_config_file_40()
return
# The following has been tested on IPython 0.12, 0.13, 1.0, 1.2, 2.0, 2.1,
# 2.2, 2.3, 2.4, 3.0, 3.1, 3.2, 4.0.
try:
IPython.core.profiledir.ProfileDir.startup_dir
except AttributeError:
pass
else:
_install_in_ipython_config_file_012()
return
# The following has been tested on IPython 0.11.
try:
IPython.core.profiledir.ProfileDir
except AttributeError:
pass
else:
_install_in_ipython_config_file_011()
return
try:
IPython.genutils.get_ipython_dir
except AttributeError:
pass
else:
_install_in_ipython_config_file_010()
return

raise RuntimeError(
"Couldn't install pyflyby autoimporter in IPython. "
"Is your IPython version too old (or too new)? "
Expand Down Expand Up @@ -555,96 +484,10 @@ def _install_in_ipython_config_file_40():
logger.info("[DONE] Removed old file %s (moved to %s)", old_fn, trash_fn)


def _install_in_ipython_config_file_012():
"""
Implementation of `install_in_ipython_config_file` for IPython 0.12+.
Tested with IPython 0.12, 0.13, 1.0, 1.2, 2.0, 2.1, 2.2, 2.3, 2.4, 3.0,
3.1, 3.2, 4.0.
"""
import IPython
ipython_dir = Filename(IPython.utils.path.get_ipython_dir())
if not ipython_dir.isdir:
raise RuntimeError(
"Couldn't find IPython config dir. Tried %s" % (ipython_dir,))
startup_dir = ipython_dir / "profile_default" / "startup"
if not startup_dir.isdir:
raise RuntimeError(
"Couldn't find IPython startup dir. Tried %s" % (startup_dir,))
fn = startup_dir / "50-pyflyby.py"
if fn.exists:
logger.info("Doing nothing, because %s already exists", fn)
return
argv = sys.argv[:]
argv[0] = os.path.realpath(argv[0])
argv = ' '.join(argv)
header = (
"# File: {fn}\n"
"#\n"
"# Generated by {argv}\n"
"#\n"
"# This file causes IPython to enable the Pyflyby Auto Importer.\n"
"#\n"
"# To uninstall, just delete this file.\n"
"#\n"
).format(**locals())
contents = header + _generate_enabler_code()
logger.info("Installing pyflyby auto importer in your IPython startup")
logger.info("Writing to %s:\n%s", fn, contents)
atomic_write_file(fn, contents)


def _install_in_ipython_config_file_011():
"""
Implementation of `install_in_ipython_config_file` for IPython 0.11.
"""
import IPython
ipython_dir = Filename(IPython.utils.path.get_ipython_dir())
fn = ipython_dir / "profile_default" / "ipython_config.py"
if not fn.exists:
raise RuntimeError(
"Couldn't find IPython startup file. Tried %s" % (fn,))
old_contents = read_file(fn).joined
if re.search(r"^ *(pyflyby[.])?enable_auto_importer[(][)]", old_contents, re.M):
logger.info("Doing nothing, because already installed in %s", fn)
return
header = (
"\n"
"\n"
"#\n"
"# Enable the Pyflyby Auto Importer.\n"
)
new_contents = header + _generate_enabler_code()
contents = old_contents.rstrip() + new_contents
logger.info("Installing pyflyby auto importer in your IPython startup")
logger.info("Appending to %s:\n%s", fn, new_contents)
atomic_write_file(fn, contents)


def _install_in_ipython_config_file_010():
"""
Implementation of `install_in_ipython_config_file` for IPython 0.10.
"""
import IPython
ipython_dir = Filename(IPython.genutils.get_ipython_dir())
fn = ipython_dir / "ipy_user_conf.py"
if not fn.exists:
raise RuntimeError(
"Couldn't find IPython config file. Tried %s" % (fn,))
old_contents = read_file(fn).joined
if re.search(r"^ *(pyflyby[.])?enable_auto_importer[(][)]", old_contents, re.M):
logger.info("Doing nothing, because already installed in %s", fn)
return
header = (
"\n"
"\n"
"#\n"
"# Enable the Pyflyby Auto Importer.\n"
)
new_contents = header + _generate_enabler_code()
contents = old_contents.rstrip() + new_contents
logger.info("Installing pyflyby auto importer in your IPython startup")
logger.info("Appending to %s:\n%s", fn, new_contents)
atomic_write_file(fn, contents)




def _ipython_in_multiline(ip):
Expand Down Expand Up @@ -725,34 +568,6 @@ def get_prompt_rlipython():
elif hasattr(ip, "prompt_manager"):
# IPython >= 0.12 (known to work including up to 1.2, 2.1)
prompt_manager = ip.prompt_manager
def get_prompt_ipython_012():
pdb_instance = _get_pdb_if_is_in_pdb()
if pdb_instance is not None:
return pdb_instance.prompt
elif _ipython_in_multiline(ip):
return prompt_manager.render("in2")
else:
return ip.separate_in + prompt_manager.render("in")
get_prompt = get_prompt_ipython_012
elif hasattr(ip.hooks, "generate_prompt"):
# IPython 0.10, 0.11
generate_prompt = ip.hooks.generate_prompt
def get_prompt_ipython_010():
pdb_instance = _get_pdb_if_is_in_pdb()
if pdb_instance is not None:
return pdb_instance.prompt
elif _ipython_in_multiline(ip):
return generate_prompt(True)
else:
if hasattr(ip, "outputcache"):
# IPython 0.10 (but not 0.11+):
# Decrement the prompt_count since it otherwise
# auto-increments. (It's hard to avoid the
# auto-increment as it happens as a side effect of
# __str__!)
ip.outputcache.prompt_count -= 1
return generate_prompt(False)
get_prompt = get_prompt_ipython_010
else:
# Too old or too new IPython version?
return NullCtx()
Expand Down

0 comments on commit c7265e5

Please sign in to comment.