MCPcopy Create free account
hub / github.com/cloudpipe/cloudpickle / test_instance_with_slots

Method test_instance_with_slots

tests/cloudpickle_test.py:2316–2343  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2314 assert pickle_depickle(t) == t
2315
2316 def test_instance_with_slots(self):
2317 for slots in [["registered_attribute"], "registered_attribute"]:
2318
2319 class ClassWithSlots:
2320 __slots__ = slots
2321
2322 def __init__(self):
2323 self.registered_attribute = 42
2324
2325 initial_obj = ClassWithSlots()
2326 depickled_obj = pickle_depickle(initial_obj, protocol=self.protocol)
2327
2328 assert depickled_obj.__class__.__slots__ == slots
2329
2330 for obj in [initial_obj, depickled_obj]:
2331 self.assertEqual(obj.registered_attribute, 42)
2332 with pytest.raises(AttributeError):
2333 obj.non_registered_attribute = 1
2334
2335 class SubclassWithSlots(ClassWithSlots):
2336 def __init__(self):
2337 self.unregistered_attribute = 1
2338
2339 obj = SubclassWithSlots()
2340 s = cloudpickle.dumps(obj, protocol=self.protocol)
2341 del SubclassWithSlots
2342 depickled_obj = cloudpickle.loads(s)
2343 assert depickled_obj.unregistered_attribute == 1
2344
2345 @unittest.skipIf(
2346 not hasattr(types, "MappingProxyType"),

Callers

nothing calls this directly

Calls 3

ClassWithSlotsClass · 0.85
pickle_depickleFunction · 0.85
SubclassWithSlotsClass · 0.85

Tested by

no test coverage detected