-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
XMLParser.__init__() leaking refs when called twice #148058
Copy link
Copy link
Open
Labels
pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Similarly to gh-147998, found with @devdanzin's Claude Code toolkit.
The XMLParser constructor does not care about already created objects if called twice:
cpython/Modules/_elementtree.c
Line 3716 in dea4083
| self->entity = PyDict_New(); |
cpython/Modules/_elementtree.c
Line 3720 in dea4083
| self->names = PyDict_New(); |
cpython/Modules/_elementtree.c
Line 3726 in dea4083
| self->parser = EXPAT(st, ParserCreate_MM)(encoding, &ExpatMemoryHandler, "}"); |
etc.
I think it exists since 52467b1
Reproduction
2026-04-03T21:46:41.206031691+0000 maurycy@weiss /home/maurycy/cpython (main) % cat 1.py
import gc
import xml.etree.ElementTree as ET
for i in range(20_000):
p = ET.XMLParser()
del p
if i % 1000 == 0:
gc.collect()
2026-04-03T21:46:41.431691265+0000 maurycy@weiss /home/maurycy/cpython (main) % cat 2.py
import gc
import xml.etree.ElementTree as ET
for i in range(20_000):
p = ET.XMLParser()
p.__init__()
del p
if i % 1000 == 0:
gc.collect()
Then:
2026-04-03T21:50:56.840438090+0000 maurycy@weiss /home/maurycy/cpython (main) % ./python.exe -X showrefcount 1.py
[0 refs, 0 blocks]
2026-04-03T21:51:01.610978599+0000 maurycy@weiss /home/maurycy/cpython (main) % ./python.exe -X showrefcount 2.py
[343430 refs, 302265 blocks]
CPython versions tested on:
CPython main branch
Operating systems tested on:
% cat /etc/debian_version
13.4
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error