def _printTraceback(self, test, err): """Print a nicely formatted traceback. :arg err: exc_info()-style traceback triple :arg test: the test that precipitated this call """ # Don't bind third item to a local var; that can create # circular refs which are expensive to collect. See the # sys.exc_info() docs. exception_type, exception_value = err[:2] extracted_tb = extract_relevant_tb( err[2], exception_type, exception_type is test.failureException) test_frame_index = index_of_test_frame(extracted_tb, exception_type, exception_value, test) if test_frame_index: # We have a good guess at which frame is the test, so # trim everything until that. We don't care to see test # framework frames. extracted_tb = extracted_tb[test_frame_index:] with self.bar.dodging(): self.stream.write(''.join( format_traceback(extracted_tb, exception_type, exception_value, self._cwd, self._term, self._options.function_color, self._options.dim_color, self._options.editor)))
def test_unicode(): """Don't have encoding explosions when a line of code contains non-ASCII.""" unicode_tb = ( [("/usr/lib/whatever.py", 69, "getMethod", """return u'あ'""")], AttributeError, AttributeError("'NoneType' object has no pants.'"), ) "".join(format_traceback(*unicode_tb))
def test_empty_tracebacks(): """Make sure we don't crash on empty tracebacks. Sometimes, stuff crashes before we even get to the test. pdbpp has been doing this a lot to me lately. When that happens, we receive an empty traceback. """ list(format_traceback([], AttributeError, AttributeError("'NoneType' object has no attribute 'pgpImportPubkey'")))
def test_none_members(): """Don't crash if the attrs of an extracted traceback are None. This can happen when using mocking. """ list( format_traceback([(None, None, None, None)], AttributeError, AttributeError('I have many nasty attributes.')))
def test_none_members(): """Don't crash if the attrs of an extracted traceback are None. This can happen when using mocking. """ list(format_traceback( [(None, None, None, None)], AttributeError, AttributeError('I have many nasty attributes.')))
def test_empty_tracebacks(): """Make sure we don't crash on empty tracebacks. Sometimes, stuff crashes before we even get to the test. pdbpp has been doing this a lot to me lately. When that happens, we receive an empty traceback. """ list(format_traceback( [], AttributeError, AttributeError("'NoneType' object has no attribute 'pgpImportPubkey'")))
def _printError(self, kind, err, test, isFailure=True): """Output a human-readable error report to the stream. kind -- the (string) type of incident the precipitated this call err -- exc_info()-style traceback triple test -- the test that precipitated this call """ if isFailure or self._options.show_advisories: writeln = self.stream.writeln write = self.stream.write with self.bar.dodging(): writeln('\n' + (self._term.bold if isFailure else '') + '%s: %s' % (kind, nose_selector(test)) + (self._term.normal if isFailure else '')) # end bold if isFailure: # Then show traceback # Don't bind third item to a local var; that can create # circular refs which are expensive to collect. See the # sys.exc_info() docs. exception_type, exception_value = err[:2] extracted_tb = extract_relevant_tb( err[2], exception_type, exception_type is test.failureException) test_frame_index = index_of_test_frame( extracted_tb, exception_type, exception_value, test) if test_frame_index: # We have a good guess at which frame is the test, so # trim everything until that. We don't care to see test # framework frames. extracted_tb = extracted_tb[test_frame_index:] write(''.join( format_traceback( extracted_tb, exception_type, exception_value, self._cwd, self._term, self._options.function_color, self._options.dim_color, self._options.editor)))
def _printTraceback(self, test, err): """Print a nicely formatted traceback. :arg err: exc_info()-style traceback triple :arg test: the test that precipitated this call """ # Don't bind third item to a local var; that can create # circular refs which are expensive to collect. See the # sys.exc_info() docs. exception_type, exception_value = err[:2] # TODO: In Python 3, the traceback is attached to the exception # instance through the __traceback__ attribute. If the instance # is saved in a local variable that persists outside the except # block, the traceback will create a reference cycle with the # current frame and its dictionary of local variables. This will # delay reclaiming dead resources until the next cyclic garbage # collection pass. extracted_tb = extract_relevant_tb( err[2], exception_type, exception_type is test.failureException) test_frame_index = index_of_test_frame( extracted_tb, exception_type, exception_value, test) if test_frame_index: # We have a good guess at which frame is the test, so # trim everything until that. We don't care to see test # framework frames. extracted_tb = extracted_tb[test_frame_index:] with self.bar.dodging(): self.stream.write(''.join( format_traceback( extracted_tb, exception_type, exception_value, self._cwd, self._term, self._options.function_color, self._options.dim_color, self._options.editor, self._options.editor_shortcut_template)))
def test_proxied_syntax_error(): """The logcapture plugin formats errors by extracting and converting the value to a string. Nose recreates a proxy exception lacking filename and lineno attributes, crashing the shortcut formatting """ string_error = ''.join([ "invalid syntax (package.py, line 111)\n", "-------------------- >> begin captured logging << --------------------\n", "DEBUG: a logged message\n", "--------------------- >> end captured logging << ---------------------" ]) # This is essentially what nose does with a string exception proxied_syntax_error = type('SyntaxError', (Exception,), {})(string_error) proxied_syntax_tb = ([ ('/usr/lib/python2.7/site-packages/nose/loader.py', 403, 'loadTestsFromName', 'module = resolve_name(addr.module)'), ('/usr/lib/python2.7/site-packages/nose/util.py', 311, 'resolve_name', "module = __import__('.'.join(parts_copy))"), ('/usr/local/venvs/project/tests.py', 8, '<module>', 'from project.package import something') ], SyntaxError, proxied_syntax_error ) ''.join(format_traceback(*proxied_syntax_tb))
def test_proxied_syntax_error(): """The logcapture plugin formats errors by extracting and converting the value to a string. Nose recreates a proxy exception lacking filename and lineno attributes, crashing the shortcut formatting """ string_error = ''.join([ "invalid syntax (package.py, line 111)\n", "-------------------- >> begin captured logging << --------------------\n", "DEBUG: a logged message\n", "--------------------- >> end captured logging << ---------------------" ]) # This is essentially what nose does with a string exception proxied_syntax_error = type('SyntaxError', (Exception, ), {})(string_error) proxied_syntax_tb = ([ ('/usr/lib/python2.7/site-packages/nose/loader.py', 403, 'loadTestsFromName', 'module = resolve_name(addr.module)'), ('/usr/lib/python2.7/site-packages/nose/util.py', 311, 'resolve_name', "module = __import__('.'.join(parts_copy))"), ('/usr/local/venvs/project/tests.py', 8, '<module>', 'from project.package import something') ], SyntaxError, proxied_syntax_error) ''.join(format_traceback(*proxied_syntax_tb))
def _printTraceback(self, test, err): """Print a nicely formatted traceback. :arg err: exc_info()-style traceback triple :arg test: the test that precipitated this call """ # Don't bind third item to a local var; that can create # circular refs which are expensive to collect. See the # sys.exc_info() docs. exception_type, exception_value = err[:2] extracted_tb = extract_relevant_tb( err[2], exception_type, exception_type is test.failureException) test_frame_index = index_of_test_frame( extracted_tb, exception_type, exception_value, test) if test_frame_index: # We have a good guess at which frame is the test, so # trim everything until that. We don't care to see test # framework frames. extracted_tb = extracted_tb[test_frame_index:] with self.bar.dodging(): self.stream.write(''.join( format_traceback( extracted_tb, exception_type, exception_value, self._cwd, self._term, self._options.function_color, self._options.dim_color, self._options.editor)))
def test_non_syntax_error(): """Exercise typical error formatting to show it doesn't crash.""" "".join(format_traceback(*attr_error_tb))
def test_syntax_error(): """Exercise special handling of syntax errors to show it doesn't crash.""" "".join(format_traceback(*syntax_error_tb))
def test_syntax_error(): """Exercise special handling of syntax errors.""" ''.join(format_traceback(*syntax_error_tb))
def test_non_syntax_error(): """Exercise typical error formatting.""" ''.join(format_traceback(*attr_error_tb))
def test_unicode(): """Don't have encoding explosions when a line of code contains non-ASCII.""" unicode_tb = ([ ("/usr/lib/whatever.py", 69, 'getMethod', """return u'あ'""") ], AttributeError, AttributeError("'NoneType' object has no pants.'")) ''.join(format_traceback(*unicode_tb))
def test_non_syntax_error(): """Exercise typical error formatting to show it doesn't crash.""" ''.join(format_traceback(*attr_error_tb))
def test_syntax_error(): """Exercise special handling of syntax errors to show it doesn't crash.""" ''.join(format_traceback(*syntax_error_tb))