[3.12] gh-101438: Avoid reference cycle in ElementTree.iterparse. (GH-114269) by miss-islington · Pull Request #114499 · python/cpython
from . import ElementPath
def iterator(source): if not hasattr(source, "read"): source = open(source, "rb") close_source = True else: close_source = False
def iterator(source): try: if not hasattr(source, "read"): source = open(source, "rb") close_source = True yield None while True: yield from pullparser.read_events() # load event buffer
class IterParseIterator(collections.abc.Iterator): __next__ = iterator(source).__next__ it = IterParseIterator() it.root = None del iterator, IterParseIterator
next(it) def __del__(self): if close_source: source.close()
it = IterParseIterator() wr = weakref.ref(it) del IterParseIterator return it