summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-10-04 09:01:03 (GMT)
committerGitHub <noreply@github.com>2023-10-04 09:01:03 (GMT)
commit35feda5bc93ab1e756f3f2739985fd36caae6920 (patch)
treedc80873e1803a7d289c5fac5c3bb2f35b809a864 /Lib
parent414f5620812584906eed8a19405f148641b248d6 (diff)
downloadcpython-35feda5bc93ab1e756f3f2739985fd36caae6920.zip
cpython-35feda5bc93ab1e756f3f2739985fd36caae6920.tar.gz
cpython-35feda5bc93ab1e756f3f2739985fd36caae6920.tar.bz2
[3.12] gh-110267: Add tests for pickling and copying PyStructSequence objects (GH-110272) (GH-110285)
(cherry picked from commit 2d4865d775123e8889c7a79fc49b4bf627176c4b) Co-authored-by: Xuehai Pan <XuehaiPan@pku.edu.cn>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_structseq.py75
1 files changed, 73 insertions, 2 deletions
diff --git a/Lib/test/test_structseq.py b/Lib/test/test_structseq.py
index a9fe193..c6c0afa 100644
--- a/Lib/test/test_structseq.py
+++ b/Lib/test/test_structseq.py
@@ -1,4 +1,6 @@
+import copy
import os
+import pickle
import time
import unittest
@@ -106,9 +108,78 @@ class StructSeqTest(unittest.TestCase):
self.assertRaises(Exc, time.struct_time, C())
- def test_reduce(self):
+ def test_pickling(self):
t = time.gmtime()
- x = t.__reduce__()
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ p = pickle.dumps(t, proto)
+ t2 = pickle.loads(p)
+ self.assertEqual(t2.__class__, t.__class__)
+ self.assertEqual(t2, t)
+ self.assertEqual(t2.tm_year, t.tm_year)
+ self.assertEqual(t2.tm_zone, t.tm_zone)
+
+ def test_pickling_with_unnamed_fields(self):
+ assert os.stat_result.n_unnamed_fields > 0
+
+ r = os.stat_result(range(os.stat_result.n_sequence_fields),
+ {'st_atime': 1.0, 'st_atime_ns': 2.0})
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ p = pickle.dumps(r, proto)
+ r2 = pickle.loads(p)
+ self.assertEqual(r2.__class__, r.__class__)
+ self.assertEqual(r2, r)
+ self.assertEqual(r2.st_mode, r.st_mode)
+ self.assertEqual(r2.st_atime, r.st_atime)
+ self.assertEqual(r2.st_atime_ns, r.st_atime_ns)
+
+ def test_copying(self):
+ n_fields = time.struct_time.n_fields
+ t = time.struct_time([[i] for i in range(n_fields)])
+
+ t2 = copy.copy(t)
+ self.assertEqual(t2.__class__, t.__class__)
+ self.assertEqual(t2, t)
+ self.assertEqual(t2.tm_year, t.tm_year)
+ self.assertEqual(t2.tm_zone, t.tm_zone)
+ self.assertIs(t2[0], t[0])
+ self.assertIs(t2.tm_year, t.tm_year)
+
+ t3 = copy.deepcopy(t)
+ self.assertEqual(t3.__class__, t.__class__)
+ self.assertEqual(t3, t)
+ self.assertEqual(t3.tm_year, t.tm_year)
+ self.assertEqual(t3.tm_zone, t.tm_zone)
+ self.assertIsNot(t3[0], t[0])
+ self.assertIsNot(t3.tm_year, t.tm_year)
+
+ def test_copying_with_unnamed_fields(self):
+ assert os.stat_result.n_unnamed_fields > 0
+
+ n_sequence_fields = os.stat_result.n_sequence_fields
+ r = os.stat_result([[i] for i in range(n_sequence_fields)],
+ {'st_atime': [1.0], 'st_atime_ns': [2.0]})
+
+ r2 = copy.copy(r)
+ self.assertEqual(r2.__class__, r.__class__)
+ self.assertEqual(r2, r)
+ self.assertEqual(r2.st_mode, r.st_mode)
+ self.assertEqual(r2.st_atime, r.st_atime)
+ self.assertEqual(r2.st_atime_ns, r.st_atime_ns)
+ self.assertIs(r2[0], r[0])
+ self.assertIs(r2.st_mode, r.st_mode)
+ self.assertIs(r2.st_atime, r.st_atime)
+ self.assertIs(r2.st_atime_ns, r.st_atime_ns)
+
+ r3 = copy.deepcopy(r)
+ self.assertEqual(r3.__class__, r.__class__)
+ self.assertEqual(r3, r)
+ self.assertEqual(r3.st_mode, r.st_mode)
+ self.assertEqual(r3.st_atime, r.st_atime)
+ self.assertEqual(r3.st_atime_ns, r.st_atime_ns)
+ self.assertIsNot(r3[0], r[0])
+ self.assertIsNot(r3.st_mode, r.st_mode)
+ self.assertIsNot(r3.st_atime, r.st_atime)
+ self.assertIsNot(r3.st_atime_ns, r.st_atime_ns)
def test_extended_getslice(self):
# Test extended slicing by comparing with list slicing.