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