Closed
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
| BPO | 16726 |
|---|---|
| Nosy | @tiran, @iritkatriel |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2012-12-19.10:56:49.606>
labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
title = 'expat ParseFile expects bytes, not string'
updated_at = <Date 2021-11-28.12:36:08.033>
user = 'https://bugs.python.org/mdehoon'bugs.python.org fields:
activity = <Date 2021-11-28.12:36:08.033>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2012-12-19.10:56:49.606>
creator = 'mdehoon'
dependencies = []
files = []
hgrepos = []
issue_num = 16726
keywords = []
message_count = 4.0
messages = ['177733', '222953', '229287', '407203']
nosy_count = 5.0
nosy_names = ['mdehoon', 'christian.heimes', 'bow', 'sbardeau', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue16726'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
mdehoon commentedon Dec 19, 2012
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:
which runs fine. Note that argument to p.Parse is a string, not bytes.
This is the corresponding example of ParseFile:
where the file test.xml only contains <?xml version="1.0"?>
This gives an error:
Opening the file test.xml in binary raises an Error:
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.
BreamoreBoy commentedon Jul 13, 2014
@michiel I'm sorry about the delay in replying to you. I can confirm the same behaviour in 3.4.1 on 3.5.0a0 on Windows 7.
sbardeau commentedon Oct 14, 2014
Same problem here with:
Python 3.4.1 (default, Jul 30 2014, 14:02:54)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
What is unclear to me if this is a bug or a feature. In particular, as described here http://stackoverflow.com/questions/1179305/expat-parsing-in-python-3, one can open the xml file as binary to solve the issue. But is this /really/ intended? Opening standard ASCII xml files as binaries!?
iritkatriel commentedon Nov 28, 2021
Reproduced on 3.11.
serhiy-storchaka commentedon Sep 2, 2026
ParseFile()supports only binary files, which is now stated explicitly in the documentation (gh-156821, GH-156822). Adding support of text files (if anybody needs this) is a separate issue.