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

Reraise sensible errors from auto_chmod #4593

Merged

Conversation

Avasam
Copy link
Contributor

@Avasam Avasam commented Aug 24, 2024

Summary of changes

My best understanding is that this is old Python2 code that was trying to re-raise an error with additional information in the message. (back when raise could take a tuple of strings)

This PR changes the nonsensical TypeError: 'Exception' object is not subscriptable that would always happen now for for sensible and complete messages with the old error as part of the trace. Optionally we could just reraise.

Reduces changes in #4192

Pull Request Checklist

@Avasam Avasam force-pushed the Reraise-sensible-errors-from-auto_chmod branch from 51cd3b3 to 450a962 Compare August 24, 2024 19:22
def auto_chmod(func: Callable[..., _T], arg: str, exc: BaseException) -> _T:
"""shutils onexc callback to automatically call chmod for certain functions."""
if os.name != 'nt':
raise OSError(f"Can't call auto_chmod on non-nt os {os.name!r}") from exc
Copy link
Contributor

@abravalheri abravalheri Aug 27, 2024

Choose a reason for hiding this comment

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

This is an interesting aspect of the previous implementation (not related to your changes): why can we not call chmod(path, stat.S_IWRITE) on non Windows OSs?

I had a look on the git blame history and found that this check was introduced in d675527, but I cannot guess from the commit message why it is restricted to Windows only.

This thread https://bugs.python.org/issue22040 seems to suggest that rather than the workaround not being applicable to other OSs, it is the problem that seems to only appear on Windows...

I wonder if we actually need this OS-specific check...

I can see other projects in the community just going for it, without checking for windows:

https://github.com/python-poetry/poetry/blob/5d19ab0d8e9b734e3452c6e1210a7f2501709397/src/poetry/utils/helpers.py#L93-L98

https://github.com/scientific-python/cookie/blob/f170dd542dfc133a0de3a36ac3c5c36470f066d7/noxfile.py#L43-L45

The most complex handling I saw were from pre-commit and pip, but still there is no OS-specific check:

https://github.com/pre-commit/pre-commit/blob/504149d2ca57f3115ccd4fa9c6ae692929b2282a/pre_commit/util.py#L205-L219

https://github.com/pypa/pip/blob/main/src/pip/_internal/utils/misc.py#L153-L191

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see why either, just ran os.chmod(".", stat.S_IWRITE) in WSL and it doesn't immediately error out or whatever.

seems to suggest that rather than the workaround not being applicable to other OSs, it is the problem that seems to only appear on Windows...

I'd make more sense to just pass-through if not Windows, or call os.chmod indescriminately.

Copy link
Contributor Author

@Avasam Avasam Aug 27, 2024

Choose a reason for hiding this comment

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

Here's another way of seeing it: if we know the error only happens on Windows, there's no point in "fixing and retrying", so it was just immediately rearising to keep the traceback to a minimum.

My suggestion here would be closer to that: #4593 (comment) (with a comment # Only retry for scenarios known to have an issue)

@Avasam Avasam force-pushed the Reraise-sensible-errors-from-auto_chmod branch from 79b06aa to ef2957a Compare August 27, 2024 16:52
@abravalheri abravalheri merged commit 903604b into pypa:main Aug 27, 2024
18 of 20 checks passed
@abravalheri
Copy link
Contributor

Thank you very much!

@Avasam Avasam deleted the Reraise-sensible-errors-from-auto_chmod branch August 27, 2024 17:16
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.

2 participants