##// END OF EJS Templates
Fix various tests in IPython.core for Python 3.
Thomas Kluyver -
Show More
@@ -5,7 +5,9 b' directory, which we are removing. For now putting this here ensures at least'
5 we do run the test, though ultimately this functionality should all be tested
5 we do run the test, though ultimately this functionality should all be tested
6 with better-isolated tests that don't rely on the global instance in iptest.
6 with better-isolated tests that don't rely on the global instance in iptest.
7 """
7 """
8 from IPython.utils import py3compat
8
9
10 @py3compat.doctest_refactor_print
9 def doctest_autocall():
11 def doctest_autocall():
10 """
12 """
11 In [1]: def f1(a,b,c):
13 In [1]: def f1(a,b,c):
@@ -59,11 +59,12 b' def test_handlers():'
59 # These are useful for checking a particular recursive alias issue
59 # These are useful for checking a particular recursive alias issue
60 ip.alias_manager.alias_table['top'] = (0, 'd:/cygwin/top')
60 ip.alias_manager.alias_table['top'] = (0, 'd:/cygwin/top')
61 ip.alias_manager.alias_table['d'] = (0, 'true')
61 ip.alias_manager.alias_table['d'] = (0, 'true')
62 run([("an_alias", 'get_ipython().system(u"true ")'), # alias
62 run([(i,py3compat.u_format(o)) for i,o in \
63 [("an_alias", 'get_ipython().system({u}"true ")'), # alias
63 # Below: recursive aliases should expand whitespace-surrounded
64 # Below: recursive aliases should expand whitespace-surrounded
64 # chars, *not* initial chars which happen to be aliases:
65 # chars, *not* initial chars which happen to be aliases:
65 ("top", 'get_ipython().system(u"d:/cygwin/top ")'),
66 ("top", 'get_ipython().system({u}"d:/cygwin/top ")'),
66 ])
67 ]])
67 ip.system = old_system_cmd
68 ip.system = old_system_cmd
68
69
69 call_idx = CallableIndexable()
70 call_idx = CallableIndexable()
@@ -72,16 +73,17 b' def test_handlers():'
72 # For many of the below, we're also checking that leading whitespace
73 # For many of the below, we're also checking that leading whitespace
73 # turns off the esc char, which it should unless there is a continuation
74 # turns off the esc char, which it should unless there is a continuation
74 # line.
75 # line.
75 run([('"no change"', '"no change"'), # normal
76 run([(i,py3compat.u_format(o)) for i,o in \
76 ("!true", 'get_ipython().system(u"true")'), # shell_escapes
77 [('"no change"', '"no change"'), # normal
77 ("!! true", 'get_ipython().magic(u"sx true")'), # shell_escapes + magic
78 ("!true", 'get_ipython().system({u}"true")'), # shell_escapes
78 ("!!true", 'get_ipython().magic(u"sx true")'), # shell_escapes + magic
79 ("!! true", 'get_ipython().magic({u}"sx true")'), # shell_escapes + magic
79 ("%lsmagic", 'get_ipython().magic(u"lsmagic ")'), # magic
80 ("!!true", 'get_ipython().magic({u}"sx true")'), # shell_escapes + magic
80 ("lsmagic", 'get_ipython().magic(u"lsmagic ")'), # magic
81 ("%lsmagic", 'get_ipython().magic({u}"lsmagic ")'), # magic
82 ("lsmagic", 'get_ipython().magic({u}"lsmagic ")'), # magic
81 #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache
83 #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache
82
84
83 # post-esc-char whitespace goes inside
85 # post-esc-char whitespace goes inside
84 ("! true", 'get_ipython().system(u" true")'),
86 ("! true", 'get_ipython().system({u}" true")'),
85
87
86 # handle_help
88 # handle_help
87
89
@@ -90,7 +92,7 b' def test_handlers():'
90 # lets us check the key paths through the handler.
92 # lets us check the key paths through the handler.
91
93
92 ("x=1 # what?", "x=1 # what?"), # no help if valid python
94 ("x=1 # what?", "x=1 # what?"), # no help if valid python
93 ])
95 ]])
94
96
95 # multi_line_specials
97 # multi_line_specials
96 ip.prefilter_manager.multi_line_specials = False
98 ip.prefilter_manager.multi_line_specials = False
@@ -103,12 +105,12 b' def test_handlers():'
103
105
104 ip.prefilter_manager.multi_line_specials = True
106 ip.prefilter_manager.multi_line_specials = True
105 # initial indents must be preserved.
107 # initial indents must be preserved.
106 run([
108 run([(i,py3compat.u_format(o)) for i,o in \
107 ('if 1:\n !true', 'if 1:\n get_ipython().system(u"true")'),
109 [('if 1:\n !true', 'if 1:\n get_ipython().system({u}"true")'),
108 ('if 2:\n lsmagic', 'if 2:\n get_ipython().magic(u"lsmagic ")'),
110 ('if 2:\n lsmagic', 'if 2:\n get_ipython().magic({u}"lsmagic ")'),
109 ('if 1:\n an_alias', 'if 1:\n get_ipython().system(u"true ")'),
111 ('if 1:\n an_alias', 'if 1:\n get_ipython().system({u}"true ")'),
110 # Weird one
112 # Weird one
111 ('if 1:\n !!true', 'if 1:\n get_ipython().magic(u"sx true")'),
113 ('if 1:\n !!true', 'if 1:\n get_ipython().magic({u}"sx true")'),
112
114
113 # Even with m_l_s on, autocall is off even with special chars
115 # Even with m_l_s on, autocall is off even with special chars
114 ('if 1:\n /fun 1 2', 'if 1:\n /fun 1 2'),
116 ('if 1:\n /fun 1 2', 'if 1:\n /fun 1 2'),
@@ -116,7 +118,7 b' def test_handlers():'
116 ('if 1:\n ,fun 1 2', 'if 1:\n ,fun 1 2'),
118 ('if 1:\n ,fun 1 2', 'if 1:\n ,fun 1 2'),
117 ('if 1:\n ?fun 1 2', 'if 1:\n ?fun 1 2'),
119 ('if 1:\n ?fun 1 2', 'if 1:\n ?fun 1 2'),
118 # What about !!
120 # What about !!
119 ])
121 ]])
120
122
121 # Objects which are instances of IPyAutocall are *always* autocalled
123 # Objects which are instances of IPyAutocall are *always* autocalled
122 autocallable = Autocallable()
124 autocallable = Autocallable()
@@ -26,6 +26,7 b' import nose.tools as nt'
26 # Our own
26 # Our own
27 from IPython.core import inputsplitter as isp
27 from IPython.core import inputsplitter as isp
28 from IPython.testing import tools as tt
28 from IPython.testing import tools as tt
29 from IPython.utils import py3compat
29
30
30 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
31 # Semi-complete examples (also used as tests)
32 # Semi-complete examples (also used as tests)
@@ -403,18 +404,20 b' def transform_checker(tests, func):'
403
404
404 syntax = \
405 syntax = \
405 dict(assign_system =
406 dict(assign_system =
406 [('a =! ls', 'a = get_ipython().getoutput(u"ls")'),
407 [(i,py3compat.u_format(o)) for i,o in \
407 ('b = !ls', 'b = get_ipython().getoutput(u"ls")'),
408 [('a =! ls', 'a = get_ipython().getoutput({u}"ls")'),
409 ('b = !ls', 'b = get_ipython().getoutput({u}"ls")'),
408 ('x=1', 'x=1'), # normal input is unmodified
410 ('x=1', 'x=1'), # normal input is unmodified
409 (' ',' '), # blank lines are kept intact
411 (' ',' '), # blank lines are kept intact
410 ],
412 ]],
411
413
412 assign_magic =
414 assign_magic =
413 [('a =% who', 'a = get_ipython().magic(u"who")'),
415 [(i,py3compat.u_format(o)) for i,o in \
414 ('b = %who', 'b = get_ipython().magic(u"who")'),
416 [('a =% who', 'a = get_ipython().magic({u}"who")'),
417 ('b = %who', 'b = get_ipython().magic({u}"who")'),
415 ('x=1', 'x=1'), # normal input is unmodified
418 ('x=1', 'x=1'), # normal input is unmodified
416 (' ',' '), # blank lines are kept intact
419 (' ',' '), # blank lines are kept intact
417 ],
420 ]],
418
421
419 classic_prompt =
422 classic_prompt =
420 [('>>> x=1', 'x=1'),
423 [('>>> x=1', 'x=1'),
@@ -438,41 +441,45 b' syntax = \\'
438
441
439 # System calls
442 # System calls
440 escaped_shell =
443 escaped_shell =
441 [ ('!ls', 'get_ipython().system(u"ls")'),
444 [(i,py3compat.u_format(o)) for i,o in \
445 [ ('!ls', 'get_ipython().system({u}"ls")'),
442 # Double-escape shell, this means to capture the output of the
446 # Double-escape shell, this means to capture the output of the
443 # subprocess and return it
447 # subprocess and return it
444 ('!!ls', 'get_ipython().getoutput(u"ls")'),
448 ('!!ls', 'get_ipython().getoutput({u}"ls")'),
445 ],
449 ]],
446
450
447 # Help/object info
451 # Help/object info
448 escaped_help =
452 escaped_help =
453 [(i,py3compat.u_format(o)) for i,o in \
449 [ ('?', 'get_ipython().show_usage()'),
454 [ ('?', 'get_ipython().show_usage()'),
450 ('?x1', 'get_ipython().magic(u"pinfo x1")'),
455 ('?x1', 'get_ipython().magic({u}"pinfo x1")'),
451 ('??x2', 'get_ipython().magic(u"pinfo2 x2")'),
456 ('??x2', 'get_ipython().magic({u}"pinfo2 x2")'),
452 ('?a.*s', 'get_ipython().magic(u"psearch a.*s")'),
457 ('?a.*s', 'get_ipython().magic({u}"psearch a.*s")'),
453 ('?%hist', 'get_ipython().magic(u"pinfo %hist")'),
458 ('?%hist', 'get_ipython().magic({u}"pinfo %hist")'),
454 ('?abc = qwe', 'get_ipython().magic(u"pinfo abc")'),
459 ('?abc = qwe', 'get_ipython().magic({u}"pinfo abc")'),
455 ],
460 ]],
456
461
457 end_help =
462 end_help =
458 [ ('x3?', 'get_ipython().magic(u"pinfo x3")'),
463 [(i,py3compat.u_format(o)) for i,o in \
459 ('x4??', 'get_ipython().magic(u"pinfo2 x4")'),
464 [ ('x3?', 'get_ipython().magic({u}"pinfo x3")'),
460 ('%hist?', 'get_ipython().magic(u"pinfo %hist")'),
465 ('x4??', 'get_ipython().magic({u}"pinfo2 x4")'),
461 ('f*?', 'get_ipython().magic(u"psearch f*")'),
466 ('%hist?', 'get_ipython().magic({u}"pinfo %hist")'),
462 ('ax.*aspe*?', 'get_ipython().magic(u"psearch ax.*aspe*")'),
467 ('f*?', 'get_ipython().magic({u}"psearch f*")'),
463 ('a = abc?', 'get_ipython().magic(u"pinfo abc", next_input=u"a = abc")'),
468 ('ax.*aspe*?', 'get_ipython().magic({u}"psearch ax.*aspe*")'),
464 ('a = abc.qe??', 'get_ipython().magic(u"pinfo2 abc.qe", next_input=u"a = abc.qe")'),
469 ('a = abc?', 'get_ipython().magic({u}"pinfo abc", next_input={u}"a = abc")'),
465 ('a = *.items?', 'get_ipython().magic(u"psearch *.items", next_input=u"a = *.items")'),
470 ('a = abc.qe??', 'get_ipython().magic({u}"pinfo2 abc.qe", next_input={u}"a = abc.qe")'),
466 ('plot(a?', 'get_ipython().magic(u"pinfo a", next_input=u"plot(a")'),
471 ('a = *.items?', 'get_ipython().magic({u}"psearch *.items", next_input={u}"a = *.items")'),
472 ('plot(a?', 'get_ipython().magic({u}"pinfo a", next_input={u}"plot(a")'),
467 ('a*2 #comment?', 'a*2 #comment?'),
473 ('a*2 #comment?', 'a*2 #comment?'),
468 ],
474 ]],
469
475
470 # Explicit magic calls
476 # Explicit magic calls
471 escaped_magic =
477 escaped_magic =
472 [ ('%cd', 'get_ipython().magic(u"cd")'),
478 [(i,py3compat.u_format(o)) for i,o in \
473 ('%cd /home', 'get_ipython().magic(u"cd /home")'),
479 [ ('%cd', 'get_ipython().magic({u}"cd")'),
474 (' %magic', ' get_ipython().magic(u"magic")'),
480 ('%cd /home', 'get_ipython().magic({u}"cd /home")'),
475 ],
481 (' %magic', ' get_ipython().magic({u}"magic")'),
482 ]],
476
483
477 # Quoting with separate arguments
484 # Quoting with separate arguments
478 escaped_quote =
485 escaped_quote =
@@ -500,12 +507,13 b' syntax = \\'
500
507
501 # Check that we transform prompts before other transforms
508 # Check that we transform prompts before other transforms
502 mixed =
509 mixed =
503 [ ('In [1]: %lsmagic', 'get_ipython().magic(u"lsmagic")'),
510 [(i,py3compat.u_format(o)) for i,o in \
504 ('>>> %lsmagic', 'get_ipython().magic(u"lsmagic")'),
511 [ ('In [1]: %lsmagic', 'get_ipython().magic({u}"lsmagic")'),
505 ('In [2]: !ls', 'get_ipython().system(u"ls")'),
512 ('>>> %lsmagic', 'get_ipython().magic({u}"lsmagic")'),
506 ('In [3]: abs?', 'get_ipython().magic(u"pinfo abs")'),
513 ('In [2]: !ls', 'get_ipython().system({u}"ls")'),
507 ('In [4]: b = %who', 'b = get_ipython().magic(u"who")'),
514 ('In [3]: abs?', 'get_ipython().magic({u}"pinfo abs")'),
508 ],
515 ('In [4]: b = %who', 'b = get_ipython().magic({u}"who")'),
516 ]],
509 )
517 )
510
518
511 # multiline syntax examples. Each of these should be a list of lists, with
519 # multiline syntax examples. Each of these should be a list of lists, with
@@ -147,7 +147,7 b' ZeroDivisionError Traceback (most recent call last)'
147 ZeroDivisionError: ...
147 ZeroDivisionError: ...
148 """
148 """
149
149
150
150 @py3compat.u_format
151 def doctest_tb_sysexit():
151 def doctest_tb_sysexit():
152 """
152 """
153 In [17]: %xmode plain
153 In [17]: %xmode plain
@@ -155,11 +155,11 b' Exception reporting mode: Plain'
155
155
156 In [18]: %run simpleerr.py exit
156 In [18]: %run simpleerr.py exit
157 An exception has occurred, use %tb to see the full traceback.
157 An exception has occurred, use %tb to see the full traceback.
158 SystemExit: (1, u'Mode = exit')
158 SystemExit: (1, {u}'Mode = exit')
159
159
160 In [19]: %run simpleerr.py exit 2
160 In [19]: %run simpleerr.py exit 2
161 An exception has occurred, use %tb to see the full traceback.
161 An exception has occurred, use %tb to see the full traceback.
162 SystemExit: (2, u'Mode = exit')
162 SystemExit: (2, {u}'Mode = exit')
163
163
164 In [20]: %tb
164 In [20]: %tb
165 Traceback (most recent call last):
165 Traceback (most recent call last):
@@ -169,7 +169,7 b' Traceback (most recent call last):'
169 sysexit(stat, mode)
169 sysexit(stat, mode)
170 File ... line 11, in sysexit
170 File ... line 11, in sysexit
171 raise SystemExit(stat, 'Mode = %s' % mode)
171 raise SystemExit(stat, 'Mode = %s' % mode)
172 SystemExit: (2, u'Mode = exit')
172 SystemExit: (2, {u}'Mode = exit')
173
173
174 In [21]: %xmode context
174 In [21]: %xmode context
175 Exception reporting mode: Context
175 Exception reporting mode: Context
@@ -197,7 +197,7 b' SystemExit Traceback (most recent call last)'
197 12
197 12
198 13 def bar(mode):
198 13 def bar(mode):
199 <BLANKLINE>
199 <BLANKLINE>
200 SystemExit: (2, u'Mode = exit')
200 SystemExit: (2, {u}'Mode = exit')
201
201
202 In [23]: %xmode verbose
202 In [23]: %xmode verbose
203 Exception reporting mode: Verbose
203 Exception reporting mode: Verbose
@@ -211,29 +211,29 b' SystemExit Traceback (most recent call last)'
211 31
211 31
212 ---> 32 bar(mode)
212 ---> 32 bar(mode)
213 global bar = <function bar at ...>
213 global bar = <function bar at ...>
214 global mode = u'exit'
214 global mode = {u}'exit'
215 <BLANKLINE>
215 <BLANKLINE>
216 ... in bar(mode=u'exit')
216 ... in bar(mode={u}'exit')
217 20 except:
217 20 except:
218 21 stat = 1
218 21 stat = 1
219 ---> 22 sysexit(stat, mode)
219 ---> 22 sysexit(stat, mode)
220 global sysexit = <function sysexit at ...>
220 global sysexit = <function sysexit at ...>
221 stat = 2
221 stat = 2
222 mode = u'exit'
222 mode = {u}'exit'
223 23 else:
223 23 else:
224 24 raise ValueError('Unknown mode')
224 24 raise ValueError('Unknown mode')
225 <BLANKLINE>
225 <BLANKLINE>
226 ... in sysexit(stat=2, mode=u'exit')
226 ... in sysexit(stat=2, mode={u}'exit')
227 9
227 9
228 10 def sysexit(stat, mode):
228 10 def sysexit(stat, mode):
229 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
229 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
230 global SystemExit = undefined
230 global SystemExit = undefined
231 stat = 2
231 stat = 2
232 mode = u'exit'
232 mode = {u}'exit'
233 12
233 12
234 13 def bar(mode):
234 13 def bar(mode):
235 <BLANKLINE>
235 <BLANKLINE>
236 SystemExit: (2, u'Mode = exit')
236 SystemExit: (2, {u}'Mode = exit')
237 """
237 """
238
238
239
239
@@ -19,6 +19,7 b' import nose.tools as nt'
19 from IPython.utils.path import get_long_path_name
19 from IPython.utils.path import get_long_path_name
20 from IPython.testing import decorators as dec
20 from IPython.testing import decorators as dec
21 from IPython.testing import tools as tt
21 from IPython.testing import tools as tt
22 from IPython.utils import py3compat
22
23
23 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
24 # Test functions begin
25 # Test functions begin
@@ -88,20 +89,20 b' def doctest_hist_r():'
88 def doctest_hist_op():
89 def doctest_hist_op():
89 """Test %hist -op
90 """Test %hist -op
90
91
91 In [1]: class b:
92 In [1]: class b(float):
92 ...: pass
93 ...: pass
93 ...:
94 ...:
94
95
95 In [2]: class s(b):
96 In [2]: class s(object):
96 ...: def __str__(self):
97 ...: def __str__(self):
97 ...: return 's'
98 ...: return 's'
98 ...:
99 ...:
99
100
100 In [3]:
101 In [3]:
101
102
102 In [4]: class r(b):
103 In [4]: class r(b):
103 ...: def __repr__(self):
104 ...: def __repr__(self):
104 ...: return 'r'
105 ...: return 'r'
105 ...:
106 ...:
106
107
107 In [5]: class sr(s,r): pass
108 In [5]: class sr(s,r): pass
@@ -117,11 +118,11 b' def doctest_hist_op():'
117
118
118 In [10]: ssrr=sr()
119 In [10]: ssrr=sr()
119
120
120 In [11]: bb
121 In [11]: 4.5
121 Out[11]: <...b instance at ...>
122 Out[11]: 4.5
122
123
123 In [12]: ss
124 In [12]: str(ss)
124 Out[12]: <...s instance at ...>
125 Out[12]: 's'
125
126
126 In [13]:
127 In [13]:
127
128
@@ -144,10 +145,10 b' def doctest_hist_op():'
144 >>> ss=s()
145 >>> ss=s()
145 >>> rr=r()
146 >>> rr=r()
146 >>> ssrr=sr()
147 >>> ssrr=sr()
147 >>> bb
148 >>> 4.5
148 <...b instance at ...>
149 4.5
149 >>> ss
150 >>> str(ss)
150 <...s instance at ...>
151 's'
151 >>>
152 >>>
152 """
153 """
153
154
@@ -167,10 +168,12 b' def test_macro_run():'
167 """Test that we can run a multi-line macro successfully."""
168 """Test that we can run a multi-line macro successfully."""
168 ip = get_ipython()
169 ip = get_ipython()
169 ip.history_manager.reset()
170 ip.history_manager.reset()
170 cmds = ["a=10", "a+=1", "print a", "%macro test 2-3"]
171 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
172 "%macro test 2-3"]
171 for cmd in cmds:
173 for cmd in cmds:
172 ip.run_cell(cmd)
174 ip.run_cell(cmd)
173 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint a\n")
175 nt.assert_equal(ip.user_ns["test"].value,
176 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
174 original_stdout = sys.stdout
177 original_stdout = sys.stdout
175 new_stdout = StringIO()
178 new_stdout = StringIO()
176 sys.stdout = new_stdout
179 sys.stdout = new_stdout
@@ -269,6 +272,7 b' def test_time():'
269 _ip.magic('time None')
272 _ip.magic('time None')
270
273
271
274
275 @py3compat.doctest_refactor_print
272 def doctest_time():
276 def doctest_time():
273 """
277 """
274 In [10]: %time None
278 In [10]: %time None
@@ -442,21 +446,22 b' def doctest_who():'
442 Out[7]: ['alpha', 'beta']
446 Out[7]: ['alpha', 'beta']
443 """
447 """
444
448
449 @py3compat.u_format
445 def doctest_precision():
450 def doctest_precision():
446 """doctest for %precision
451 """doctest for %precision
447
452
448 In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain']
453 In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain']
449
454
450 In [2]: %precision 5
455 In [2]: %precision 5
451 Out[2]: u'%.5f'
456 Out[2]: {u}'%.5f'
452
457
453 In [3]: f.float_format
458 In [3]: f.float_format
454 Out[3]: u'%.5f'
459 Out[3]: {u}'%.5f'
455
460
456 In [4]: %precision %e
461 In [4]: %precision %e
457 Out[4]: u'%e'
462 Out[4]: {u}'%e'
458
463
459 In [5]: f(3.1415927)
464 In [5]: f(3.1415927)
460 Out[5]: u'3.141593e+00'
465 Out[5]: {u}'3.141593e+00'
461 """
466 """
462
467
@@ -21,6 +21,7 b' from nose import SkipTest'
21
21
22 from IPython.testing import decorators as dec
22 from IPython.testing import decorators as dec
23 from IPython.testing import tools as tt
23 from IPython.testing import tools as tt
24 from IPython.utils.py3compat import doctest_refactor_print
24
25
25 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
26 # Test functions begin
27 # Test functions begin
@@ -57,7 +58,7 b' def doctest_run_builtins():'
57
58
58 In [3]: f = open(fname,'w')
59 In [3]: f = open(fname,'w')
59
60
60 In [4]: f.write('pass\n')
61 In [4]: dummy= f.write('pass\n')
61
62
62 In [5]: f.flush()
63 In [5]: f.flush()
63
64
@@ -84,6 +85,7 b' def doctest_run_builtins():'
84 ....:
85 ....:
85 """
86 """
86
87
88 @doctest_refactor_print
87 def doctest_reset_del():
89 def doctest_reset_del():
88 """Test that resetting doesn't cause errors in __del__ methods.
90 """Test that resetting doesn't cause errors in __del__ methods.
89
91
General Comments 0
You need to be logged in to leave comments. Login now