[Python-Dev] fixing tests on windows

Tim Golden mail at timgolden.me.uk
Tue Apr 1 18:13:34 CEST 2008


Tim Golden wrote:
> Steven Bethard wrote:
>> At the sprints, I ran into a bunch of similar errors running the test
>> suite on my Windows Vista box, even on tests that were properly
>> cleaning up after themselves in tearDown(). I even tried putting in
>> sleeps as long as 1 second, to no avail. The only way to get the test
>> suite to run without these errors was to stop the Windows Search
>> Service, fully disable Icon Overlays for TortoiseSVN, and then close
>> down all open folders.
>>
>> Any chance the boxes the tests are being run on are running the
>> Windows Search Service or have Icon Overlays enabled for TortoiseSVN?
>>
>> (If anyone has any ideas of how to get around these problems, I'd love
>> to hear them. Seems like I shouldn't have to disable these services.)
> 
> I'm not sure I'm going to help here, but the reason that
> this happens is that certain services -- and the Desktop
> Search tools are notorious for this -- get file handles with
> FILE_SHARE_DELETE, which means that the file isn't actually
> removed when it's deleted; only when the last of those handles
> closes.
> 
> I did see a suggestion somewhere that, to circumvent this issue,
> instead of removing it a file you need to move it to %TEMP%
> (or wherever) and delete it there. I'll try to knock up a test
> case to see if this works.

OK: to confirm, the following test seems to indicate that
doing a (local - same volume) rename followed by a remove
will do the right thing, even when a FILE_SHARE_DELETE handle
is held.

If this is the thing to do, presumably test_support should
grow a "remove_file" which does something of this sort?

TJG

<code>
import os, sys
import win32file

FILENAME = "test"

def rename_and_remove (filename):
   os.rename (filename, filename + ".deleted")
   os.remove (filename + ".deleted")

def remove_only (filename):
   os.remove (filename)

def test (remove):
   open (FILENAME, "w").close ()
   hFile = win32file.CreateFile (
     FILENAME,
     win32file.GENERIC_READ, win32file.FILE_SHARE_DELETE,
     None, win32file.OPEN_EXISTING, 0, 0
   )
   try:
     remove (FILENAME)
     try:
       open (FILENAME, "w").close ()
     except IOError:
       print "Couldn't recreate"
     else:
       print "Could recreate"
   finally:
     hFile.Close ()

   try:
     open (FILENAME, "w").close ()
   except IOError:
     print "Couldn't recreate"
   else:
     print "Could recreate"

if __name__ =='__main__':
   print
   print "Should not work"
   test (remove_only)

   print
   print "Should work"
   test (rename_and_remove)

</code>



More information about the Python-Dev mailing list