Skip to content

Commit 4b219ce

Browse files
matrixisemiss-islington
authored andcommitted
bpo-36043: FileCookieJar supports os.PathLike (GH-11945)
https://bugs.python.org/issue36043
1 parent bda918b commit 4b219ce

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

Doc/library/http.cookiejar.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ The following classes are provided:
7171
:meth:`load` or :meth:`revert` method is called. Subclasses of this class are
7272
documented in section :ref:`file-cookie-jar-classes`.
7373

74+
.. versionchanged:: 3.8
75+
76+
The filename parameter supports a :term:`path-like object`.
77+
7478

7579
.. class:: CookiePolicy()
7680

@@ -341,6 +345,9 @@ writing.
341345
compatible with the libwww-perl library's ``Set-Cookie3`` file format. This is
342346
convenient if you want to store cookies in a human-readable file.
343347

348+
.. versionchanged:: 3.8
349+
350+
The filename parameter supports a :term:`path-like object`.
344351

345352
.. _cookie-policy-objects:
346353

Lib/http/cookiejar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
__all__ = ['Cookie', 'CookieJar', 'CookiePolicy', 'DefaultCookiePolicy',
2929
'FileCookieJar', 'LWPCookieJar', 'LoadError', 'MozillaCookieJar']
3030

31+
import os
3132
import copy
3233
import datetime
3334
import re
@@ -1762,10 +1763,7 @@ def __init__(self, filename=None, delayload=False, policy=None):
17621763
"""
17631764
CookieJar.__init__(self, policy)
17641765
if filename is not None:
1765-
try:
1766-
filename+""
1767-
except:
1768-
raise ValueError("filename must be string-like")
1766+
filename = os.fspath(filename)
17691767
self.filename = filename
17701768
self.delayload = bool(delayload)
17711769

Lib/test/test_http_cookiejar.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
import unittest
88
import urllib.request
9+
import pathlib
910

1011
from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
1112
parse_ns_headers, join_header_words, split_header_words, Cookie,
@@ -313,6 +314,30 @@ def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
313314

314315

315316
class FileCookieJarTests(unittest.TestCase):
317+
def test_constructor_with_str(self):
318+
filename = test.support.TESTFN
319+
c = LWPCookieJar(filename)
320+
self.assertEqual(c.filename, filename)
321+
322+
def test_constructor_with_path_like(self):
323+
filename = pathlib.Path(test.support.TESTFN)
324+
c = LWPCookieJar(filename)
325+
self.assertEqual(c.filename, os.fspath(filename))
326+
327+
def test_constructor_with_none(self):
328+
c = LWPCookieJar(None)
329+
self.assertIsNone(c.filename)
330+
331+
def test_constructor_with_other_types(self):
332+
class A:
333+
pass
334+
335+
for type_ in (int, float, A):
336+
with self.subTest(filename=type_):
337+
with self.assertRaises(TypeError):
338+
instance = type_()
339+
c = LWPCookieJar(filename=instance)
340+
316341
def test_lwp_valueless_cookie(self):
317342
# cookies with no value should be saved and loaded consistently
318343
filename = test.support.TESTFN
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`FileCookieJar` supports :term:`path-like object`. Contributed by Stéphane Wirtel

0 commit comments

Comments
 (0)