##// END OF EJS Templates
Do not call %run with quotes, since on Windows quotes aren't stripped....
Fernando Perez -
Show More
@@ -1,174 +1,174 b''
1 1 """Tests for code execution (%run and related), which is particularly tricky.
2 2
3 3 Because of how %run manages namespaces, and the fact that we are trying here to
4 4 verify subtle object deletion and reference counting issues, the %run tests
5 5 will be kept in this separate file. This makes it easier to aggregate in one
6 6 place the tricks needed to handle it; most other magics are much easier to test
7 7 and we do so in a common test_magic file.
8 8 """
9 9 from __future__ import absolute_import
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # stdlib
16 16 import os
17 17 import sys
18 18 import tempfile
19 19
20 20 # third-party
21 21 import nose.tools as nt
22 22
23 23 # our own
24 24 from IPython.utils.platutils import find_cmd
25 25 from IPython.utils import genutils
26 26 from IPython.testing import decorators as dec
27 27 from IPython.testing import tools as tt
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Test functions begin
31 31 #-----------------------------------------------------------------------------
32 32
33 33 def doctest_refbug():
34 34 """Very nasty problem with references held by multiple runs of a script.
35 35 See: https://bugs.launchpad.net/ipython/+bug/269966
36 36
37 37 In [1]: _ip.clear_main_mod_cache()
38 38 # random
39 39
40 40 In [2]: %run refbug
41 41
42 42 In [3]: call_f()
43 43 lowercased: hello
44 44
45 45 In [4]: %run refbug
46 46
47 47 In [5]: call_f()
48 48 lowercased: hello
49 49 lowercased: hello
50 50 """
51 51
52 52
53 53 def doctest_run_builtins():
54 54 r"""Check that %run doesn't damage __builtins__.
55 55
56 56 In [1]: import tempfile
57 57
58 58 In [2]: bid1 = id(__builtins__)
59 59
60 60 In [3]: fname = tempfile.mkstemp('.py')[1]
61 61
62 62 In [3]: f = open(fname,'w')
63 63
64 64 In [4]: f.write('pass\n')
65 65
66 66 In [5]: f.flush()
67 67
68 68 In [6]: t1 = type(__builtins__)
69 69
70 In [7]: %run "$fname"
70 In [7]: %run $fname
71 71
72 72 In [7]: f.close()
73 73
74 74 In [8]: bid2 = id(__builtins__)
75 75
76 76 In [9]: t2 = type(__builtins__)
77 77
78 78 In [10]: t1 == t2
79 79 Out[10]: True
80 80
81 81 In [10]: bid1 == bid2
82 82 Out[10]: True
83 83
84 84 In [12]: try:
85 85 ....: os.unlink(fname)
86 86 ....: except:
87 87 ....: pass
88 88 ....:
89 89 """
90 90
91 91 # For some tests, it will be handy to organize them in a class with a common
92 92 # setup that makes a temp file
93 93
94 94 class TestMagicRunPass(tt.TempFileMixin):
95 95
96 96 def setup(self):
97 97 """Make a valid python temp file."""
98 98 self.mktmp('pass\n')
99 99
100 100 def run_tmpfile(self):
101 101 _ip = get_ipython()
102 102 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
103 103 # See below and ticket https://bugs.launchpad.net/bugs/366353
104 _ip.magic('run "%s"' % self.fname)
104 _ip.magic('run %s' % self.fname)
105 105
106 106 def test_builtins_id(self):
107 107 """Check that %run doesn't damage __builtins__ """
108 108 _ip = get_ipython()
109 109 # Test that the id of __builtins__ is not modified by %run
110 110 bid1 = id(_ip.user_ns['__builtins__'])
111 111 self.run_tmpfile()
112 112 bid2 = id(_ip.user_ns['__builtins__'])
113 113 tt.assert_equals(bid1, bid2)
114 114
115 115 def test_builtins_type(self):
116 116 """Check that the type of __builtins__ doesn't change with %run.
117 117
118 118 However, the above could pass if __builtins__ was already modified to
119 119 be a dict (it should be a module) by a previous use of %run. So we
120 120 also check explicitly that it really is a module:
121 121 """
122 122 _ip = get_ipython()
123 123 self.run_tmpfile()
124 124 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
125 125
126 126 def test_prompts(self):
127 127 """Test that prompts correctly generate after %run"""
128 128 self.run_tmpfile()
129 129 _ip = get_ipython()
130 130 p2 = str(_ip.outputcache.prompt2).strip()
131 131 nt.assert_equals(p2[:3], '...')
132 132
133 133
134 134 class TestMagicRunSimple(tt.TempFileMixin):
135 135
136 136 def test_simpledef(self):
137 137 """Test that simple class definitions work."""
138 138 src = ("class foo: pass\n"
139 139 "def f(): return foo()")
140 140 self.mktmp(src)
141 141 _ip.magic('run %s' % self.fname)
142 142 _ip.runlines('t = isinstance(f(), foo)')
143 143 nt.assert_true(_ip.user_ns['t'])
144 144
145 145 # We have to skip these in win32 because genutils.getoutputerr() crashes,
146 146 # due to the fact that subprocess does not support close_fds when
147 147 # redirecting stdout/err. So unless someone who knows more tells us how to
148 148 # implement genutils.getoutputerr() in win32, we're stuck avoiding these.
149 149 @dec.skip_win32
150 150 def test_obj_del(self):
151 151 """Test that object's __del__ methods are called on exit."""
152 152
153 153 # This test is known to fail on win32.
154 154 # See ticket https://bugs.launchpad.net/bugs/366334
155 155 src = ("class A(object):\n"
156 156 " def __del__(self):\n"
157 157 " print 'object A deleted'\n"
158 158 "a = A()\n")
159 159 self.mktmp(src)
160 160 tt.ipexec_validate(self.fname, 'object A deleted')
161 161
162 162 @dec.skip_win32
163 163 def test_tclass(self):
164 164 mydir = os.path.dirname(__file__)
165 165 tc = os.path.join(mydir, 'tclass')
166 166 src = ("%%run '%s' C-first\n"
167 167 "%%run '%s' C-second\n") % (tc, tc)
168 168 self.mktmp(src, '.ipy')
169 169 out = """\
170 170 ARGV 1-: ['C-first']
171 171 ARGV 1-: ['C-second']
172 172 tclass.py: deleting object: C-first
173 173 """
174 174 tt.ipexec_validate(self.fname, out)
General Comments 0
You need to be logged in to leave comments. Login now