Skip to content

Conversation

fangyi-zhou
Copy link
Contributor

@fangyi-zhou fangyi-zhou commented Jul 20, 2025

As reported in #154, if a caught exception is bound to a name e in a except block, the caught name e should be out of scope after the corresponding except scope.

This is reflected in the spec as follows
https://docs.python.org/3/reference/compound_stmts.html#except-clause:

When an exception has been assigned using as target, it is cleared at the end of the except clause.

This PR aims to model this effect by marking the caught exception name as uninitialised at the end of the current except block.

Closes #154.

@meta-cla meta-cla bot added the cla signed label Jul 20, 2025
@fangyi-zhou fangyi-zhou force-pushed the fix-exception-leaking branch from 718cf0a to 7207921 Compare July 22, 2025 21:50
@fangyi-zhou fangyi-zhou changed the title (Partially) fix caught exception leaking through scope Fix caught exception leaking through scope Jul 22, 2025
@fangyi-zhou fangyi-zhou changed the title Fix caught exception leaking through scope Fix caught exception leaking through except scope Jul 22, 2025
@fangyi-zhou fangyi-zhou marked this pull request as ready for review July 22, 2025 22:09
@yangdanny97 yangdanny97 requested a review from stroxler July 22, 2025 23:10
);
}

if let Some(ref name) = last_caught_exception_name {
Copy link
Contributor

@yangdanny97 yangdanny97 Jul 22, 2025

Choose a reason for hiding this comment

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

I'm confused why this block & the one on 816 are necessary - doesn't each exception get marked as uninitialized on 799?

Copy link
Contributor Author

@fangyi-zhou fangyi-zhou Jul 23, 2025

Choose a reason for hiding this comment

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

I'll try to give a more detailed answer later. In short, this has something to do with how we error on uninit variable (cf. PR #666).

If we only error at the end of the except scope, then the uninit does nothing in the next scope since silent lookup failures won't error.
So we inject an uninit to make it error.

On the other hand, we get slightly confusing and error messages of "maybe uninit" if we only inject uninit in the next scope due to how flow merging works.

I concede this is a somewhat hacky solution, let me know if you have a better idea. Sorry for the concise explanation.

Edit: Please disregard what I said previously. I agree with you that this is unnecessary

As reported in facebook#154, if a caught exception is bound to a name `e` in a
`try`/`except` block, the caught name `e` should be out of scope after
the current `except` scope.

This is reflected in the spec as follows
https://docs.python.org/3/reference/compound_stmts.html#except-clause:

> When an exception has been assigned using as target, it is cleared at the end of the except clause.

This PR aims to model this effect by marking the caught exception name
as uninitialised at the end of the `except` clause.
@fangyi-zhou fangyi-zhou force-pushed the fix-exception-leaking branch from 7207921 to d4cfbaa Compare July 23, 2025 21:39
@fangyi-zhou
Copy link
Contributor Author

fangyi-zhou commented Jul 23, 2025

Sorry for the earlier confusion. I was under the impression that adding another uninit in the next scope was necessary, but I seem to got confused by myself earlier during debugging. I've validated my first commit and concluded that adding an uninit in the end of the scope should be sufficient.

It looks like I was chasing a non-existent bug earlier; but on the positive side, the solution to the issue is straightforward.

@facebook-github-bot
Copy link
Contributor

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D78898967.

Copy link
Contributor

@stroxler stroxler left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@facebook-github-bot
Copy link
Contributor

@yangdanny97 merged this pull request in b0586d9.

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.

Del the name of the exception at the end of the exception handler

4 participants