requests / toolbelt

A toolbelt of useful classes and functions to be used with python-requests
https://toolbelt.readthedocs.org
Other
996 stars 184 forks source link

Pickling and unpickling `BaseUrlSession` will loose `base_url` #375

Closed Inokinoki closed 5 months ago

Inokinoki commented 6 months ago

Pickle and unpickle a BaseUrlSession will result in an empty URL:

>>> from requests_toolbelt.sessions import BaseUrlSession
>>> import pickle
>>> session = BaseUrlSession(base_url="https://www.test.com")             
>>> with open("pickled_session.pkl", "wb") as f:
...     pickle.dump(session, f)
... 
>>> with open("pickled_session.pkl", "rb") as f:
...     s = pickle.load(f)
...   
>>> s.base_url
>>> session.base_url
'https://www.test.com'

In requests, the attributes are handled the in:

    def __getstate__(self):
        state = {attr: getattr(self, attr, None) for attr in self.__attrs__}
        return state

    def __setstate__(self, state):
        for attr, value in state.items():
            setattr(self, attr, value)

I think base_url should also be handled in BaseUrlSession. I can create a PR for this and ask for you review

sigmavirus24 commented 6 months ago

I'll review that for sure