Show More
@@ -0,0 +1,51 b'' | |||||
|
1 | """Tests for IPython.core.ultratb | |||
|
2 | """ | |||
|
3 | ||||
|
4 | import os.path | |||
|
5 | import unittest | |||
|
6 | ||||
|
7 | from IPython.testing import tools as tt | |||
|
8 | from IPython.utils.syspathcontext import prepended_to_syspath | |||
|
9 | from IPython.utils.tempdir import TemporaryDirectory | |||
|
10 | ||||
|
11 | ip = get_ipython() | |||
|
12 | ||||
|
13 | file_1 = """1 | |||
|
14 | 2 | |||
|
15 | 3 | |||
|
16 | def f(): | |||
|
17 | 1/0 | |||
|
18 | """ | |||
|
19 | ||||
|
20 | file_2 = """def f(): | |||
|
21 | 1/0 | |||
|
22 | """ | |||
|
23 | ||||
|
24 | class ChangedPyFileTest(unittest.TestCase): | |||
|
25 | def test_changing_py_file(self): | |||
|
26 | """Traceback produced if the line where the error occurred is missing? | |||
|
27 | ||||
|
28 | https://github.com/ipython/ipython/issues/1456 | |||
|
29 | """ | |||
|
30 | with TemporaryDirectory() as td: | |||
|
31 | fname = os.path.join(td, "foo.py") | |||
|
32 | with open(fname, "w") as f: | |||
|
33 | f.write(file_1) | |||
|
34 | ||||
|
35 | with prepended_to_syspath(td): | |||
|
36 | ip.run_cell("import foo") | |||
|
37 | ||||
|
38 | with tt.AssertPrints("ZeroDivisionError"): | |||
|
39 | ip.run_cell("foo.f()") | |||
|
40 | ||||
|
41 | # Make the file shorter, so the line of the error is missing. | |||
|
42 | with open(fname, "w") as f: | |||
|
43 | f.write(file_2) | |||
|
44 | ||||
|
45 | # For some reason, this was failing on the *second* call after | |||
|
46 | # changing the file, so we call f() twice. | |||
|
47 | with tt.AssertNotPrints("Internal Python error", channel='stderr'): | |||
|
48 | with tt.AssertPrints("ZeroDivisionError"): | |||
|
49 | ip.run_cell("foo.f()") | |||
|
50 | with tt.AssertPrints("ZeroDivisionError"): | |||
|
51 | ip.run_cell("foo.f()") |
@@ -126,15 +126,10 b' def inspect_error():' | |||||
126 | error('Internal Python error in the inspect module.\n' |
|
126 | error('Internal Python error in the inspect module.\n' | |
127 | 'Below is the traceback from this internal error.\n') |
|
127 | 'Below is the traceback from this internal error.\n') | |
128 |
|
128 | |||
129 |
|
129 | # This function is a monkeypatch we apply to the Python inspect module. We have | ||
130 | # N.B. This function is a monkeypatch we are currently not applying. |
|
130 | # now found when it's needed (see discussion on issue gh-1456), and we have a | |
131 | # It was written some time ago, to fix an apparent Python bug with |
|
131 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if | |
132 | # codeobj.co_firstlineno . Unfortunately, we don't know under what conditions |
|
132 | # the monkeypatch is not applied. TK, Aug 2012. | |
133 | # the bug occurred, so we can't tell if it has been fixed. If it reappears, we |
|
|||
134 | # will apply the monkeypatch again. Also, note that findsource() is not called |
|
|||
135 | # by our code at this time - we don't know if it was when the monkeypatch was |
|
|||
136 | # written, or if the monkeypatch is needed for some other code (like a debugger). |
|
|||
137 | # For the discussion about not applying it, see gh-1229. TK, Jan 2011. |
|
|||
138 | def findsource(object): |
|
133 | def findsource(object): | |
139 | """Return the entire source file and starting line number for an object. |
|
134 | """Return the entire source file and starting line number for an object. | |
140 |
|
135 | |||
@@ -210,10 +205,8 b' def findsource(object):' | |||||
210 | return lines, lnum |
|
205 | return lines, lnum | |
211 | raise IOError('could not find code object') |
|
206 | raise IOError('could not find code object') | |
212 |
|
207 | |||
213 | # Not applying the monkeypatch - see above the function for details. TK, Jan 2012 |
|
208 | # Monkeypatch inspect to apply our bugfix. This code only works with Python >= 2.5 | |
214 | # Monkeypatch inspect to apply our bugfix. This code only works with py25 |
|
209 | inspect.findsource = findsource | |
215 | #if sys.version_info[:2] >= (2,5): |
|
|||
216 | # inspect.findsource = findsource |
|
|||
217 |
|
210 | |||
218 | def fix_frame_records_filenames(records): |
|
211 | def fix_frame_records_filenames(records): | |
219 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
212 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
@@ -306,7 +306,10 b' else:' | |||||
306 | super(MyStringIO, self).write(s) |
|
306 | super(MyStringIO, self).write(s) | |
307 |
|
307 | |||
308 | notprinted_msg = """Did not find {0!r} in printed output (on {1}): |
|
308 | notprinted_msg = """Did not find {0!r} in printed output (on {1}): | |
309 | {2!r}""" |
|
309 | ------- | |
|
310 | {2!s} | |||
|
311 | ------- | |||
|
312 | """ | |||
310 |
|
313 | |||
311 | class AssertPrints(object): |
|
314 | class AssertPrints(object): | |
312 | """Context manager for testing that code prints certain text. |
|
315 | """Context manager for testing that code prints certain text. | |
@@ -338,6 +341,12 b' class AssertPrints(object):' | |||||
338 | assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed) |
|
341 | assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed) | |
339 | return False |
|
342 | return False | |
340 |
|
343 | |||
|
344 | printed_msg = """Found {0!r} in printed output (on {1}): | |||
|
345 | ------- | |||
|
346 | {2!s} | |||
|
347 | ------- | |||
|
348 | """ | |||
|
349 | ||||
341 | class AssertNotPrints(AssertPrints): |
|
350 | class AssertNotPrints(AssertPrints): | |
342 | """Context manager for checking that certain output *isn't* produced. |
|
351 | """Context manager for checking that certain output *isn't* produced. | |
343 |
|
352 | |||
@@ -346,7 +355,7 b' class AssertNotPrints(AssertPrints):' | |||||
346 | self.tee.flush() |
|
355 | self.tee.flush() | |
347 | setattr(sys, self.channel, self.orig_stream) |
|
356 | setattr(sys, self.channel, self.orig_stream) | |
348 | printed = self.buffer.getvalue() |
|
357 | printed = self.buffer.getvalue() | |
349 |
assert self.s not in printed, |
|
358 | assert self.s not in printed, printed_msg.format(self.s, self.channel, printed) | |
350 | return False |
|
359 | return False | |
351 |
|
360 | |||
352 | @contextmanager |
|
361 | @contextmanager |
General Comments 0
You need to be logged in to leave comments.
Login now