Repository navigation
bpo-41530: Handle unhandled IsADirectoryError and PermissionError in zoneinfo.ZoneInfo #21839
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
Changes from 10 commits
aa75c26
dbf37fd
65f2569
768bdb3
4cbaa99
8ad2ec7
0065b49
db83f0d
bbf42e3
eafd623
ff1d9ed
c2e6fca
814d98c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ def load_tzdata(key): | |
| try: | ||
| return importlib.resources.open_binary(package_name, resource_name) | ||
| except (ImportError, FileNotFoundError, UnicodeEncodeError): | ||
| # There are three types of exception that can be raised that all amount | ||
| # There are some types of exception that can be raised that all amount | ||
| # to "we cannot find this key": | ||
| # | ||
| # ImportError: If package_name doesn't exist (e.g. if tzdata is not | ||
|
|
@@ -22,6 +22,23 @@ def load_tzdata(key): | |
| # UnicodeEncodeError: If package_name or resource_name are not UTF-8, | ||
| # such as keys containing a surrogate character. | ||
| raise ZoneInfoNotFoundError(f"No time zone found with key {key}") | ||
| except OSError as exc: | ||
| # Exceptions inherited from OSError can be raised in various scenarios: | ||
| # | ||
| # IsADirectoryError: If the resource_name links to a directory | ||
| # (e.g. Australia) | ||
| # PermissionError: If the resource_name links to a directory on Windows | ||
| # (e.g. Pacific) | ||
| import os | ||
| import importlib.util | ||
|
|
||
| package_spec = importlib.util.find_spec(package_name) | ||
| resource_path = os.path.join(os.path.dirname(package_spec.origin), key) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imports+two lines of code is a risk of raising a new exception. You may do that before and use the path to load the resource. I'm not sure. Is it possible that tzdata is in a ZIP file, or in memory but not on disk? Maybe it's overcomplicated and maybe it's better to convert any OSError into a ZoneInfoNotFoundError. I let @pganssle decides what is the best.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don't like this at all. In That will complicate things a bit for the backport, though, which doesn't currently require I'm inclined to go the simple way and catch all
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jaraco Do you know if there's an equivalent of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Python 3.8 and earlier,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jaraco Well, we're not really concerned with using a directory as a resource, the problem is that it appears that Is there no way to do that in the Python 3.8
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. swapped to using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pganssle Apologies for having dropped the ball on this one. I don't believe there's a reliable in the older importlib.resources API to detect that a directory is present. It was meant to be the case that a directory was indistinguishable from 'not a resource'. What I would recommend though is to use |
||
|
|
||
| if os.path.isdir(resource_path): | ||
| raise ZoneInfoNotFoundError(f"No time zone found with key {key}") | ||
| else: | ||
| raise | ||
|
|
||
|
|
||
| def load_data(fobj): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.