]> granicus.if.org Git - python/commitdiff
Add more tests for preserving identity in marshal. (GH-13736)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 2 Jun 2019 06:03:59 +0000 (09:03 +0300)
committerGitHub <noreply@github.com>
Sun, 2 Jun 2019 06:03:59 +0000 (09:03 +0300)
Lib/test/test_marshal.py

index a3bd350c77b95bf63497373cf32cc7a4f87686e8..ace1593999d4ebf9ea05adc29fdde378f3f31735 100644 (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)