Skip to content

gh-125522: Fix bare except in test_uuid #129018

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

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

tomasr8
Copy link
Member

@tomasr8 tomasr8 commented Jan 19, 2025

The intent of the helper function is only to check whether a module can be imported, so catching all exceptions could hide other issues.

(I'm adding the backport labels seeing as the other PRs were also backported)

@tomasr8 tomasr8 added skip news needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Jan 19, 2025
@bedevere-app bedevere-app bot added the tests Tests in the Lib/test dir label Jan 19, 2025
@graingert graingert self-requested a review January 19, 2025 12:05
Co-authored-by: Thomas Grainger <[email protected]>
@@ -21,7 +21,7 @@ def importable(name):
try:
__import__(name)
return True
except:
except ModuleNotFoundError:
Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to be maximally pedantic you can go even more specific:

Suggested change
except ModuleNotFoundError:
except ModuleNotFoundError as e:
if e.name == name:
return False
raise

Eg if a module is successfully found but then imports another module which isn't found, this will raise but the code in the PR will not

Copy link
Member Author

Choose a reason for hiding this comment

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

There's only one other place in the repo that looks at e.name so this seems to be a pretty uncommon pattern and maybe not worth for test code? Though I don't have a strong preference either way :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I'm cool with it either way

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, I'll keep it as is for now and let's see what other reviewers think :)

@tomasr8 tomasr8 requested a review from iritkatriel January 29, 2025 20:21
@@ -21,7 +21,7 @@ def importable(name):
try:
__import__(name)
return True
except:
except ModuleNotFoundError:
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure this is the only exception type that can come from __import__(name)?

I guess since it's a test it's fine to assume (if the tests pass).

Copy link
Member Author

Choose a reason for hiding this comment

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

Not 100% sure, but I think anything else would be unexpected here and would warrant looking into

@iritkatriel iritkatriel merged commit a1a4e9f into python:main Jan 29, 2025
41 checks passed
@miss-islington-app
Copy link

Thanks @tomasr8 for the PR, and @iritkatriel for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 29, 2025
(cherry picked from commit a1a4e9f)

Co-authored-by: Tomas R. <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jan 29, 2025

GH-129455 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jan 29, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 29, 2025
(cherry picked from commit a1a4e9f)

Co-authored-by: Tomas R. <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jan 29, 2025

GH-129456 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Jan 29, 2025
@tomasr8 tomasr8 deleted the test-uuid-bare-except branch January 29, 2025 21:05
iritkatriel pushed a commit that referenced this pull request Jan 29, 2025
gh-125522: Fix bare except in `test_uuid` (GH-129018)
(cherry picked from commit a1a4e9f)

Co-authored-by: Tomas R <[email protected]>
iritkatriel pushed a commit that referenced this pull request Jan 29, 2025
gh-125522: Fix bare except in `test_uuid` (GH-129018)
(cherry picked from commit a1a4e9f)

Co-authored-by: Tomas R <[email protected]>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants