From 5b5b4992ffdc3c3dc3b28c004f211288fb6affcf Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sat, 21 Apr 2007 20:48:43 +0200 Subject: added some assertions --- wsgitools/adapters.py | 8 ++++++++ wsgitools/applications.py | 4 ++++ wsgitools/filters.py | 9 +++++++++ wsgitools/middlewares.py | 19 +++++++++++++++++++ wsgitools/scgi/asynchronous.py | 2 ++ wsgitools/scgi/forkpool.py | 4 ++++ 6 files changed, 46 insertions(+) diff --git a/wsgitools/adapters.py b/wsgitools/adapters.py index 9f2ab1c..f575295 100644 --- a/wsgitools/adapters.py +++ b/wsgitools/adapters.py @@ -12,7 +12,11 @@ class WSGI2to1Adapter: self.app = app def __call__(self, environ, start_response): """WSGI 1.0 interface""" + assert isinstance(environ, dict) status, headers, iterable = self.app(environ) + assert isinstance(status, str) + assert isinstance(headers, list) + assert hasattr(iterable, "__iter__") start_response(status, headers) return iterable @@ -25,14 +29,18 @@ class WSGI1to2Adapter: self.app = app def __call__(self, environ): """some interface that might somewhen be known as WSGI 2.0""" + assert isinstance(environ, dict) results = [None, None, []] def start_response(status, headers, exc_info=None): + assert isinstance(status, str) + assert isinstance(headers, list) results[0] = status results[1] = headers def write_callable(data): results[2].append(data) return write_callable iterable = self.app(environ, start_response) + assert hasattr(iterable, "__iter__") if isinstance(iterable, list): # retaining .close attribute this way iterable[:0] = results[2] diff --git a/wsgitools/applications.py b/wsgitools/applications.py index 4ddf7ea..5ef2e8a 100644 --- a/wsgitools/applications.py +++ b/wsgitools/applications.py @@ -8,6 +8,9 @@ class StaticContent: content contains the data to be delivered to the client. It is either a string or some kind of iterable yielding strings. """ + assert isinstance(status, str) + assert isinstance(headers, list) + assert hasattr(content, "__iter__") self.status = status self.headers = headers length = -1 @@ -23,6 +26,7 @@ class StaticContent: headers.append(("Content-length", str(length))) def __call__(self, environ, start_response): """wsgi interface""" + assert isinstance(environ, dict) start_response(self.status, self.headers) if environ["REQUEST_METHOD"].upper() == "HEAD": return [] diff --git a/wsgitools/filters.py b/wsgitools/filters.py index 0cca3b4..7f83bd9 100644 --- a/wsgitools/filters.py +++ b/wsgitools/filters.py @@ -119,10 +119,13 @@ class WSGIFilterMiddleware: self.filterclass = filterclass def __call__(self, environ, start_response): """wsgi interface""" + assert isinstance(environ, dict) filter = self.filterclass() environ = filter.filter_environ(environ) def modified_start_response(status, headers, exc_info=None): + assert isinstance(status, str) + assert isinstance(headers, list) exc_info = filter.filter_exc_info(exc_info) status = filter.filter_status(status) headers = (filter.filter_header(h, v) for h, v in headers) @@ -134,6 +137,7 @@ class WSGIFilterMiddleware: return modified_write ret = self.app(environ, modified_start_response) + assert hasattr(ret, "__iter__") def modified_close(): filter.handle_close() @@ -162,11 +166,13 @@ class RequestLogWSGIFilter(BaseWSGIFilter): log has to be a file-like object.""" return lambda:cls(log) def __init__(self, log=sys.stdout): + assert hasattr(log, "write") self.log = log self.time = time.strftime("%d/%b/%Y:%T %z") self.length = 0 def filter_environ(self, environ): """BaseWSGIFilter interface""" + assert isinstance(environ, dict) self.remote = environ.get("REMOTE_ADDR", "?") self.user = environ.get("REMOTE_USER", "-") self.reqmethod = environ["REQUEST_METHOD"] @@ -177,6 +183,7 @@ class RequestLogWSGIFilter(BaseWSGIFilter): return environ def filter_status(self, status): """BaseWSGIFilter interface""" + assert isinstance(status, str) self.status = status.split()[0] return status def filter_data(self, data): @@ -250,6 +257,7 @@ class GzipWSGIFilter(BaseWSGIFilter): self.gzip = None def filter_environ(self, environ): """BaseWSGIFilter interface""" + assert isinstance(environ, dict) if "HTTP_ACCEPT_ENCODING" in environ: acceptenc = environ["HTTP_ACCEPT_ENCODING"].split(',') acceptenc = map(str.strip, acceptenc) @@ -260,6 +268,7 @@ class GzipWSGIFilter(BaseWSGIFilter): return environ def filter_headers(self, headers): """BaseWSGIFilter interface""" + assert isinstance(headers, list) if self.compress: headers.append(("Content-encoding", "gzip")) return headers diff --git a/wsgitools/middlewares.py b/wsgitools/middlewares.py index 597c584..4020f90 100644 --- a/wsgitools/middlewares.py +++ b/wsgitools/middlewares.py @@ -17,6 +17,7 @@ class SubdirMiddleware: self.mapping = mapping def __call__(self, environ, start_response): """wsgi interface""" + assert isinstance(environ, dict) app = None script = environ["PATH_INFO"] path_info = "" @@ -45,8 +46,11 @@ class NoWriteCallableMiddleware: self.app = app def __call__(self, environ, start_response): """wsgi interface""" + assert isinstance(environ, dict) todo = [] def modified_start_response(status, headers, exc_info=None): + assert isinstance(status, str) + assert isinstance(headers, list) if exc_info is not None: todo.append(None) return start_response(status, headers, exc_info) @@ -56,6 +60,7 @@ class NoWriteCallableMiddleware: return sio.write ret = self.app(environ, modified_start_response) + assert hasattr(ret, "__iter__") if todo and todo[0] is None: return ret @@ -101,8 +106,11 @@ class ContentLengthMiddleware: self.maxstore = maxstore def __call__(self, environ, start_response): """wsgi interface""" + assert isinstance(environ, dict) todo = [] def modified_start_response(status, headers, exc_info=None): + assert isinstance(status, str) + assert isinstance(headers, list) if (exc_info is not None or [v for h, v in headers if h.lower() == "content-length"]): todo[:] = (None,) @@ -114,6 +122,7 @@ class ContentLengthMiddleware: return raise_not_imp ret = self.app(environ, modified_start_response) + assert hasattr(ret, "__iter__") if todo and todo[0] is None: # nothing to do #print "content-length: nothing" @@ -185,6 +194,7 @@ class CachingMiddleware: self.cache = {} def __call__(self, environ, start_response): """wsgi interface""" + assert isinstance(environ, dict) if not self.storable(environ): return self.app(environ, start_response) path = environ.get("SCRIPT_NAME", "/") @@ -198,6 +208,8 @@ class CachingMiddleware: del self.cache[path] cache_object = [time.time(), "", [], []] def modified_start_respesponse(status, headers, exc_info): + assert isinstance(status, str) + assert isinstance(headers, list) if exc_info is not None: return self.app(status, headers, exc_info) cache_object[1] = status @@ -207,7 +219,10 @@ class CachingMiddleware: cache_object[3].append(data) write(data) return modified_write + ret = self.app(environ, modified_start_respesponse) + assert hasattr(ret, "__iter__") + if isinstance(ret, list): cache_object[3].extend(ret) self.cache[path] = cache_object @@ -244,6 +259,7 @@ class BasicAuthMiddleware: def __call__(self, environ, start_response): """wsgi interface""" + assert isinstance(environ, dict) auth = environ.get("HTTP_AUTHORIZATION") if not auth or ' ' not in auth: return self.authorization_required(environ, start_response) @@ -283,7 +299,10 @@ class TracebackMiddleware: def __call__(self, environ, start_response): """wsgi interface""" try: + assert isinstance(environ, dict) ret = self.app(environ, start_response) + assert hasattr(ret, "__iter__") + if isinstance(ret, list): return ret # Take the first element of the iterator and possibly catch an diff --git a/wsgitools/scgi/asynchronous.py b/wsgitools/scgi/asynchronous.py index 9510b4d..ff226de 100644 --- a/wsgitools/scgi/asynchronous.py +++ b/wsgitools/scgi/asynchronous.py @@ -130,6 +130,8 @@ class SCGIConnection(asyncore.dispatcher): self.inbuff = "" def start_response(self, status, headers, exc_info=None): + assert isinstance(status, str) + assert isinstance(headers, list) if exc_info: if self.outheaders == True: try: diff --git a/wsgitools/scgi/forkpool.py b/wsgitools/scgi/forkpool.py index 1ff2bee..6476458 100644 --- a/wsgitools/scgi/forkpool.py +++ b/wsgitools/scgi/forkpool.py @@ -85,6 +85,7 @@ class SCGIServer: maxworkers is the maximum number of workers that can be spawned on demand maxrequests is the number of requests a worker processes before dying """ + assert hasattr(error, "write") self.wsgiapp = wsgiapp self.port = port self.interface = interface @@ -217,7 +218,10 @@ class SCGIServer: "wsgi.multithread": False, "wsgi.multiprocess": True, "wsgi.run_once": False}) + result = self.wsgiapp(environ, start_response) + assert hasattr(result, "__iter__") + for data in result: con.send(data) if hasattr(result, "close"): -- cgit v1.2.3