Show More
@@ -1,291 +1,304 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tests for the key interactiveshell module. |
|
3 | 3 | |
|
4 | 4 | Historically the main classes in interactiveshell have been under-tested. This |
|
5 | 5 | module should grow as many single-method tests as possible to trap many of the |
|
6 | 6 | recurring bugs we seem to encounter with high-level interaction. |
|
7 | 7 | |
|
8 | 8 | Authors |
|
9 | 9 | ------- |
|
10 | 10 | * Fernando Perez |
|
11 | 11 | """ |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Copyright (C) 2011 The IPython Development Team |
|
14 | 14 | # |
|
15 | 15 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | 16 | # the file COPYING, distributed as part of this software. |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Imports |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # stdlib |
|
23 | 23 | import os |
|
24 | 24 | import shutil |
|
25 | 25 | import tempfile |
|
26 | 26 | import unittest |
|
27 | 27 | from os.path import join |
|
28 | 28 | import sys |
|
29 | 29 | from StringIO import StringIO |
|
30 | 30 | |
|
31 | from IPython.testing.decorators import skipif | |
|
31 | 32 | from IPython.utils import io |
|
32 | 33 | |
|
33 | 34 | #----------------------------------------------------------------------------- |
|
34 | 35 | # Tests |
|
35 | 36 | #----------------------------------------------------------------------------- |
|
36 | 37 | |
|
37 | 38 | class InteractiveShellTestCase(unittest.TestCase): |
|
38 | 39 | def test_naked_string_cells(self): |
|
39 | 40 | """Test that cells with only naked strings are fully executed""" |
|
40 | 41 | ip = get_ipython() |
|
41 | 42 | # First, single-line inputs |
|
42 | 43 | ip.run_cell('"a"\n') |
|
43 | 44 | self.assertEquals(ip.user_ns['_'], 'a') |
|
44 | 45 | # And also multi-line cells |
|
45 | 46 | ip.run_cell('"""a\nb"""\n') |
|
46 | 47 | self.assertEquals(ip.user_ns['_'], 'a\nb') |
|
47 | 48 | |
|
48 | 49 | def test_run_empty_cell(self): |
|
49 | 50 | """Just make sure we don't get a horrible error with a blank |
|
50 | 51 | cell of input. Yes, I did overlook that.""" |
|
51 | 52 | ip = get_ipython() |
|
52 | 53 | old_xc = ip.execution_count |
|
53 | 54 | ip.run_cell('') |
|
54 | 55 | self.assertEquals(ip.execution_count, old_xc) |
|
55 | 56 | |
|
56 | 57 | def test_run_cell_multiline(self): |
|
57 | 58 | """Multi-block, multi-line cells must execute correctly. |
|
58 | 59 | """ |
|
59 | 60 | ip = get_ipython() |
|
60 | 61 | src = '\n'.join(["x=1", |
|
61 | 62 | "y=2", |
|
62 | 63 | "if 1:", |
|
63 | 64 | " x += 1", |
|
64 | 65 | " y += 1",]) |
|
65 | 66 | ip.run_cell(src) |
|
66 | 67 | self.assertEquals(ip.user_ns['x'], 2) |
|
67 | 68 | self.assertEquals(ip.user_ns['y'], 3) |
|
68 | 69 | |
|
69 | 70 | def test_multiline_string_cells(self): |
|
70 | 71 | "Code sprinkled with multiline strings should execute (GH-306)" |
|
71 | 72 | ip = get_ipython() |
|
72 | 73 | ip.run_cell('tmp=0') |
|
73 | 74 | self.assertEquals(ip.user_ns['tmp'], 0) |
|
74 | 75 | ip.run_cell('tmp=1;"""a\nb"""\n') |
|
75 | 76 | self.assertEquals(ip.user_ns['tmp'], 1) |
|
76 | 77 | |
|
77 | 78 | def test_dont_cache_with_semicolon(self): |
|
78 | 79 | "Ending a line with semicolon should not cache the returned object (GH-307)" |
|
79 | 80 | ip = get_ipython() |
|
80 | 81 | oldlen = len(ip.user_ns['Out']) |
|
81 | 82 | a = ip.run_cell('1;', store_history=True) |
|
82 | 83 | newlen = len(ip.user_ns['Out']) |
|
83 | 84 | self.assertEquals(oldlen, newlen) |
|
84 | 85 | #also test the default caching behavior |
|
85 | 86 | ip.run_cell('1', store_history=True) |
|
86 | 87 | newlen = len(ip.user_ns['Out']) |
|
87 | 88 | self.assertEquals(oldlen+1, newlen) |
|
88 | 89 | |
|
89 | 90 | def test_In_variable(self): |
|
90 | 91 | "Verify that In variable grows with user input (GH-284)" |
|
91 | 92 | ip = get_ipython() |
|
92 | 93 | oldlen = len(ip.user_ns['In']) |
|
93 | 94 | ip.run_cell('1;', store_history=True) |
|
94 | 95 | newlen = len(ip.user_ns['In']) |
|
95 | 96 | self.assertEquals(oldlen+1, newlen) |
|
96 | 97 | self.assertEquals(ip.user_ns['In'][-1],'1;') |
|
97 | 98 | |
|
98 | 99 | def test_magic_names_in_string(self): |
|
99 | 100 | ip = get_ipython() |
|
100 | 101 | ip.run_cell('a = """\n%exit\n"""') |
|
101 | 102 | self.assertEquals(ip.user_ns['a'], '\n%exit\n') |
|
102 | 103 | |
|
103 | 104 | def test_alias_crash(self): |
|
104 | 105 | """Errors in prefilter can't crash IPython""" |
|
105 | 106 | ip = get_ipython() |
|
106 | 107 | ip.run_cell('%alias parts echo first %s second %s') |
|
107 | 108 | # capture stderr: |
|
108 | 109 | save_err = io.stderr |
|
109 | 110 | io.stderr = StringIO() |
|
110 | 111 | ip.run_cell('parts 1') |
|
111 | 112 | err = io.stderr.getvalue() |
|
112 | 113 | io.stderr = save_err |
|
113 | 114 | self.assertEquals(err.split(':')[0], 'ERROR') |
|
114 | 115 | |
|
115 | 116 | def test_trailing_newline(self): |
|
116 | 117 | """test that running !(command) does not raise a SyntaxError""" |
|
117 | 118 | ip = get_ipython() |
|
118 | 119 | ip.run_cell('!(true)\n', False) |
|
119 | 120 | ip.run_cell('!(true)\n\n\n', False) |
|
120 | 121 | |
|
121 | 122 | def test_gh_597(self): |
|
122 | 123 | """Pretty-printing lists of objects with non-ascii reprs may cause |
|
123 | 124 | problems.""" |
|
124 | 125 | class Spam(object): |
|
125 | 126 | def __repr__(self): |
|
126 | 127 | return "\xe9"*50 |
|
127 | 128 | import IPython.core.formatters |
|
128 | 129 | f = IPython.core.formatters.PlainTextFormatter() |
|
129 | 130 | f([Spam(),Spam()]) |
|
130 | 131 | |
|
131 | 132 | |
|
132 | 133 | def test_future_flags(self): |
|
133 | 134 | """Check that future flags are used for parsing code (gh-777)""" |
|
134 | 135 | ip = get_ipython() |
|
135 | 136 | ip.run_cell('from __future__ import print_function') |
|
136 | 137 | try: |
|
137 | 138 | ip.run_cell('prfunc_return_val = print(1,2, sep=" ")') |
|
138 | 139 | assert 'prfunc_return_val' in ip.user_ns |
|
139 | 140 | finally: |
|
140 | 141 | # Reset compiler flags so we don't mess up other tests. |
|
141 | 142 | ip.compile.reset_compiler_flags() |
|
142 | 143 | |
|
143 | 144 | def test_future_unicode(self): |
|
144 | 145 | """Check that unicode_literals is imported from __future__ (gh #786)""" |
|
145 | 146 | ip = get_ipython() |
|
146 | 147 | try: |
|
147 | 148 | ip.run_cell(u'byte_str = "a"') |
|
148 | 149 | assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default |
|
149 | 150 | ip.run_cell('from __future__ import unicode_literals') |
|
150 | 151 | ip.run_cell(u'unicode_str = "a"') |
|
151 | 152 | assert isinstance(ip.user_ns['unicode_str'], unicode) # strings literals are now unicode |
|
152 | 153 | finally: |
|
153 | 154 | # Reset compiler flags so we don't mess up other tests. |
|
154 | 155 | ip.compile.reset_compiler_flags() |
|
155 | 156 | |
|
156 | 157 | def test_can_pickle(self): |
|
157 | 158 | "Can we pickle objects defined interactively (GH-29)" |
|
158 | 159 | ip = get_ipython() |
|
159 | 160 | ip.reset() |
|
160 | 161 | ip.run_cell(("class Mylist(list):\n" |
|
161 | 162 | " def __init__(self,x=[]):\n" |
|
162 | 163 | " list.__init__(self,x)")) |
|
163 | 164 | ip.run_cell("w=Mylist([1,2,3])") |
|
164 | 165 | |
|
165 | 166 | from cPickle import dumps |
|
166 | 167 | |
|
167 | 168 | # We need to swap in our main module - this is only necessary |
|
168 | 169 | # inside the test framework, because IPython puts the interactive module |
|
169 | 170 | # in place (but the test framework undoes this). |
|
170 | 171 | _main = sys.modules['__main__'] |
|
171 | 172 | sys.modules['__main__'] = ip.user_module |
|
172 | 173 | try: |
|
173 | 174 | res = dumps(ip.user_ns["w"]) |
|
174 | 175 | finally: |
|
175 | 176 | sys.modules['__main__'] = _main |
|
176 | 177 | self.assertTrue(isinstance(res, bytes)) |
|
177 | 178 | |
|
178 | 179 | def test_global_ns(self): |
|
179 | 180 | "Code in functions must be able to access variables outside them." |
|
180 | 181 | ip = get_ipython() |
|
181 | 182 | ip.run_cell("a = 10") |
|
182 | 183 | ip.run_cell(("def f(x):\n" |
|
183 | 184 | " return x + a")) |
|
184 | 185 | ip.run_cell("b = f(12)") |
|
185 | 186 | self.assertEqual(ip.user_ns["b"], 22) |
|
186 | 187 | |
|
187 | 188 | def test_bad_custom_tb(self): |
|
188 | 189 | """Check that InteractiveShell is protected from bad custom exception handlers""" |
|
189 | 190 | ip = get_ipython() |
|
190 | 191 | from IPython.utils import io |
|
191 | 192 | save_stderr = io.stderr |
|
192 | 193 | try: |
|
193 | 194 | # capture stderr |
|
194 | 195 | io.stderr = StringIO() |
|
195 | 196 | ip.set_custom_exc((IOError,), lambda etype,value,tb: 1/0) |
|
196 | 197 | self.assertEquals(ip.custom_exceptions, (IOError,)) |
|
197 | 198 | ip.run_cell(u'raise IOError("foo")') |
|
198 | 199 | self.assertEquals(ip.custom_exceptions, ()) |
|
199 | 200 | self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue()) |
|
200 | 201 | finally: |
|
201 | 202 | io.stderr = save_stderr |
|
202 | 203 | |
|
203 | 204 | def test_bad_custom_tb_return(self): |
|
204 | 205 | """Check that InteractiveShell is protected from bad return types in custom exception handlers""" |
|
205 | 206 | ip = get_ipython() |
|
206 | 207 | from IPython.utils import io |
|
207 | 208 | save_stderr = io.stderr |
|
208 | 209 | try: |
|
209 | 210 | # capture stderr |
|
210 | 211 | io.stderr = StringIO() |
|
211 | 212 | ip.set_custom_exc((NameError,),lambda etype,value,tb, tb_offset=None: 1) |
|
212 | 213 | self.assertEquals(ip.custom_exceptions, (NameError,)) |
|
213 | 214 | ip.run_cell(u'a=abracadabra') |
|
214 | 215 | self.assertEquals(ip.custom_exceptions, ()) |
|
215 | 216 | self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue()) |
|
216 | 217 | finally: |
|
217 | 218 | io.stderr = save_stderr |
|
218 | 219 | |
|
219 | 220 | def test_drop_by_id(self): |
|
220 | 221 | ip = get_ipython() |
|
221 | 222 | myvars = {"a":object(), "b":object(), "c": object()} |
|
222 | 223 | ip.push(myvars, interactive=False) |
|
223 | 224 | for name in myvars: |
|
224 | 225 | assert name in ip.user_ns, name |
|
225 | 226 | assert name in ip.user_ns_hidden, name |
|
226 | 227 | ip.user_ns['b'] = 12 |
|
227 | 228 | ip.drop_by_id(myvars) |
|
228 | 229 | for name in ["a", "c"]: |
|
229 | 230 | assert name not in ip.user_ns, name |
|
230 | 231 | assert name not in ip.user_ns_hidden, name |
|
231 | 232 | assert ip.user_ns['b'] == 12 |
|
232 | 233 | ip.reset() |
|
233 | 234 | |
|
234 | 235 | def test_var_expand(self): |
|
235 | 236 | ip = get_ipython() |
|
236 | 237 | ip.user_ns['f'] = u'Ca\xf1o' |
|
237 | 238 | self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o') |
|
238 | 239 | self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o') |
|
239 | 240 | self.assertEqual(ip.var_expand(u'echo {f[:-1]}'), u'echo Ca\xf1') |
|
240 | 241 | self.assertEqual(ip.var_expand(u'echo {1*2}'), u'echo 2') |
|
241 | 242 | |
|
242 | 243 | ip.user_ns['f'] = b'Ca\xc3\xb1o' |
|
243 | 244 | # This should not raise any exception: |
|
244 | 245 | ip.var_expand(u'echo $f') |
|
245 | 246 | |
|
246 | 247 | def test_bad_var_expand(self): |
|
247 | 248 | """var_expand on invalid formats shouldn't raise""" |
|
248 | 249 | ip = get_ipython() |
|
249 | 250 | |
|
250 | 251 | # SyntaxError |
|
251 | 252 | self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}") |
|
252 | 253 | # NameError |
|
253 | 254 | self.assertEqual(ip.var_expand(u"{asdf}"), u"{asdf}") |
|
254 | 255 | # ZeroDivisionError |
|
255 | 256 | self.assertEqual(ip.var_expand(u"{1/0}"), u"{1/0}") |
|
256 | 257 | |
|
258 | @skipif(sys.version_info[0] >= 3, "softspace removed in py3") | |
|
259 | def test_print_softspace(self): | |
|
260 | """Verify that softspace is handled correctly when executing multiple | |
|
261 | statements. | |
|
262 | ||
|
263 | In [1]: print 1; print 2 | |
|
264 | 1 | |
|
265 | 2 | |
|
266 | ||
|
267 | In [2]: print 1,; print 2 | |
|
268 | 1 2 | |
|
269 | """ | |
|
257 | 270 | |
|
258 | 271 | class TestSafeExecfileNonAsciiPath(unittest.TestCase): |
|
259 | 272 | |
|
260 | 273 | def setUp(self): |
|
261 | 274 | self.BASETESTDIR = tempfile.mkdtemp() |
|
262 | 275 | self.TESTDIR = join(self.BASETESTDIR, u"åäö") |
|
263 | 276 | os.mkdir(self.TESTDIR) |
|
264 | 277 | with open(join(self.TESTDIR, u"åäötestscript.py"), "w") as sfile: |
|
265 | 278 | sfile.write("pass\n") |
|
266 | 279 | self.oldpath = os.getcwdu() |
|
267 | 280 | os.chdir(self.TESTDIR) |
|
268 | 281 | self.fname = u"åäötestscript.py" |
|
269 | 282 | |
|
270 | 283 | |
|
271 | 284 | def tearDown(self): |
|
272 | 285 | os.chdir(self.oldpath) |
|
273 | 286 | shutil.rmtree(self.BASETESTDIR) |
|
274 | 287 | |
|
275 | 288 | def test_1(self): |
|
276 | 289 | """Test safe_execfile with non-ascii path |
|
277 | 290 | """ |
|
278 | 291 | _ip.shell.safe_execfile(self.fname, {}, raise_exceptions=True) |
|
279 | 292 | |
|
280 | 293 | |
|
281 | 294 | class TestSystemRaw(unittest.TestCase): |
|
282 | 295 | def test_1(self): |
|
283 | 296 | """Test system_raw with non-ascii cmd |
|
284 | 297 | """ |
|
285 | 298 | cmd = ur'''python -c "'åäö'" ''' |
|
286 | 299 | _ip.shell.system_raw(cmd) |
|
287 | 300 | |
|
288 | 301 | |
|
289 | 302 | def test__IPYTHON__(): |
|
290 | 303 | # This shouldn't raise a NameError, that's all |
|
291 | 304 | __IPYTHON__ |
General Comments 0
You need to be logged in to leave comments.
Login now