Back to index

python3.2  3.2.2
Public Member Functions
test.test_traceback.SyntaxTracebackCases Class Reference

List of all members.

Public Member Functions

def get_exception_format
def syntax_error_with_caret
def syntax_error_with_caret_2
def syntax_error_bad_indentation
def test_caret
def test_nocaret
def test_bad_indentation
def test_base_exception
def test_format_exception_only_bad__str__
def test_without_exception
def test_encoded_file

Detailed Description

Definition at line 14 of file test_traceback.py.


Member Function Documentation

Definition at line 18 of file test_traceback.py.

00018 
00019     def get_exception_format(self, func, exc):
00020         try:
00021             func()
00022         except exc as value:
00023             return traceback.format_exception_only(exc, value)
00024         else:
00025             raise ValueError("call did not raise exception")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 32 of file test_traceback.py.

00032 
00033     def syntax_error_bad_indentation(self):
00034         compile("def spam():\n  print(1)\n print(2)", "?", "exec")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 26 of file test_traceback.py.

00026 
00027     def syntax_error_with_caret(self):
00028         compile("def fact(x):\n\treturn x!\n", "?", "exec")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 29 of file test_traceback.py.

00029 
00030     def syntax_error_with_caret_2(self):
00031         compile("1 +\n", "?", "exec")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 55 of file test_traceback.py.

00055 
00056     def test_bad_indentation(self):
00057         err = self.get_exception_format(self.syntax_error_bad_indentation,
00058                                         IndentationError)
00059         self.assertEqual(len(err), 4)
00060         self.assertEqual(err[1].strip(), "print(2)")
00061         self.assertIn("^", err[2])
00062         self.assertEqual(err[1].find(")"), err[2].find("^"))

Here is the call graph for this function:

Definition at line 63 of file test_traceback.py.

00063 
00064     def test_base_exception(self):
00065         # Test that exceptions derived from BaseException are formatted right
00066         e = KeyboardInterrupt()
00067         lst = traceback.format_exception_only(e.__class__, e)
00068         self.assertEqual(lst, ['KeyboardInterrupt\n'])

Here is the call graph for this function:

Definition at line 35 of file test_traceback.py.

00035 
00036     def test_caret(self):
00037         err = self.get_exception_format(self.syntax_error_with_caret,
00038                                         SyntaxError)
00039         self.assertEqual(len(err), 4)
00040         self.assertTrue(err[1].strip() == "return x!")
00041         self.assertIn("^", err[2]) # third line has caret
00042         self.assertEqual(err[1].find("!"), err[2].find("^")) # in the right place
00043 
00044         err = self.get_exception_format(self.syntax_error_with_caret_2,
00045                                         SyntaxError)
00046         self.assertIn("^", err[2]) # third line has caret
00047         self.assertTrue(err[2].count('\n') == 1) # and no additional newline
00048         self.assertTrue(err[1].find("+") == err[2].find("^")) # in the right place

Here is the call graph for this function:

Definition at line 86 of file test_traceback.py.

00086 
00087     def test_encoded_file(self):
00088         # Test that tracebacks are correctly printed for encoded source files:
00089         # - correct line number (Issue2384)
00090         # - respect file encoding (Issue3975)
00091         import tempfile, sys, subprocess, os
00092 
00093         # The spawned subprocess has its stdout redirected to a PIPE, and its
00094         # encoding may be different from the current interpreter, on Windows
00095         # at least.
00096         process = subprocess.Popen([sys.executable, "-c",
00097                                     "import sys; print(sys.stdout.encoding)"],
00098                                    stdout=subprocess.PIPE,
00099                                    stderr=subprocess.STDOUT)
00100         stdout, stderr = process.communicate()
00101         output_encoding = str(stdout, 'ascii').splitlines()[0]
00102 
00103         def do_test(firstlines, message, charset, lineno):
00104             # Raise the message in a subprocess, and catch the output
00105             try:
00106                 output = open(TESTFN, "w", encoding=charset)
00107                 output.write("""{0}if 1:
00108                     import traceback;
00109                     raise RuntimeError('{1}')
00110                     """.format(firstlines, message))
00111                 output.close()
00112                 process = subprocess.Popen([sys.executable, TESTFN],
00113                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
00114                 stdout, stderr = process.communicate()
00115                 stdout = stdout.decode(output_encoding).splitlines()
00116             finally:
00117                 unlink(TESTFN)
00118 
00119             # The source lines are encoded with the 'backslashreplace' handler
00120             encoded_message = message.encode(output_encoding,
00121                                              'backslashreplace')
00122             # and we just decoded them with the output_encoding.
00123             message_ascii = encoded_message.decode(output_encoding)
00124 
00125             err_line = "raise RuntimeError('{0}')".format(message_ascii)
00126             err_msg = "RuntimeError: {0}".format(message_ascii)
00127 
00128             self.assertIn(("line %s" % lineno), stdout[1],
00129                 "Invalid line number: {0!r} instead of {1}".format(
00130                     stdout[1], lineno))
00131             self.assertTrue(stdout[2].endswith(err_line),
00132                 "Invalid traceback line: {0!r} instead of {1!r}".format(
00133                     stdout[2], err_line))
00134             self.assertTrue(stdout[3] == err_msg,
00135                 "Invalid error message: {0!r} instead of {1!r}".format(
00136                     stdout[3], err_msg))
00137 
00138         do_test("", "foo", "ascii", 3)
00139         for charset in ("ascii", "iso-8859-1", "utf-8", "GBK"):
00140             if charset == "ascii":
00141                 text = "foo"
00142             elif charset == "GBK":
00143                 text = "\u4E02\u5100"
00144             else:
00145                 text = "h\xe9 ho"
00146             do_test("# coding: {0}\n".format(charset),
00147                     text, charset, 4)
00148             do_test("#!shebang\n# coding: {0}\n".format(charset),
00149                     text, charset, 5)
00150 

Here is the call graph for this function:

Definition at line 69 of file test_traceback.py.

00069 
00070     def test_format_exception_only_bad__str__(self):
00071         class X(Exception):
00072             def __str__(self):
00073                 1/0
00074         err = traceback.format_exception_only(X, X())
00075         self.assertEqual(len(err), 1)
00076         str_value = '<unprintable %s object>' % X.__name__
00077         if X.__module__ in ('__main__', 'builtins'):
00078             str_name = X.__name__
00079         else:
00080             str_name = '.'.join([X.__module__, X.__name__])
00081         self.assertEqual(err[0], "%s: %s\n" % (str_name, str_value))

Here is the call graph for this function:

Definition at line 49 of file test_traceback.py.

00049 
00050     def test_nocaret(self):
00051         exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
00052         err = traceback.format_exception_only(SyntaxError, exc)
00053         self.assertEqual(len(err), 3)
00054         self.assertEqual(err[1].strip(), "bad syntax")

Here is the call graph for this function:

Definition at line 82 of file test_traceback.py.

00082 
00083     def test_without_exception(self):
00084         err = traceback.format_exception_only(None, None)
00085         self.assertEqual(err, ['None\n'])

Here is the call graph for this function:


The documentation for this class was generated from the following file: