8000 bpo-45125: Improves pickling docs and tests for `shared_memory` by sobolevn · Pull Request #28294 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-45125: Improves pickling docs and tests for shared_memory #28294

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 4 commits into from
Oct 1, 2021
Merged
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
Address review
  • Loading branch information
sobolevn committed Oct 1, 2021
commit 1bb847c8b4f23d1f5ef6dd47d13a77e053a7de65
58 changes: 31 additions & 27 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3935,36 +3935,37 @@ def test_shared_memory_pickle_unpickle(self):

# Test pickling
pickled_sms = pickle.dumps(sms, protocol=proto)
self.assertNotIn(b'pickle', pickled_sms)

# Test unpickling
sms2 = pickle.loads(pickled_sms)
self.assertIsInstance(sms2, sms.__class__)
self.assertIsInstance(sms2, shared_memory.SharedMemory)
self.assertEqual(sms.name, sms2.name)
self.assertEqual(
bytes(sms.buf[0:6]), bytes(sms2.buf[0:6]), b'pickle')
self.assertEqual(bytes(sms.buf[0:6]), b'pickle')
self.assertEqual(bytes(sms2.buf[0:6]), b'pickle')

# Test that unpickled version is still the same SharedMemory
sms.buf[0:6] = b'newval'
self.assertEqual(
bytes(sms.buf[0:6]), bytes(sms2.buf[0:6]), b'newval')
self.assertEqual(bytes(sms.buf[0:6]), b'newval')
self.assertEqual(bytes(sms2.buf[0:6]), b'newval')

sms2.buf[0:6] = b'oldval'
self.assertEqual(
bytes(sms.buf[0:6]), bytes(sms2.buf[0:6]), b'oldval')
self.assertEqual(bytes(sms.buf[0:6]), b'oldval')
self.assertEqual(bytes(sms2.buf[0:6]), b'oldval')

def test_shared_memory_pickle_unpickle_dead_object(self):
sms = shared_memory.SharedMemory(create=True, size=512)
sms.buf[0:6] = b'pickle'
pickled_sms = pickle.dumps(sms)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
sms = shared_memory.SharedMemory(create=True, size=512)
sms.buf[0:6] = b'pickle'
pickled_sms = pickle.dumps(sms)

# Now, we are going to kill the original object.
# So, unpickled one won't be able to attach to it.
sms.close()
sms.unlink()
# Now, we are going to kill the original object.
# So, unpickled one won't be able to attach to it.
sms.close()
sms.unlink()

with self.assertRaises(FileNotFoundError):
pickle.loads(pickled_sms)
with self.assertRaises(FileNotFoundError):
pickle.loads(pickled_sms)

def test_shared_memory_across_processes(self):
# bpo-40135: don't define shared memory block's name in case of
Expand Down Expand Up @@ -4194,11 +4195,12 @@ def test_shared_memory_ShareableList_pickling(self):
deserialized_sl, shared_memory.ShareableList)
self.assertEqual(deserialized_sl[-1], 9)
self.assertIsNot(sl, deserialized_sl)

deserialized_sl[4] = "changed"
self.assertEqual(sl[4], "changed")
sl[3] = "newvalue"
self.assertEqual(deserialized_sl[3], "newvalue")

# Verify data is not being put into the pickled representation.
name = 'a' * len(sl.shm.name)
larger_sl = shared_memory.ShareableList(range(400))
self.addCleanup(larger_sl.shm.unlink)
serialized_larger_sl = pickle.dumps(larger_sl, protocol=proto)
Expand All @@ -4209,16 +4211,18 @@ def test_shared_memory_ShareableList_pickling(self):
sl.shm.close()

def test_shared_memory_ShareableList_pickling_dead_object(self):
sl = shared_memory.ShareableList(range(10))
serialized_sl = pickle.dumps(sl)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
sl = shared_memory.ShareableList(range(10))
serialized_sl = pickle.dumps(sl)

# Now, we are going to kill the original object.
# So, unpickled one won't be able to attach to it.
sl.shm.close()
sl.shm.unlink()
# Now, we are going to kill the original object.
# So, unpickled one won't be able to attach to it.
sl.shm.close()
sl.shm.unlink()

with self.assertRaises(FileNotFoundError):
pickle.loads(serialized_sl)
with self.assertRaises(FileNotFoundError):
pickle.loads(serialized_sl)

def test_shared_memory_cleaned_after_process_termination(self):
cmd = '''if 1:
Expand Down
0