Title: ElementTree.parse doesn't close files it opens which means they cannot be removed on Windows
Type: Severity: normal
Components: Versions:
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: pekka.klarck, pjenvey
Priority: Keywords:

Created on 2010-04-14.11:07:22 by pekka.klarck, last changed 2010-04-14.20:15:37 by pekka.klarck.

msg5699 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-14.11:07:21
The bug can be reproduced by running the code below on Windows:

from xml.etree import ElementTree as ET
import os

path = 'temp.xml'
f = open(path, 'w')


The result of execution is this error:

Traceback (most recent call last):
  File "", line 10, in <module>
  File "C:\jython2.5.1\Lib\", line 342, in remove
    raise OSError(0, "couldn't delete file", path)
OSError: [Errno 0] couldn't delete file: 'temp.xml'
msg5700 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-14.11:23:18
The reason for the bug is the ElementTree.parse doesn't close the source file it opens. This bug doesn't appear with CPython because there the source file is immediately garbage collected and thus automatically closed when the parse method is exited. On Unixes the example in the original description passes also with Jython, apparently because there the OS doesn't lock the opened file similarly as on Windows.

I tested that adding `source.close()` at the end of the parse method was enough to make the original example pass. That's probably not the right way to fix this, though, as the source should be closed only if it's opened inside parse. I don't have Jython development environment set up and cannot create patch now, but I can do that later if there is a need.

IMHO code in Python stdlib shouldn't rely on CPython gc behavior and the fix should thus be done there. Should I open a separate issue there?
msg5705 (view) Author: Philip Jenvey (pjenvey) Date: 2010-04-14.16:10:10
dupe of #1479. there's already a CPython ticket
msg5706 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-14.20:15:37
I seem to be good in finding already reported bugs. This time I actually search the tracker before submitting but apparently I searched only the open issues. Anyway, great that this is fixed and hope 2.5.2 is out soon!
Date User Action Args
2010-04-14 20:15:37pekka.klarcksetmessages: + msg5706
2010-04-14 16:10:11pjenveysetstatus: open -> closed
resolution: duplicate
messages: + msg5705
nosy: + pjenvey
2010-04-14 11:23:19pekka.klarcksetmessages: + msg5700
2010-04-14 11:07:22pekka.klarckcreate