diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-06-02 06:03:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-02 06:03:59 (GMT) |
commit | d71f3170ac9c850f6d1f9bffaa628dc473df5e75 (patch) | |
tree | 0c79f8a5119db7e13142019f9ee5f067fc3407af | |
parent | b7daabd711274a009e70556020efeae502a85f0b (diff) | |
download | cpython-d71f3170ac9c850f6d1f9bffaa628dc473df5e75.zip cpython-d71f3170ac9c850f6d1f9bffaa628dc473df5e75.tar.gz cpython-d71f3170ac9c850f6d1f9bffaa628dc473df5e75.tar.bz2 |
Add more tests for preserving identity in marshal. (GH-13736)
-rw-r--r-- | Lib/test/test_marshal.py | 69 |
1 files changed, 51 insertions, 18 deletions
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py index a3bd350..ace1593 100644 --- a/Lib/test/test_marshal.py +++ b/Lib/test/test_marshal.py @@ -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) |