Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updates
  • Loading branch information
xiangyan99 committed Feb 7, 2024
commit 78b49565e0aea817bc8a38da4353e9026708cdcc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def __init__(

self._cache = kwargs.pop("_cache", None)
self._cae_cache = kwargs.pop("_cae_cache", None)
if self._cache or self._cae_cache:
self._custom_cache = True
else:
self._custom_cache = False
self._cache_options = kwargs.pop("cache_persistence_options", None)

super(MsalCredential, self).__init__()
Expand Down Expand Up @@ -118,10 +122,16 @@ def __getstate__(self) -> Dict[str, Any]:
# Remove the non-picklable entries
del state["_client_applications"]
del state["_cae_client_applications"]
if not self._custom_cache:
del state["_cache"]
del state["_cae_cache"]
return state

def __setstate__(self, state: Dict[str, Any]) -> None:
self.__dict__.update(state)
# Re-create the unpickable entries
self._client_applications = {}
self._cae_client_applications = {}
if not self._custom_cache:
self._cache = None
self._cae_cache = None
10 changes: 9 additions & 1 deletion sdk/identity/azure-identity/tests/test_pickling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
# ------------------------------------
import pickle
from azure.identity import DefaultAzureCredential

from azure.identity._internal.msal_credentials import MsalCredential

def test_pickle_dac():
cred = DefaultAzureCredential()
with open("data.pkl", "wb") as outfile:
pickle.dump(cred, outfile)
with open("data.pkl", "rb") as infile:
data_loaded = pickle.load(infile)

def test_pickle_msal_credential():
cred = MsalCredential(client_id="CLIENT_ID")
app = cred._get_app()
with open("data.pkl", "wb") as outfile:
pickle.dump(cred, outfile)
with open("data.pkl", "rb") as infile:
data_loaded = pickle.load(infile)