Repository: web2py/web2py

Issue:
Exception handling in gluon/contrib/gateways/fcgi.py is incompatible with Python 3
Exception handling is not compatible with Python 3.
To make it compatible with both Python 2 and 3, handling should be changed from

    except socket.error, e:
        if e[0] == errno.EAGAIN:

to

    except socket.error as e:
        if e.errno == errno.EAGAIN:

Followup to #1905. There are no other files in the same case


Discussion:

Resolving patch:
diff --git a/gluon/contrib/gateways/fcgi.py b/gluon/contrib/gateways/fcgi.py
--- a/gluon/contrib/gateways/fcgi.py
+++ b/gluon/contrib/gateways/fcgi.py
@@ -470,8 +470,8 @@ def _recvall(sock, length):
         while length:
             try:
                 data = sock.recv(length)
-            except socket.error, e:
-                if e[0] == errno.EAGAIN:
+            except socket.error as e:
+                if e.errno == errno.EAGAIN:
                     select.select([sock], [], [])
                     continue
                 else:
@@ -527,8 +527,8 @@ def _sendall(sock, data):
         while length:
             try:
                 sent = sock.send(data)
-            except socket.error, e:
-                if e[0] == errno.EAGAIN:
+            except socket.error as e:
+                if e.errno == errno.EAGAIN:
                     select.select([], [sock], [])
                     continue
                 else:
@@ -665,7 +665,7 @@ def run(self):
             except EOFError:
                 break
             except (select.error, socket.error), e:
-                if e[0] == errno.EBADF: # Socket was closed by Request.
+                if e.errno == errno.EBADF: # Socket was closed by Request.
                     break
                 raise
 
@@ -990,11 +990,11 @@ def _setupSocket(self):
                                  socket.SOCK_STREAM)
             try:
                 sock.getpeername()
-            except socket.error, e:
-                if e[0] == errno.ENOTSOCK:
+            except socket.error as e:
+                if e.errno == errno.ENOTSOCK:
                     # Not a socket, assume CGI context.
                     isFCGI = False
-                elif e[0] != errno.ENOTCONN:
+                elif e.errno != errno.ENOTCONN:
                     raise
 
             # FastCGI/CGI discrimination is broken on Mac OS X.
@@ -1078,15 +1078,15 @@ def run(self, timeout=1.0):
             try:
                 r, w, e = select.select([sock], [], [], timeout)
             except select.error, e:
-                if e[0] == errno.EINTR:
+                if e.errno == errno.EINTR:
                     continue
                 raise
 
             if r:
                 try:
                     clientSock, addr = sock.accept()
-                except socket.error, e:
-                    if e[0] in (errno.EINTR, errno.EAGAIN):
+                except socket.error as e:
+                    if e.errno in (errno.EINTR, errno.EAGAIN):
                         continue
                     raise
 
@@ -1273,8 +1273,8 @@ def start_response(status, response_headers, exc_info=None):
                 finally:
                     if hasattr(result, 'close'):
                         result.close()
-            except socket.error, e:
-                if e[0] != errno.EPIPE:
+            except socket.error as e:
+                if e.errno != errno.EPIPE:
                     raise # Don't let EPIPE propagate beyond server
         finally:
             if not self.multithreaded:
@@ -1329,4 +1329,3 @@ def test_app(environ, start_response):
               '</body></html>\n'
 
     WSGIServer(test_app).run()
-


Test patch: