diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst --- a/Doc/library/wsgiref.rst +++ b/Doc/library/wsgiref.rst @@ -179,20 +179,21 @@ also provides these miscellaneous utilit .. module:: wsgiref.headers :synopsis: WSGI response header tools. This module provides a single class, :class:`Headers`, for convenient manipulation of WSGI response headers using a mapping-like interface. -.. class:: Headers(headers) +.. class:: Headers([headers]) Create a mapping-like object wrapping *headers*, which must be a list of header - name/value tuples as described in :pep:`3333`. + name/value tuples as described in :pep:`3333`. The default value of *headers* is + an empty list. :class:`Headers` objects support typical mapping operations including :meth:`__getitem__`, :meth:`get`, :meth:`__setitem__`, :meth:`setdefault`, :meth:`__delitem__` and :meth:`__contains__`. For each of these methods, the key is the header name (treated case-insensitively), and the value is the first value associated with that header name. Setting a header deletes any existing values for that header, then adds a new value at the end of the wrapped header list. Headers' existing order is generally maintained, with @@ -246,16 +247,20 @@ manipulation of WSGI response headers us h.add_header('content-disposition', 'attachment', filename='bud.gif') The above will add a header that looks like this:: Content-Disposition: attachment; filename="bud.gif" + .. versionchanged:: 3.5 + The *headers* parameter is optional. + + :mod:`wsgiref.simple_server` -- a simple WSGI HTTP server --------------------------------------------------------- .. module:: wsgiref.simple_server :synopsis: A simple WSGI HTTP server. This module implements a simple HTTP server (based on :mod:`http.server`) diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst --- a/Doc/whatsnew/3.5.rst +++ b/Doc/whatsnew/3.5.rst @@ -225,16 +225,22 @@ socket ------ * New :meth:`socket.socket.sendfile` method allows to send a file over a socket by using high-performance :func:`os.sendfile` function on UNIX resulting in uploads being from 2x to 3x faster than when using plain :meth:`socket.socket.send`. (contributed by Giampaolo Rodola' in :issue:`17552`) +wsgiref +------- + +* The *headers* parameter of :class:`wsgiref.headers.Headers` is optional now. + (Contributed by Pablo Torres Navarrete and SilentGhost in :issue:`5800`.) + xmlrpc ------ * :class:`xmlrpc.client.ServerProxy` is now a :term:`context manager` (contributed by Claudiu Popa in :issue:`20627`). Optimizations diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py --- a/Lib/test/test_wsgiref.py +++ b/Lib/test/test_wsgiref.py @@ -333,24 +333,25 @@ class UtilityTests(TestCase): ).split(): for alt in hop, hop.title(), hop.upper(), hop.lower(): self.assertFalse(util.is_hop_by_hop(alt)) class HeaderTests(TestCase): def testMappingInterface(self): test = [('x','y')] + self.assertEqual(len(Headers()), 0) self.assertEqual(len(Headers([])),0) self.assertEqual(len(Headers(test[:])),1) self.assertEqual(Headers(test[:]).keys(), ['x']) self.assertEqual(Headers(test[:]).values(), ['y']) self.assertEqual(Headers(test[:]).items(), test) self.assertIsNot(Headers(test).items(), test) # must be copy! - h=Headers([]) + h = Headers() del h['foo'] # should not raise an error h['Foo'] = 'bar' for m in h.__contains__, h.get, h.get_all, h.__getitem__: self.assertTrue(m('foo')) self.assertTrue(m('Foo')) self.assertTrue(m('FOO')) self.assertFalse(m('bar')) @@ -365,19 +366,18 @@ class HeaderTests(TestCase): self.assertEqual(h.setdefault("foo","whee"), "baz") self.assertEqual(h.setdefault("zoo","whee"), "whee") self.assertEqual(h["foo"],"baz") self.assertEqual(h["zoo"],"whee") def testRequireList(self): self.assertRaises(TypeError, Headers, "foo") - def testExtras(self): - h = Headers([]) + h = Headers() self.assertEqual(str(h),'\r\n') h.add_header('foo','bar',baz="spam") self.assertEqual(h['foo'], 'bar; baz="spam"') self.assertEqual(str(h),'foo: bar; baz="spam"\r\n\r\n') h.add_header('Foo','bar',cheese=None) self.assertEqual(h.get_all('foo'), diff --git a/Lib/wsgiref/headers.py b/Lib/wsgiref/headers.py --- a/Lib/wsgiref/headers.py +++ b/Lib/wsgiref/headers.py @@ -21,20 +21,20 @@ def _formatparam(param, value=None, quot return '%s="%s"' % (param, value) else: return '%s=%s' % (param, value) else: return param class Headers: - """Manage a collection of HTTP response headers""" - def __init__(self,headers): + def __init__(self, headers=None): + headers = headers or [] if type(headers) is not list: raise TypeError("Headers must be a list of name/value tuples") self._headers = headers if __debug__: for k, v in headers: self._convert_string_type(k) self._convert_string_type(v)