Skip to content

PYTHON-1915: Prohibit copying ClientSession objects #480

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

Merged
merged 3 commits into from
Aug 21, 2020
Merged
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
1 change: 1 addition & 0 deletions doc/contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ The following is a list of people who have contributed to
- Terence Honles (terencehonles)
- Paul Fisher (thetorpedodog)
- Julius Park (juliusgeo)
- Ishmum Jawad Khan (ishmum123)
3 changes: 3 additions & 0 deletions pymongo/client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,9 @@ def _start_retryable_write(self):
self._check_ended()
self._server_session.inc_transaction_id()

def __copy__(self):
raise TypeError('A ClientSession cannot be copied, create a new session instead')


class _ServerSession(object):
def __init__(self, generation):
Expand Down
5 changes: 5 additions & 0 deletions test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,11 @@ def drop_db():

wait_until(drop_db, 'dropped database after w=0 writes')

def test_session_not_copyable(self):
client = self.client
with client.start_session() as s:
self.assertRaises(TypeError, lambda: copy.copy(s))


class TestCausalConsistency(unittest.TestCase):

Expand Down