Index: Lib/doctest.py =================================================================== --- Lib/doctest.py (revision 79135) +++ Lib/doctest.py (working copy) @@ -233,6 +233,13 @@ # This regexp matches the start of non-blank lines: return re.sub('(?m)^(?!$)', indent*' ', s) +def _unindent(s, unindent=4): + """ + Strip the given number of space characters from the beginning of + every non-blank line in `s`, and return the result. + """ + return '\n'.join([l[unindent:] for l in s.split('\n')]) + def _exception_traceback(exc_info): """ Return a string containing a traceback message for the given @@ -561,11 +568,11 @@ argument `name` is a name identifying this string, and is only used for error messages. """ + # If all lines begin with the same indentation, then strip it. string = string.expandtabs() - # If all lines begin with the same indentation, then strip it. min_indent = self._min_indent(string) if min_indent > 0: - string = '\n'.join([l[min_indent:] for l in string.split('\n')]) + string = _unindent(string, min_indent) output = [] charno, lineno = 0, 0 @@ -1255,6 +1262,11 @@ self.debugger.set_continue() # ==== Example Finished ==== got = self._fakeout.getvalue() # the actual output + if "\t" in got: + # normalize tabs in output + got = _indent(got, example.indent) + got = got.expandtabs() + got = _unindent(got, example.indent) self._fakeout.truncate(0) outcome = FAILURE # guilty until proved innocent or insane @@ -1469,7 +1481,7 @@ """ def check_output(self, want, got, optionflags): """ - Return True iff the actual output from an example (`got`) + Return True if the actual output from an example (`got`) matches the expected output (`want`). These strings are always considered to match if they are identical; but depending on what option flags the test runner is using, Index: Lib/test/test_doctest.py =================================================================== --- Lib/test/test_doctest.py (revision 79135) +++ Lib/test/test_doctest.py (working copy) @@ -2064,7 +2064,14 @@ foo \n """ +def test_tabs_in_test(): + r""" + Tabs are expanded correctly: + >>> print "\tx" + x + """ + def test_unittest_reportflags(): """Default unittest reporting flags can be set to control reporting