Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable _ipython_display_ in terminal IPython #10249

Merged
merged 2 commits into from Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions IPython/core/tests/test_formatters.py
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
Expand Up @@ -203,6 +203,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
@@ -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