##// END OF EJS Templates
ensure a fallback exists, so use local std{in,out,err}...
ensure a fallback exists, so use local std{in,out,err} Since IOStream instances require a valid fallback stream, use the locally defined std{in,out,err} instead of sys.std{in,out,err} in IOTerm's __init__ method. Note that the local std{in,out,err} are IOStream instances as well, that fall back to os.devnull

File last commit:

r5298:467b5b29
r6652:183d9879
Show More
test_prefilter.py
94 lines | 3.0 KiB | text/x-python | PythonLexer
Fernando Perez
Add transformers to understand code pasted with >>> or IPython prompts....
r2426 """Tests for input manipulation machinery."""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import nose.tools as nt
from IPython.testing import tools as tt, decorators as dec
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618 from IPython.testing.globalipapp import get_ipython
Fernando Perez
Add transformers to understand code pasted with >>> or IPython prompts....
r2426
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
Fernando Perez
Add some extra tests for prefiltering.
r2551 ip = get_ipython()
Fernando Perez
Add transformers to understand code pasted with >>> or IPython prompts....
r2426 @dec.parametric
def test_prefilter():
"""Test user input conversions"""
# pairs of (raw, expected correct) input
pairs = [ ('2+2','2+2'),
('>>> 2+2','2+2'),
('>>> # This is a comment\n'
'... 2+2',
'# This is a comment\n'
'2+2'),
# Some IPython input
('In [1]: 1', '1'),
('In [2]: for i in range(5):\n'
' ...: print i,',
'for i in range(5):\n'
' print i,'),
]
for raw, correct in pairs:
yield nt.assert_equals(ip.prefilter(raw), correct)
Fernando Perez
Add some extra tests for prefiltering.
r2551
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298
Fernando Perez
Add some extra tests for prefiltering.
r2551 @dec.parametric
def test_autocall_binops():
Thomas Kluyver
Replace links to launchpad bugs in comments/docstrings with equivalent github links.
r3917 """See https://github.com/ipython/ipython/issues/81"""
Fernando Perez
Add some extra tests for prefiltering.
r2551 ip.magic('autocall 2')
f = lambda x: x
ip.user_ns['f'] = f
try:
yield nt.assert_equals(ip.prefilter('f 1'),'f(1)')
for t in ['f +1', 'f -1']:
yield nt.assert_equals(ip.prefilter(t), t)
finally:
ip.magic('autocall 0')
del ip.user_ns['f']
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618 @dec.parametric
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298 def test_issue_114():
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618 """Check that multiline string literals don't expand as magic
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298 see http://github.com/ipython/ipython/issues/114"""
Fernando Perez
Fix test for issue #114, in this case the test had a small error....
r2648
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618 template = '"""\n%s\n"""'
Fernando Perez
Fix test for issue #114, in this case the test had a small error....
r2648 # Store the current value of multi_line_specials and turn it off before
# running test, since it could be true (case in which the test doesn't make
# sense, as multiline string literals *will* expand as magic in that case).
msp = ip.prefilter_manager.multi_line_specials
ip.prefilter_manager.multi_line_specials = False
try:
for mgk in ip.lsmagic():
raw = template % mgk
yield nt.assert_equals(ip.prefilter(raw), raw)
finally:
ip.prefilter_manager.multi_line_specials = msp
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298
def test_prefilter_attribute_errors():
"""Capture exceptions thrown by user objects on attribute access.
See http://github.com/ipython/ipython/issues/988."""
class X(object):
def __getattr__(self, k):
raise ValueError('broken object')
def __call__(self, x):
return x
# Create a callable broken object
ip.user_ns['x'] = X()
ip.magic('autocall 2')
try:
# Even if x throws an attribute error when looking at its rewrite
# attribute, we should not crash. So the test here is simply making
# the prefilter call and not having an exception.
ip.prefilter('x 1')
finally:
del ip.user_ns['x']
ip.magic('autocall 0')