New submission from Serhiy Storchaka <storch...@gmail.com>:

Minidom can parse ASCII-encoded XML data, but can't create it.

>>> from xml.dom.minidom import parseString
>>> doc = parseString(b'<?xml version="1.0" 
>>> encoding="us-ascii"?><foo>&#x20ac;</foo>')
>>> doc.toxml('us-ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/xml/dom/minidom.py", line 47, in toxml
    return self.toprettyxml("", "", encoding)
  File "/home/serhiy/py/cpython/Lib/xml/dom/minidom.py", line 56, in toprettyxml
    self.writexml(writer, "", indent, newl, encoding)
  File "/home/serhiy/py/cpython/Lib/xml/dom/minidom.py", line 1798, in writexml
    node.writexml(writer, indent, addindent, newl)
  File "/home/serhiy/py/cpython/Lib/xml/dom/minidom.py", line 868, in writexml
    self.childNodes[0].writexml(writer, '', '', '')
  File "/home/serhiy/py/cpython/Lib/xml/dom/minidom.py", line 1090, in writexml
    _write_data(writer, "%s%s%s" % (indent, self.data, newl))
  File "/home/serhiy/py/cpython/Lib/xml/dom/minidom.py", line 304, in 
_write_data
    writer.write(data)
  File "/home/serhiy/py/cpython/Lib/codecs.py", line 355, in write
    data, consumed = self.encode(object, self.errors)
UnicodeEncodeError: 'ascii' codec can't encode character '\u20ac' in position 
0: ordinal not in range(128)

Same for other non-unicode encodings.

Suggested simple patch solves this issue.

----------
components: Library (Lib), Unicode, XML
files: minidom_toxml_encoding.patch
keywords: patch
messages: 165020
nosy: ezio.melotti, storchaka
priority: normal
severity: normal
status: open
title: Minidom can't create ASCII representation
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3
Added file: http://bugs.python.org/file26319/minidom_toxml_encoding.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15296>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to