Update wsgiref for PEP 3333, and fix errors introduced into the test suite by converting type() checks to isinstance().

(When WSGI specifies a built-in type, it does NOT mean "this type or a subclass" -- it means 'type(x) is SpecifiedType'.)
This commit is contained in:
Phillip J. Eby 2010-11-02 22:28:59 +00:00
parent 5a43f72d1b
commit e159422ce9
4 changed files with 67 additions and 93 deletions

View file

@ -128,11 +128,10 @@ def assert_(cond, *args):
raise AssertionError(*args)
def check_string_type(value, title):
if isinstance(value, str):
if type (value) is str:
return value
assert isinstance(value, bytes), \
"{0} must be a string or bytes object (not {1})".format(title, value)
return str(value, "iso-8859-1")
raise AssertionError(
"{0} must be of type str (got {1})".format(title, repr(value)))
def validator(application):
@ -197,20 +196,21 @@ class InputWrapper:
def read(self, *args):
assert_(len(args) == 1)
v = self.input.read(*args)
assert_(isinstance(v, bytes))
assert_(type(v) is bytes)
return v
def readline(self):
v = self.input.readline()
assert_(isinstance(v, bytes))
def readline(self, *args):
assert_(len(args) <= 1)
v = self.input.readline(*args)
assert_(type(v) is bytes)
return v
def readlines(self, *args):
assert_(len(args) <= 1)
lines = self.input.readlines(*args)
assert_(isinstance(lines, list))
assert_(type(lines) is list)
for line in lines:
assert_(isinstance(line, bytes))
assert_(type(line) is bytes)
return lines
def __iter__(self):
@ -229,7 +229,7 @@ class ErrorWrapper:
self.errors = wsgi_errors
def write(self, s):
assert_(isinstance(s, str))
assert_(type(s) is str)
self.errors.write(s)
def flush(self):
@ -248,7 +248,7 @@ class WriteWrapper:
self.writer = wsgi_writer
def __call__(self, s):
assert_(isinstance(s, (str, bytes)))
assert_(type(s) is bytes)
self.writer(s)
class PartialIteratorWrapper:
@ -275,6 +275,8 @@ class IteratorWrapper:
assert_(not self.closed,
"Iterator read after closed")
v = next(self.iterator)
if type(v) is not bytes:
assert_(False, "Iterator yielded non-bytestring (%r)" % (v,))
if self.check_start_response is not None:
assert_(self.check_start_response,
"The application returns and we started iterating over its body, but start_response has not yet been called")
@ -294,7 +296,7 @@ class IteratorWrapper:
"Iterator garbage collected without being closed")
def check_environ(environ):
assert_(isinstance(environ, dict),
assert_(type(environ) is dict,
"Environment is not of the right type: %r (environment: %r)"
% (type(environ), environ))
@ -321,11 +323,11 @@ def check_environ(environ):
if '.' in key:
# Extension, we don't care about its type
continue
assert_(isinstance(environ[key], str),
assert_(type(environ[key]) is str,
"Environmental variable %s is not a string: %r (value: %r)"
% (key, type(environ[key]), environ[key]))
assert_(isinstance(environ['wsgi.version'], tuple),
assert_(type(environ['wsgi.version']) is tuple,
"wsgi.version should be a tuple (%r)" % (environ['wsgi.version'],))
assert_(environ['wsgi.url_scheme'] in ('http', 'https'),
"wsgi.url_scheme unknown: %r" % environ['wsgi.url_scheme'])
@ -385,12 +387,12 @@ def check_status(status):
% status, WSGIWarning)
def check_headers(headers):
assert_(isinstance(headers, list),
assert_(type(headers) is list,
"Headers (%r) must be of type list: %r"
% (headers, type(headers)))
header_names = {}
for item in headers:
assert_(isinstance(item, tuple),
assert_(type(item) is tuple,
"Individual headers (%r) must be of type tuple: %r"
% (item, type(item)))
assert_(len(item) == 2)
@ -428,14 +430,14 @@ def check_content_type(status, headers):
assert_(0, "No Content-Type header found in headers (%s)" % headers)
def check_exc_info(exc_info):
assert_(exc_info is None or isinstance(exc_info, tuple),
assert_(exc_info is None or type(exc_info) is tuple,
"exc_info (%r) is not a tuple: %r" % (exc_info, type(exc_info)))
# More exc_info checks?
def check_iterator(iterator):
# Technically a string is legal, which is why it's a really bad
# Technically a bytestring is legal, which is why it's a really bad
# idea, because it may cause the response to be returned
# character-by-character
assert_(not isinstance(iterator, (str, bytes)),
"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 a bytestring.")