##// END OF EJS Templates
protect IPython from bad custom exception handlers...
protect IPython from bad custom exception handlers Previously, errors in custom handlers would result in the custom exception handler's error being printed in lieu of the real exception, and certain cases could cause infinite loops. Now, if CustomTB fails it is unregistered immediately, and the original TB is also displayed. IPython's own BdbQuit_IPython_excepthook had an invalid signature, which revealed this issue, and has also been fixed. test included. closes #692

File last commit:

r4991:ed5078c9
r4991:ed5078c9
Show More
test_interactiveshell.py
166 lines | 6.2 KiB | text/x-python | PythonLexer
/ IPython / core / tests / test_interactiveshell.py
Fernando Perez
Fix bug with execution of naked multiline strings....
r3300 """Tests for the key interactiveshell module.
Historically the main classes in interactiveshell have been under-tested. This
module should grow as many single-method tests as possible to trap many of the
recurring bugs we seem to encounter with high-level interaction.
Authors
-------
* Fernando Perez
"""
#-----------------------------------------------------------------------------
# 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.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# stdlib
import unittest
MinRK
always use StringIO, never cStringIO...
r4794 from StringIO import StringIO
MinRK
short error message on AliasError in run_cell...
r3822
Paul Ivanov
added the skip_known decorator
r3503 from IPython.testing import decorators as dec
MinRK
short error message on AliasError in run_cell...
r3822 from IPython.utils import io
Fernando Perez
Fix bug with execution of naked multiline strings....
r3300
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
class InteractiveShellTestCase(unittest.TestCase):
def test_naked_string_cells(self):
"""Test that cells with only naked strings are fully executed"""
ip = get_ipython()
# First, single-line inputs
ip.run_cell('"a"\n')
self.assertEquals(ip.user_ns['_'], 'a')
# And also multi-line cells
ip.run_cell('"""a\nb"""\n')
self.assertEquals(ip.user_ns['_'], 'a\nb')
Paul Ivanov
added test for GH-306
r3499
Thomas Kluyver
Fix blank input crashing IPython, +unittest
r3441 def test_run_empty_cell(self):
"""Just make sure we don't get a horrible error with a blank
cell of input. Yes, I did overlook that."""
ip = get_ipython()
Thomas Kluyver
Don't increment execution_count on empty cells. +test.
r3706 old_xc = ip.execution_count
Thomas Kluyver
Fix blank input crashing IPython, +unittest
r3441 ip.run_cell('')
Thomas Kluyver
Don't increment execution_count on empty cells. +test.
r3706 self.assertEquals(ip.execution_count, old_xc)
Fernando Perez
BUG: multi-line, multi-block cells were broken. Test added....
r3467
Paul Ivanov
added test for GH-306
r3499 def test_run_cell_multiline(self):
Fernando Perez
BUG: multi-line, multi-block cells were broken. Test added....
r3467 """Multi-block, multi-line cells must execute correctly.
"""
ip = get_ipython()
src = '\n'.join(["x=1",
"y=2",
"if 1:",
" x += 1",
" y += 1",])
ip.run_cell(src)
self.assertEquals(ip.user_ns['x'], 2)
self.assertEquals(ip.user_ns['y'], 3)
Paul Ivanov
added test for GH-306
r3499
def test_multiline_string_cells(self):
Paul Ivanov
added test for GH-307
r3500 "Code sprinkled with multiline strings should execute (GH-306)"
Paul Ivanov
added test for GH-306
r3499 ip = get_ipython()
ip.run_cell('tmp=0')
self.assertEquals(ip.user_ns['tmp'], 0)
ip.run_cell('tmp=1;"""a\nb"""\n')
self.assertEquals(ip.user_ns['tmp'], 1)
Paul Ivanov
added test for GH-307
r3500
def test_dont_cache_with_semicolon(self):
"Ending a line with semicolon should not cache the returned object (GH-307)"
ip = get_ipython()
oldlen = len(ip.user_ns['Out'])
a = ip.run_cell('1;')
newlen = len(ip.user_ns['Out'])
self.assertEquals(oldlen, newlen)
#also test the default caching behavior
Paul Ivanov
added test for GH-284: ensure In variable is works
r3501 ip.run_cell('1')
Paul Ivanov
added test for GH-307
r3500 newlen = len(ip.user_ns['Out'])
self.assertEquals(oldlen+1, newlen)
Paul Ivanov
added the skip_known decorator
r3503
Paul Ivanov
added test for GH-284: ensure In variable is works
r3501 def test_In_variable(self):
"Verify that In variable grows with user input (GH-284)"
ip = get_ipython()
oldlen = len(ip.user_ns['In'])
ip.run_cell('1;')
newlen = len(ip.user_ns['In'])
self.assertEquals(oldlen+1, newlen)
self.assertEquals(ip.user_ns['In'][-1],'1;')
Thomas Kluyver
Test for magic names in multiline strings.
r3707
def test_magic_names_in_string(self):
ip = get_ipython()
Thomas Kluyver
Fix bug with magic names in multi-line strings. run_cell now uses inputsplitter for static transformations.
r3709 ip.run_cell('a = """\n%exit\n"""')
self.assertEquals(ip.user_ns['a'], '\n%exit\n')
MinRK
prevent errors in prefilter from crashing IPython...
r3821
def test_alias_crash(self):
"""Errors in prefilter can't crash IPython"""
ip = get_ipython()
ip.run_cell('%alias parts echo first %s second %s')
MinRK
short error message on AliasError in run_cell...
r3822 # capture stderr:
save_err = io.stderr
io.stderr = StringIO()
MinRK
prevent errors in prefilter from crashing IPython...
r3821 ip.run_cell('parts 1')
MinRK
short error message on AliasError in run_cell...
r3822 err = io.stderr.getvalue()
io.stderr = save_err
self.assertEquals(err.split(':')[0], 'ERROR')
MinRK
fix SyntaxError on !(command)...
r3908
def test_trailing_newline(self):
"""test that running !(command) does not raise a SyntaxError"""
ip = get_ipython()
ip.run_cell('!(true)\n', False)
ip.run_cell('!(true)\n\n\n', False)
Julian Taylor
Add test for non-ascii characters in PlainTextFormatter
r4281
def test_gh_597(self):
Thomas Kluyver
Brief docstring to explain test.
r4375 """Pretty-printing lists of objects with non-ascii reprs may cause
problems."""
Julian Taylor
Add test for non-ascii characters in PlainTextFormatter
r4281 class Spam(object):
def __repr__(self):
return "\xe9"*50
import IPython.core.formatters
f = IPython.core.formatters.PlainTextFormatter()
f([Spam(),Spam()])
Thomas Kluyver
Parse user code to AST using compiler flags....
r4795
def test_future_flags(self):
"""Check that future flags are used for parsing code (gh-777)"""
ip = get_ipython()
ip.run_cell('from __future__ import print_function')
try:
ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
assert 'prfunc_return_val' in ip.user_ns
finally:
# Reset compiler flags so we don't mess up other tests.
ip.compile.reset_compiler_flags()
Olivier Verdier
TST: add future unicode_literals test (#786)
r4811
def test_future_unicode(self):
"""Check that unicode_literals is imported from __future__ (gh #786)"""
ip = get_ipython()
try:
Olivier Verdier
TST: simpler check for unicode literals (#790)...
r4820 ip.run_cell(u'byte_str = "a"')
Olivier Verdier
TST: remove assert messages (#790)...
r4821 assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default
Olivier Verdier
TST: add future unicode_literals test (#786)
r4811 ip.run_cell('from __future__ import unicode_literals')
Olivier Verdier
TST: simpler check for unicode literals (#790)...
r4820 ip.run_cell(u'unicode_str = "a"')
Olivier Verdier
TST: remove assert messages (#790)...
r4821 assert isinstance(ip.user_ns['unicode_str'], unicode) # strings literals are now unicode
Olivier Verdier
TST: add future unicode_literals test (#786)
r4811 finally:
# Reset compiler flags so we don't mess up other tests.
ip.compile.reset_compiler_flags()
MinRK
protect IPython from bad custom exception handlers...
r4991
def test_bad_custom_tb(self):
"""Check that InteractiveShell is protected from bad custom exception handlers"""
ip = get_ipython()
from IPython.utils import io
save_stderr = io.stderr
try:
# capture stderr
io.stderr = StringIO()
ip.set_custom_exc((IOError,),lambda etype,value,tb: None)
self.assertEquals(ip.custom_exceptions, (IOError,))
ip.run_cell(u'raise IOError("foo")')
self.assertEquals(ip.custom_exceptions, ())
self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
finally:
io.stderr = save_stderr