-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
bpo-42060: Remove assert from http/client.py
#22737
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
Assert statements are removed when optimization is requested, which in turn may cause that those safeguards are lifted
Lib/http/client.py
Outdated
| self.close() | ||
| raise | ||
| assert response.will_close != _UNKNOWN | ||
| if self.chunked == _UNKNOWN: |
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 feel that this behavior is different than the original code's
assert response.will_close != _UNKNOWN
Furthermore, I'm unsure if HTTPException, is the correct exception to use here; the documentation states that ConnectionError is usually raised by getresponse
I would suggest instead:
if response.will_close == _UNKNOWN:
raise ConnectionError("Unknown if response will close.")Thanks for the contribution!
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.
Oh, you're completely right. I took the three as equals and missed the mark. Thanks for the review!
|
Hi, the Travis CI tests are failing due to whitespace issues, You can run |
|
Done, @Fidget-Spinner! I'm not very familiar with cpython's testing suite myself. Is there a way to add a test that will be executed with the optimization turned on? If so, I could add a test for this specific PR itself |
|
@fbidu, unfortunately, I'm not too clear on this myself. You'll have to get help from someone else, sorry. AFAIK, (from looking around the Travis and Ubuntu automated tests), they don't test with -O, and there are other parts of the stdlib that have asserts that break during -O too, eg. https://bugs.python.org/issue23819. From my cursory look around the python test suite, I don't know of any tests that specifically test with -O. One possible way I can think of is to either call from test.support.script_helper import spawn_python, kill_python
p = spawn_python('-O', '-c', '...')
kill_python(p)As for where to put such a test, the unittests are found in Once again, please take my advice with a large grain of salt, I'm unsure if tests for -O are usually written. |
| @@ -0,0 +1 @@ | |||
| Replace `asserts` on HTTPResponse and HTTPConnection with `if...raises` so that the conditional checks work while running with the optimization flag switched on No newline at end of file | |||
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.
reST requires two backticks for code, not one like markdown :).
| Replace `asserts` on HTTPResponse and HTTPConnection with `if...raises` so that the conditional checks work while running with the optimization flag switched on | |
| Replace ``assert`` in :class:`http.client.HTTPResponse` and :class:`http.client.HTTPConnection` with ``raise`` | |
| so that conditional checks work while running with Python's runtime optimization | |
| ``-O`` flag enabled. |
|
@pitrou @serhiy-storchaka @vadmium The asserts who conversion is proposed were added by: |
serhiy-storchaka
left a comment
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.
Please add tests if these exceptions can be triggered by malformed input.
If it is not possible, using assert is correct.
|
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 |
|
This PR is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days |
|
There had been no response to the request for changes, so I am closing this as abandoned. |
This PR removes
assertstatement from the stdlib's HTTPclient.pyand switches by anif...raiseapproach that won't be removed if optimization is requestedhttps://bugs.python.org/issue42060