Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46458: emit code for else of a try block immediately after the try body #30751

Merged
merged 8 commits into from
Jan 27, 2022

Conversation

iritkatriel
Copy link
Member

@iritkatriel iritkatriel commented Jan 21, 2022

This change makes us emit the code for an else of a try immediately after the try body so it avoids the jump to the else block. It adds a jump from the end of the else block to after the except blocks, so we are net neutral in terms of jumps.

However, since there is only one exit from the try-except-else part, the finally block doesn't get inlined so we gain a bit in terms of code size (but we still have the same number of jumps).

https://bugs.python.org/issue46458

@markshannon
Copy link
Member

Just to check. There is no change in behavior and all the new tests pass on main as well?

@iritkatriel
Copy link
Member Author

Just to check. There is no change in behavior and all the new tests pass on main as well?

The new tests in test_exception_variations and test_sys_settrace indeed pass on main.

@iritkatriel
Copy link
Member Author

Just to check. There is no change in behavior and all the new tests pass on main as well?

The new tests in test_exception_variations and test_sys_settrace indeed pass on main.

@markshannon Any further thoughts on this?

My intention is to merge this as a first step and then move on to the larger refactor that removes the jump from the end of orelse to the finally clause of the happy path.

Then I want to try and ensure that we have only one copy of finally for error cases (because there is no
point bloating the code to inline them).

When possible, I prefer to break changes up into steps which are easier to review (and later debug if necessary).

@markshannon
Copy link
Member

Looks good to me.

When possible, I prefer to break changes up into steps which are easier to review (and later debug if necessary).

I agree.

Comment on lines +198 to +199
self.assertFalse(hit_inner_else)
self.assertFalse(hit_else)
Copy link
Contributor

@erlend-aasland erlend-aasland Jan 27, 2022

Choose a reason for hiding this comment

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

Would these tests be slightly easier to read if the self.assertFalses were inlined as self.fail()? It might be easier to visualise the correct path with just a short glance.

Copy link
Member Author

Choose a reason for hiding this comment

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

You could do that for the assertFalse, but not for the assertTrue.

Copy link
Contributor

@erlend-aasland erlend-aasland Jan 27, 2022

Choose a reason for hiding this comment

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

Yes, that was the point :) The self.fails will clearly indicate the wrong path; you'll quickly be able to tell the correct path from the incorrect path.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, possibly that would be better. I followed the style of the existing test here.

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, readability is more important than following the existing style. But, I'm nitpicking. It's your pr :)

@iritkatriel iritkatriel merged commit 3d2ce34 into python:main Jan 27, 2022
@iritkatriel
Copy link
Member Author

Looks good to me.

Thank you for the review @markshannon.

@iritkatriel iritkatriel deleted the orelse branch January 27, 2022 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants