diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst index 1cef2e9464e..223814030f1 100644 --- a/Doc/library/wsgiref.rst +++ b/Doc/library/wsgiref.rst @@ -184,10 +184,11 @@ 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`, @@ -251,6 +252,10 @@ manipulation of WSGI response headers using a mapping-like interface. Content-Disposition: attachment; filename="bud.gif" + .. versionchanged:: 3.5 + *headers* parameter is optional. + + :mod:`wsgiref.simple_server` -- a simple WSGI HTTP server --------------------------------------------------------- diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst index 9fff48cf55f..0842143f2d3 100644 --- a/Doc/whatsnew/3.5.rst +++ b/Doc/whatsnew/3.5.rst @@ -230,6 +230,12 @@ socket :meth:`socket.socket.send`. (contributed by Giampaolo Rodola' in :issue:`17552`) +wsgiref +------- + +* *headers* parameter of :class:`wsgiref.headers.Headers` is now optional. + (Contributed by Pablo Torres Navarrete and SilentGhost in :issue:`5800`.) + xmlrpc ------ diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py index 901f3c99c66..190f3baa94b 100644 --- a/Lib/test/test_wsgiref.py +++ b/Lib/test/test_wsgiref.py @@ -338,6 +338,7 @@ 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']) @@ -345,7 +346,7 @@ class HeaderTests(TestCase): 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' @@ -370,9 +371,8 @@ class HeaderTests(TestCase): 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") diff --git a/Lib/wsgiref/headers.py b/Lib/wsgiref/headers.py index d93962831ae..7931bc79189 100644 --- a/Lib/wsgiref/headers.py +++ b/Lib/wsgiref/headers.py @@ -26,10 +26,10 @@ def _formatparam(param, value=None, quote=1): class Headers: - """Manage a collection of HTTP response headers""" - def __init__(self,headers): + def __init__(self, headers=None): + headers = headers if headers is not None else [] if type(headers) is not list: raise TypeError("Headers must be a list of name/value tuples") self._headers = headers diff --git a/Misc/NEWS b/Misc/NEWS index ad521b57ac2..04287755fe6 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -103,6 +103,9 @@ Core and Builtins Library ------- +- Issue #5800: headers parameter of wsgiref.headers.Headers is now optional. + Initial patch by Pablo Torres Navarrete and SilentGhost. + - Issue #21781: ssl.RAND_add() now supports strings longer than 2 GB. - Issue #21679: Prevent extraneous fstat() calls during open(). Patch by