py3: don’t subscript socket.error stable
authorManuel Jacob <me@manueljacob.de>
Thu, 02 Jun 2022 04:39:49 +0200
branchstable
changeset 49274 b5fe10b3c9f5
parent 49272 fda7ec505dc5
child 49276 3b102efde517
py3: don’t subscript socket.error On Python 2, socket.error was subscriptable. On Python 3, socket.error is an alias to OSError and is not subscriptable. The except block passes the exception to self.send_error(). This fails on both Python 2 (if it was executed) and Python 3, as it expects a string. Getting the attribute .strerror works on Python 2 and Python 3, and has the same effect as the previous code on Python 2.
tests/test-http-proxy.t
tests/tinyproxy.py
--- a/tests/test-http-proxy.t	Thu Jun 02 02:05:11 2022 +0200
+++ b/tests/test-http-proxy.t	Thu Jun 02 04:39:49 2022 +0200
@@ -104,6 +104,13 @@
   new changesets 83180e7845de
   updating to branch default
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
+proxy can't connect to server
+
+  $ http_proxy=http://localhost:$HGPORT1/ hg --config http_proxy.always=True clone http://localhost:$HGPORT2/ h
+  abort: HTTP Error 404: Connection refused
+  [100]
+
   $ cat proxy.log
   * - - [*] "GET http://localhost:$HGPORT/?cmd=capabilities HTTP/1.1" - - (glob)
   $LOCALIP - - [$LOGDATE$] "GET http://localhost:$HGPORT/?cmd=batch HTTP/1.1" - - x-hgarg-1:cmds=heads+%3Bknown+nodes%3D x-hgproto-1:0.1 0.2 comp=$USUAL_COMPRESSIONS$ partial-pull (glob)
@@ -120,3 +127,5 @@
   * - - [*] "GET http://localhost:$HGPORT/?cmd=capabilities HTTP/1.1" - - (glob)
   $LOCALIP - - [$LOGDATE$] "GET http://localhost:$HGPORT/?cmd=batch HTTP/1.1" - - x-hgarg-1:cmds=heads+%3Bknown+nodes%3D x-hgproto-1:0.1 0.2 comp=$USUAL_COMPRESSIONS$ partial-pull (glob)
   $LOCALIP - - [$LOGDATE$] "GET http://localhost:$HGPORT/?cmd=getbundle HTTP/1.1" - - x-hgarg-1:bookmarks=1&$USUAL_BUNDLE_CAPS$&cg=1&common=0000000000000000000000000000000000000000&heads=83180e7845de420a1bb46896fd5fe05294f8d629&listkeys=bookmarks&phases=1 x-hgproto-1:0.1 0.2 comp=$USUAL_COMPRESSIONS$ partial-pull (glob)
+  * - - [*] code 404, message Connection refused (glob)
+  $LOCALIP - - [$LOGDATE$] "GET http://localhost:$HGPORT2/?cmd=capabilities HTTP/1.1" 404 - (glob)
--- a/tests/tinyproxy.py	Thu Jun 02 02:05:11 2022 +0200
+++ b/tests/tinyproxy.py	Thu Jun 02 04:39:49 2022 +0200
@@ -74,12 +74,8 @@
         print("\t" "connect to %s:%d" % host_port)
         try:
             soc.connect(host_port)
-        except socket.error as arg:
-            try:
-                msg = arg[1]
-            except (IndexError, TypeError):
-                msg = arg
-            self.send_error(404, msg)
+        except socket.error as e:
+            self.send_error(404, e.strerror)
             return 0
         return 1