1 """Tests for the key interactiveshell module, where the main ipython class is defined. 2 """ 3 4 import stack_data 5 import sys 6 7 SV_VERSION = tuple([int(x) for x in stack_data.__version__.split(".")[0:2]]) 8 9 10 def test_reset(): 11 """reset must clear most namespaces.""" 12 13 # Check that reset runs without error 14 ip.reset() 15 16 # Once we've reset it (to clear of any junk that might have been there from 17 # other tests, we can count how many variables are in the user's namespace 18 nvars_user_ns = len(ip.user_ns) 19 nvars_hidden = len(ip.user_ns_hidden) 20 21 # Now add a few variables to user_ns, and check that reset clears them 22 ip.user_ns['x'] = 1 23 ip.user_ns['y'] = 1 24 ip.reset() 25 26 # Finally, check that all namespaces have only as many variables as we 27 # expect to find in them: 28 assert len(ip.user_ns) == nvars_user_ns 29 assert len(ip.user_ns_hidden) == nvars_hidden 30 31 32 # Tests for reporting of exceptions in various modes, handling of SystemExit, 33 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell. 34 35 def doctest_tb_plain(): 36 """ 37 In [18]: xmode plain 38 Exception reporting mode: Plain 39 40 In [19]: run simpleerr.py 41 Traceback (most recent call last): 42 File ...:... 43 bar(mode) 44 File ...:... in bar 45 div0() 46 File ...:... in div0 47 x/y 48 ZeroDivisionError: ... 49 """ 50 51 52 def doctest_tb_context(): 53 """ 54 In [3]: xmode context 55 Exception reporting mode: Context 56 57 In [4]: run simpleerr.py 58 --------------------------------------------------------------------------- 59 ZeroDivisionError Traceback (most recent call last) 60 <BLANKLINE> 61 ... 62 30 except IndexError: 63 31 mode = 'div' 64 ---> 33 bar(mode) 65 <BLANKLINE> 66 ... in bar(mode) 67 15 "bar" 68 16 if mode=='div': 69 ---> 17 div0() 70 18 elif mode=='exit': 71 19 try: 72 <BLANKLINE> 73 ... in div0() 74 6 x = 1 75 7 y = 0 76 ----> 8 x/y 77 <BLANKLINE> 78 ZeroDivisionError: ...""" 79 80 81 def doctest_tb_verbose(): 82 """ 83 In [5]: xmode verbose 84 Exception reporting mode: Verbose 85 86 In [6]: run simpleerr.py 87 --------------------------------------------------------------------------- 88 ZeroDivisionError Traceback (most recent call last) 89 <BLANKLINE> 90 ... 91 30 except IndexError: 92 31 mode = 'div' 93 ---> 33 bar(mode) 94 mode = 'div' 95 <BLANKLINE> 96 ... in bar(mode='div') 97 15 "bar" 98 16 if mode=='div': 99 ---> 17 div0() 100 18 elif mode=='exit': 101 19 try: 102 <BLANKLINE> 103 ... in div0() 104 6 x = 1 105 7 y = 0 106 ----> 8 x/y 107 x = 1 108 y = 0 109 <BLANKLINE> 110 ZeroDivisionError: ... 111 """ 112 113 114 def doctest_tb_sysexit(): 115 """ 116 In [17]: %xmode plain 117 Exception reporting mode: Plain 118 119 In [18]: %run simpleerr.py exit 120 An exception has occurred, use %tb to see the full traceback. 121 SystemExit: (1, 'Mode = exit') 122 123 In [19]: %run simpleerr.py exit 2 124 An exception has occurred, use %tb to see the full traceback. 125 SystemExit: (2, 'Mode = exit') 126 127 In [20]: %tb 128 Traceback (most recent call last): 129 File ...:... in execfile 130 exec(compiler(f.read(), fname, "exec"), glob, loc) 131 File ...:... 132 bar(mode) 133 File ...:... in bar 134 sysexit(stat, mode) 135 File ...:... in sysexit 136 raise SystemExit(stat, f"Mode = {mode}") 137 SystemExit: (2, 'Mode = exit') 138 139 In [21]: %xmode context 140 Exception reporting mode: Context 141 142 In [22]: %tb 143 --------------------------------------------------------------------------- 144 SystemExit Traceback (most recent call last) 145 File ..., in execfile(fname, glob, loc, compiler) 146 ... with open(fname, "rb") as f: 147 ... compiler = compiler or compile 148 ---> ... exec(compiler(f.read(), fname, "exec"), glob, loc) 149 ... 150 30 except IndexError: 151 31 mode = 'div' 152 ---> 33 bar(mode) 153 <BLANKLINE> 154 ...bar(mode) 155 21 except: 156 22 stat = 1 157 ---> 23 sysexit(stat, mode) 158 24 else: 159 25 raise ValueError('Unknown mode') 160 <BLANKLINE> 161 ...sysexit(stat, mode) 162 10 def sysexit(stat, mode): 163 ---> 11 raise SystemExit(stat, f"Mode = {mode}") 164 <BLANKLINE> 165 SystemExit: (2, 'Mode = exit') 166 """ 167 168 169 if SV_VERSION < (0, 6): 170 171 def doctest_tb_sysexit_verbose_stack_data_05(): 172 """ 173 In [18]: %run simpleerr.py exit 174 An exception has occurred, use %tb to see the full traceback. 175 SystemExit: (1, 'Mode = exit') 176 177 In [19]: %run simpleerr.py exit 2 178 An exception has occurred, use %tb to see the full traceback. 179 SystemExit: (2, 'Mode = exit') 180 181 In [23]: %xmode verbose 182 Exception reporting mode: Verbose 183 184 In [24]: %tb 185 --------------------------------------------------------------------------- 186 SystemExit Traceback (most recent call last) 187 <BLANKLINE> 188 ... 189 30 except IndexError: 190 31 mode = 'div' 191 ---> 33 bar(mode) 192 mode = 'exit' 193 <BLANKLINE> 194 ... in bar(mode='exit') 195 ... except: 196 ... stat = 1 197 ---> ... sysexit(stat, mode) 198 mode = 'exit' 199 stat = 2 200 ... else: 201 ... raise ValueError('Unknown mode') 202 <BLANKLINE> 203 ... in sysexit(stat=2, mode='exit') 204 10 def sysexit(stat, mode): 205 ---> 11 raise SystemExit(stat, f"Mode = {mode}") 206 stat = 2 207 <BLANKLINE> 208 SystemExit: (2, 'Mode = exit') 209 """ 210 211 212 def test_run_cell(): 213 import textwrap 214 215 ip.run_cell("a = 10\na+=1") 216 ip.run_cell("assert a == 11\nassert 1") 217 218 assert ip.user_ns["a"] == 11 219 complex = textwrap.dedent( 220 """ 221 if 1: 222 print "hello" 223 if 1: 224 print "world" 225 226 if 2: 227 print "foo" 228 229 if 3: 230 print "bar" 231 232 if 4: 233 print "bar" 234 235 """ 236 ) 237 # Simply verifies that this kind of input is run 238 ip.run_cell(complex) 239 240 241 def test_db(): 242 """Test the internal database used for variable persistence.""" 243 ip.db["__unittest_"] = 12 244 assert ip.db["__unittest_"] == 12 245 del ip.db["__unittest_"] 246 assert "__unittest_" not in ip.db 247