]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36043: FileCookieJar supports os.PathLike (GH-11945)
authorStéphane Wirtel <stephane@wirtel.be>
Fri, 1 Mar 2019 20:40:54 +0000 (21:40 +0100)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 1 Mar 2019 20:40:54 +0000 (12:40 -0800)
https://bugs.python.org/issue36043

Doc/library/http.cookiejar.rst
Lib/http/cookiejar.py
Lib/test/test_http_cookiejar.py
Misc/NEWS.d/next/Library/2019-02-19-19-53-46.bpo-36043.l867v0.rst [new file with mode: 0644]

index 4c8be2917b22dab731f7eea50834d7a532fa0468..a9d7321a42c43f4656f18ba84da5b82738bb7b56 100644 (file)
@@ -71,6 +71,10 @@ The following classes are provided:
    :meth:`load` or :meth:`revert` method is called.  Subclasses of this class are
    documented in section :ref:`file-cookie-jar-classes`.
 
+   .. versionchanged:: 3.8
+
+      The filename parameter supports a :term:`path-like object`.
+
 
 .. class:: CookiePolicy()
 
@@ -341,6 +345,9 @@ writing.
    compatible with the libwww-perl library's ``Set-Cookie3`` file format.  This is
    convenient if you want to store cookies in a human-readable file.
 
+   .. versionchanged:: 3.8
+
+      The filename parameter supports a :term:`path-like object`.
 
 .. _cookie-policy-objects:
 
index 0ba8200f325a629ed4eb1b88ae4e277afdc9deb6..befe7653c53041a0c581af9c1be6fa286037bc50 100644 (file)
@@ -28,6 +28,7 @@ http://wwwsearch.sf.net/):
 __all__ = ['Cookie', 'CookieJar', 'CookiePolicy', 'DefaultCookiePolicy',
            'FileCookieJar', 'LWPCookieJar', 'LoadError', 'MozillaCookieJar']
 
+import os
 import copy
 import datetime
 import re
@@ -1762,10 +1763,7 @@ class FileCookieJar(CookieJar):
         """
         CookieJar.__init__(self, policy)
         if filename is not None:
-            try:
-                filename+""
-            except:
-                raise ValueError("filename must be string-like")
+            filename = os.fspath(filename)
         self.filename = filename
         self.delayload = bool(delayload)
 
index 8dbea3325d9b9907d6d59b9d24a06f6b83f95976..170549caf5b435e3ac500230635a73873df5e39e 100644 (file)
@@ -6,6 +6,7 @@ import test.support
 import time
 import unittest
 import urllib.request
+import pathlib
 
 from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
      parse_ns_headers, join_header_words, split_header_words, Cookie,
@@ -313,6 +314,30 @@ def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
 
 
 class FileCookieJarTests(unittest.TestCase):
+    def test_constructor_with_str(self):
+        filename = test.support.TESTFN
+        c = LWPCookieJar(filename)
+        self.assertEqual(c.filename, filename)
+
+    def test_constructor_with_path_like(self):
+        filename = pathlib.Path(test.support.TESTFN)
+        c = LWPCookieJar(filename)
+        self.assertEqual(c.filename, os.fspath(filename))
+
+    def test_constructor_with_none(self):
+        c = LWPCookieJar(None)
+        self.assertIsNone(c.filename)
+
+    def test_constructor_with_other_types(self):
+        class A:
+            pass
+
+        for type_ in (int, float, A):
+            with self.subTest(filename=type_):
+                with self.assertRaises(TypeError):
+                    instance = type_()
+                    c = LWPCookieJar(filename=instance)
+
     def test_lwp_valueless_cookie(self):
         # cookies with no value should be saved and loaded consistently
         filename = test.support.TESTFN
diff --git a/Misc/NEWS.d/next/Library/2019-02-19-19-53-46.bpo-36043.l867v0.rst b/Misc/NEWS.d/next/Library/2019-02-19-19-53-46.bpo-36043.l867v0.rst
new file mode 100644 (file)
index 0000000..f4911a0
--- /dev/null
@@ -0,0 +1 @@
+:class:`FileCookieJar` supports :term:`path-like object`. Contributed by Stéphane Wirtel