changeset 5550:49f8dd576950

flup fcgi_base: avoid socket.fromfd AttributeError on win32 if cgi is forced, give helpful exception for fcgi
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 20 Feb 2010 13:17:48 +0100
parents d18c06596cd5
children 661057dc4d09
files MoinMoin/support/flup/server/fcgi_base.py
diffstat 1 files changed, 27 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/support/flup/server/fcgi_base.py	Sat Feb 20 03:01:45 2010 +0100
+++ b/MoinMoin/support/flup/server/fcgi_base.py	Sat Feb 20 13:17:48 2010 +0100
@@ -1000,27 +1000,39 @@
                 }
 
     def _setupSocket(self):
-        if self._bindAddress is None: # Run as a normal FastCGI?
-            isFCGI = True
-
-            sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,
-                                 socket.SOCK_STREAM)
-            try:
-                sock.getpeername()
-            except socket.error, e:
-                if e[0] == errno.ENOTSOCK:
-                    # Not a socket, assume CGI context.
-                    isFCGI = False
-                elif e[0] != errno.ENOTCONN:
-                    raise
+        if self._bindAddress is None:
+            # Run as a normal FastCGI?
 
             # FastCGI/CGI discrimination is broken on Mac OS X.
             # Set the environment variable FCGI_FORCE_CGI to "Y" or "y"
             # if you want to run your app as a simple CGI. (You can do
             # this with Apache's mod_env [not loaded by default in OS X
             # client, ha ha] and the SetEnv directive.)
-            if not isFCGI or self.forceCGI or \
-               os.environ.get('FCGI_FORCE_CGI', 'N').upper().startswith('Y'):
+            forceCGI = self.forceCGI or \
+               os.environ.get('FCGI_FORCE_CGI', 'N').upper().startswith('Y')
+
+            if forceCGI:
+                isFCGI = False
+            else:
+                if not hasattr(socket, 'fromfd'):
+                    # can happen on win32, no socket.fromfd there!
+                    raise ValueError(
+                        'Dynamic FCGI server not available on this platform. '
+                        'You must use a static or external one by providing a '
+                        'legal bindAddress.')
+                sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,
+                                     socket.SOCK_STREAM)
+                isFCGI = True
+                try:
+                    sock.getpeername()
+                except socket.error, e:
+                    if e[0] == errno.ENOTSOCK:
+                        # Not a socket, assume CGI context.
+                        isFCGI = False
+                    elif e[0] != errno.ENOTCONN:
+                        raise
+
+            if not isFCGI:
                 req = self.cgirequest_class(self)
                 req.run()
                 sys.exit(0)