Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude Requester.__auth_lock from serialization #2440

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion github/Requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def __init__(
self.__app_auth = app_auth
self.__base_url = base_url

self.__auth_lock = RLock()
self.__init_auth_lock()

if password is not None:
login = login_or_token
Expand Down Expand Up @@ -362,6 +362,19 @@ def __init__(
self.__userAgent = user_agent
self.__verify = verify

def __init_auth_lock(self):
self.__auth_lock = RLock()

def __getstate__(self):
# do not serialize the lock
state = self.__dict__.copy()
del state["_Requester__auth_lock"]
return state

def __setstate__(self, state):
self.__dict__.update(state)
self.__init_auth_lock()

def _must_refresh_token(self) -> bool:
"""Check if it is time to refresh the API token gotten from the GitHub app installation"""
if not self.__installation_authorization:
Expand Down
2 changes: 2 additions & 0 deletions tests/Framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def __init__(self, file, *args, **kwds):
class BasicTestCase(unittest.TestCase):
recordMode = False
tokenAuthMode = False
appAuthMode = False
jwtAuthMode = False
retry = None
pool_size = None
Expand All @@ -285,6 +286,7 @@ def setUp(self):
self.login = GithubCredentials.login
self.password = GithubCredentials.password
self.oauth_token = GithubCredentials.oauth_token
self.app_auth = GithubCredentials.app_auth
self.jwt = GithubCredentials.jwt
self.app_id = GithubCredentials.app_id
self.app_private_key = GithubCredentials.app_private_key
Expand Down
28 changes: 28 additions & 0 deletions tests/Pickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pickle
import unittest

import github
from github.AppAuthentication import AppAuthentication
from github.Repository import Repository

REPO_NAME = "PyGithub/PyGithub"


# https://github.com/PyGithub/PyGithub/issues/2436
# https://github.com/PyGithub/PyGithub/pull/2437
class TestPickle(unittest.TestCase):
def test_pickle_github(self):
gh = github.Github("token")
gh2 = pickle.loads(pickle.dumps(gh))
self.assertIsInstance(gh2, github.Github)

def test_pickle_github_with_app_auth(self):
gh = github.Github("token", app_auth=AppAuthentication("id", "key", 123))
gh2 = pickle.loads(pickle.dumps(gh))
self.assertIsInstance(gh2, github.Github)

def test_pickle_repository(self):
gh = github.Github("token")
repo = gh.get_repo(REPO_NAME, lazy=True)
repo2 = pickle.loads(pickle.dumps(repo))
self.assertIsInstance(repo2, Repository)