Skip to content

Commit 959039b

Browse files
authored
PYTHON-1915: Prohibit copying ClientSession objects (#480)
1 parent 031492a commit 959039b

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ The following is a list of people who have contributed to
8888
- Terence Honles (terencehonles)
8989
- Paul Fisher (thetorpedodog)
9090
- Julius Park (juliusgeo)
91+
- Ishmum Jawad Khan (ishmum123)

pymongo/client_session.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,9 @@ def _start_retryable_write(self):
827827
self._check_ended()
828828
self._server_session.inc_transaction_id()
829829

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

831834
class _ServerSession(object):
832835
def __init__(self, generation):

test/test_session.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,11 @@ def drop_db():
779779

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

782+
def test_session_not_copyable(self):
783+
client = self.client
784+
with client.start_session() as s:
785+
self.assertRaises(TypeError, lambda: copy.copy(s))
786+
782787

783788
class TestCausalConsistency(unittest.TestCase):
784789

0 commit comments

Comments
 (0)