GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||
GeezerVison is showing github.com — gh-65106: shutil._unpack_zipfile add warnings for skipped files by akulakov · Pull Request #29910 · python/cpython Conversation
|
| if skipped: | ||
| import warnings | ||
| warnings.warn(f'unpack {filename}: {skipped} file(s) skipped' | ||
| ' (due to absolute path or `..` path component)') |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
warnings are a mecanism for a module author to communicate about bad usage to other developers calling the code, not to communicate with end-users: https://docs.python.org/3/howto/logging.html#when-to-use-logging
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
So using logging.warning() here should be appropriate?
Edit: I'm not really sure between logging.info, warnings.warn and logging.warning. This function can be used in both libraries and end user apps.
This might be an edge case that falls between warnings.warn and logging.warning? If so, I don't mind using one that you prefer.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I looked for a similar usages in the library and found this:
Line 1480 in f42a06b
| if len(comment) > ZIP_MAX_COMMENT: |
it's very similar in that both cases warn about something being lost when creating / unpacking a zip file: some contained files and a part of a comment respectively. That warning was added in 2014.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good find! @serhiy-storchaka added that change, maybe he can comment here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I just replaced existing prints with warnings. I am not sure that it is a good solution, but it at least gives the user some control: warnings can be printed, silenced or converted to exceptions.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I found another example where warning would be for the end user rather than app author: https://github.com/pganssle/zoneinfo/blob/c54d700fcba78b20733390ee3014809ad69858f6/src/backports/zoneinfo/_tzpath.py#L58
-- this one is from 2020 by Paul Ganssle
However I think for this use case, I don't see any issue with using logging.warning or logging.error. But I wonder which one of those would be more appropriate. It fits the description of logging.error more closely but at the same time treating it as an "error" might be too strong.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In general logging.warning would be cleaner here than using the warnings module. As Éric says, this is a data-driven problem that is of interest to the end-user, less so to the developer of the application.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@akulakov you could in fact ask on python-dev about the general practice if you want feedback; but I don't think this is particularly controversial: libraries can log things to end-users, and many do. I'd just use the logging module here, and as a follow-up task, fix the other warnings call you found in zipfile.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@ambv: I agree; I was going to update it but got a little distracted -- will push an update today or tomorrow.. Thanks for looking at this as well!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
A few functions in shutil.py accept logger as an arg and use it to print info/debug logs. I think in this case there's no reason to follow this pattern because we want the warnings to be displayed by default.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ran 164 tests in 2.291s
OK (skipped=24)
Looks ok.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
See also #111824 which fixes the original issue in a different way. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 90 days with no activity. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.