doctest.testfile universal newline -- only when module_relative=True?

Peter Donis peterdonis at alum.mit.edu
Thu Jan 10 19:54:30 EST 2008


When running a doctest text file with doctest.testfile, I noticed that
universal newline support did not appear to work when module_relative
is False. My text file was saved on a Windows machine but I was
testing it on a Linux machine, hence the newline mismatch (doctest
would throw a SyntaxError for every incorrect newline).

I looked at the svn trunk history for doctest.py and found the
following patch:

http://svn.python.org/view?rev=59082&view=rev

This patch corrected for the lack of universal newline support in
package.__loader__ .get_data(), but that only applies when
module_relative is True. If it is False, the _load_testfile function
just calls open(filename) with the default mode of 'r'. It seems to
me that, for consistent behavior when module_relative is False, the
mode should be 'rU'.

Here's a diff against the current svn trunk that corrects this' I've
tested it on my machine and it runs correctly:


--- doctest_trunk.py	2008-01-10 18:59:15.000000000 -0500
+++ doctest_modified.py	2008-01-10 18:59:15.000000000 -0500
@@ -213,7 +213,8 @@
                 # 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
-    return open(filename).read(), filename
+    # Here we just need to ensure universal newline mode when opening
+    return open(filename, 'rU').read(), filename
 
 def _indent(s, indent=4):
     """

Has anyone else noticed this behavior? If it seems worthwhile, I can
submit this to the patch tracker. (It would also seem that there should
be a corresponding patch to test_doctest to include this case--that
should be easy enough for me to generate from what I have already.)

Peter Donis



More information about the Python-list mailing list