##// END OF EJS Templates
TEST: Add a regression test.
Scott Sanderson -
Show More
@@ -1,197 +1,222 b''
1 1 # encoding: utf-8
2 2 """Tests for IPython.core.ultratb
3 3 """
4 4 import io
5 5 import os.path
6 from textwrap import dedent
6 7 import unittest
7 8
9
8 10 from IPython.testing import tools as tt
9 11 from IPython.testing.decorators import onlyif_unicode_paths
10 12 from IPython.utils.syspathcontext import prepended_to_syspath
11 13 from IPython.utils.tempdir import TemporaryDirectory
12 14 from IPython.utils.py3compat import PY3
13 15
14 16 ip = get_ipython()
15 17
16 18 file_1 = """1
17 19 2
18 20 3
19 21 def f():
20 22 1/0
21 23 """
22 24
23 25 file_2 = """def f():
24 26 1/0
25 27 """
26 28
27 29 class ChangedPyFileTest(unittest.TestCase):
28 30 def test_changing_py_file(self):
29 31 """Traceback produced if the line where the error occurred is missing?
30 32
31 33 https://github.com/ipython/ipython/issues/1456
32 34 """
33 35 with TemporaryDirectory() as td:
34 36 fname = os.path.join(td, "foo.py")
35 37 with open(fname, "w") as f:
36 38 f.write(file_1)
37 39
38 40 with prepended_to_syspath(td):
39 41 ip.run_cell("import foo")
40 42
41 43 with tt.AssertPrints("ZeroDivisionError"):
42 44 ip.run_cell("foo.f()")
43 45
44 46 # Make the file shorter, so the line of the error is missing.
45 47 with open(fname, "w") as f:
46 48 f.write(file_2)
47 49
48 50 # For some reason, this was failing on the *second* call after
49 51 # changing the file, so we call f() twice.
50 52 with tt.AssertNotPrints("Internal Python error", channel='stderr'):
51 53 with tt.AssertPrints("ZeroDivisionError"):
52 54 ip.run_cell("foo.f()")
53 55 with tt.AssertPrints("ZeroDivisionError"):
54 56 ip.run_cell("foo.f()")
55 57
56 58 iso_8859_5_file = u'''# coding: iso-8859-5
57 59
58 60 def fail():
59 61 """Π΄Π±Π˜Π–"""
60 62 1/0 # Π΄Π±Π˜Π–
61 63 '''
62 64
63 65 class NonAsciiTest(unittest.TestCase):
64 66 @onlyif_unicode_paths
65 67 def test_nonascii_path(self):
66 68 # Non-ascii directory name as well.
67 69 with TemporaryDirectory(suffix=u'Γ©') as td:
68 70 fname = os.path.join(td, u"fooΓ©.py")
69 71 with open(fname, "w") as f:
70 72 f.write(file_1)
71 73
72 74 with prepended_to_syspath(td):
73 75 ip.run_cell("import foo")
74 76
75 77 with tt.AssertPrints("ZeroDivisionError"):
76 78 ip.run_cell("foo.f()")
77 79
78 80 def test_iso8859_5(self):
79 81 with TemporaryDirectory() as td:
80 82 fname = os.path.join(td, 'dfghjkl.py')
81 83
82 84 with io.open(fname, 'w', encoding='iso-8859-5') as f:
83 85 f.write(iso_8859_5_file)
84 86
85 87 with prepended_to_syspath(td):
86 88 ip.run_cell("from dfghjkl import fail")
87 89
88 90 with tt.AssertPrints("ZeroDivisionError"):
89 91 with tt.AssertPrints(u'Π΄Π±Π˜Π–', suppress=False):
90 92 ip.run_cell('fail()')
91 93
94
95 class NestedGenExprTestCase(unittest.TestCase):
96 """
97 Regression test for the following issues:
98 https://github.com/ipython/ipython/issues/8293
99 https://github.com/ipython/ipython/issues/8205
100 """
101 def test_nested_genexpr(self):
102 code = dedent(
103 """\
104 class SpecificException(Exception):
105 pass
106
107 def foo(x):
108 raise SpecificException("Success!")
109
110 sum(sum(foo(x) for _ in [0]) for x in [0])
111 """
112 )
113 with tt.AssertPrints('SpecificException: Success!', suppress=False):
114 ip.run_cell(code)
115
116
92 117 indentationerror_file = """if True:
93 118 zoon()
94 119 """
95 120
96 121 class IndentationErrorTest(unittest.TestCase):
97 122 def test_indentationerror_shows_line(self):
98 123 # See issue gh-2398
99 124 with tt.AssertPrints("IndentationError"):
100 125 with tt.AssertPrints("zoon()", suppress=False):
101 126 ip.run_cell(indentationerror_file)
102 127
103 128 with TemporaryDirectory() as td:
104 129 fname = os.path.join(td, "foo.py")
105 130 with open(fname, "w") as f:
106 131 f.write(indentationerror_file)
107 132
108 133 with tt.AssertPrints("IndentationError"):
109 134 with tt.AssertPrints("zoon()", suppress=False):
110 135 ip.magic('run %s' % fname)
111 136
112 137 se_file_1 = """1
113 138 2
114 139 7/
115 140 """
116 141
117 142 se_file_2 = """7/
118 143 """
119 144
120 145 class SyntaxErrorTest(unittest.TestCase):
121 146 def test_syntaxerror_without_lineno(self):
122 147 with tt.AssertNotPrints("TypeError"):
123 148 with tt.AssertPrints("line unknown"):
124 149 ip.run_cell("raise SyntaxError()")
125 150
126 151 def test_changing_py_file(self):
127 152 with TemporaryDirectory() as td:
128 153 fname = os.path.join(td, "foo.py")
129 154 with open(fname, 'w') as f:
130 155 f.write(se_file_1)
131 156
132 157 with tt.AssertPrints(["7/", "SyntaxError"]):
133 158 ip.magic("run " + fname)
134 159
135 160 # Modify the file
136 161 with open(fname, 'w') as f:
137 162 f.write(se_file_2)
138 163
139 164 # The SyntaxError should point to the correct line
140 165 with tt.AssertPrints(["7/", "SyntaxError"]):
141 166 ip.magic("run " + fname)
142 167
143 168 def test_non_syntaxerror(self):
144 169 # SyntaxTB may be called with an error other than a SyntaxError
145 170 # See e.g. gh-4361
146 171 try:
147 172 raise ValueError('QWERTY')
148 173 except ValueError:
149 174 with tt.AssertPrints('QWERTY'):
150 175 ip.showsyntaxerror()
151 176
152 177
153 178 class Python3ChainedExceptionsTest(unittest.TestCase):
154 179 DIRECT_CAUSE_ERROR_CODE = """
155 180 try:
156 181 x = 1 + 2
157 182 print(not_defined_here)
158 183 except Exception as e:
159 184 x += 55
160 185 x - 1
161 186 y = {}
162 187 raise KeyError('uh') from e
163 188 """
164 189
165 190 EXCEPTION_DURING_HANDLING_CODE = """
166 191 try:
167 192 x = 1 + 2
168 193 print(not_defined_here)
169 194 except Exception as e:
170 195 x += 55
171 196 x - 1
172 197 y = {}
173 198 raise KeyError('uh')
174 199 """
175 200
176 201 SUPPRESS_CHAINING_CODE = """
177 202 try:
178 203 1/0
179 204 except Exception:
180 205 raise ValueError("Yikes") from None
181 206 """
182 207
183 208 def test_direct_cause_error(self):
184 209 if PY3:
185 210 with tt.AssertPrints(["KeyError", "NameError", "direct cause"]):
186 211 ip.run_cell(self.DIRECT_CAUSE_ERROR_CODE)
187 212
188 213 def test_exception_during_handling_error(self):
189 214 if PY3:
190 215 with tt.AssertPrints(["KeyError", "NameError", "During handling"]):
191 216 ip.run_cell(self.EXCEPTION_DURING_HANDLING_CODE)
192 217
193 218 def test_suppress_exception_chaining(self):
194 219 if PY3:
195 220 with tt.AssertNotPrints("ZeroDivisionError"), \
196 221 tt.AssertPrints("ValueError", suppress=False):
197 222 ip.run_cell(self.SUPPRESS_CHAINING_CODE)
General Comments 0
You need to be logged in to leave comments. Login now