[issue16726] expat ParseFile expects bytes, not string

Michiel de Hoon report at bugs.python.org
Wed Dec 19 11:56:49 CET 2012


New submission from Michiel de Hoon:

The expat parser in xml.parsers.expat has a Parse method and a ParseFile method. The Parse method parses a string, however the ParseFile method wants bytes.

This is a minimal example of the Parse method:

>>> import xml.parsers.expat
>>> p = xml.parsers.expat.ParserCreate()
>>> p.Parse('<?xml version="1.0"?>')

which runs fine. Note that argument to p.Parse is a string, not bytes.

This is the corresponding example of ParseFile:

>>> import xml.parsers.expat
>>> handle = open("test.xml")
>>> p = xml.parsers.expat.ParserCreate()
>>> p.ParseFile(handle)

where the file test.xml only contains <?xml version="1.0"?>
This gives an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: read() did not return a bytes object (type=str)

Opening the file test.xml in binary raises an Error:

>>> import xml.parsers.expat
>>> handle = open("test.xml", "rb")
>>> p = xml.parsers.expat.ParserCreate()
>>> p.ParseFile(handle)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
xml.parsers.expat.ExpatError: no element found: line 2, column 0

suggesting that in reality, the expat Parser needs a string, not bytes.
(the same error appears with a more meaningful XML file).

I would expect that both Parse and ParseFile accept strings, but not bytes.

----------
messages: 177733
nosy: mdehoon
priority: normal
severity: normal
status: open
title: expat ParseFile expects bytes, not string
type: behavior
versions: Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16726>
_______________________________________


More information about the Python-bugs-list mailing list