From 6931cc974328dafe3f306e23b8c12aaebc08739b Mon Sep 17 00:00:00 2001 From: Pierre LALET Date: Wed, 18 Oct 2017 14:19:54 +0200 Subject: [PATCH] Fix IPython: check for old versions --- scapy/main.py | 78 ++++++++++++++++++++++++++----------------------- scapy/themes.py | 9 +++++- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/scapy/main.py b/scapy/main.py index 48680b029..865f97e1c 100644 --- a/scapy/main.py +++ b/scapy/main.py @@ -17,11 +17,6 @@ import logging from random import choice import types -try: - import IPython # Allow testing -except: - pass - # Never add any global import, in main.py, that would trigger a warning messsage # before the console handlers gets added in interact() from scapy.error import log_interactive, log_loading, log_scapy, warning @@ -477,44 +472,55 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20): if mybanner is not None: the_banner += "\n" the_banner += mybanner - - IPYTHON=False - if not conf.interactive_shell or conf.interactive_shell.lower() in ["ipython", "auto"]: + + if not conf.interactive_shell or conf.interactive_shell.lower() in [ + "ipython", "auto" + ]: try: - IPython - IPYTHON=True - except NameError as e: - log_loading.warning("IPython not available. Using standard Python shell instead. " - "AutoCompletion, History are disabled.") - IPYTHON=False + import IPython + from IPython.terminal.embed import InteractiveShellEmbed + except ImportError: + log_loading.warning( + "IPython not available. Using standard Python shell " + "instead.\nAutoCompletion, History are disabled." + ) + IPYTHON = False + else: + IPYTHON = True + else: + IPYTHON = False init_session(session_name, mydict) if IPYTHON: banner = the_banner + " using IPython %s\n" % IPython.__version__ - from IPython.terminal.embed import InteractiveShellEmbed - from IPython.terminal.prompts import Prompts, Token - from traitlets.config.loader import Config - from scapy.packet import Packet - - cfg = Config() - try: - get_ipython - except NameError: - # Set "classic" prompt style when launched from run_scapy(.bat) files - apply_ipython_style(shell=cfg.TerminalInteractiveShell) # Register and apply scapy color+prompt style - cfg.TerminalInteractiveShell.confirm_exit = False # Remove confirm exit - cfg.TerminalInteractiveShell.separate_in = u'' # Remove spacing line - - cfg.TerminalInteractiveShell.hist_file = conf.histfile - - # configuration can thus be specified here. - ipshell = InteractiveShellEmbed(config=cfg, - banner1=banner, - hist_file=conf.histfile if conf.histfile else None, - user_ns=SESSION) - + from traitlets.config.loader import Config + except ImportError: + log_loading.warning( + "traitlets not available. Some Scapy shell features won't be " + "available." + ) + ipshell = InteractiveShellEmbed( + banner1=banner, + user_ns=SESSION, + ) + else: + cfg = Config() + try: + get_ipython + except NameError: + # Set "classic" prompt style when launched from run_scapy(.bat) files + # Register and apply scapy color+prompt style + apply_ipython_style(shell=cfg.TerminalInteractiveShell) + cfg.TerminalInteractiveShell.confirm_exit = False + cfg.TerminalInteractiveShell.separate_in = u'' + cfg.TerminalInteractiveShell.hist_file = conf.histfile + # configuration can thus be specified here. + ipshell = InteractiveShellEmbed(config=cfg, + banner1=banner, + hist_file=conf.histfile if conf.histfile else None, + user_ns=SESSION) ipshell(local_ns=SESSION) else: code.interact(banner = the_banner, local=SESSION) diff --git a/scapy/themes.py b/scapy/themes.py index 173194ef4..0ec5b720d 100644 --- a/scapy/themes.py +++ b/scapy/themes.py @@ -291,7 +291,14 @@ class HTMLTheme2(HTMLTheme): def apply_ipython_style(shell): """Updates the specified IPython console shell with the conf.color_theme scapy theme.""" - from IPython.terminal.prompts import Prompts, Token + try: + from IPython.terminal.prompts import Prompts, Token + except: + from scapy.error import log_loading + log_loading.warning( + "IPython too old. Some Scapy shell features won't be available." + ) + return from scapy.config import conf if isinstance(conf.prompt, Prompts): shell.prompts_class = conf.prompt # Set custom prompt style