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