Image.open should close file when it cannot identify image

Oliver Tonnhofer <[email protected]>
Newsgroups gmane.comp.python.image
Message-ID <[email protected]>
Hi,

I want to remove files that PIL can't open. But the following fails on  
Windows, because Image.open doesn't close the file.

----
import os
import Image
try:
     f = open('test.test', 'w')
     f.close()
     Image.open('test.test')
finally:
     os.remove('test.test')
----
results in:
WindowsError: [Error 32] The process cannot access the file because it  
is being
used by another process: 'test.test'


Below is a simple fix.

Regards,
Oliver

PS: please cc me on replies to the list.

----
--- PIL/Image.py.orig	2009-10-02 12:02:26.000000000 +0200
+++ PIL/Image.py	2009-10-02 12:03:12.000000000 +0200
@@ -1913,6 +1913,9 @@
          except (SyntaxError, IndexError, TypeError):
              pass

+    if filename:
+        fp.close()
+
      raise IOError("cannot identify image file")

  #

_______________________________________________
Image-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/image-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.