[issue7443] test.support.unlink issue on Windows platform

Tim Golden report at bugs.python.org
Fri Apr 9 10:34:49 CEST 2010


Tim Golden <mail at timgolden.me.uk> added the comment:

In one window run the attached script (assumes you have pywin32 installed) with a parameter of the directory the TESTFN file will end up in. Then run, eg, test_zipfile in another window. For me:

c:\temp> watch_dir.py C:\work_in_progress\make-snapshots\trunk\python\Lib

C:\work_in_progress\make-snapshots\trunk\python\Lib> ..\pcbuild\python.exe -m test.test_zipfile

Obviously, you'd have to change the path to be wherever you're running the test suite from.

The watch_dir script sits there looking for file activity, then takes and releases a delete-share handle on the file. It's enough to disrupt certain tests (such as test_zipfile) pretty much every time. Other tests are affected less, or only the first few times. Not sure why, but it's certainly enough to reproduce the general effect of TortoiseSVN or indexer or virus checker.

----------
Added file: http://bugs.python.org/file16839/watch_dir.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7443>
_______________________________________
-------------- next part --------------
import os, sys

import winerror
import win32file
import win32con

if __name__ == '__main__':
  path_to_watch = sys.argv[1]
  hDir = win32file.CreateFile (
    path_to_watch,
    1, # FILE_LIST_DIRECTORY
    win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
    None,
    win32con.OPEN_EXISTING,
    win32con.FILE_FLAG_BACKUP_SEMANTICS,
    None
  )
  print "=> Watching", path_to_watch

  watching = set ()
  handles = []
  try:
    while 1:
      results = win32file.ReadDirectoryChangesW (
        hDir, 1024, True,
        win32con.FILE_NOTIFY_CHANGE_FILE_NAME,
        None, None
      )
      for action, filename in results:
        filename = os.path.join (path_to_watch, filename)
        if action == 1 and filename not in watching:
          try:
            handle = win32file.CreateFile (
              filename,
              0, win32file.FILE_SHARE_DELETE,
              None, win32file.OPEN_EXISTING, 0, 0
            )
            handles.append (handle)
          except win32file.error, (errno, module, message):
            if errno == winerror.ERROR_SHARING_VIOLATION:
              print ".. Can't hold", repr (filename)
            else:
              print ".. Problem with %r: %s" % (filename, message)
          else:
            watching.add (filename)
            print ".. Holding", repr (filename)
            handle.Close ()
            handles.remove (handle)
            watching.discard (filename)
            print ".. Released", repr (filename)

  finally:
    for handle in handles:
      handle.Close ()

  hDir.Close ()


More information about the Python-bugs-list mailing list