summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-06-09 01:28:30 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-06-09 01:28:30 (GMT)
commite98839a1f48b2915f1cc747884e64f4d6e4c8e7a (patch)
treec4ae6eafe55d4bd16e295b32c4334bb2cd57a7e6 /Lib/test
parentf4fcdb6b8c0ee8bc4d2eee5ea3b848a4cd905ad6 (diff)
downloadcpython-e98839a1f48b2915f1cc747884e64f4d6e4c8e7a.zip
cpython-e98839a1f48b2915f1cc747884e64f4d6e4c8e7a.tar.gz
cpython-e98839a1f48b2915f1cc747884e64f4d6e4c8e7a.tar.bz2
Issue3065: Fixed pickling of named tuples. Added tests.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_collections.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index a770155..4f823e3 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -1,12 +1,14 @@
import unittest, doctest
from test import test_support
from collections import namedtuple
+import pickle, cPickle, copy
from collections import Hashable, Iterable, Iterator
from collections import Sized, Container, Callable
from collections import Set, MutableSet
from collections import Mapping, MutableMapping
from collections import Sequence, MutableSequence
+TestNT = namedtuple('TestNT', 'x y z') # type used for pickle tests
class TestNamedTuple(unittest.TestCase):
@@ -108,7 +110,7 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(Dot(1)._replace(d=999), (999,))
self.assertEqual(Dot(1)._fields, ('d',))
- n = 10000
+ n = 5000
import string, random
names = list(set(''.join([random.choice(string.ascii_letters)
for j in range(10)]) for i in range(n)))
@@ -130,6 +132,23 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(b2, tuple(b2_expected))
self.assertEqual(b._fields, tuple(names))
+ def test_pickle(self):
+ p = TestNT(x=10, y=20, z=30)
+ for module in pickle, cPickle:
+ loads = getattr(module, 'loads')
+ dumps = getattr(module, 'dumps')
+ for protocol in -1, 0, 1, 2:
+ q = loads(dumps(p, protocol))
+ self.assertEqual(p, q)
+ self.assertEqual(p._fields, q._fields)
+
+ def test_copy(self):
+ p = TestNT(x=10, y=20, z=30)
+ for copier in copy.copy, copy.deepcopy:
+ q = copier(p)
+ self.assertEqual(p, q)
+ self.assertEqual(p._fields, q._fields)
+
class TestOneTrickPonyABCs(unittest.TestCase):
def test_Hashable(self):