diff options
author | R David Murray <rdmurray@bitdance.com> | 2014-10-03 01:53:50 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2014-10-03 01:53:50 (GMT) |
commit | 9840db838475e675339a97b727ddbe0a3e162eb5 (patch) | |
tree | 363fd5c95abcc0cd5da9f875aef255f0feeb9051 /Lib | |
parent | 52d0a1b16f08b7de6479b3e653a6d1f18a10f290 (diff) | |
download | cpython-9840db838475e675339a97b727ddbe0a3e162eb5.zip cpython-9840db838475e675339a97b727ddbe0a3e162eb5.tar.gz cpython-9840db838475e675339a97b727ddbe0a3e162eb5.tar.bz2 |
#8473: make doctest.testfile use universal newline mode.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/doctest.py | 2 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 26 |
2 files changed, 27 insertions, 1 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 86c9839..6811406 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -216,7 +216,7 @@ def _load_testfile(filename, package, module_relative): # get_data() opens files as 'rb', so one must do the equivalent # conversion as universal newlines would do. return file_contents.replace(os.linesep, '\n'), filename - with open(filename) as f: + with open(filename, 'U') as f: return f.read(), filename # Use sys.stdout encoding for ouput. diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index a58c4ae..07a2997 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -2569,6 +2569,32 @@ bothering with the current sys.stdout encoding. >>> sys.argv = save_argv """ +def test_lineendings(): r""" +*nix systems use \n line endings, while Windows systems use \r\n. Python +handles this using universal newline mode for reading files. Let's make +sure doctest does so (issue 8473) by creating temporary test files using each +of the two line disciplines. One of the two will be the "wrong" one for the +platform the test is run on. + +Windows line endings first: + + >>> import tempfile, os + >>> fn = tempfile.mktemp() + >>> open(fn, 'w').write('Test:\r\n\r\n >>> x = 1 + 1\r\n\r\nDone.\r\n') + >>> doctest.testfile(fn, False) + TestResults(failed=0, attempted=1) + >>> os.remove(fn) + +And now *nix line endings: + + >>> fn = tempfile.mktemp() + >>> open(fn, 'w').write('Test:\n\n >>> x = 1 + 1\n\nDone.\n') + >>> doctest.testfile(fn, False) + TestResults(failed=0, attempted=1) + >>> os.remove(fn) + +""" + # old_test1, ... used to live in doctest.py, but cluttered it. Note # that these use the deprecated doctest.Tester, so should go away (or # be rewritten) someday. |