[Biopython-dev] test_Nexus.py and NamedTemporaryFile mode

Peter biopython at maubp.freeserve.co.uk
Mon Mar 30 07:15:06 EDT 2009


I've been running the test suite again on Windows, and was reminded of
this open issue with NamedTemporaryFile on Windows...

On Fri, Feb 13, 2009 at 5:02 PM, Peter <biopython at maubp.freeserve.co.uk> wrote:
> On Tue, Feb 10, 2009 at 11:25 AM, Michiel de Hoon <mjldehoon at yahoo.com> wrote:
>>
>>> The test_Nexus tearDown used to make sure the temp output
>>> files were removed.  This is important on Windows which
>>> does not do this automatically.  I see you now allocate
>>> "random" filenames using tempfile.NamedTemporaryFile(...)
>>> so presumably we would need to record these so that the
>>> tearDown method knows what temp files to remove.
>>
>> From reading the Python documentation, the file created by
>> tempfile.NamedTemporaryFile is removed automatically
>> when the file handle is closed, even on Windows.
>
> That's good to know.  On a related point, I've just found
> test_Nexus.py is failing on Windows XP with Python 2.6 (but is fine
> with Python 2.3, 2.4 and 2.5):
>
> C:\repository\biopython\Tests>c:\python26\python test_Nexus.py
> Test Nexus module ... ERROR
> Test Tree module. ... ok
>
> ======================================================================
> ERROR: Test Nexus module
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>  File "test_Nexus.py", line 114, in test_NexusTest1
>    f1=tempfile.NamedTemporaryFile(mode='r+w+b')
>  File "c:\python26\lib\tempfile.py", line 445, in NamedTemporaryFile
>    file = _os.fdopen(fd, mode, bufsize)
> OSError: [Errno 22] Invalid argument
>
> ----------------------------------------------------------------------
> Ran 2 tests in 0.016s
>
> FAILED (errors=1)

You can recreate this at the python 2.6 prompt with the one line:
f1=tempfile.NamedTemporaryFile(mode='r+w+b')

I couldn't solve this from looking at the Python documentation, but
after some Google searching the answer seems to be just to use the
default mode (w+b):
f1=tempfile.NamedTemporaryFile()

This works on Windows with Python 2.3 to 2.6, and also works on Mac OS
X and Linux too (only one version of Python tested here).  Fix checked
into CVS.

Peter



More information about the Biopython-dev mailing list