Skip to content

gh-83895: Accept input larger than 2 GiB in the C implementation of ElementTree - #156746

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-83895-large-input
Open

gh-83895: Accept input larger than 2 GiB in the C implementation of ElementTree#156746
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-83895-large-input

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 31, 2026

Copy link
Copy Markdown
Member

XMLParser.feed() and ElementTree.parse() raised OverflowError: size does not fit in an int for input larger than 2 GiB, because Expat takes the length as an int. The data is now fed to Expat in chunks of 1 MiB, exactly as xml.parsers.expat has done since bpo-17089, so the pure Python implementation already accepted such input:

>>> data = b'<r>' + b'x' * ((1 << 31) + (1 << 27)) + b'</r>'
>>> len(ET.fromstring(data).text)
2281701376

Chunking is not slower for ordinary documents. Parsing 1 GiB with a million elements takes 2.37 s with chunks and 2.60 s with a single XML_Parse() call, presumably because 1 MiB chunks stay in cache.

test_length_overflow, which asserted the old error, is replaced by a bigmem test of the new behaviour, plus two cheap tests which run always, because the chunking loop is entered for any input larger than 1 MiB.

…n of ElementTree

XMLParser.feed() and ElementTree.parse() raised OverflowError, because Expat
takes the length as an int.  The data is now fed to Expat in chunks of 1 MiB,
as xml.parsers.expat does since bpo-17089, so the pure Python implementation
already accepted such input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant