-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-33034: Added explicit exception message when cast fails #6078
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
Conversation
Lib/urllib/parse.py
Outdated
port = int(port, 10) | ||
if not ( 0 <= port <= 65535): | ||
raise ValueError("Port out of range 0-65535") | ||
except ValueError: |
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 think it would be better to move int(port, 10)
into try...except block and raise ValueError
with your suggested wording:
try:
port = int(port, 10)
except ValueError:
raise ValueError('...') from None
if not ( 0 <= port <= 65535):
...
We also need a test case for this.
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.
That sounds good. Will update now and include a test.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I have made the requested changes; please review again. |
Thanks for making the requested changes! @berkerpeksag: please review the changes made to this pull request. |
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.
We also need a news entry for this. See https://devguide.python.org/committing/#what-s-new-and-news-entries for details.
Lib/test/test_urlparse.py
Outdated
def test_issue33034(self): | ||
# Test case to asset a valid port as an integer | ||
p1 = urllib.parse.urlparse('https://www.python.org:80') | ||
self.assertEqual(p1.port, 80) |
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.
This case is already tested so we can drop it.
Lib/test/test_urlparse.py
Outdated
@@ -936,6 +936,16 @@ def test_issue14072(self): | |||
self.assertEqual(p2.scheme, 'tel') | |||
self.assertEqual(p2.path, '+31641044153') | |||
|
|||
def test_issue33034(self): |
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.
Style nitpick: Could you please give the test a more descriptive name?
Lib/test/test_urlparse.py
Outdated
|
||
# Test case to assert ValueError when a string is parsed as a port | ||
p2 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle') | ||
with self.assertRaises(ValueError): |
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.
Since the exception type is unchanged and only the exception message is changed, I think we also need to add an assert for the new exception message.
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.
Great idea. Update to the PR coming right up.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
…case, added assert to check error message
I have made the requested changes; please review again. |
Thanks for making the requested changes! @berkerpeksag: please review the changes made to this pull request. |
Looks like there is a white space issue? Will try and resolve by pushing another commit. |
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 left mostly trivial comments. In general, this looks good to me, thanks!
However, I still need some time to reread the discussion and play with the patch :)
Lib/test/test_urlparse.py
Outdated
# Assert ValueError when int(string) is parsed as a port value | ||
# Asset that the error message is port could not be cast to integer value | ||
p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle') | ||
with self.assertRaises(ValueError) as valueError: |
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.
Style nit: We usually use cm
as a name for unittest context managers.
@@ -0,0 +1 @@ | |||
Providing an explicit message when casting a port that is a string instead of an integer. Port could not be cast to integer value. |
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.
Style nit: Please wrap lines at 80 characters.
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 may also want to mention urlparse
in the news entry :) For context, this text is going to be listed at https://docs.python.org/3/whatsnew/changelog.html
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.
And please add "Patch by Matt Eaton."
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.
Thank you very much!
I have made the requested changes; please review again. |
Thanks for making the requested changes! @berkerpeksag: please review the changes made to this pull request. |
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
… to now use assertRaisesRegex()
Looks like there is a white space issue in the build. Updating. |
I have made the requested changes; please review again. |
Thanks for making the requested changes! @berkerpeksag, @ericvsmith: please review the changes made to this pull request. |
Just wanted to check in and ask if there was anything that I should be addressing on this PR? |
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 https://bugs.python.org/issue33034#msg314005 you said:
The new ValueError message raised in this patch does work for both urlparse and urlsplit.
We also need a test case for urlsplit
function (I forgot this in my earlier comments, sorry) and of course, we also need to adjust the NEWS entry a bit (it currently only mentions the urlparse
case)
Lib/test/test_urlparse.py
Outdated
# Asset that the error message is: | ||
# port oracle could not be cast to integer value | ||
p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle') | ||
with self.assertRaisesRegex(ValueError, "Port could not be " \ |
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.
Style nit: It's better store the exception message in a msg
or message
variable.
Lib/urllib/parse.py
Outdated
try: | ||
port = int(port, 10) | ||
except ValueError: | ||
raise ValueError("Port could not be cast " \ |
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.
We could use an f-string here:
f'Port could not be cast to integer value as {port!r}'
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.
Style nit: I'd either use a msg
or message
variable or a different indentation style instead of backslashes:
message = f'Port could not be cast to integer value as {port!r}'
raise ValueError(message) from None
or
raise ValueError(
f'Port could not be cast to integer value as {port!r}'
) from None
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.
Will update. Thank you!
Applied changes and updated the PR. Added the test for |
Thanks for making the requested changes! @ericvsmith, @berkerpeksag: please review the changes made to this pull request. |
@@ -0,0 +1,3 @@ | |||
Providing an explicit error message when casting the port property to anything | |||
that is not and integer value using ``urlparse()`` and ``urlsplit()``. Port |
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.
Just a quick question: "... is not and integer ..." is 'and' a typo 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.
Yes it is. Thank you! Updating.
I have made the requested changes; please review again. |
Thanks for making the requested changes! @ericvsmith, @berkerpeksag: please review the changes made to this pull request. |
LGTM, thanks! I will wait for a few days to see what @ericvsmith thinks about the PR. |
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.
Looks good to me. Thanks!
bpo-33034: Added explicit exception message when cast fails to parse.py.
BPO Issue: https://bugs.python.org/issue33034
https://bugs.python.org/issue33034