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

P

Peter Donis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top