annotate Lib/test/test_pickle.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 9a7317ce6db9
children 680959a3ae2e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14594
a1e2e8d55ef0 Fix Bug #114293:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10476
diff changeset
1 import pickle
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
2 from cStringIO import StringIO
27500
ae31fcb7b58e Temporary hacks to arrange that the pickle tests relying on protocol 2
Tim Peters <tim.peters@gmail.com>
parents: 27458
diff changeset
3
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 21482
diff changeset
4 from test import test_support
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
5
27500
ae31fcb7b58e Temporary hacks to arrange that the pickle tests relying on protocol 2
Tim Peters <tim.peters@gmail.com>
parents: 27458
diff changeset
6 from test.pickletester import AbstractPickleTests
ae31fcb7b58e Temporary hacks to arrange that the pickle tests relying on protocol 2
Tim Peters <tim.peters@gmail.com>
parents: 27458
diff changeset
7 from test.pickletester import AbstractPickleModuleTests
ae31fcb7b58e Temporary hacks to arrange that the pickle tests relying on protocol 2
Tim Peters <tim.peters@gmail.com>
parents: 27458
diff changeset
8 from test.pickletester import AbstractPersistentPicklerTests
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
9 from test.pickletester import AbstractPicklerUnpicklerObjectTests
27500
ae31fcb7b58e Temporary hacks to arrange that the pickle tests relying on protocol 2
Tim Peters <tim.peters@gmail.com>
parents: 27458
diff changeset
10
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 27537
diff changeset
11 class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
12
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
13 def dumps(self, arg, proto=0, fast=0):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
14 # Ignore fast
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
15 return pickle.dumps(arg, proto)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
16
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
17 def loads(self, buf):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
18 # Ignore fast
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
19 return pickle.loads(buf)
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20444
diff changeset
20
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
21 module = pickle
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
22 error = KeyError
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
23
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
24 class PicklerTests(AbstractPickleTests):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
25
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
26 error = KeyError
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
27
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
28 def dumps(self, arg, proto=0, fast=0):
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
29 f = StringIO()
27458
4c03ef06c3ff Rename 'bin' arg to 'proto'. Keep the default at 0 lest the tests
Guido van Rossum <guido@python.org>
parents: 26201
diff changeset
30 p = pickle.Pickler(f, proto)
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
31 if fast:
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
32 p.fast = fast
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
33 p.dump(arg)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
34 f.seek(0)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
35 return f.read()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
36
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
37 def loads(self, buf):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
38 f = StringIO(buf)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
39 u = pickle.Unpickler(f)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
40 return u.load()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
41
26201
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
42 class PersPicklerTests(AbstractPersistentPicklerTests):
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
43
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
44 def dumps(self, arg, proto=0, fast=0):
26201
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
45 class PersPickler(pickle.Pickler):
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
46 def persistent_id(subself, obj):
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
47 return self.persistent_id(obj)
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
48 f = StringIO()
27458
4c03ef06c3ff Rename 'bin' arg to 'proto'. Keep the default at 0 lest the tests
Guido van Rossum <guido@python.org>
parents: 26201
diff changeset
49 p = PersPickler(f, proto)
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
50 if fast:
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
51 p.fast = fast
26201
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
52 p.dump(arg)
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
53 f.seek(0)
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
54 return f.read()
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
55
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
56 def loads(self, buf):
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
57 class PersUnpickler(pickle.Unpickler):
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
58 def persistent_load(subself, obj):
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
59 return self.persistent_load(obj)
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
60 f = StringIO(buf)
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
61 u = PersUnpickler(f)
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
62 return u.load()
7e58a1504872 Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24442
diff changeset
63
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
64 class PicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
65
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
66 pickler_class = pickle.Pickler
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
67 unpickler_class = pickle.Unpickler
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
68
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
69
21482
76225434a6fe Fix the test control support for the pickle & cPickle tests so the tests run
Fred Drake <fdrake@acm.org>
parents: 20547
diff changeset
70 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 27901
diff changeset
71 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 27901
diff changeset
72 PickleTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 27901
diff changeset
73 PicklerTests,
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
74 PersPicklerTests,
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 45468
diff changeset
75 PicklerUnpicklerObjectTests,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 27901
diff changeset
76 )
27537
fdc52b119709 pickle.py has a few doctest'ed internal functions, so run their tests.
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
77 test_support.run_doctest(pickle)
21482
76225434a6fe Fix the test control support for the pickle & cPickle tests so the tests run
Fred Drake <fdrake@acm.org>
parents: 20547
diff changeset
78
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
79 if __name__ == "__main__":
21482
76225434a6fe Fix the test control support for the pickle & cPickle tests so the tests run
Fred Drake <fdrake@acm.org>
parents: 20547
diff changeset
80 test_main()