Skip to content

expat ParseFile expects bytes, not string #60930

Closed

Description

@mdehoon
mannequin
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']

Activity

  1. mdehoon commented on Dec 19, 2012

    mdehoonmannequin
    MannequinAuthor

    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.

  2. BreamoreBoy commented on Jul 13, 2014

    BreamoreBoymannequin
    Mannequin

    @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.

  3. sbardeau commented on Oct 14, 2014

    sbardeaumannequin
    Mannequin

    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!?

  4. iritkatriel commented on Nov 28, 2021

    @iritkatriel
    Member

    Reproduced on 3.11.

  5. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.11only security fixes
    on Nov 28, 2021
  6. transferred this issue fromon Apr 10, 2022
  7. serhiy-storchaka commented on Sep 2, 2026

    @serhiy-storchaka
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions