Add more tests for preserving identity in marshal. (GH-13736)

This commit is contained in:
Serhiy Storchaka 2019-06-02 09:03:59 +03:00 committed by GitHub
parent b7daabd711
commit d71f3170ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 18 deletions

View File

@ -383,10 +383,7 @@ 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
@ -394,11 +391,12 @@ class InstancingTestCase(unittest.TestCase, HelperMixin):
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
@ -414,20 +412,54 @@ class InstancingTestCase(unittest.TestCase, HelperMixin):
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:
@ -438,10 +470,11 @@ class InstancingTestCase(unittest.TestCase, HelperMixin):
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)