##// END OF EJS Templates
Refine unicode tests for ultratb
Thomas Kluyver -
Show More
@@ -1,101 +1,115 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 6 import unittest
7 7
8 8 from IPython.testing import tools as tt
9 from IPython.testing.decorators import onlyif_unicode_paths
9 10 from IPython.utils.syspathcontext import prepended_to_syspath
10 11 from IPython.utils.tempdir import TemporaryDirectory
11 12
12 13 ip = get_ipython()
13 14
14 15 file_1 = """1
15 16 2
16 17 3
17 18 def f():
18 19 1/0
19 20 """
20 21
21 22 file_2 = """def f():
22 23 1/0
23 24 """
24 25
25 26 class ChangedPyFileTest(unittest.TestCase):
26 27 def test_changing_py_file(self):
27 28 """Traceback produced if the line where the error occurred is missing?
28 29
29 30 https://github.com/ipython/ipython/issues/1456
30 31 """
31 32 with TemporaryDirectory() as td:
32 33 fname = os.path.join(td, "foo.py")
33 34 with open(fname, "w") as f:
34 35 f.write(file_1)
35 36
36 37 with prepended_to_syspath(td):
37 38 ip.run_cell("import foo")
38 39
39 40 with tt.AssertPrints("ZeroDivisionError"):
40 41 ip.run_cell("foo.f()")
41 42
42 43 # Make the file shorter, so the line of the error is missing.
43 44 with open(fname, "w") as f:
44 45 f.write(file_2)
45 46
46 47 # For some reason, this was failing on the *second* call after
47 48 # changing the file, so we call f() twice.
48 49 with tt.AssertNotPrints("Internal Python error", channel='stderr'):
49 50 with tt.AssertPrints("ZeroDivisionError"):
50 51 ip.run_cell("foo.f()")
51 52 with tt.AssertPrints("ZeroDivisionError"):
52 53 ip.run_cell("foo.f()")
53 54
54 55 iso_8859_5_file = u'''# coding: iso-8859-5
55 56
56 57 def fail():
57 58 """дбИЖ"""
58 59 1/0 # дбИЖ
59 60 '''
60 61
61 62 class NonAsciiTest(unittest.TestCase):
62 def test_iso8859_5(self):
63 @onlyif_unicode_paths
64 def test_nonascii_path(self):
63 65 # Non-ascii directory name as well.
64 66 with TemporaryDirectory(suffix=u'é') as td:
67 fname = os.path.join(td, u"fooé.py")
68 with open(fname, "w") as f:
69 f.write(file_1)
70
71 with prepended_to_syspath(td):
72 ip.run_cell("import foo")
73
74 with tt.AssertPrints("ZeroDivisionError"):
75 ip.run_cell("foo.f()")
76
77 def test_iso8859_5(self):
78 with TemporaryDirectory() as td:
65 79 fname = os.path.join(td, 'dfghjkl.py')
66 80
67 81 with io.open(fname, 'w', encoding='iso-8859-5') as f:
68 82 f.write(iso_8859_5_file)
69 83
70 84 with prepended_to_syspath(td):
71 85 ip.run_cell("from dfghjkl import fail")
72 86
73 87 with tt.AssertPrints("ZeroDivisionError"):
74 88 with tt.AssertPrints(u'дбИЖ', suppress=False):
75 89 ip.run_cell('fail()')
76 90
77 91 indentationerror_file = """if True:
78 92 zoon()
79 93 """
80 94
81 95 class IndentationErrorTest(unittest.TestCase):
82 96 def test_indentationerror_shows_line(self):
83 97 # See issue gh-2398
84 98 with tt.AssertPrints("IndentationError"):
85 99 with tt.AssertPrints("zoon()", suppress=False):
86 100 ip.run_cell(indentationerror_file)
87 101
88 102 with TemporaryDirectory() as td:
89 103 fname = os.path.join(td, "foo.py")
90 104 with open(fname, "w") as f:
91 105 f.write(indentationerror_file)
92 106
93 107 with tt.AssertPrints("IndentationError"):
94 108 with tt.AssertPrints("zoon()", suppress=False):
95 109 ip.magic('run %s' % fname)
96 110
97 111 class SyntaxErrorTest(unittest.TestCase):
98 112 def test_syntaxerror_without_lineno(self):
99 113 with tt.AssertNotPrints("TypeError"):
100 114 with tt.AssertPrints("line unknown"):
101 115 ip.run_cell("raise SyntaxError()")
General Comments 0
You need to be logged in to leave comments. Login now