Follow-up from gh-148427 / PR #148435.
In ExpatParser.external_entity_ref() (Lib/xml/sax/expatreader.py), when an exception occurs during external entity parsing, the _entity_stack cleanup (restoring self._parser and self._source) is skipped because except Exception: return 0 exits the method before reaching lines 430–431:
try:
xmlreader.IncrementalParser.parse(self, source)
except Exception:
return 0 # exits here, cleanup below never runs
(self._parser, self._source) = self._entity_stack[-1] # skipped
del self._entity_stack[-1] # skipped
return 1
After an error, self._parser still points to the sub-parser for the external entity, self._source points to the entity's input source, and the original parser/source pair is stuck in _entity_stack.
In practice this probably doesn't matter much since the parser is usually abandoned after a SAXParseException, but it's still a state corruption if someone tries to reuse the parser or inspect its state after catching the exception.
A finally clause would fix this, but that was intentionally kept out of gh-148427 to keep the scope minimal.
Follow-up from gh-148427 / PR #148435.
In
ExpatParser.external_entity_ref()(Lib/xml/sax/expatreader.py), when an exception occurs during external entity parsing, the_entity_stackcleanup (restoringself._parserandself._source) is skipped becauseexcept Exception: return 0exits the method before reaching lines 430–431:After an error,
self._parserstill points to the sub-parser for the external entity,self._sourcepoints to the entity's input source, and the original parser/source pair is stuck in_entity_stack.In practice this probably doesn't matter much since the parser is usually abandoned after a
SAXParseException, but it's still a state corruption if someone tries to reuse the parser or inspect its state after catching the exception.A
finallyclause would fix this, but that was intentionally kept out of gh-148427 to keep the scope minimal.