Repository navigation
[WIP] gh-83906: Suppress only handled exception by "raise ... from ...". #18629
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -469,9 +469,12 @@ def _prefix_from_ip_int(cls, ip_int): | |
| return prefixlen | ||
|
|
||
| @classmethod | ||
| def _report_invalid_netmask(cls, netmask_str): | ||
| def _report_invalid_netmask(cls, netmask_str, suppress=True): | ||
| msg = '%r is not a valid netmask' % netmask_str | ||
| raise NetmaskValueError(msg) from None | ||
| if suppress: | ||
| raise NetmaskValueError(msg) from None | ||
| else: | ||
| raise NetmaskValueError(msg) | ||
|
|
||
| @classmethod | ||
| def _prefix_from_prefix_string(cls, prefixlen_str): | ||
|
|
@@ -489,13 +492,13 @@ def _prefix_from_prefix_string(cls, prefixlen_str): | |
| # int allows a leading +/- as well as surrounding whitespace, | ||
| # so we ensure that isn't the case | ||
| if not (prefixlen_str.isascii() and prefixlen_str.isdigit()): | ||
| cls._report_invalid_netmask(prefixlen_str) | ||
| cls._report_invalid_netmask(prefixlen_str, suppress=False) | ||
| try: | ||
| prefixlen = int(prefixlen_str) | ||
| except ValueError: | ||
| cls._report_invalid_netmask(prefixlen_str) | ||
| if not (0 <= prefixlen <= cls._max_prefixlen): | ||
| cls._report_invalid_netmask(prefixlen_str) | ||
| cls._report_invalid_netmask(prefixlen_str, suppress=False) | ||
| return prefixlen | ||
|
|
||
| @classmethod | ||
|
|
@@ -1160,19 +1163,24 @@ def _make_netmask(cls, arg): | |
| if isinstance(arg, int): | ||
| prefixlen = arg | ||
| if not (0 <= prefixlen <= cls._max_prefixlen): | ||
| cls._report_invalid_netmask(prefixlen) | ||
| cls._report_invalid_netmask(prefixlen, suppress=False) | ||
| else: | ||
| try: | ||
| # Check for a netmask in prefix length form | ||
| prefixlen = cls._prefix_from_prefix_string(arg) | ||
| except NetmaskValueError: | ||
| # Check for a netmask or hostmask in dotted-quad form. | ||
| # This may raise NetmaskValueError. | ||
| prefixlen = cls._prefix_from_ip_string(arg) | ||
| prefixlen = cls._prefix_from_string(arg) | ||
|
Member
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. It was moved to a separate function because it is hard to write it correctly without try:
prefixlen = cls._prefix_from_prefix_string(arg)
except NetmaskValueError:
try:
prefixlen = cls._prefix_from_ip_string(arg)
except BaseException as exc:
raise exc from None |
||
| netmask = IPv4Address(cls._ip_int_from_prefix(prefixlen)) | ||
| cls._netmask_cache[arg] = netmask, prefixlen | ||
| return cls._netmask_cache[arg] | ||
|
|
||
| @classmethod | ||
| def _prefix_from_string(cls, arg): | ||
| try: | ||
| # Check for a netmask in prefix length form | ||
| return cls._prefix_from_prefix_string(arg) | ||
| except NetmaskValueError: | ||
| pass | ||
| # Check for a netmask or hostmask in dotted-quad form. | ||
| # This may raise NetmaskValueError. | ||
| return cls._prefix_from_ip_string(arg) | ||
|
|
||
| @classmethod | ||
| def _ip_int_from_string(cls, ip_str): | ||
| """Turn the given IP string into an integer for comparison. | ||
|
|
@@ -1592,7 +1600,7 @@ def _make_netmask(cls, arg): | |
| if isinstance(arg, int): | ||
| prefixlen = arg | ||
| if not (0 <= prefixlen <= cls._max_prefixlen): | ||
| cls._report_invalid_netmask(prefixlen) | ||
| cls._report_invalid_netmask(prefixlen, suppress=False) | ||
| else: | ||
| prefixlen = cls._prefix_from_prefix_string(arg) | ||
| netmask = IPv6Address(cls._ip_int_from_prefix(prefixlen)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 some cases there is no other exception to suppress. When we call this function not from the
exceptblock.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.
You can check whether
sys.exception()isNone.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.
Though, do you need to? What's the harm in "from None" when you are not in an except block?