Skip to content

gh-148427: Fix bare except in expatreader.external_entity_ref()#148435

Open
WYSIATI wants to merge 5 commits intopython:mainfrom
WYSIATI:fix-expatreader-bare-except-148427
Open

gh-148427: Fix bare except in expatreader.external_entity_ref()#148435
WYSIATI wants to merge 5 commits intopython:mainfrom
WYSIATI:fix-expatreader-bare-except-148427

Conversation

@WYSIATI
Copy link
Copy Markdown
Contributor

@WYSIATI WYSIATI commented Apr 12, 2026

ExpatParser.external_entity_ref() uses a bare except: that silently swallows KeyboardInterrupt and SystemExit during external entity parsing. It also leaks the _entity_stack on errors since the cleanup code only runs on the success path.

This changes except: to except Exception: and moves the stack cleanup into a finally block.

I verified in pyexpat.c that the C layer handles Python exception propagation correctly — call_with_frame() calls XML_StopParser() on callback failure, and get_parse_result() checks PyErr_Occurred() before inspecting the return value, so letting KeyboardInterrupt through is safe.

Change bare `except:` to `except Exception:` so that KeyboardInterrupt
and SystemExit propagate instead of being silently swallowed during
external entity parsing. Move entity stack cleanup into a `finally`
clause so the parser state is restored on both success and error paths.
@WYSIATI WYSIATI force-pushed the fix-expatreader-bare-except-148427 branch from 738bda2 to 2833a88 Compare April 12, 2026 11:03
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 12, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

WYSIATI added 2 commits April 12, 2026 20:04
- Keep FIXME comment about error info
- Use mock.Mock with startElement.side_effect instead of inner classes
- Narrow assertRaises to only the triggering feed() call
- Update NEWS wording per reviewer suggestion
The side_effect must only raise for the external entity's element
(<entity/>), not for the main document's element (<doc>), since
startElement is called for both.
Comment on lines +429 to +431
finally:
(self._parser, self._source) = self._entity_stack[-1]
del self._entity_stack[-1]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure about this. Strictly speaking this is actually a behavior change. After an exception I don't know whether we should leave the stack alone or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The finally is a separate concern from the except:except Exception: fix. I can remove it to keep this PR focused on the bare except issue. Without finally, the stack still leaks on errors. that's pre-existing behavior and could be addressed separately if needed.

I decided to resolve them together, however I can drop it if you recommend so :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, don't make multiple changes per PR. We need a separate issue and a separate PR. Please read https://devguide.python.org/getting-started/pull-request-lifecycle.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing the link, I will be more cautious with future PRs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I would like to create a separate issue for this this stack problem, if no objection

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do so.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue #148448 for the stack issue

Use @support.subTests to replace three separate tests with one
parameterized test, per reviewer suggestion.

if issubclass(exc_type, Exception):
self.assertRaises(SAXParseException, parser.feed, trigger)
self.assertEqual(len(parser._entity_stack), 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to remove this line once you reverted the other unrelated change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean we should revert the finally change?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Drop the finally clause per reviewer feedback — the stack cleanup
behavior change is a separate concern. Remove the entity stack
assertion that depended on it.
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update the documentation (if it mentions exceptions). If it doesn't mention exceptions, no need to update it.

@picnixz picnixz dismissed their stale review April 12, 2026 13:07

changes were done

@WYSIATI
Copy link
Copy Markdown
Contributor Author

WYSIATI commented Apr 12, 2026

Can you also update the documentation (if it mentions exceptions). If it doesn't mention exceptions, no need to update it.

Checked — the docs don't mention exceptions in the context of external entity handling, so no doc update needed

I have made the requested changes; please review again

@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 12, 2026

Thanks for making the requested changes!

: please review the changes made to this pull request.

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.

2 participants