Currently, http.cookiejar only supports path-like objects for FileCookieJar and its sub-classes (MozillaCookieJar and LWPCookieJar). So there’s no clean way to load interoperable cookie format data other than from the disk.
Reasoning:
Cookie files can contain sensitive info, so you may not want to write it to disk.
Its useful to provide the user other options to load data, other than from the disk, especially with keys/“secrets” (which cookies can contain). E.g. environment variable or standard input.
As I understand it, its preferable for python libraries to support file-like objects rather than just paths, when the path itself is not relevant (as in this case - http.cookiejar only uses for error messages for failing to load the path).
Workarounds:
Using the private function _really_load (not desirable as its a private function)
Making a fork of http.cookiejar (not desirable as it requires maintenance and this appears to require a small change to the http.cookiejar code)
What would it mean to provide a file-like object? Would that simply load the cookies but not save? None of the rest of your post refers to being able to mutate the cookie jar, but I’m not certain if that’s your intention here.
An unsaveable cookie jar subclass that can be given an existing file’s contents would seem like a useful feature. But if it needs to be able to save back afterwards, that’d be more complicated.
That’s talking about an actual file object, not the much larger concept of a “file-like” object. For a few very obvious examples of distinctions, a file object has a .fileno() method that gives its file descriptor, but in general, file-like objects won’t (because the entire point is that they don’t have to be backed by file descriptors); and they won’t be expected to support file locking, memory mapping, or anything of that nature. They also aren’t necessarily seekable.
The io module, and particularly io.StringIO and io.BytesIO, will give you a good idea of what people usually expect of a file-like object. It is not uncommon for file-like objects to be EITHER readable OR writable, but not both; one such example is what you get from a zipfile:
When you have an open zip file, you can open a file inside that, and get back a readable or writable file-like object, but there’s no mention of + being valid here. TBH this would potentially be a good use-case for a read-only cookiejar - you could have an archive of all your cookies, encrypted for a bit of extra protection, and then you perform HTTP requests on them directly.
For my specific use case, I’d think I would need io.StringIO, so I could provide a string from an environment variable. I don’t need to be able to write to the file. (Although for my use case, it could probably just be a plain string, rather than io.StringIO, I guess).
Ah, that might be an alternate and equally valid approach. (I don’t know enough about the http module to know how its cookie jar is implemented, so I don’t know which would be easier.) A cookie jar that can be directly loaded from a (byte?) string would be quite useful.
You’re likely a lot more familiar with the code than I am; perhaps you could write a patch, adding either StringIO/BytesIO or plain str/bytes loading. Then someone smarter than me can evaluate whether it’s worth adding.