Issue2413

classification
Title: ElementTree.write doesn't close files if used with invalid encoding
Type: behaviour Severity: normal
Components: Library Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jeff.allen Nosy List: jeff.allen, pekka.klarck, pjenvey, zyasoft
Priority: normal Keywords:

Created on 2015-10-21.08:45:50 by pekka.klarck, last changed 2016-09-14.16:15:37 by zyasoft.

Messages
msg10367 (view) Author: Pekka Klärck (pekka.klarck) Date: 2015-10-21.08:45:49
This seems to be a variation of #1479. To reproduce:

E:\>jython
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[Java HotSpot(TM) Client VM (Oracle Corporation)] on java1.8.0_60
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree import ElementTree as ET
>>> elem = ET.XML('<test/>')
>>> tree = ET.ElementTree(elem)
>>> tree.write('test.xml', encoding='blaaaah')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\jython2.7.0\Lib\xml\etree\ElementTree.py", line 818, in write
    qnames, namespaces = _namespaces(
  File "C:\jython2.7.0\Lib\xml\etree\ElementTree.py", line 886, in _namespaces
    add_qname(tag)
  File "C:\jython2.7.0\Lib\xml\etree\ElementTree.py", line 870, in add_qname
    qnames[qname] = encode(qname)
  File "C:\jython2.7.0\Lib\xml\etree\ElementTree.py", line 870, in add_qname
    qnames[qname] = encode(qname)
  File "C:\jython2.7.0\Lib\xml\etree\ElementTree.py", line 845, in encode
    return text.encode(encoding)
LookupError: unknown encoding 'blaaaah'
>>> import os
>>> os.remove('test.xml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: unlink(): an unknown error occurred: E:\test.xml


A workaround is opening and closing output files explicitly:

    with open('test.xml', 'wb') as output:
        tree.write(output, ...)
msg10368 (view) Author: Jim Baker (zyasoft) Date: 2015-10-21.21:29:18
The fix is more or less the workaround Peke identified; in https://github.com/jythontools/jython/blob/master/lib-python/2.7/xml/etree/ElementTree.py#L789 which defines the write method, opened files must always be closed via a finally. I assume this never came up on non refcounted systems, and presumably it could be also a bug in PyPy today for example. Adding Phil...

It is not fixed in current CPython 2.7 source:
https://github.com/python/cpython/blob/2.7/Lib/xml/etree/ElementTree.py#L787
msg10371 (view) Author: Pekka Klärck (pekka.klarck) Date: 2015-10-22.21:17:05
Yeah, I would assume the same bug appears everywhere that uses CPython ElementTree but doesn't have refcounting GC. It is obviously pretty minor compared to #1479. I only noticed it because we had a test for invalid encoding and subsequent tests started to fail.
msg10372 (view) Author: Philip Jenvey (pjenvey) Date: 2015-10-22.21:25:34
Yup. The other fix was applied to CPython's Lib, so this one should definitely be fixed over there too
msg10385 (view) Author: Jim Baker (zyasoft) Date: 2015-10-26.20:41:38
Phil, are we still seeing updates against CPython 2.7 that are not security related? We haven't bothered sending updates to upstream for quite a while in Jython 2.7, because that's been our assumption.
msg10408 (view) Author: Jim Baker (zyasoft) Date: 2015-10-29.22:32:19
Let's see if we can get this fixed for 2.7.1, it is pretty easy to do so. We can look at upstreaming later.
msg10710 (view) Author: Jim Baker (zyasoft) Date: 2016-02-03.20:15:37
Fix now the xml stdlib has been updated (#2124). Just requires a minimal patch of course.
msg10724 (view) Author: Jim Baker (zyasoft) Date: 2016-02-05.16:08:26
Target RC
msg10927 (view) Author: Jeff Allen (jeff.allen) Date: 2016-09-02.18:51:16
I'll take, as you've made it so easy.
msg10930 (view) Author: Jeff Allen (jeff.allen) Date: 2016-09-03.08:52:09
Not quite so easy. Well, this particular bug is ok, but in attempting to add a doctest to test_xml_etree_jy, I find that quite a few other tests also fail. We'll handle those separately in #2518.

Stepping out of the time machine in 2009, it's the same :(
msg10934 (view) Author: Jim Baker (zyasoft) Date: 2016-09-05.16:27:13
Fixed as of https://hg.python.org/jython/rev/c168765aae66

But at least a fair number of gains since 2009, even if the progress is incremental. Hopefully Jython 3 will get us closer to the front and really seeing these fixes pushed up.
History
Date User Action Args
2016-09-14 16:15:37zyasoftsetstatus: pending -> closed
2016-09-05 16:27:14zyasoftsetstatus: open -> pending
resolution: accepted -> fixed
messages: + msg10934
2016-09-03 08:52:09jeff.allensetmessages: + msg10930
2016-09-02 18:51:16jeff.allensetversions: + Jython 2.7
nosy: + jeff.allen
messages: + msg10927
assignee: jeff.allen
components: + Library
type: behaviour
resolution: accepted
2016-02-18 21:35:07zyasoftsetpriority: high -> normal
2016-02-05 16:08:26zyasoftsetpriority: normal -> high
messages: + msg10724
2016-02-03 20:15:37zyasoftsetmessages: + msg10710
2015-12-30 00:11:50zyasoftsettitle: ElemetTree.write doesn't close files if used with invalid encoding -> ElementTree.write doesn't close files if used with invalid encoding
2015-12-30 00:02:10zyasoftsetpriority: normal
2015-10-29 22:32:19zyasoftsetmessages: + msg10408
milestone: Jython 2.7.1
2015-10-26 20:41:38zyasoftsetmessages: + msg10385
2015-10-22 21:25:34pjenveysetmessages: + msg10372
2015-10-22 21:17:05pekka.klarcksetmessages: + msg10371
2015-10-21 21:29:19zyasoftsetnosy: + pjenvey, zyasoft
messages: + msg10368
2015-10-21 08:45:50pekka.klarckcreate