##// END OF EJS Templates
Add tests to ensure that %run does not modify __builtins__...
Fernando Perez -
Show More
@@ -126,9 +126,13 b' prompt_specials_color = {'
126 126 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
127 127 # can get numbers displayed in whatever color they want.
128 128 r'\N': '${self.cache.prompt_count}',
129
129 130 # Prompt/history count, with the actual digits replaced by dots. Used
130 131 # mainly in continuation prompts (prompt_in2)
132 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
133 # More robust form of the above expression, that uses __builtins__
131 134 r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',
135
132 136 # Current working directory
133 137 r'\w': '${os.getcwd()}',
134 138 # Current time
@@ -84,8 +84,18 b' def _run_ns_sync(self,arg_s,runner=None):'
84 84 This is strictly needed for running doctests that call %run.
85 85 """
86 86
87 finder = py_file_finder(_run_ns_sync.test_filename)
87 # When tests call %run directly (not via doctest) these function attributes
88 # are not set
89 try:
90 fname = _run_ns_sync.test_filename
91 except AttributeError:
92 fname = arg_s
93
94 finder = py_file_finder(fname)
88 95 out = _ip.IP.magic_run_ori(arg_s,runner,finder)
96
97 # Simliarly, there is no test_globs when a test is NOT a doctest
98 if hasattr(_run_ns_sync,'test_globs'):
89 99 _run_ns_sync.test_globs.update(_ip.user_ns)
90 100 return out
91 101
@@ -12,7 +12,6 b' import nose.tools as nt'
12 12
13 13 # From our own code
14 14 from IPython.testing import decorators as dec
15
16 15 #-----------------------------------------------------------------------------
17 16 # Test functions begin
18 17
@@ -64,6 +63,59 b' def doctest_hist_f():'
64 63 In [11]: %history -n -f $tfile 3
65 64 """
66 65
66 def doctest_run_builtins():
67 """Check that %run doesn't damage __builtins__ via a doctest.
68
69 This is similar to the test_run_builtins, but I want *both* forms of the
70 test to catch any possible glitches in our testing machinery, since that
71 modifies %run somewhat. So for this, we have both a normal test (below)
72 and a doctest (this one).
73
74 In [1]: import tempfile
75
76 In [2]: bid1 = id(__builtins__)
77
78 In [3]: f = tempfile.NamedTemporaryFile()
79
80 In [4]: f.write('pass\\n')
81
82 In [5]: f.flush()
83
84 In [6]: print 'B1:',type(__builtins__)
85 B1: <type 'module'>
86
87 In [7]: %run $f.name
88
89 In [8]: bid2 = id(__builtins__)
90
91 In [9]: print 'B2:',type(__builtins__)
92 B2: <type 'module'>
93
94 In [10]: bid1 == bid2
95 Out[10]: True
96 """
97
98 def test_run_builtins():
99 """Check that %run doesn't damage __builtins__ """
100 import sys
101 import tempfile
102 import types
103
104 # Make an empty file and put 'pass' in it
105 f = tempfile.NamedTemporaryFile()
106 f.write('pass\n')
107 f.flush()
108
109 # Our first test is that the id of __builtins__ is not modified by %run
110 bid1 = id(__builtins__)
111 _ip.magic('run %s' % f.name)
112 bid2 = id(__builtins__)
113 yield nt.assert_equals,bid1,bid2
114 # However, the above could pass if __builtins__ was already modified to be
115 # a dict (it should be a module) by a previous use of %run. So we also
116 # check explicitly that it really is a module:
117 yield nt.assert_equals,type(__builtins__),type(sys)
118
67 119
68 120 def doctest_hist_r():
69 121 """Test %hist -r
General Comments 0
You need to be logged in to leave comments. Login now