From 6ee28e22e4f71d63ac76c24fcc6019ecd6732a10 2014-09-28 03:25:23 From: MinRK Date: 2014-09-28 03:25:23 Subject: [PATCH] handle missing 'text/plain' in displayhook avoids KeyError when repr fails --- diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py index 8addec1..63b54d4 100644 --- a/IPython/core/displayhook.py +++ b/IPython/core/displayhook.py @@ -2,25 +2,11 @@ """Displayhook for IPython. This defines a callable class that IPython uses for `sys.displayhook`. - -Authors: - -* Fernando Perez -* Brian Granger -* Robert Kern """ -#----------------------------------------------------------------------------- -# Copyright (C) 2008-2011 The IPython Development Team -# Copyright (C) 2001-2007 Fernando Perez -# -# 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. + from __future__ import print_function import sys @@ -32,10 +18,6 @@ from IPython.utils.py3compat import builtin_mod from IPython.utils.traitlets import Instance from IPython.utils.warn import warn -#----------------------------------------------------------------------------- -# Main displayhook class -#----------------------------------------------------------------------------- - # TODO: Move the various attributes (cache_size, [others now moved]). Some # of these are also attributes of InteractiveShell. They should be on ONE object # only and the other objects should ask that one object for their values. @@ -168,6 +150,9 @@ class DisplayHook(Configurable): md_dict : dict (optional) The metadata dict to be associated with the display data. """ + if 'text/plain' not in format_dict: + # nothing to do + return # We want to print because we want to always make sure we have a # newline, even if all the prompt separators are ''. This is the # standard IPython behavior. @@ -221,6 +206,9 @@ class DisplayHook(Configurable): def log_output(self, format_dict): """Log the output.""" + if 'text/plain' not in format_dict: + # nothing to do + return if self.shell.logger.log_output: self.shell.logger.log_write(format_dict['text/plain'], 'output') self.shell.history_manager.output_hist_reprs[self.prompt_count] = \