-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
bpo-20907: shutil._unpack_zipfile add warnings for skipped files #29910
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
base: main
Are you sure you want to change the base?
bpo-20907: shutil._unpack_zipfile add warnings for skipped files #29910
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.
Choose a reason for 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
There was a problem hiding this comment.
Choose a reason for 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.
- it's not necessarily true (but can be) that client app needs to be modified -- for example if it needs to always extract all files, it can be changed to use ZipFile module
- it's also not true that client app cannot do anything about it -- it can potentially use ZipFIle module or 3rd party module or custom logic.
- logging.info seems not specific enough because we're warning somebody (user or developer) that files are skipped.
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.
There was a problem hiding this comment.
Choose a reason for 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.
There was a problem hiding this comment.
Choose a reason for 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.
There was a problem hiding this comment.
Choose a reason for 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.
There was a problem hiding this comment.
Choose a reason for 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.
There was a problem hiding this comment.
Choose a reason for 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.
There was a problem hiding this comment.
Choose a reason for 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
.
There was a problem hiding this comment.
Choose a reason for 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!
There was a problem hiding this comment.
Choose a reason for 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.
There was a problem hiding this comment.
Choose a reason for 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.
See also #111824 which fixes the original issue in a different way. |
Note that test zip only has a '..' path component for testing, and no absolute path. My zip archiver (as well as Python zip archiver) do not create absolute paths. It seems like all (?) modern zip archivers don't create absolute paths. If someone knows of an easy way to add such a path to the test zip file, I can update the unit test for that.
https://bugs.python.org/issue20907