Skip to content

Add more tests for preserving identity in marshal. #13736

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
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
69 changes: 51 additions & 18 deletions Lib/test/test_marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,22 +383,20 @@ def CollectObjectIDs(ids, obj):
return len(ids)

class InstancingTestCase(unittest.TestCase, HelperMixin):
intobj = 123321
floatobj = 1.2345
strobj = "abcde"*3
dictobj = {"hello":floatobj, "goodbye":floatobj, floatobj:"hello"}
keys = (123, 1.2345, 'abc', (123, 'abc'), frozenset({123, 'abc'}))

def helper3(self, rsample, recursive=False, simple=False):
#we have two instances
sample = (rsample, rsample)

n0 = CollectObjectIDs(set(), sample)

s3 = marshal.dumps(sample, 3)
n3 = CollectObjectIDs(set(), marshal.loads(s3))
for v in range(3, marshal.version + 1):
s3 = marshal.dumps(sample, v)
n3 = CollectObjectIDs(set(), marshal.loads(s3))

#same number of instances generated
self.assertEqual(n3, n0)
#same number of instances generated
self.assertEqual(n3, n0)

if not recursive:
#can compare with version 2
Expand All @@ -414,20 +412,54 @@ def helper3(self, rsample, recursive=False, simple=False):
self.assertGreaterEqual(len(s2), len(s3))

def testInt(self):
self.helper(self.intobj)
self.helper3(self.intobj, simple=True)
intobj = 123321
self.helper(intobj)
self.helper3(intobj, simple=True)

def testFloat(self):
self.helper(self.floatobj)
self.helper3(self.floatobj)
floatobj = 1.2345
self.helper(floatobj)
self.helper3(floatobj)

def testStr(self):
self.helper(self.strobj)
self.helper3(self.strobj)
strobj = "abcde"*3
self.helper(strobj)
self.helper3(strobj)

def testBytes(self):
bytesobj = b"abcde"*3
self.helper(bytesobj)
self.helper3(bytesobj)

def testList(self):
for obj in self.keys:
listobj = [obj, obj]
self.helper(listobj)
self.helper3(listobj)

def testTuple(self):
for obj in self.keys:
tupleobj = (obj, obj)
self.helper(tupleobj)
self.helper3(tupleobj)

def testSet(self):
for obj in self.keys:
setobj = {(obj, 1), (obj, 2)}
self.helper(setobj)
self.helper3(setobj)

def testFrozenSet(self):
for obj in self.keys:
frozensetobj = frozenset({(obj, 1), (obj, 2)})
self.helper(frozensetobj)
self.helper3(frozensetobj)

def testDict(self):
self.helper(self.dictobj)
self.helper3(self.dictobj)
for obj in self.keys:
dictobj = {"hello": obj, "goodbye": obj, obj: "hello"}
self.helper(dictobj)
self.helper3(dictobj)

def testModule(self):
with open(__file__, "rb") as f:
Expand All @@ -438,10 +470,11 @@ def testModule(self):
self.helper3(code)

def testRecursion(self):
d = dict(self.dictobj)
obj = 1.2345
d = {"hello": obj, "goodbye": obj, obj: "hello"}
d["self"] = d
self.helper3(d, recursive=True)
l = [self.dictobj]
l = [obj, obj]
l.append(l)
self.helper3(l, recursive=True)

Expand Down