Skip to content

Commit 98873a3

Browse files
Only read the curl format, do not write it
Writing 0 instead of an empty field makes the file incompatible with unpatched Python, so it is left for a separate change.
1 parent f2b80ad commit 98873a3

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

Lib/http/cookiejar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
21092109
if cookie.expires is not None:
21102110
expires = str(cookie.expires)
21112111
else:
2112-
expires = "0"
2112+
expires = ""
21132113
if cookie.value is None:
21142114
# cookies.txt regards 'Set-Cookie: foo' as a cookie
21152115
# with no name, whereas http.cookiejar regards it as a

Lib/test/test_http_cookiejar.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,30 +2048,33 @@ def test_session_cookies(self):
20482048
# we didn't have session cookies in the first place
20492049
self.assertNotEqual(counter["session_before"], 0)
20502050

2051-
def test_curl_format(self):
2052-
# Check compatibility with the curl and Wget cookie file format,
2053-
# which uses 0 for session cookies (gh-61364).
2051+
def test_load_session_cookies(self):
2052+
# curl and Wget write 0 in the expires field for session cookies,
2053+
# while we write an empty field. Both should be read (gh-61364).
20542054
filename = os_helper.TESTFN
20552055
self.addCleanup(os_helper.unlink, filename)
20562056
expires = int(time.time() + 3600)
20572057
with open(filename, "w") as f:
20582058
f.write(NETSCAPE_HEADER_TEXT)
2059-
f.write("www.foo.com\tFALSE\t/\tFALSE\t%u\tfoo1\tbar\n" % expires)
2060-
f.write("www.foo.com\tFALSE\t/\tFALSE\t0\tfoo2\tbar\n")
2061-
f.write("www.foo.com\tFALSE\t/\tFALSE\t\tfoo3\tbar\n")
2059+
f.write("www.foo.com\tFALSE\t/\tFALSE\t%u\tperm\tbar\n" % expires)
2060+
f.write("www.foo.com\tFALSE\t/\tFALSE\t0\tcurl_session\tbar\n")
2061+
f.write("www.foo.com\tFALSE\t/\tFALSE\t\tour_session\tbar\n")
20622062

20632063
c = MozillaCookieJar()
20642064
c.revert(filename)
2065-
self.assertEqual(len(c), 1)
2066-
c.revert(filename, ignore_discard=True)
2067-
self.assertEqual(len(c), 3)
2065+
self.assertEqual([cookie.name for cookie in c], ["perm"])
20682066

2069-
# Session cookies are saved with 0, not with an empty field.
2070-
c.save(filename, ignore_discard=True)
2071-
with open(filename) as f:
2072-
for line in f:
2073-
if line.strip() and not line.startswith('#'):
2074-
self.assertRegex(line.split('\t')[4], r'^\d+$')
2067+
c = MozillaCookieJar()
2068+
c.revert(filename, ignore_discard=True)
2069+
self.assertEqual(sorted(cookie.name for cookie in c),
2070+
["curl_session", "our_session", "perm"])
2071+
for cookie in c:
2072+
if cookie.name == "perm":
2073+
self.assertEqual(cookie.expires, expires)
2074+
self.assertFalse(cookie.discard)
2075+
else:
2076+
self.assertIsNone(cookie.expires)
2077+
self.assertTrue(cookie.discard)
20752078

20762079

20772080
if __name__ == "__main__":

Misc/NEWS.d/next/Library/2019-02-08-17-33-49.bpo-17164.k6W5Sp.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`http.cookiejar.MozillaCookieJar` now reads session cookies written
2+
by curl and Wget, which use ``0`` in the expiration time field.
3+
Contributed by Jérémie Detrey.

0 commit comments

Comments
 (0)