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
Fix PY2 regression in connect() error handling. #150
Conversation
This autoformatting PR actually changed the behavior of the error handling: https://github.com/httplib2/httplib2/pull/105/files#diff-c6669c781a2dee1b2d2671cab4e21c66L985 "raise socket.err, msg" is not the same as "raise socket.error(msg)" in the case where msg is an exception. For instance consider: msg = socket.timeout("foo") raise socket.error, msg # => socket.timeout: foo raise socket.error(msg) # => socket.error: foo This has the effect of potentially changing the type of the error raised by connect(). Interestingly the PY3 copy of the code doesn't have this problem (probably cause it doesn't have msg at all; not sure if it's actually needed but I figured might as well keep it for PY2 in case it is). The change I propose here will restore the original behavior prior to the autoformat change, and will ensure that both the PY2 and PY3 copies of the code raise the same error type in the event of e.g. a socket.timeout.
Codecov Report
@@ Coverage Diff @@
## master #150 +/- ##
==========================================
+ Coverage 75.34% 75.51% +0.17%
==========================================
Files 8 8
Lines 2600 2602 +2
==========================================
+ Hits 1959 1965 +6
+ Misses 641 637 -4
Continue to review full report at Codecov.
|
some admittedly hacky reproduction instructions (just monkey-patching socket.connect to raise a timeout error):
This will raise socket.timeout in Python 3 and in older versions of httplib2 (prior to aa1b95b), but a ResponseNotReady error in Python 2 with the latest httplib2. |
python2/httplib2/__init__.py
Outdated
@@ -1241,7 +1244,7 @@ def connect(self): | |||
continue | |||
break | |||
if not self.sock: | |||
raise socket.error(msg) | |||
raise socket_err if socket_err else socket.error(msg) |
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.
raise a or b
would work too?
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.
even better, thanks!
There is only one assignment to |
Thanks, it's clear now. |
Moved msg inline for clarity as well.
Cool - I moved msg inline for clarity, and also applied an identical change to connect() in HTTPSConnectionWithTimeout since I forgot to include that originally. Let me know if anything else is needed. |
Thank you. |
0.15.0 python2: regression in connect() error handling httplib2/httplib2#150 add support for password protected certificate files httplib2/httplib2#143 feature: Http.close() to clean persistent connections and sensitive data httplib2/httplib2#149
0.15.0 python2: regression in connect() error handling httplib2/httplib2#150 add support for password protected certificate files httplib2/httplib2#143 feature: Http.close() to clean persistent connections and sensitive data httplib2/httplib2#149
This autoformatting PR actually changed the behavior of the error handling:
https://github.com/httplib2/httplib2/pull/105/files#diff-c6669c781a2dee1b2d2671cab4e21c66L985
"raise socket.err, msg" is not the same as "raise socket.error(msg)" in the case where msg is an exception. For instance consider:
msg = socket.timeout("foo")
raise socket.error, msg # => socket.timeout: foo
raise socket.error(msg) # => socket.error: foo
This has the effect of potentially changing the type of the error raised by connect(). Interestingly the PY3 copy of the code doesn't have this problem (probably cause it doesn't have msg at all; not sure if it's actually needed but I figured might as well keep it for PY2 in case it is).
The change I propose here will restore the original behavior prior to the autoformat change, and will ensure that both the PY2 and PY3 copies of the code raise the same error type in the event of e.g. a socket.timeout.