Skip to content

Commit

Permalink
[3.11] pythongh-110267: Add tests for pickling and copying PyStructSe…
Browse files Browse the repository at this point in the history
…quence objects (pythonGH-110272) (pythonGH-110284)

(cherry picked from commit 2d4865d)

Co-authored-by: Xuehai Pan <[email protected]>
  • Loading branch information
miss-islington and XuehaiPan authored Oct 4, 2023
1 parent 4e44927 commit 5039db7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
75 changes: 73 additions & 2 deletions Lib/test/test_structseq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import copy
import os
import pickle
import time
import unittest

Expand Down Expand Up @@ -106,9 +108,78 @@ def __len__(self):

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add tests for pickling and copying PyStructSequence objects.
Patched by Xuehai Pan.

0 comments on commit 5039db7

Please sign in to comment.