Skip to content

Commit 1813c17

Browse files
committed
#2211: properly document the Morsel behavior changes.
Also deprecate the undocumented set argument instead of removing it already in 3.5. Initial patch by Demian Brecht.
1 parent 0deefd5 commit 1813c17

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

Doc/library/http.cookies.rst

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,38 +143,43 @@ Morsel Objects
143143

144144
The keys are case-insensitive.
145145

146+
.. versionchanged:: 3.5
147+
:meth:`~Morsel.__eq__` now takes :attr:`~Morsel.key` and :attr:`~Morsel.value`
148+
into account.
149+
146150

147151
.. attribute:: Morsel.value
148152

149153
The value of the cookie.
150154

151155
.. deprecated:: 3.5
152-
Setting :attr:`~Morsel.value` directly has been deprecated in favour of
153-
using :func:`~Morsel.set`
156+
assigning to ``value``; use :meth:`~Morsel.set` instead.
154157

155158

156159
.. attribute:: Morsel.coded_value
157160

158161
The encoded value of the cookie --- this is what should be sent.
159162

160163
.. deprecated:: 3.5
161-
Setting :attr:`~Morsel.coded_value` directly has been deprecated in
162-
favour of using :func:`~Morsel.set`
164+
assigning to ``coded_value``; use :meth:`~Morsel.set` instead.
163165

164166

165167
.. attribute:: Morsel.key
166168

167169
The name of the cookie.
168170

169171
.. deprecated:: 3.5
170-
Setting :attr:`~Morsel.key` directly has been deprecated in
171-
favour of using :func:`~Morsel.set`
172+
assigning to ``key``; use :meth:`~Morsel.set` instead.
172173

173174

174175
.. method:: Morsel.set(key, value, coded_value)
175176

176177
Set the *key*, *value* and *coded_value* attributes.
177178

179+
.. deprecated:: 3.5
180+
The undocumented *LegalChars* parameter is ignored and will be removed in
181+
a future version.
182+
178183

179184
.. method:: Morsel.isReservedKey(K)
180185

@@ -205,6 +210,24 @@ Morsel Objects
205210
The meaning for *attrs* is the same as in :meth:`output`.
206211

207212

213+
.. method:: Morsel.update(values)
214+
215+
Update the values in the Morsel dictionary with the values in the dictionary
216+
*values*. Raise an error if any of the keys in the *values* dict is not a
217+
valid :rfc:`2109` attribute.
218+
219+
.. versionchanged:: 3.5
220+
an error is raised for invalid keys.
221+
222+
223+
.. method:: Morsel.copy(value)
224+
225+
Return a shallow copy of the Morsel object.
226+
227+
.. versionchanged:: 3.5
228+
return a Morsel object instead of a dict.
229+
230+
208231
.. _cookie-example:
209232

210233
Example

Doc/whatsnew/3.5.rst

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,12 @@ Deprecated Python modules, functions and methods
525525
``True``, but this default is deprecated. Specify the *decode_data* keyword
526526
with an appropriate value to avoid the deprecation warning.
527527

528-
* :class:`~http.cookies.Morsel` has previously allowed for setting attributes
529-
:attr:`~http.cookies.Morsel.key`, :attr:`~http.cookies.Morsel.value` and
530-
:attr:`~http.cookies.Morsel.coded_value`. Use the preferred
531-
:func:`~http.cookies.Morsel.set` method in order to avoid the deprecation
532-
warning.
528+
* Directly assigning values to the :attr:`~http.cookies.Morsel.key`,
529+
:attr:`~http.cookies.Morsel.value` and
530+
:attr:`~http.cookies.Morsel.coded_value` of :class:`~http.cookies.Morsel`
531+
objects is deprecated. Use the :func:`~http.cookies.Morsel.set` method
532+
instead. In addition, the undocumented *LegalChars* parameter of
533+
:func:`~http.cookies.Morsel.set` is deprecated, and is now ignored.
533534

534535
* Passing a format string as keyword argument *format_string* to the
535536
:meth:`~string.Formatter.format` method of the :class:`string.Formatter`
@@ -635,6 +636,16 @@ Changes in the Python API
635636
string (e.g. ``'x+'`` instead of ``'x*'``). Patterns that could only match
636637
an empty string (such as ``'\b'``) now raise an error.
637638

639+
* The :class:`~http.cookies.Morsel` dict-like interface has been made self
640+
consistent: morsel comparison now takes the :attr:`~http.cookies.Morsel.key`
641+
and :attr:`~http.cookies.Morsel.value` into account,
642+
:meth:`~http.cookies.Morsel.copy` now results in a
643+
:class:`~http.cookies.Morsel` instance rather than a *dict*, and
644+
:meth:`~http.cookies.Morsel.update` will no raise an exception if any of the
645+
keys in the update dictionary are invalid. In addition, the undocumented
646+
*LegalChars* parameter of :func:`~http.cookies.Morsel.set` is deprecated and
647+
is now ignored. (:issue:`2211`)
648+
638649
Changes in the C API
639650
--------------------
640651

Lib/http/cookies.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,14 @@ def update(self, values):
366366
def isReservedKey(self, K):
367367
return K.lower() in self._reserved
368368

369-
def set(self, key, val, coded_val):
369+
def set(self, key, val, coded_val, LegalChars=_LegalChars):
370+
if LegalChars != _LegalChars:
371+
import warnings
372+
warnings.warn(
373+
'LegalChars parameter is deprecated, ignored and will '
374+
'be removed in future versions.', DeprecationWarning,
375+
stacklevel=2)
376+
370377
if key.lower() in self._reserved:
371378
raise CookieError('Attempt to set a reserved key %r' % (key,))
372379
if not _is_legal_key(key):

Lib/test/test_http_cookies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ def test_deprecation(self):
261261
morsel.value = ''
262262
with self.assertWarnsRegex(DeprecationWarning, r'\bcoded_value\b'):
263263
morsel.coded_value = ''
264+
with self.assertWarnsRegex(DeprecationWarning, r'\bLegalChars\b'):
265+
morsel.set('key', 'value', 'coded_value', LegalChars='.*')
264266

265267
def test_eq(self):
266268
base_case = ('key', 'value', '"value"')

0 commit comments

Comments
 (0)