@@ -3927,26 +3927,31 @@ def test_invalid_shared_memory_cration(self):
3927
3927
sms_invalid = shared_memory .SharedMemory (create = True )
3928
3928
3929
3929
def test_shared_memory_pickle_unpickle (self ):
3930
- sms = shared_memory .SharedMemory (create = True , size = 512 )
3931
- self .addCleanup (sms .unlink )
3932
- sms .buf [0 :6 ] = b'pickle'
3933
-
3934
- # Test pickling
3935
- pickled_sms = pickle .dumps (sms )
3936
- self .assertNotIn (b'pickle' , pickled_sms )
3937
-
3938
- # Test unpickling
3939
- sms2 = pickle .loads (pickled_sms )
3940
- self .assertIsInstance (sms2 , sms .__class__ )
3941
- self .assertEqual (sms .name , sms2 .name )
3942
- self .assertEqual (bytes (sms .buf [0 :6 ]), bytes (sms2 .buf [0 :6 ]), b'pickle' )
3943
-
3944
- # Test that unpickled version is still the same SharedMemory
3945
- sms .buf [0 :6 ] = b'newval'
3946
- self .assertEqual (bytes (sms .buf [0 :6 ]), bytes (sms2 .buf [0 :6 ]), b'newval' )
3947
-
3948
- sms2 .buf [0 :6 ] = b'oldval'
3949
- self .assertEqual (bytes (sms .buf [0 :6 ]), bytes (sms2 .buf [0 :6 ]), b'oldval' )
3930
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
3931
+ with self .subTest (proto = proto ):
3932
+ sms = shared_memory .SharedMemory (create = True , size = 512 )
3933
+ self .addCleanup (sms .unlink )
3934
+ sms .buf [0 :6 ] = b'pickle'
3935
+
3936
+ # Test pickling
3937
+ pickled_sms = pickle .dumps (sms , protocol = proto )
3938
+ self .assertNotIn (b'pickle' , pickled_sms )
3939
+
3940
+ # Test unpickling
3941
+ sms2 = pickle .loads (pickled_sms )
3942
+ self .assertIsInstance (sms2 , sms .__class__ )
3943
+ self .assertEqual (sms .name , sms2 .name )
3944
+ self .assertEqual (
3945
+ bytes (sms .buf [0 :6 ]), bytes (sms2 .buf [0 :6 ]), b'pickle' )
3946
+
3947
+ # Test that unpickled version is still the same SharedMemory
3948
+ sms .buf [0 :6 ] = b'newval'
3949
+ self .assertEqual (
3950
+ bytes (sms .buf [0 :6 ]), bytes (sms2 .buf [0 :6 ]), b'newval' )
3951
+
3952
+ sms2 .buf [0 :6 ] = b'oldval'
3953
+ self .assertEqual (
3954
+ bytes (sms .buf [0 :6 ]), bytes (sms2 .buf [0 :6 ]), b'oldval' )
3950
3955
3951
3956
def test_shared_memory_pickle_unpickle_dead_object (self ):
3952
3957
sms = shared_memory .SharedMemory (create = True , size = 512 )
@@ -4178,27 +4183,30 @@ def test_shared_memory_ShareableList_basics(self):
4178
4183
empty_sl .shm .unlink ()
4179
4184
4180
4185
def test_shared_memory_ShareableList_pickling (self ):
4181
- sl = shared_memory .ShareableList (range (10 ))
4182
- self .addCleanup (sl .shm .unlink )
4183
-
4184
- serialized_sl = pickle .dumps (sl )
4185
- deserialized_sl = pickle .loads (serialized_sl )
4186
- self .assertIsInstance (deserialized_sl , shared_memory .ShareableList )
4187
- self .assertEqual (deserialized_sl [- 1 ], 9 )
4188
- self .assertIsNot (sl , deserialized_sl )
4189
- deserialized_sl [4 ] = "changed"
4190
- self .assertEqual (sl [4 ], "changed" )
4191
-
4192
- # Verify data is not being put into the pickled representation.
4193
- name = 'a' * len (sl .shm .name )
4194
- larger_sl = shared_memory .ShareableList (range (400 ))
4195
- self .addCleanup (larger_sl .shm .unlink )
4196
- serialized_larger_sl = pickle .dumps (larger_sl )
4197
- self .assertEqual (len (serialized_sl ), len (serialized_larger_sl ))
4198
- larger_sl .shm .close ()
4199
-
4200
- deserialized_sl .shm .close ()
4201
- sl .shm .close ()
4186
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
4187
+ with self .subTest (proto = proto ):
4188
+ sl = shared_memory .ShareableList (range (10 ))
4189
+ self .addCleanup (sl .shm .unlink )
4190
+
4191
+ serialized_sl = pickle .dumps (sl , protocol = proto )
4192
+ deserialized_sl = pickle .loads (serialized_sl )
4193
+ self .assertIsInstance (
4194
+ deserialized_sl , shared_memory .ShareableList )
4195
+ self .assertEqual (deserialized_sl [- 1 ], 9 )
4196
+ self .assertIsNot (sl , deserialized_sl )
4197
+ deserialized_sl [4 ] = "changed"
4198
+ self .assertEqual (sl [4 ], "changed" )
4199
+
4200
+ # Verify data is not being put into the pickled representation.
4201
+ name = 'a' * len (sl .shm .name )
4202
+ larger_sl = shared_memory .ShareableList (range (400 ))
4203
+ self .addCleanup (larger_sl .shm .unlink )
4204
+ serialized_larger_sl = pickle .dumps (larger_sl , protocol = proto )
4205
+ self .assertEqual (len (serialized_sl ), len (serialized_larger_sl ))
4206
+ larger_sl .shm .close ()
4207
+
4208
+ deserialized_sl .shm .close ()
4209
+ sl .shm .close ()
4202
4210
4203
4211
def test_shared_memory_ShareableList_pickling_dead_object (self ):
4204
4212
sl = shared_memory .ShareableList (range (10 ))
0 commit comments