Skip to content

Commit

Permalink
Backport PR #10249: disable _ipython_display_ in terminal IPython
Browse files Browse the repository at this point in the history
We already try to disable all formatters other than text/plain. The ipython_display formatter is not in that mime-type list, so it was not being disabled with the others.

closes  10247
  • Loading branch information
takluyver authored and MeeseeksDev[bot] committed Feb 9, 2017
1 parent b819394 commit 39717ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions IPython/core/tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ def __repr__(self):
def _ipython_display_(self):
raise NotImplementedError

save_enabled = f.ipython_display_formatter.enabled
f.ipython_display_formatter.enabled = True

yes = SelfDisplaying()
no = NotSelfDisplaying()

Expand All @@ -420,6 +423,9 @@ def _ipython_display_(self):
nt.assert_equal(md, {})
nt.assert_equal(catcher, [yes])

f.ipython_display_formatter.enabled = save_enabled


def test_json_as_string_deprecated():
class JSONString(object):
def _repr_json_(self):
Expand Down
2 changes: 2 additions & 0 deletions IPython/terminal/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def init_display_formatter(self):
super(TerminalInteractiveShell, self).init_display_formatter()
# terminal only supports plain text
self.display_formatter.active_types = ['text/plain']
# disable `_ipython_display_`
self.display_formatter.ipython_display_formatter.enabled = False

def init_prompt_toolkit_cli(self):
if self.simple_prompt:
Expand Down
36 changes: 30 additions & 6 deletions IPython/terminal/tests/test_interactivshell.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# -*- coding: utf-8 -*-
"""Tests for the TerminalInteractiveShell and related pieces."""
#-----------------------------------------------------------------------------
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import sys
import unittest

from IPython.core.inputtransformer import InputTransformer
from IPython.testing import tools as tt
from IPython.utils.capture import capture_output

# Decorator for interaction loop tests -----------------------------------------

Expand Down Expand Up @@ -99,6 +96,33 @@ def test_plain_text_only(self):
ip = get_ipython()
formatter = ip.display_formatter
assert formatter.active_types == ['text/plain']
assert not formatter.ipython_display_formatter.enabled

class Test(object):
def __repr__(self):
return "<Test %i>" % id(self)

def _repr_html_(self):
return '<html>'

# verify that HTML repr isn't computed
obj = Test()
data, _ = formatter.format(obj)
self.assertEqual(data, {'text/plain': repr(obj)})

class Test2(Test):
def _ipython_display_(self):
from IPython.display import display
display('<custom>')

# verify that _ipython_display_ shortcut isn't called
obj = Test2()
with capture_output() as captured:
data, _ = formatter.format(obj)

self.assertEqual(data, {'text/plain': repr(obj)})
assert captured.stdout == ''



class SyntaxErrorTransformer(InputTransformer):
Expand Down

0 comments on commit 39717ca

Please sign in to comment.