classification
Title: doctest _load_testfile function -- newline handling seems incorrect
Type: behavior
Components: Tests Versions: Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: pdonis
Priority: normal Keywords: easy

Created on 2008-01-12 05:57 by pdonis, last changed 2008-01-12 18:32 by pdonis.

Files
File name Uploaded Description Edit Remove
doctest-fixes.diff pdonis, 2008-01-12 05:57 Diff against revision 59907 showing suggested fix
doctest-fixes1.diff pdonis, 2008-01-12 18:32 Revised diff against revision 59932
Messages
msg59801 (view) Author: Peter Donis (pdonis) Date: 2008-01-12 05:57
When running doctest.testfile on a Linux machine, testing a txt file 
saved on a Windows machine, doctest raised a SyntaxError exception for 
each Windows newline in the txt file. On examining the code in the 
_load_testfile function, it looks to me like there are actually two 
issues:

(1) When there is no package keyword argument given, or the package 
argument points to a package without a __loader__.get_data method, the 
txt file data is retrieved by calling open(filename).read(); this is 
the code path that my Windows-saved file triggered. However, since the 
default file mode for open is 'r', not 'rU', there is no universal 
newline conversion done on the file data. This was the issue I 
initially observed.

(2) When there is a package.__loader__.get_data method found, that 
method reads the data (using file mode 'rb'), and then newline 
conversion is performed by this line:

return file_contents.replace(os.linesep, '\n')

This does not seem to match what universal newline conversion does; 
that is supposed to convert both '\r\n' and '\r' to '\n', but running 
on Linux, os.linesep is '\n', so the replace operation in the current 
code is a no-op, even if the txt file was saved with Windows or Mac 
newlines. It seems to me that the correct operation would be:

for linesep in ('\r\n', '\r'):
   file_contents = file_contents.replace(linesep, '\n')

I have attached a diff against the current svn trunk showing my 
suggested fix for both of the above issues.

Peter Donis
msg59802 (view) Author: Peter Donis (pdonis) Date: 2008-01-12 06:01
Edit: I should have said that the attached diff also includes changes 
to test_doctest.py to test for the correct newline behavior. Because 
the test setup is a little complex, I added an auxiliary script, 
doctest_testfile.py, and an accompanying text file, 
doctest_testfile.txt, which intentionally contains mismatched newlines 
for use in the test.
msg59834 (view) Author: Peter Donis (pdonis) Date: 2008-01-12 18:32
I've uploaded a revised diff with two small improvements:

(1) Removed a redundant os.isfile check in 
PackageLoaderTestImporter.get_data() in doctest_testfile.py. (The 
open() builtin already raises IOError if the file can't be opened.)

(2) Added doctest.master = None in three places in doctest_testfile.py 
so the expected output is now nothing instead of three lines of

*** DocTestRunner.merge: 'doctest_testfile.txt' in both testers; 
summing outcomes.

Changed the expected output in test_doctest.py to correspond.
History
Date User Action Args
2008-01-12 18:32:32pdonissetfiles: + doctest-fixes1.diff
messages: + msg59834
2008-01-12 16:58:27christian.heimessetkeywords: + easy
2008-01-12 16:58:19christian.heimessetpriority: normal
2008-01-12 06:01:23pdonissetmessages: + msg59802
2008-01-12 05:57:33pdoniscreate