Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
909843b4
Commit
909843b4
authored
Dec 15, 2014
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #22777: Test pickling with all protocols.
parent
1d5b8243
Changes
30
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
30 changed files
with
725 additions
and
645 deletions
+725
-645
Lib/ctypes/test/test_pickling.py
Lib/ctypes/test/test_pickling.py
+9
-11
Lib/test/audiotests.py
Lib/test/audiotests.py
+3
-2
Lib/test/datetimetester.py
Lib/test/datetimetester.py
+6
-5
Lib/test/seq_tests.py
Lib/test/seq_tests.py
+4
-3
Lib/test/test_array.py
Lib/test/test_array.py
+11
-10
Lib/test/test_bool.py
Lib/test/test_bool.py
+3
-4
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+14
-11
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+17
-16
Lib/test/test_bz2.py
Lib/test/test_bz2.py
+6
-4
Lib/test/test_collections.py
Lib/test/test_collections.py
+36
-45
Lib/test/test_configparser.py
Lib/test/test_configparser.py
+97
-85
Lib/test/test_decimal.py
Lib/test/test_decimal.py
+98
-96
Lib/test/test_deque.py
Lib/test/test_deque.py
+23
-20
Lib/test/test_dict.py
Lib/test/test_dict.py
+50
-47
Lib/test/test_email/test_pickleable.py
Lib/test/test_email/test_pickleable.py
+8
-6
Lib/test/test_enumerate.py
Lib/test/test_enumerate.py
+15
-14
Lib/test/test_functools.py
Lib/test/test_functools.py
+3
-2
Lib/test/test_iter.py
Lib/test/test_iter.py
+17
-16
Lib/test/test_itertools.py
Lib/test/test_itertools.py
+102
-68
Lib/test/test_list.py
Lib/test/test_list.py
+22
-20
Lib/test/test_lzma.py
Lib/test/test_lzma.py
+5
-4
Lib/test/test_memoryio.py
Lib/test/test_memoryio.py
+2
-1
Lib/test/test_minidom.py
Lib/test/test_minidom.py
+38
-37
Lib/test/test_os.py
Lib/test/test_os.py
+14
-8
Lib/test/test_random.py
Lib/test/test_random.py
+8
-6
Lib/test/test_range.py
Lib/test/test_range.py
+24
-22
Lib/test/test_set.py
Lib/test/test_set.py
+25
-23
Lib/test/test_tuple.py
Lib/test/test_tuple.py
+22
-20
Lib/test/test_xml_dom_minicompat.py
Lib/test/test_xml_dom_minicompat.py
+13
-12
Lib/test/test_xml_etree.py
Lib/test/test_xml_etree.py
+30
-27
No files found.
Lib/ctypes/test/test_pickling.py
View file @
909843b4
...
...
@@ -14,9 +14,9 @@ class X(Structure):
class
Y
(
X
):
_fields_
=
[(
"str"
,
c_char_p
)]
class
PickleTest
(
unittest
.
TestCase
)
:
class
PickleTest
:
def
dumps
(
self
,
item
):
return
pickle
.
dumps
(
item
)
return
pickle
.
dumps
(
item
,
self
.
proto
)
def
loads
(
self
,
item
):
return
pickle
.
loads
(
item
)
...
...
@@ -67,17 +67,15 @@ class PickleTest(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
lambda
:
self
.
dumps
(
item
))
def
test_wchar
(
self
):
pickle
.
dumps
(
c_char
(
b"x"
))
self
.
dumps
(
c_char
(
b"x"
))
# Issue 5049
pickle
.
dumps
(
c_wchar
(
"x"
))
self
.
dumps
(
c_wchar
(
"x"
))
class
PickleTest_1
(
PickleTest
):
def
dumps
(
self
,
item
):
return
pickle
.
dumps
(
item
,
1
)
class
PickleTest_2
(
PickleTest
):
def
dumps
(
self
,
item
):
return
pickle
.
dumps
(
item
,
2
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
name
=
'PickleTest_%s'
%
proto
globals
()[
name
]
=
type
(
name
,
(
PickleTest
,
unittest
.
TestCase
),
{
'proto'
:
proto
})
if
__name__
==
"__main__"
:
unittest
.
main
()
Lib/test/audiotests.py
View file @
909843b4
...
...
@@ -45,7 +45,8 @@ class AudioTests:
self
.
assertEqual
(
params
.
comptype
,
comptype
)
self
.
assertEqual
(
params
.
compname
,
compname
)
dump
=
pickle
.
dumps
(
params
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
dump
=
pickle
.
dumps
(
params
,
proto
)
self
.
assertEqual
(
pickle
.
loads
(
dump
),
params
)
...
...
Lib/test/datetimetester.py
View file @
909843b4
...
...
@@ -1682,7 +1682,8 @@ class TestDateTime(TestDate):
def
test_more_pickling
(
self
):
a
=
self
.
theclass
(
2003
,
2
,
7
,
16
,
48
,
37
,
444116
)
s
=
pickle
.
dumps
(
a
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
s
=
pickle
.
dumps
(
a
,
proto
)
b
=
pickle
.
loads
(
s
)
self
.
assertEqual
(
b
.
year
,
2003
)
self
.
assertEqual
(
b
.
month
,
2
)
...
...
Lib/test/seq_tests.py
View file @
909843b4
...
...
@@ -392,6 +392,7 @@ class CommonTest(unittest.TestCase):
def
test_pickle
(
self
):
lst
=
self
.
type2test
([
4
,
5
,
6
,
7
])
lst2
=
pickle
.
loads
(
pickle
.
dumps
(
lst
))
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
lst2
=
pickle
.
loads
(
pickle
.
dumps
(
lst
,
proto
))
self
.
assertEqual
(
lst2
,
lst
)
self
.
assertNotEqual
(
id
(
lst2
),
id
(
lst
))
Lib/test/test_array.py
View file @
909843b4
...
...
@@ -285,8 +285,9 @@ class BaseTest:
def
test_iterator_pickle
(
self
):
data
=
array
.
array
(
self
.
typecode
,
self
.
example
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
orgit
=
iter
(
data
)
d
=
pickle
.
dumps
(
orgit
)
d
=
pickle
.
dumps
(
orgit
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
orgit
),
type
(
it
))
self
.
assertEqual
(
list
(
it
),
list
(
data
))
...
...
@@ -294,7 +295,7 @@ class BaseTest:
if
len
(
data
):
it
=
pickle
.
loads
(
d
)
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
self
.
assertEqual
(
list
(
it
),
list
(
data
)[
1
:])
def
test_insert
(
self
):
...
...
Lib/test/test_bool.py
View file @
909843b4
...
...
@@ -269,10 +269,9 @@ class BoolTest(unittest.TestCase):
def
test_pickle
(
self
):
import
pickle
self
.
assertIs
(
pickle
.
loads
(
pickle
.
dumps
(
True
)),
True
)
self
.
assertIs
(
pickle
.
loads
(
pickle
.
dumps
(
False
)),
False
)
self
.
assertIs
(
pickle
.
loads
(
pickle
.
dumps
(
True
,
True
)),
True
)
self
.
assertIs
(
pickle
.
loads
(
pickle
.
dumps
(
False
,
True
)),
False
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
self
.
assertIs
(
pickle
.
loads
(
pickle
.
dumps
(
True
,
proto
)),
True
)
self
.
assertIs
(
pickle
.
loads
(
pickle
.
dumps
(
False
,
proto
)),
False
)
def
test_picklevalues
(
self
):
# Test for specific backwards-compatible pickle values
...
...
Lib/test/test_builtin.py
View file @
909843b4
...
...
@@ -121,9 +121,9 @@ def map_char(arg):
class
BuiltinTest
(
unittest
.
TestCase
):
# Helper to check picklability
def
check_iter_pickle
(
self
,
it
,
seq
):
def
check_iter_pickle
(
self
,
it
,
seq
,
proto
):
itorg
=
it
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
list
(
it
),
seq
)
...
...
@@ -134,7 +134,7 @@ class BuiltinTest(unittest.TestCase):
next
(
it
)
except
StopIteration
:
return
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
list
(
it
),
seq
[
1
:])
...
...
@@ -636,9 +636,10 @@ class BuiltinTest(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
list
,
filter
(
42
,
(
1
,
2
)))
def
test_filter_pickle
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
f1
=
filter
(
filter_char
,
"abcdeabcde"
)
f2
=
filter
(
filter_char
,
"abcdeabcde"
)
self
.
check_iter_pickle
(
f1
,
list
(
f2
)
)
self
.
check_iter_pickle
(
f1
,
list
(
f2
),
proto
)
def
test_getattr
(
self
):
self
.
assertTrue
(
getattr
(
sys
,
'stdout'
)
is
sys
.
stdout
)
...
...
@@ -834,9 +835,10 @@ class BuiltinTest(unittest.TestCase):
self
.
assertRaises
(
RuntimeError
,
list
,
map
(
badfunc
,
range
(
5
)))
def
test_map_pickle
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
m1
=
map
(
map_char
,
"Is this the real life?"
)
m2
=
map
(
map_char
,
"Is this the real life?"
)
self
.
check_iter_pickle
(
m1
,
list
(
m2
)
)
self
.
check_iter_pickle
(
m1
,
list
(
m2
),
proto
)
def
test_max
(
self
):
self
.
assertEqual
(
max
(
'123123'
),
'3'
)
...
...
@@ -1433,8 +1435,9 @@ class BuiltinTest(unittest.TestCase):
a
=
(
1
,
2
,
3
)
b
=
(
4
,
5
,
6
)
t
=
[(
1
,
4
),
(
2
,
5
),
(
3
,
6
)]
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
z1
=
zip
(
a
,
b
)
self
.
check_iter_pickle
(
z1
,
t
)
self
.
check_iter_pickle
(
z1
,
t
,
proto
)
def
test_format
(
self
):
# Test the basic machinery of the format() builtin. Don't test
...
...
Lib/test/test_bytes.py
View file @
909843b4
...
...
@@ -555,10 +555,11 @@ class BaseBytesTest:
self.assertEqual(b, q)
def test_iterator_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
for b in b"", b"a", b"abc", b"
\
xff
ab
\
x80
", b"
\
0
\
0
\
377
\
0
\
0
":
it = itorg = iter(self.type2test(b))
data = list(self.type2test(b))
d = pickle.dumps(it
)
d = pickle.dumps(it, proto
)
it = pickle.loads(d)
self.assertEqual(type(itorg), type(it))
self.assertEqual(list(it), data)
...
...
@@ -568,7 +569,7 @@ class BaseBytesTest:
next(it)
except StopIteration:
continue
d = pickle.dumps(it
)
d = pickle.dumps(it, proto
)
it = pickle.loads(d)
self.assertEqual(list(it), data[1:])
...
...
Lib/test/test_bz2.py
View file @
909843b4
...
...
@@ -646,8 +646,9 @@ class BZ2CompressorTest(BaseTest):
data
=
None
def
testPickle
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
with
self
.
assertRaises
(
TypeError
):
pickle
.
dumps
(
BZ2Compressor
()
)
pickle
.
dumps
(
BZ2Compressor
(),
proto
)
class
BZ2DecompressorTest
(
BaseTest
):
...
...
@@ -702,8 +703,9 @@ class BZ2DecompressorTest(BaseTest):
decompressed
=
None
def
testPickle
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
with
self
.
assertRaises
(
TypeError
):
pickle
.
dumps
(
BZ2Decompressor
()
)
pickle
.
dumps
(
BZ2Decompressor
(),
proto
)
class
CompressDecompressTest
(
BaseTest
):
...
...
Lib/test/test_collections.py
View file @
909843b4
...
...
@@ -63,10 +63,17 @@ class TestChainMap(unittest.TestCase):
for
m1
,
m2
in
zip
(
d
.
maps
[
1
:],
e
.
maps
[
1
:]):
self
.
assertIs
(
m1
,
m2
)
for
e
in
[
pickle
.
loads
(
pickle
.
dumps
(
d
)),
copy
.
deepcopy
(
d
),
# check deep copies
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
e
=
pickle
.
loads
(
pickle
.
dumps
(
d
,
proto
))
self
.
assertEqual
(
d
,
e
)
self
.
assertEqual
(
d
.
maps
,
e
.
maps
)
self
.
assertIsNot
(
d
,
e
)
for
m1
,
m2
in
zip
(
d
.
maps
,
e
.
maps
):
self
.
assertIsNot
(
m1
,
m2
,
e
)
for
e
in
[
copy
.
deepcopy
(
d
),
eval
(
repr
(
d
))
]:
# check deep copies
]:
self
.
assertEqual
(
d
,
e
)
self
.
assertEqual
(
d
.
maps
,
e
.
maps
)
self
.
assertIsNot
(
d
,
e
)
...
...
@@ -1110,28 +1117,21 @@ class TestCounter(unittest.TestCase):
# Check that counters are copyable, deepcopyable, picklable, and
#have a repr/eval round-trip
words
=
Counter
(
'which witch had which witches wrist watch'
.
split
())
update_test
=
Counter
()
update_test
.
update
(
words
)
for
label
,
dup
in
[
(
'words.copy()'
,
words
.
copy
()),
(
'copy.copy(words)'
,
copy
.
copy
(
words
)),
(
'copy.deepcopy(words)'
,
copy
.
deepcopy
(
words
)),
(
'pickle.loads(pickle.dumps(words, 0))'
,
pickle
.
loads
(
pickle
.
dumps
(
words
,
0
))),
(
'pickle.loads(pickle.dumps(words, 1))'
,
pickle
.
loads
(
pickle
.
dumps
(
words
,
1
))),
(
'pickle.loads(pickle.dumps(words, 2))'
,
pickle
.
loads
(
pickle
.
dumps
(
words
,
2
))),
(
'pickle.loads(pickle.dumps(words, -1))'
,
pickle
.
loads
(
pickle
.
dumps
(
words
,
-
1
))),
(
'eval(repr(words))'
,
eval
(
repr
(
words
))),
(
'update_test'
,
update_test
),
(
'Counter(words)'
,
Counter
(
words
)),
]:
with
self
.
subTest
(
label
=
label
):
def
check
(
dup
):
msg
=
"
\
n
copy: %s
\
n
words: %s"
%
(
dup
,
words
)
self
.
assertIsNot
(
dup
,
words
,
msg
)
self
.
assertEqual
(
dup
,
words
)
check
(
words
.
copy
())
check
(
copy
.
copy
(
words
))
check
(
copy
.
deepcopy
(
words
))
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
with
self
.
subTest
(
proto
=
proto
):
check
(
pickle
.
loads
(
pickle
.
dumps
(
words
,
proto
)))
check
(
eval
(
repr
(
words
)))
update_test
=
Counter
()
update_test
.
update
(
words
)
check
(
update_test
)
check
(
Counter
(
words
))
def
test_copy_subclass
(
self
):
class
MyCounter
(
Counter
):
...
...
@@ -1433,30 +1433,21 @@ class TestOrderedDict(unittest.TestCase):
# and have a repr/eval round-trip
pairs
=
[(
'c'
,
1
),
(
'b'
,
2
),
(
'a'
,
3
),
(
'd'
,
4
),
(
'e'
,
5
),
(
'f'
,
6
)]
od
=
OrderedDict
(
pairs
)
update_test
=
OrderedDict
()
update_test
.
update
(
od
)
for
label
,
dup
in
[
(
'od.copy()'
,
od
.
copy
()),
(
'copy.copy(od)'
,
copy
.
copy
(
od
)),
(
'copy.deepcopy(od)'
,
copy
.
deepcopy
(
od
)),
(
'pickle.loads(pickle.dumps(od, 0))'
,
pickle
.
loads
(
pickle
.
dumps
(
od
,
0
))),
(
'pickle.loads(pickle.dumps(od, 1))'
,
pickle
.
loads
(
pickle
.
dumps
(
od
,
1
))),
(
'pickle.loads(pickle.dumps(od, 2))'
,
pickle
.
loads
(
pickle
.
dumps
(
od
,
2
))),
(
'pickle.loads(pickle.dumps(od, 3))'
,
pickle
.
loads
(
pickle
.
dumps
(
od
,
3
))),
(
'pickle.loads(pickle.dumps(od, -1))'
,
pickle
.
loads
(
pickle
.
dumps
(
od
,
-
1
))),
(
'eval(repr(od))'
,
eval
(
repr
(
od
))),
(
'update_test'
,
update_test
),
(
'OrderedDict(od)'
,
OrderedDict
(
od
)),
]:
with
self
.
subTest
(
label
=
label
):
def
check
(
dup
):
msg
=
"
\
n
copy: %s
\
n
od: %s"
%
(
dup
,
od
)
self
.
assertIsNot
(
dup
,
od
,
msg
)
self
.
assertEqual
(
dup
,
od
)
check
(
od
.
copy
())
check
(
copy
.
copy
(
od
))
check
(
copy
.
deepcopy
(
od
))
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
with
self
.
subTest
(
proto
=
proto
):
check
(
pickle
.
loads
(
pickle
.
dumps
(
od
,
proto
)))
check
(
eval
(
repr
(
od
)))
update_test
=
OrderedDict
()
update_test
.
update
(
od
)
check
(
update_test
)
check
(
OrderedDict
(
od
))
def
test_yaml_linkage
(
self
):
# Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
...
...
Lib/test/test_configparser.py
View file @
909843b4
...
...
@@ -1591,7 +1591,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def
test_error
(
self
):
import
pickle
e1
=
configparser
.
Error
(
'value'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
repr
(
e1
),
repr
(
e2
))
...
...
@@ -1599,7 +1600,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def
test_nosectionerror
(
self
):
import
pickle
e1
=
configparser
.
NoSectionError
(
'section'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1609,7 +1611,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def
test_nooptionerror
(
self
):
import
pickle
e1
=
configparser
.
NoOptionError
(
'option'
,
'section'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1620,7 +1623,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def
test_duplicatesectionerror
(
self
):
import
pickle
e1
=
configparser
.
DuplicateSectionError
(
'section'
,
'source'
,
123
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1633,7 +1637,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
import
pickle
e1
=
configparser
.
DuplicateOptionError
(
'section'
,
'option'
,
'source'
,
123
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1646,7 +1651,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def
test_interpolationerror
(
self
):
import
pickle
e1
=
configparser
.
InterpolationError
(
'option'
,
'section'
,
'msg'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1658,7 +1664,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
import
pickle
e1
=
configparser
.
InterpolationMissingOptionError
(
'option'
,
'section'
,
'rawval'
,
'reference'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1670,7 +1677,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def
test_interpolationsyntaxerror
(
self
):
import
pickle
e1
=
configparser
.
InterpolationSyntaxError
(
'option'
,
'section'
,
'msg'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1682,7 +1690,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
import
pickle
e1
=
configparser
.
InterpolationDepthError
(
'option'
,
'section'
,
'rawval'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1696,7 +1705,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
e1
.
append
(
1
,
'line1'
)
e1
.
append
(
2
,
'line2'
)
e1
.
append
(
3
,
'line3'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1707,7 +1717,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
e1
.
append
(
1
,
'line1'
)
e1
.
append
(
2
,
'line2'
)
e1
.
append
(
3
,
'line3'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
@@ -1718,7 +1729,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def
test_missingsectionheadererror
(
self
):
import
pickle
e1
=
configparser
.
MissingSectionHeaderError
(
'filename'
,
123
,
'line'
)
pickled
=
pickle
.
dumps
(
e1
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
pickled
=
pickle
.
dumps
(
e1
,
proto
)
e2
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
e1
.
message
,
e2
.
message
)
self
.
assertEqual
(
e1
.
args
,
e2
.
args
)
...
...
Lib/test/test_decimal.py
View file @
909843b4
...
...
@@ -2406,6 +2406,7 @@ class PythonAPItests(unittest.TestCase):
self
.
assertNotIsInstance
(
Decimal
(
0
),
numbers
.
Real
)
def
test_pickle
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
Decimal
=
self
.
decimal
.
Decimal
savedecimal
=
sys
.
modules
[
'decimal'
]
...
...
@@ -2413,7 +2414,7 @@ class PythonAPItests(unittest.TestCase):
# Round trip
sys
.
modules
[
'decimal'
]
=
self
.
decimal
d
=
Decimal
(
'-3.141590000'
)
p
=
pickle
.
dumps
(
d
)
p
=
pickle
.
dumps
(
d
,
proto
)
e
=
pickle
.
loads
(
p
)
self
.
assertEqual
(
d
,
e
)
...
...
@@ -2423,14 +2424,14 @@ class PythonAPItests(unittest.TestCase):
y
=
P
.
Decimal
(
'-3.123e81723'
)
sys
.
modules
[
'decimal'
]
=
C
sx
=
pickle
.
dumps
(
x
)
sx
=
pickle
.
dumps
(
x
,
proto
)
sys
.
modules
[
'decimal'
]
=
P
r
=
pickle
.
loads
(
sx
)
self
.
assertIsInstance
(
r
,
P
.
Decimal
)
self
.
assertEqual
(
r
,
y
)
sys
.
modules
[
'decimal'
]
=
P
sy
=
pickle
.
dumps
(
y
)
sy
=
pickle
.
dumps
(
y
,
proto
)
sys
.
modules
[
'decimal'
]
=
C
r
=
pickle
.
loads
(
sy
)
self
.
assertIsInstance
(
r
,
C
.
Decimal
)
...
...
@@ -2440,14 +2441,14 @@ class PythonAPItests(unittest.TestCase):
y
=
P
.
Decimal
(
'-3.123e81723'
).
as_tuple
()
sys
.
modules
[
'decimal'
]
=
C
sx
=
pickle
.
dumps
(
x
)
sx
=
pickle
.
dumps
(
x
,
proto
)
sys
.
modules
[
'decimal'
]
=
P
r
=
pickle
.
loads
(
sx
)
self
.
assertIsInstance
(
r
,
P
.
DecimalTuple
)
self
.
assertEqual
(
r
,
y
)
sys
.
modules
[
'decimal'
]
=
P
sy
=
pickle
.
dumps
(
y
)
sy
=
pickle
.
dumps
(
y
,
proto
)
sys
.
modules
[
'decimal'
]
=
C
r
=
pickle
.
loads
(
sy
)
self
.
assertIsInstance
(
r
,
C
.
DecimalTuple
)
...
...
@@ -2777,6 +2778,7 @@ class ContextAPItests(unittest.TestCase):
def
test_pickle
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
Context
=
self
.
decimal
.
Context
savedecimal
=
sys
.
modules
[
'decimal'
]
...
...
@@ -2784,7 +2786,7 @@ class ContextAPItests(unittest.TestCase):
# Round trip
sys
.
modules
[
'decimal'
]
=
self
.
decimal
c
=
Context
()
e
=
pickle
.
loads
(
pickle
.
dumps
(
c
))
e
=
pickle
.
loads
(
pickle
.
dumps
(
c
,
proto
))
self
.
assertEqual
(
c
.
prec
,
e
.
prec
)
self
.
assertEqual
(
c
.
Emin
,
e
.
Emin
)
...
...
@@ -2817,7 +2819,7 @@ class ContextAPItests(unittest.TestCase):
flags
=
OrderedSignals
[
dumper
][:
fi
],
traps
=
OrderedSignals
[
dumper
][:
ti
]
)
s
=
pickle
.
dumps
(
c
)
s
=
pickle
.
dumps
(
c
,
proto
)
# The other module loads
sys
.
modules
[
'decimal'
]
=
loader
...
...
Lib/test/test_deque.py
View file @
909843b4
...
...
@@ -474,15 +474,16 @@ class TestBasic(unittest.TestCase):
def
test_iterator_pickle
(
self
):
data
=
deque
(
range
(
200
))
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
it
=
itorg
=
iter
(
data
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
list
(
it
),
list
(
data
))
it
=
pickle
.
loads
(
d
)
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
self
.
assertEqual
(
list
(
it
),
list
(
data
)[
1
:])
def
test_deepcopy
(
self
):
...
...
@@ -614,7 +615,8 @@ class TestSubclass(unittest.TestCase):
self
.
assertEqual
(
type
(
d
),
type
(
e
))
self
.
assertEqual
(
list
(
d
),
list
(
e
))
s
=
pickle
.
dumps
(
d
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
s
=
pickle
.
dumps
(
d
,
proto
)
e
=
pickle
.
loads
(
s
)
self
.
assertNotEqual
(
id
(
d
),
id
(
e
))
self
.
assertEqual
(
type
(
d
),
type
(
e
))
...
...
@@ -630,7 +632,8 @@ class TestSubclass(unittest.TestCase):
self
.
assertEqual
(
type
(
d
),
type
(
e
))
self
.
assertEqual
(
list
(
d
),
list
(
e
))
s
=
pickle
.
dumps
(
d
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
s
=
pickle
.
dumps
(
d
,
proto
)
e
=
pickle
.
loads
(
s
)
self
.
assertNotEqual
(
id
(
d
),
id
(
e
))
self
.
assertEqual
(
type
(
d
),
type
(
e
))
...
...
Lib/test/test_dict.py
View file @
909843b4
...
...
@@ -837,9 +837,10 @@ class DictTest(unittest.TestCase):
self
.
_tracked
(
MyDict
())
def
test_iterator_pickling
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
data
=
{
1
:
"a"
,
2
:
"b"
,
3
:
"c"
}
it
=
iter
(
data
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
sorted
(
it
),
sorted
(
data
))
...
...
@@ -847,44 +848,46 @@ class DictTest(unittest.TestCase):
try
:
drop
=
next
(
it
)
except
StopIteration
:
return
d
=
pickle
.
dumps
(
it
)
continue
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
del
data
[
drop
]
self
.
assertEqual
(
sorted
(
it
),
sorted
(
data
))
def
test_itemiterator_pickling
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
data
=
{
1
:
"a"
,
2
:
"b"
,
3
:
"c"
}
# dictviews aren't picklable, only their iterators
itorg
=
iter
(
data
.
items
())
d
=
pickle
.
dumps
(
itorg
)
d
=
pickle
.
dumps
(
itorg
,
proto
)
it
=
pickle
.
loads
(
d
)
# note that the type of type of the unpickled iterator
# is not necessarily the same as the original. It is
# merely an object supporting the iterator protocol, yielding
# the same objects as the original one.
# self.assertEqual(type(itorg), type(it))
self
.
assertTrue
(
isinstance
(
it
,
collections
.
abc
.
Iterator
)
)
self
.
assertIsInstance
(
it
,
collections
.
abc
.
Iterator
)
self
.
assertEqual
(
dict
(
it
),
data
)
it
=
pickle
.
loads
(
d
)
drop
=
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
del
data
[
drop
[
0
]]
self
.
assertEqual
(
dict
(
it
),
data
)
def
test_valuesiterator_pickling
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
):
data
=
{
1
:
"a"
,
2
:
"b"
,
3
:
"c"
}
# data.values() isn't picklable, only its iterator
it
=
iter
(
data
.
values
())
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
sorted
(
list
(
it
)),
sorted
(
list
(
data
.
values
())))
it
=
pickle
.
loads
(
d
)
drop
=
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
values
=
list
(
it
)
+
[
drop
]
self
.
assertEqual
(
sorted
(
values
),
sorted
(
list
(
data
.
values
())))
...
...
Lib/test/test_email/test_pickleable.py
View file @
909843b4
...
...
@@ -30,7 +30,8 @@ class TestPickleCopyHeader(TestEmailBase):
def
header_as_pickle
(
self
,
name
,
value
):
header
=
self
.
header_factory
(
name
,
value
)
p
=
pickle
.
dumps
(
header
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
p
=
pickle
.
dumps
(
header
,
proto
)
h
=
pickle
.
loads
(
p
)
self
.
assertEqual
(
str
(
h
),
str
(
header
))
...
...
@@ -65,7 +66,8 @@ class TestPickleCopyMessage(TestEmailBase):
self
.
assertEqual
(
msg2
.
as_string
(),
msg
.
as_string
())
def
msg_as_pickle
(
self
,
msg
):
p
=
pickle
.
dumps
(
msg
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
p
=
pickle
.
dumps
(
msg
,
proto
)
msg2
=
pickle
.
loads
(
p
)
self
.
assertEqual
(
msg2
.
as_string
(),
msg
.
as_string
())
...
...
Lib/test/test_enumerate.py
View file @
909843b4
...
...
@@ -66,7 +66,8 @@ class N:
class
PickleTest
:
# Helper to check picklability
def
check_pickle
(
self
,
itorg
,
seq
):
d
=
pickle
.
dumps
(
itorg
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
d
=
pickle
.
dumps
(
itorg
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
list
(
it
),
seq
)
...
...
@@ -76,8 +77,8 @@ class PickleTest:
next
(
it
)
except
StopIteration
:
self
.
assertFalse
(
seq
[
1
:])
return
d
=
pickle
.
dumps
(
it
)
continue
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
list
(
it
),
seq
[
1
:])
...
...
Lib/test/test_functools.py
View file @
909843b4
...
...
@@ -182,7 +182,8 @@ class TestPartialC(TestPartial, unittest.TestCase):
def
test_pickle
(
self
):
f
=
self
.
partial
(
signature
,
'asdf'
,
bar
=
True
)
f
.
add_something_to__dict__
=
True
f_copy
=
pickle
.
loads
(
pickle
.
dumps
(
f
))
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
f_copy
=
pickle
.
loads
(
pickle
.
dumps
(
f
,
proto
))
self
.
assertEqual
(
signature
(
f
),
signature
(
f_copy
))
# Issue 6083: Reference counting bug
...
...
Lib/test/test_iter.py
View file @
909843b4
...
...
@@ -76,7 +76,8 @@ class TestCase(unittest.TestCase):
# Helper to check picklability
def
check_pickle
(
self
,
itorg
,
seq
):
d
=
pickle
.
dumps
(
itorg
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
d
=
pickle
.
dumps
(
itorg
,
proto
)
it
=
pickle
.
loads
(
d
)
# Cannot assert type equality because dict iterators unpickle as list
# iterators.
...
...
@@ -88,8 +89,8 @@ class TestCase(unittest.TestCase):
try
:
next
(
it
)
except
StopIteration
:
return
d
=
pickle
.
dumps
(
it
)
continue
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
list
(
it
),
seq
[
1
:])
...
...
Lib/test/test_itertools.py
View file @
909843b4
This diff is collapsed.
Click to expand it.
Lib/test/test_list.py
View file @
909843b4
...
...
@@ -74,28 +74,30 @@ class ListTest(list_tests.CommonTest):
# Userlist iterators don't support pickling yet since
# they are based on generators.
data
=
self
.
type2test
([
4
,
5
,
6
,
7
])
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
it
=
itorg
=
iter
(
data
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
data
))
it
=
pickle
.
loads
(
d
)
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
data
)[
1
:])
def
test_reversed_pickle
(
self
):
data
=
self
.
type2test
([
4
,
5
,
6
,
7
])
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
it
=
itorg
=
reversed
(
data
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
reversed
(
data
)))
it
=
pickle
.
loads
(
d
)
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
reversed
(
data
))[
1
:])
def
test_no_comdat_folding
(
self
):
...
...
Lib/test/test_lzma.py
View file @
909843b4
...
...
@@ -220,10 +220,11 @@ class CompressorDecompressorTestCase(unittest.TestCase):
# Pickling raises an exception; there's no way to serialize an lzma_stream.
def
test_pickle
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
with
self
.
assertRaises
(
TypeError
):
pickle
.
dumps
(
LZMACompressor
()
)
pickle
.
dumps
(
LZMACompressor
(),
proto
)
with
self
.
assertRaises
(
TypeError
):
pickle
.
dumps
(
LZMADecompressor
()
)
pickle
.
dumps
(
LZMADecompressor
(),
proto
)
class
CompressDecompressFunctionTestCase
(
unittest
.
TestCase
):
...
...
Lib/test/test_memoryio.py
View file @
909843b4
...
...
@@ -366,13 +366,14 @@ class MemoryTestMixin:
# the module-level.
import
__main__
PickleTestMemIO
.
__module__
=
'__main__'
PickleTestMemIO
.
__qualname__
=
PickleTestMemIO
.
__name__
__main__
.
PickleTestMemIO
=
PickleTestMemIO
submemio
=
PickleTestMemIO
(
buf
,
80
)
submemio
.
seek
(
2
)
# We only support pickle protocol 2 and onward since we use extended
# __reduce__ API of PEP 307 to provide pickling support.
for
proto
in
range
(
2
,
pickle
.
HIGHEST_PROTOCOL
):
for
proto
in
range
(
2
,
pickle
.
HIGHEST_PROTOCOL
+
1
):
for
obj
in
(
memio
,
submemio
):
obj2
=
pickle
.
loads
(
pickle
.
dumps
(
obj
,
protocol
=
proto
))
self
.
assertEqual
(
obj
.
getvalue
(),
obj2
.
getvalue
())
...
...
Lib/test/test_minidom.py
View file @
909843b4
...
...
@@ -1468,7 +1468,8 @@ class MinidomTest(unittest.TestCase):
" <!ENTITY ent SYSTEM 'http://xml.python.org/entity'>
\
n
"
"]><doc attr='value'> text
\
n
"
"<?pi sample?> <!-- comment --> <e/> </doc>"
)
s
=
pickle
.
dumps
(
doc
)
for
proto
in
range
(
2
,
pickle
.
HIGHEST_PROTOCOL
+
1
):
s
=
pickle
.
dumps
(
doc
,
proto
)
doc2
=
pickle
.
loads
(
s
)
stack
=
[(
doc
,
doc2
)]
while
stack
:
...
...
Lib/test/test_os.py
View file @
909843b4
...
...
@@ -265,8 +265,11 @@ class StatAttributeTests(unittest.TestCase):
def
test_stat_result_pickle
(
self
):
result
=
os
.
stat
(
self
.
fname
)
p
=
pickle
.
dumps
(
result
)
self
.
assertIn
(
b'
\
x03
cos
\
n
stat_result
\
n
'
,
p
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
p
=
pickle
.
dumps
(
result
,
proto
)
self
.
assertIn
(
b'stat_result'
,
p
)
if
proto
<
4
:
self
.
assertIn
(
b'cos
\
n
stat_result
\
n
'
,
p
)
unpickled
=
pickle
.
loads
(
p
)
self
.
assertEqual
(
result
,
unpickled
)
...
...
@@ -324,8 +327,11 @@ class StatAttributeTests(unittest.TestCase):
if
e
.
errno
==
errno
.
ENOSYS
:
self
.
skipTest
(
'os.statvfs() failed with ENOSYS'
)
p
=
pickle
.
dumps
(
result
)
self
.
assertIn
(
b'
\
x03
cos
\
n
statvfs_result
\
n
'
,
p
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
p
=
pickle
.
dumps
(
result
,
proto
)
self
.
assertIn
(
b'statvfs_result'
,
p
)
if
proto
<
4
:
self
.
assertIn
(
b'cos
\
n
statvfs_result
\
n
'
,
p
)
unpickled
=
pickle
.
loads
(
p
)
self
.
assertEqual
(
result
,
unpickled
)
...
...
Lib/test/test_random.py
View file @
909843b4
...
...
@@ -159,7 +159,8 @@ class TestBasicOps:
self
.
assertEqual
(
y1
,
y2
)
def
test_pickling
(
self
):
state
=
pickle
.
dumps
(
self
.
gen
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
state
=
pickle
.
dumps
(
self
.
gen
,
proto
)
origseq
=
[
self
.
gen
.
random
()
for
i
in
range
(
10
)]
newgen
=
pickle
.
loads
(
state
)
restoredseq
=
[
newgen
.
random
()
for
i
in
range
(
10
)]
...
...
@@ -215,7 +216,8 @@ class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
self
.
assertEqual
(
self
.
gen
.
gauss_next
,
None
)
def
test_pickling
(
self
):
self
.
assertRaises
(
NotImplementedError
,
pickle
.
dumps
,
self
.
gen
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
self
.
assertRaises
(
NotImplementedError
,
pickle
.
dumps
,
self
.
gen
,
proto
)
def
test_53_bits_per_float
(
self
):
# This should pass whenever a C double has 53 bit precision.
...
...
Lib/test/test_range.py
View file @
909843b4
...
...
@@ -366,7 +366,7 @@ class RangeTest(unittest.TestCase):
it
=
itorg
=
iter
(
range
(
*
t
))
data
=
list
(
range
(
*
t
))
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
list
(
it
),
data
)
...
...
@@ -376,30 +376,32 @@ class RangeTest(unittest.TestCase):
next
(
it
)
except
StopIteration
:
continue
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
list
(
it
),
data
[
1
:])
def
test_exhausted_iterator_pickling
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
r
=
range
(
2
**
65
,
2
**
65
+
2
)
i
=
iter
(
r
)
while
True
:
r
=
next
(
i
)
if
r
==
2
**
65
+
1
:
break
d
=
pickle
.
dumps
(
i
)
d
=
pickle
.
dumps
(
i
,
proto
)
i2
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
list
(
i
),
[])
self
.
assertEqual
(
list
(
i2
),
[])
def
test_large_exhausted_iterator_pickling
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
r
=
range
(
20
)
i
=
iter
(
r
)
while
True
:
r
=
next
(
i
)
if
r
==
19
:
break
d
=
pickle
.
dumps
(
i
)
d
=
pickle
.
dumps
(
i
,
proto
)
i2
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
list
(
i
),
[])
self
.
assertEqual
(
list
(
i2
),
[])
...
...
Lib/test/test_set.py
View file @
909843b4
...
...
@@ -231,27 +231,28 @@ class TestJointOps:
self
.
assertEqual
(
self
.
s
,
dup
,
"%s != %s"
%
(
self
.
s
,
dup
))
if
type
(
self
.
s
)
not
in
(
set
,
frozenset
):
self
.
s
.
x
=
10
p
=
pickle
.
dumps
(
self
.
s
)
p
=
pickle
.
dumps
(
self
.
s
,
i
)
dup
=
pickle
.
loads
(
p
)
self
.
assertEqual
(
self
.
s
.
x
,
dup
.
x
)
def
test_iterator_pickling
(
self
):
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
itorg
=
iter
(
self
.
s
)
data
=
self
.
thetype
(
self
.
s
)
d
=
pickle
.
dumps
(
itorg
)
d
=
pickle
.
dumps
(
itorg
,
proto
)
it
=
pickle
.
loads
(
d
)
# Set iterators unpickle as list iterators due to the
# undefined order of set items.
# self.assertEqual(type(itorg), type(it))
self
.
assertTrue
(
isinstance
(
it
,
collections
.
abc
.
Iterator
)
)
self
.
assertIsInstance
(
it
,
collections
.
abc
.
Iterator
)
self
.
assertEqual
(
self
.
thetype
(
it
),
data
)
it
=
pickle
.
loads
(
d
)
try
:
drop
=
next
(
it
)
except
StopIteration
:
return
d
=
pickle
.
dumps
(
it
)
continue
d
=
pickle
.
dumps
(
it
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
self
.
thetype
(
it
),
data
-
self
.
thetype
((
drop
,)))
...
...
@@ -851,7 +852,8 @@ class TestBasicOps:
self
.
assertEqual
(
setiter
.
__length_hint__
(),
len
(
self
.
set
))
def
test_pickling
(
self
):
p
=
pickle
.
dumps
(
self
.
set
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
p
=
pickle
.
dumps
(
self
.
set
,
proto
)
copy
=
pickle
.
loads
(
p
)
self
.
assertEqual
(
self
.
set
,
copy
,
"%s != %s"
%
(
self
.
set
,
copy
))
...
...
Lib/test/test_tuple.py
View file @
909843b4
...
...
@@ -169,28 +169,30 @@ class TupleTest(seq_tests.CommonTest):
# Userlist iterators don't support pickling yet since
# they are based on generators.
data
=
self
.
type2test
([
4
,
5
,
6
,
7
])
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
itorg
=
iter
(
data
)
d
=
pickle
.
dumps
(
itorg
)
d
=
pickle
.
dumps
(
itorg
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
data
))
it
=
pickle
.
loads
(
d
)
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
data
)[
1
:])
def
test_reversed_pickle
(
self
):
data
=
self
.
type2test
([
4
,
5
,
6
,
7
])
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
itorg
=
reversed
(
data
)
d
=
pickle
.
dumps
(
itorg
)
d
=
pickle
.
dumps
(
itorg
,
proto
)
it
=
pickle
.
loads
(
d
)
self
.
assertEqual
(
type
(
itorg
),
type
(
it
))
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
reversed
(
data
)))
it
=
pickle
.
loads
(
d
)
next
(
it
)
d
=
pickle
.
dumps
(
it
)
d
=
pickle
.
dumps
(
it
,
proto
)
self
.
assertEqual
(
self
.
type2test
(
it
),
self
.
type2test
(
reversed
(
data
))[
1
:])
def
test_no_comdat_folding
(
self
):
...
...
Lib/test/test_xml_dom_minicompat.py
View file @
909843b4
...
...
@@ -84,16 +84,17 @@ class NodeListTestCase(unittest.TestCase):
def
test_nodelist_pickle_roundtrip
(
self
):
# Test pickling and unpickling of a NodeList.
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
# Empty NodeList.
node_list
=
NodeList
()
pickled
=
pickle
.
dumps
(
node_list
)
pickled
=
pickle
.
dumps
(
node_list
,
proto
)
unpickled
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
unpickled
,
node_list
)
# Non-empty NodeList.
node_list
.
append
(
1
)
node_list
.
append
(
2
)
pickled
=
pickle
.
dumps
(
node_list
)
pickled
=
pickle
.
dumps
(
node_list
,
proto
)
unpickled
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
unpickled
,
node_list
)
...
...
Lib/test/test_xml_etree.py
View file @
909843b4
...
...
@@ -121,11 +121,11 @@ class ElementTestCase:
def
setUpClass
(
cls
):
cls
.
modules
=
{
pyET
,
ET
}
def
pickleRoundTrip
(
self
,
obj
,
name
,
dumper
,
loader
):
def
pickleRoundTrip
(
self
,
obj
,
name
,
dumper
,
loader
,
proto
):
save_m
=
sys
.
modules
[
name
]
try
:
sys
.
modules
[
name
]
=
dumper
temp
=
pickle
.
dumps
(
obj
)
temp
=
pickle
.
dumps
(
obj
,
proto
)
sys
.
modules
[
name
]
=
loader
result
=
pickle
.
loads
(
temp
)
except
pickle
.
PicklingError
as
pe
:
...
...
@@ -1677,6 +1677,7 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
def
test_pickle
(
self
):
# issue #16076: the C implementation wasn't pickleable.
for
proto
in
range
(
2
,
pickle
.
HIGHEST_PROTOCOL
+
1
):
for
dumper
,
loader
in
product
(
self
.
modules
,
repeat
=
2
):
e
=
dumper
.
Element
(
'foo'
,
bar
=
42
)
e
.
text
=
"text goes here"
...
...
@@ -1686,7 +1687,7 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
e
.
findall
(
'.//grandchild'
)[
0
].
set
(
'attr'
,
'other value'
)
e2
=
self
.
pickleRoundTrip
(
e
,
'xml.etree.ElementTree'
,
dumper
,
loader
)
dumper
,
loader
,
proto
)
self
.
assertEqual
(
e2
.
tag
,
'foo'
)
self
.
assertEqual
(
e2
.
attrib
[
'bar'
],
42
)
...
...
@@ -1694,6 +1695,7 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
self
.
assertEqualElements
(
e
,
e2
)
def
test_pickle_issue18997
(
self
):
for
proto
in
range
(
2
,
pickle
.
HIGHEST_PROTOCOL
+
1
):
for
dumper
,
loader
in
product
(
self
.
modules
,
repeat
=
2
):
XMLTEXT
=
"""<?xml version="1.0"?>
<group><dogs>4</dogs>
...
...
@@ -1701,7 +1703,8 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
e1
=
dumper
.
fromstring
(
XMLTEXT
)
if
hasattr
(
e1
,
'__getstate__'
):
self
.
assertEqual
(
e1
.
__getstate__
()[
'tag'
],
'group'
)
e2
=
self
.
pickleRoundTrip
(
e1
,
'xml.etree.ElementTree'
,
dumper
,
loader
)
e2
=
self
.
pickleRoundTrip
(
e1
,
'xml.etree.ElementTree'
,
dumper
,
loader
,
proto
)
self
.
assertEqual
(
e2
.
tag
,
'group'
)
self
.
assertEqual
(
e2
[
0
].
tag
,
'dogs'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment