You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ElementTree parser limitation of input string size #83895
I am trying to parse a very large XML file. Here is the output:
python3.7.4 crif_parser.py
Retrieved 3593891712 characters <- this is printed from my script
Traceback (most recent call last):
File "crif_parser.py", line 9, in <module>
tree =ET.fromstring(data)
File "python3/3.7.4/lib/python3.7/xml/etree/ElementTree.py", line 1315, in XML
parser.feed(text)
OverflowError: size does not fit in an int
I'd suggest feeding the data into the parser in chunks, or letting it read from a file-like object, or something like that.
Also, you probably want to do incremental processing on the data (see the XMLPullParser and iterparse), because reading 3.5GB of XML data into an in-memory tree can easily result in 10x the memory usage. You may have 40GB of RAM on your machine, but even then, I'd still recommend processing the data in incrementally.
I personally don't think we should do something about this. It's very easy to work around the failure by using the much more efficientparse() function instead of first reading so much data into a string in memory and then parsing from that. That just uselessly wastes both memory and time. Whether it's a good idea to parse the whole document into memory at all is then yet another question. Most people should be better off using iterparse() or the XMLPullParser instead.
If nothing else, the failure at least makes users aware that they are not doing something recommended.
For modern big machines with dozens gigabytes of memory, it might be not so senseless (and be a good trade-off for its simplicity). If it's an int32 limitation, lifting it to int64 might be good
The C implementation raised OverflowError because Expat takes the length as an int. xml.parsers.expat has fed Expat in chunks since bpo-17089 (2013), so the pure Python implementation of ElementTree already accepted input larger than 2 GiB; only the accelerator refused it:
@scoder, your advice stays right: parse() or iterparse() is better than reading gigabytes into a string first. But xml.parsers.expat accepts such input, so this was not a deliberate limitation, and the accelerated module should not be able to do less than the pure Python one. When I fixed pyexpat in bpo-17089 I could not test this half of it: machines had 2 GiB of RAM. The new test needs 6 GiB and is skipped unless -M is given.
Chunking is not slower for ordinary documents either: 1 GiB with a million elements is parsed in 2.37 s in chunks of 1 MiB and in 2.60 s in a single call.
Activity
AnanthVijalapuram commentedon Feb 21, 2020
I am trying to parse a very large XML file. Here is the output:
scoder commentedon Sep 8, 2020
I'd suggest feeding the data into the parser in chunks, or letting it read from a file-like object, or something like that.
Also, you probably want to do incremental processing on the data (see the XMLPullParser and iterparse), because reading 3.5GB of XML data into an in-memory tree can easily result in 10x the memory usage. You may have 40GB of RAM on your machine, but even then, I'd still recommend processing the data in incrementally.
[-]ElementTree limitation[/-][+]ElementTree parser limitation of input string size[/+][-]ElementTree limitation[/-][+]ElementTree parser limitation of input string size[/+]vadimkantorov commentedon Dec 14, 2023
This error is hit when reading wiktionary dumps :(
scoder commentedon Dec 15, 2023
I personally don't think we should do something about this. It's very easy to work around the failure by using the much more efficient
parse()function instead of first reading so much data into a string in memory and then parsing from that. That just uselessly wastes both memory and time. Whether it's a good idea to parse the whole document into memory at all is then yet another question. Most people should be better off usingiterparse()or theXMLPullParserinstead.If nothing else, the failure at least makes users aware that they are not doing something recommended.
vadimkantorov commentedon Dec 15, 2023
For modern big machines with dozens gigabytes of memory, it might be not so senseless (and be a good trade-off for its simplicity). If it's an int32 limitation, lifting it to int64 might be good
serhiy-storchaka commentedon Aug 31, 2026
PR #156746 removes the limitation.
The C implementation raised
OverflowErrorbecause Expat takes the length as anint.xml.parsers.expathas fed Expat in chunks since bpo-17089 (2013), so the pure Python implementation of ElementTree already accepted input larger than 2 GiB; only the accelerator refused it:@scoder, your advice stays right:
parse()oriterparse()is better than reading gigabytes into a string first. Butxml.parsers.expataccepts such input, so this was not a deliberate limitation, and the accelerated module should not be able to do less than the pure Python one. When I fixedpyexpatin bpo-17089 I could not test this half of it: machines had 2 GiB of RAM. The new test needs 6 GiB and is skipped unless-Mis given.Chunking is not slower for ordinary documents either: 1 GiB with a million elements is parsed in 2.37 s in chunks of 1 MiB and in 2.60 s in a single call.
Merge branch 'main' into pythongh-83895-large-input
gh-83895: Accept input larger than 2 GiB in the C implementation of E…
[3.14] gh-83895: Accept input larger than 2 GiB in the C implementati…
[3.13] gh-83895: Accept input larger than 2 GiB in the C implementati…
scoder commentedon Sep 1, 2026
Change looks good and helpful to me. Thanks for implementing it.
pythongh-83895: Accept input larger than 2 GiB in the C implementatio…
[3.15] gh-83895: Accept input larger than 2 GiB in the C implementati…