Sync w/external release 0.1.2. Please see PEP 360 before making changes to external packages.

This commit is contained in:
Phillip J. Eby 2006-06-12 04:04:32 +00:00
parent 6e73aaab47
commit 403019b115
7 changed files with 146 additions and 59 deletions

View file

@ -80,7 +80,7 @@ def run_amock(app=hello_app, data="GET / HTTP/1.0\n\n"):
def compare_generic_iter(test, make_it, match): def compare_generic_iter(make_it,match):
"""Utility to compare a generic 2.1/2.2+ iterator with an iterable """Utility to compare a generic 2.1/2.2+ iterator with an iterable
If running under Python 2.2+, this tests the iterator using iter()/next(), If running under Python 2.2+, this tests the iterator using iter()/next(),
@ -90,7 +90,7 @@ def compare_generic_iter(test, make_it, match):
it = make_it() it = make_it()
n = 0 n = 0
for item in match: for item in match:
test.assertEqual(it[n], item) if not it[n]==item: raise AssertionError
n+=1 n+=1
try: try:
it[n] it[n]
@ -106,10 +106,15 @@ def compare_generic_iter(test, make_it, match):
else: else:
# Only test iter mode under 2.2+ # Only test iter mode under 2.2+
it = make_it() it = make_it()
test.assert_(iter(it) is it) if not iter(it) is it: raise AssertionError
for item in match: for item in match:
test.assertEqual(it.next(), item) if not it.next()==item: raise AssertionError
test.assertRaises(StopIteration, it.next) try:
it.next()
except StopIteration:
pass
else:
raise AssertionError("Too many items from .next()",it)
@ -203,7 +208,7 @@ class UtilityTests(TestCase):
def make_it(text=text,size=size): def make_it(text=text,size=size):
return util.FileWrapper(StringIO(text),size) return util.FileWrapper(StringIO(text),size)
compare_generic_iter(self, make_it, match) compare_generic_iter(make_it,match)
it = make_it() it = make_it()
self.failIf(it.filelike.closed) self.failIf(it.filelike.closed)

View file

@ -1,6 +1,6 @@
Metadata-Version: 1.0 Metadata-Version: 1.0
Name: wsgiref Name: wsgiref
Version: 0.1 Version: 0.1.2
Summary: WSGI (PEP 333) Reference Library Summary: WSGI (PEP 333) Reference Library
Author: Phillip J. Eby Author: Phillip J. Eby
Author-email: web-sig@python.org Author-email: web-sig@python.org

View file

@ -473,3 +473,20 @@ class CGIHandler(BaseCGIHandler):
self, sys.stdin, sys.stdout, sys.stderr, dict(os.environ.items()), self, sys.stdin, sys.stdout, sys.stderr, dict(os.environ.items()),
multithread=False, multiprocess=True multithread=False, multiprocess=True
) )
#

View file

@ -187,3 +187,19 @@ class Headers:
else: else:
parts.append(_formatparam(k.replace('_', '-'), v)) parts.append(_formatparam(k.replace('_', '-'), v))
self._headers.append((_name, "; ".join(parts))) self._headers.append((_name, "; ".join(parts)))
#

View file

@ -190,3 +190,16 @@ if __name__ == '__main__':
import webbrowser import webbrowser
webbrowser.open('http://localhost:8000/xyz?abc') webbrowser.open('http://localhost:8000/xyz?abc')
httpd.handle_request() # serve one request, then exit httpd.handle_request() # serve one request, then exit
#

View file

@ -171,3 +171,35 @@ _hoppish = {
def is_hop_by_hop(header_name): def is_hop_by_hop(header_name):
"""Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header""" """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header"""
return _hoppish(header_name.lower()) return _hoppish(header_name.lower())
#

View file

@ -124,6 +124,10 @@ class WSGIWarning(Warning):
Raised in response to WSGI-spec-related warnings Raised in response to WSGI-spec-related warnings
""" """
def assert_(cond, *args):
if not cond:
raise AssertionError(*args)
def validator(application): def validator(application):
""" """
@ -137,8 +141,8 @@ def validator(application):
""" """
def lint_app(*args, **kw): def lint_app(*args, **kw):
assert len(args) == 2, "Two arguments required" assert_(len(args) == 2, "Two arguments required")
assert not kw, "No keyword arguments allowed" assert_(not kw, "No keyword arguments allowed")
environ, start_response = args environ, start_response = args
check_environ(environ) check_environ(environ)
@ -148,9 +152,9 @@ def validator(application):
start_response_started = [] start_response_started = []
def start_response_wrapper(*args, **kw): def start_response_wrapper(*args, **kw):
assert len(args) == 2 or len(args) == 3, ( assert_(len(args) == 2 or len(args) == 3, (
"Invalid number of arguments: %s" % args) "Invalid number of arguments: %s" % (args,)))
assert not kw, "No keyword arguments allowed" assert_(not kw, "No keyword arguments allowed")
status = args[0] status = args[0]
headers = args[1] headers = args[1]
if len(args) == 3: if len(args) == 3:
@ -170,7 +174,7 @@ def validator(application):
environ['wsgi.errors'] = ErrorWrapper(environ['wsgi.errors']) environ['wsgi.errors'] = ErrorWrapper(environ['wsgi.errors'])
iterator = application(environ, start_response_wrapper) iterator = application(environ, start_response_wrapper)
assert iterator is not None and iterator != False, ( assert_(iterator is not None and iterator != False,
"The application must return an iterator, if only an empty list") "The application must return an iterator, if only an empty list")
check_iterator(iterator) check_iterator(iterator)
@ -185,22 +189,22 @@ class InputWrapper:
self.input = wsgi_input self.input = wsgi_input
def read(self, *args): def read(self, *args):
assert len(args) <= 1 assert_(len(args) <= 1)
v = self.input.read(*args) v = self.input.read(*args)
assert type(v) is type("") assert_(type(v) is type(""))
return v return v
def readline(self): def readline(self):
v = self.input.readline() v = self.input.readline()
assert type(v) is type("") assert_(type(v) is type(""))
return v return v
def readlines(self, *args): def readlines(self, *args):
assert len(args) <= 1 assert_(len(args) <= 1)
lines = self.input.readlines(*args) lines = self.input.readlines(*args)
assert type(lines) is type([]) assert_(type(lines) is type([]))
for line in lines: for line in lines:
assert type(line) is type("") assert_(type(line) is type(""))
return lines return lines
def __iter__(self): def __iter__(self):
@ -211,7 +215,7 @@ class InputWrapper:
yield line yield line
def close(self): def close(self):
assert 0, "input.close() must not be called" assert_(0, "input.close() must not be called")
class ErrorWrapper: class ErrorWrapper:
@ -219,7 +223,7 @@ class ErrorWrapper:
self.errors = wsgi_errors self.errors = wsgi_errors
def write(self, s): def write(self, s):
assert type(s) is type("") assert_(type(s) is type(""))
self.errors.write(s) self.errors.write(s)
def flush(self): def flush(self):
@ -230,7 +234,7 @@ class ErrorWrapper:
self.write(line) self.write(line)
def close(self): def close(self):
assert 0, "errors.close() must not be called" assert_(0, "errors.close() must not be called")
class WriteWrapper: class WriteWrapper:
@ -238,7 +242,7 @@ class WriteWrapper:
self.writer = wsgi_writer self.writer = wsgi_writer
def __call__(self, s): def __call__(self, s):
assert type(s) is type("") assert_(type(s) is type(""))
self.writer(s) self.writer(s)
class PartialIteratorWrapper: class PartialIteratorWrapper:
@ -262,11 +266,11 @@ class IteratorWrapper:
return self return self
def next(self): def next(self):
assert not self.closed, ( assert_(not self.closed,
"Iterator read after closed") "Iterator read after closed")
v = self.iterator.next() v = self.iterator.next()
if self.check_start_response is not None: if self.check_start_response is not None:
assert self.check_start_response, ( assert_(self.check_start_response,
"The application returns and we started iterating over its body, but start_response has not yet been called") "The application returns and we started iterating over its body, but start_response has not yet been called")
self.check_start_response = None self.check_start_response = None
return v return v
@ -280,11 +284,11 @@ class IteratorWrapper:
if not self.closed: if not self.closed:
sys.stderr.write( sys.stderr.write(
"Iterator garbage collected without being closed") "Iterator garbage collected without being closed")
assert self.closed, ( assert_(self.closed,
"Iterator garbage collected without being closed") "Iterator garbage collected without being closed")
def check_environ(environ): def check_environ(environ):
assert type(environ) is DictType, ( assert_(type(environ) is DictType,
"Environment is not of the right type: %r (environment: %r)" "Environment is not of the right type: %r (environment: %r)"
% (type(environ), environ)) % (type(environ), environ))
@ -292,11 +296,11 @@ def check_environ(environ):
'wsgi.version', 'wsgi.input', 'wsgi.errors', 'wsgi.version', 'wsgi.input', 'wsgi.errors',
'wsgi.multithread', 'wsgi.multiprocess', 'wsgi.multithread', 'wsgi.multiprocess',
'wsgi.run_once']: 'wsgi.run_once']:
assert key in environ, ( assert_(key in environ,
"Environment missing required key: %r" % key) "Environment missing required key: %r" % (key,))
for key in ['HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH']: for key in ['HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH']:
assert key not in environ, ( assert_(key not in environ,
"Environment should not have the key: %s " "Environment should not have the key: %s "
"(use %s instead)" % (key, key[5:])) "(use %s instead)" % (key, key[5:]))
@ -311,13 +315,13 @@ def check_environ(environ):
if '.' in key: if '.' in key:
# Extension, we don't care about its type # Extension, we don't care about its type
continue continue
assert type(environ[key]) is StringType, ( assert_(type(environ[key]) is StringType,
"Environmental variable %s is not a string: %r (value: %r)" "Environmental variable %s is not a string: %r (value: %r)"
% (key, type(environ[key]), environ[key])) % (key, type(environ[key]), environ[key]))
assert type(environ['wsgi.version']) is TupleType, ( assert_(type(environ['wsgi.version']) is TupleType,
"wsgi.version should be a tuple (%r)" % environ['wsgi.version']) "wsgi.version should be a tuple (%r)" % (environ['wsgi.version'],))
assert environ['wsgi.url_scheme'] in ('http', 'https'), ( assert_(environ['wsgi.url_scheme'] in ('http', 'https'),
"wsgi.url_scheme unknown: %r" % environ['wsgi.url_scheme']) "wsgi.url_scheme unknown: %r" % environ['wsgi.url_scheme'])
check_input(environ['wsgi.input']) check_input(environ['wsgi.input'])
@ -330,45 +334,45 @@ def check_environ(environ):
"Unknown REQUEST_METHOD: %r" % environ['REQUEST_METHOD'], "Unknown REQUEST_METHOD: %r" % environ['REQUEST_METHOD'],
WSGIWarning) WSGIWarning)
assert (not environ.get('SCRIPT_NAME') assert_(not environ.get('SCRIPT_NAME')
or environ['SCRIPT_NAME'].startswith('/')), ( or environ['SCRIPT_NAME'].startswith('/'),
"SCRIPT_NAME doesn't start with /: %r" % environ['SCRIPT_NAME']) "SCRIPT_NAME doesn't start with /: %r" % environ['SCRIPT_NAME'])
assert (not environ.get('PATH_INFO') assert_(not environ.get('PATH_INFO')
or environ['PATH_INFO'].startswith('/')), ( or environ['PATH_INFO'].startswith('/'),
"PATH_INFO doesn't start with /: %r" % environ['PATH_INFO']) "PATH_INFO doesn't start with /: %r" % environ['PATH_INFO'])
if environ.get('CONTENT_LENGTH'): if environ.get('CONTENT_LENGTH'):
assert int(environ['CONTENT_LENGTH']) >= 0, ( assert_(int(environ['CONTENT_LENGTH']) >= 0,
"Invalid CONTENT_LENGTH: %r" % environ['CONTENT_LENGTH']) "Invalid CONTENT_LENGTH: %r" % environ['CONTENT_LENGTH'])
if not environ.get('SCRIPT_NAME'): if not environ.get('SCRIPT_NAME'):
assert environ.has_key('PATH_INFO'), ( assert_(environ.has_key('PATH_INFO'),
"One of SCRIPT_NAME or PATH_INFO are required (PATH_INFO " "One of SCRIPT_NAME or PATH_INFO are required (PATH_INFO "
"should at least be '/' if SCRIPT_NAME is empty)") "should at least be '/' if SCRIPT_NAME is empty)")
assert environ.get('SCRIPT_NAME') != '/', ( assert_(environ.get('SCRIPT_NAME') != '/',
"SCRIPT_NAME cannot be '/'; it should instead be '', and " "SCRIPT_NAME cannot be '/'; it should instead be '', and "
"PATH_INFO should be '/'") "PATH_INFO should be '/'")
def check_input(wsgi_input): def check_input(wsgi_input):
for attr in ['read', 'readline', 'readlines', '__iter__']: for attr in ['read', 'readline', 'readlines', '__iter__']:
assert hasattr(wsgi_input, attr), ( assert_(hasattr(wsgi_input, attr),
"wsgi.input (%r) doesn't have the attribute %s" "wsgi.input (%r) doesn't have the attribute %s"
% (wsgi_input, attr)) % (wsgi_input, attr))
def check_errors(wsgi_errors): def check_errors(wsgi_errors):
for attr in ['flush', 'write', 'writelines']: for attr in ['flush', 'write', 'writelines']:
assert hasattr(wsgi_errors, attr), ( assert_(hasattr(wsgi_errors, attr),
"wsgi.errors (%r) doesn't have the attribute %s" "wsgi.errors (%r) doesn't have the attribute %s"
% (wsgi_errors, attr)) % (wsgi_errors, attr))
def check_status(status): def check_status(status):
assert type(status) is StringType, ( assert_(type(status) is StringType,
"Status must be a string (not %r)" % status) "Status must be a string (not %r)" % status)
# Implicitly check that we can turn it into an integer: # Implicitly check that we can turn it into an integer:
status_code = status.split(None, 1)[0] status_code = status.split(None, 1)[0]
assert len(status_code) == 3, ( assert_(len(status_code) == 3,
"Status codes must be three characters: %r" % status_code) "Status codes must be three characters: %r" % status_code)
status_int = int(status_code) status_int = int(status_code)
assert status_int >= 100, "Status code is invalid: %r" % status_int assert_(status_int >= 100, "Status code is invalid: %r" % status_int)
if len(status) < 4 or status[3] != ' ': if len(status) < 4 or status[3] != ' ':
warnings.warn( warnings.warn(
"The status string (%r) should be a three-digit integer " "The status string (%r) should be a three-digit integer "
@ -376,28 +380,28 @@ def check_status(status):
% status, WSGIWarning) % status, WSGIWarning)
def check_headers(headers): def check_headers(headers):
assert type(headers) is ListType, ( assert_(type(headers) is ListType,
"Headers (%r) must be of type list: %r" "Headers (%r) must be of type list: %r"
% (headers, type(headers))) % (headers, type(headers)))
header_names = {} header_names = {}
for item in headers: for item in headers:
assert type(item) is TupleType, ( assert_(type(item) is TupleType,
"Individual headers (%r) must be of type tuple: %r" "Individual headers (%r) must be of type tuple: %r"
% (item, type(item))) % (item, type(item)))
assert len(item) == 2 assert_(len(item) == 2)
name, value = item name, value = item
assert name.lower() != 'status', ( assert_(name.lower() != 'status',
"The Status header cannot be used; it conflicts with CGI " "The Status header cannot be used; it conflicts with CGI "
"script, and HTTP status is not given through headers " "script, and HTTP status is not given through headers "
"(value: %r)." % value) "(value: %r)." % value)
header_names[name.lower()] = None header_names[name.lower()] = None
assert '\n' not in name and ':' not in name, ( assert_('\n' not in name and ':' not in name,
"Header names may not contain ':' or '\\n': %r" % name) "Header names may not contain ':' or '\\n': %r" % name)
assert header_re.search(name), "Bad header name: %r" % name assert_(header_re.search(name), "Bad header name: %r" % name)
assert not name.endswith('-') and not name.endswith('_'), ( assert_(not name.endswith('-') and not name.endswith('_'),
"Names may not end in '-' or '_': %r" % name) "Names may not end in '-' or '_': %r" % name)
assert not bad_header_value_re.search(value), ( if bad_header_value_re.search(value):
"Bad header value: %r (bad char: %r)" assert_(0, "Bad header value: %r (bad char: %r)"
% (value, bad_header_value_re.search(value).group(0))) % (value, bad_header_value_re.search(value).group(0)))
def check_content_type(status, headers): def check_content_type(status, headers):
@ -409,13 +413,13 @@ def check_content_type(status, headers):
if name.lower() == 'content-type': if name.lower() == 'content-type':
if code not in NO_MESSAGE_BODY: if code not in NO_MESSAGE_BODY:
return return
assert 0, (("Content-Type header found in a %s response, " assert_(0, ("Content-Type header found in a %s response, "
"which must not return content.") % code) "which must not return content.") % code)
if code not in NO_MESSAGE_BODY: if code not in NO_MESSAGE_BODY:
assert 0, "No Content-Type header found in headers (%s)" % headers assert_(0, "No Content-Type header found in headers (%s)" % headers)
def check_exc_info(exc_info): def check_exc_info(exc_info):
assert exc_info is None or type(exc_info) is type(()), ( assert_(exc_info is None or type(exc_info) is type(()),
"exc_info (%r) is not a tuple: %r" % (exc_info, type(exc_info))) "exc_info (%r) is not a tuple: %r" % (exc_info, type(exc_info)))
# More exc_info checks? # More exc_info checks?
@ -423,6 +427,6 @@ def check_iterator(iterator):
# Technically a string is legal, which is why it's a really bad # Technically a string is legal, which is why it's a really bad
# idea, because it may cause the response to be returned # idea, because it may cause the response to be returned
# character-by-character # character-by-character
assert not isinstance(iterator, str), ( assert_(not isinstance(iterator, str),
"You should not return a string as your application iterator, " "You should not return a string as your application iterator, "
"instead return a single-item list containing that string.") "instead return a single-item list containing that string.")