##// END OF EJS Templates
Fix problems with side effects and python 2.6....
Fernando Perez -
Show More
@@ -15,6 +15,7 b' __docformat__ = "restructuredtext en"'
15 15 from copy import copy, deepcopy
16 16 from cStringIO import StringIO
17 17 import string
18 import sys
18 19
19 20 from nose.tools import assert_equal
20 21
@@ -23,22 +24,6 b' from IPython.ipapi import get as get_ipython0'
23 24 from IPython.testing.plugin.ipdoctest import default_argv
24 25
25 26
26 def safe_deepcopy(d):
27 """ Deep copy every key of the given dict, when possible. Elsewhere
28 do a copy.
29 """
30 copied_d = dict()
31 for key, value in d.iteritems():
32 try:
33 copied_d[key] = deepcopy(value)
34 except:
35 try:
36 copied_d[key] = copy(value)
37 except:
38 copied_d[key] = value
39 return copied_d
40
41
42 27 class TestPrefilterFrontEnd(PrefilterFrontEnd):
43 28
44 29 input_prompt_template = string.Template('')
@@ -72,17 +57,34 b' def isolate_ipython0(func):'
72 57 with arguments.
73 58 """
74 59 def my_func():
75 iplib = get_ipython0()
76 if iplib is None:
60 ip0 = get_ipython0()
61 if ip0 is None:
77 62 return func()
78 ipython0 = iplib.IP
79 global_ns = safe_deepcopy(ipython0.user_global_ns)
80 user_ns = safe_deepcopy(ipython0.user_ns)
63 # We have a real ipython running...
64 user_ns = ip0.IP.user_ns
65 user_global_ns = ip0.IP.user_global_ns
66
67 # Previously the isolation was attempted with a deep copy of the user
68 # dicts, but we found cases where this didn't work correctly. I'm not
69 # quite sure why, but basically it did damage the user namespace, such
70 # that later tests stopped working correctly. Instead we use a simpler
71 # approach, just computing the list of added keys to the namespace and
72 # eliminating those afterwards. Existing keys that may have been
73 # modified remain modified. So far this has proven to be robust.
74
75 # Compute set of old local/global keys
76 old_locals = set(user_ns.keys())
77 old_globals = set(user_global_ns.keys())
81 78 try:
82 79 out = func()
83 80 finally:
84 ipython0.user_ns = user_ns
85 ipython0.user_global_ns = global_ns
81 # Find new keys, and if any, remove them
82 new_locals = set(user_ns.keys()) - old_locals
83 new_globals = set(user_global_ns.keys()) - old_globals
84 for k in new_locals:
85 del user_ns[k]
86 for k in new_globals:
87 del user_global_ns[k]
86 88 # Undo the hack at creation of PrefilterFrontEnd
87 89 from IPython import iplib
88 90 iplib.InteractiveShell.isthreaded = False
@@ -97,7 +99,7 b' def test_execution():'
97 99 """ Test execution of a command.
98 100 """
99 101 f = TestPrefilterFrontEnd()
100 f.input_buffer = 'print 1'
102 f.input_buffer = 'print(1)'
101 103 f._on_enter()
102 104 out_value = f.out.getvalue()
103 105 assert_equal(out_value, '1\n')
@@ -228,7 +230,14 b' def test_completion_indexing():'
228 230 f._on_enter()
229 231 f.input_buffer = 'a[0].'
230 232 f.complete_current_input()
231 assert_equal(f.input_buffer, 'a[0].__')
233
234 if sys.version_info[:2] >= (2,6):
235 # In Python 2.6, ints picked up a few non __ methods, so now there are
236 # no completions.
237 assert_equal(f.input_buffer, 'a[0].')
238 else:
239 # Right answer for 2.4/2.5
240 assert_equal(f.input_buffer, 'a[0].__')
232 241
233 242
234 243 @isolate_ipython0
@@ -238,8 +247,13 b' def test_completion_equal():'
238 247 f = TestPrefilterFrontEnd()
239 248 f.input_buffer = 'a=1.'
240 249 f.complete_current_input()
241 assert_equal(f.input_buffer, 'a=1.__')
242
250 if sys.version_info[:2] >= (2,6):
251 # In Python 2.6, ints picked up a few non __ methods, so now there are
252 # no completions.
253 assert_equal(f.input_buffer, 'a=1.')
254 else:
255 # Right answer for 2.4/2.5
256 assert_equal(f.input_buffer, 'a=1.__')
243 257
244 258
245 259 if __name__ == '__main__':
General Comments 0
You need to be logged in to leave comments. Login now