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
8587b3c0
Commit
8587b3c0
authored
Feb 13, 2003
by
Tim Peters
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
parent
fe62bc91
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
10 deletions
+20
-10
Lib/pickle.py
Lib/pickle.py
+7
-3
Lib/test/pickletester.py
Lib/test/pickletester.py
+4
-2
Modules/cPickle.c
Modules/cPickle.c
+9
-5
No files found.
Lib/pickle.py
View file @
8587b3c0
...
@@ -47,6 +47,10 @@ compatible_formats = ["1.0", # Original protocol 0
...
@@ -47,6 +47,10 @@ compatible_formats = ["1.0", # Original protocol 0
"2.0"
,
# Protocol 2
"2.0"
,
# Protocol 2
]
# Old format versions we can read
]
# Old format versions we can read
# Keep in synch with cPickle. This is the highest protocol number we
# know how to read.
HIGHEST_PROTOCOL
=
2
# Why use struct.pack() for pickling but marshal.loads() for
# Why use struct.pack() for pickling but marshal.loads() for
# unpickling? struct.pack() is 40% faster than marshal.dumps(), but
# unpickling? struct.pack() is 40% faster than marshal.dumps(), but
# marshal.loads() is twice as fast as struct.unpack()!
# marshal.loads() is twice as fast as struct.unpack()!
...
@@ -200,9 +204,9 @@ class Pickler:
...
@@ -200,9 +204,9 @@ class Pickler:
if
protocol
is
None
:
if
protocol
is
None
:
protocol
=
0
protocol
=
0
if
protocol
<
0
:
if
protocol
<
0
:
protocol
=
2
protocol
=
HIGHEST_PROTOCOL
elif
protocol
not
in
(
0
,
1
,
2
)
:
elif
not
0
<=
protocol
<=
HIGHEST_PROTOCOL
:
raise
ValueError
,
"pickle protocol must be 0, 1 or 2"
raise
ValueError
(
"pickle protocol must be <= %d"
%
HIGHEST_PROTOCOL
)
self
.
write
=
file
.
write
self
.
write
=
file
.
write
self
.
memo
=
{}
self
.
memo
=
{}
self
.
proto
=
int
(
protocol
)
self
.
proto
=
int
(
protocol
)
...
...
Lib/test/pickletester.py
View file @
8587b3c0
import
unittest
import
unittest
import
pickle
import
pickle
import
cPickle
import
pickletools
import
pickletools
import
copy_reg
import
copy_reg
...
@@ -7,8 +8,9 @@ from test.test_support import TestFailed, have_unicode, TESTFN
...
@@ -7,8 +8,9 @@ from test.test_support import TestFailed, have_unicode, TESTFN
# Tests that try a number of pickle protocols should have a
# Tests that try a number of pickle protocols should have a
# for proto in protocols:
# for proto in protocols:
# kind of outer loop. Bump the 3 to 4 if/when protocol 3 is invented.
# kind of outer loop.
protocols
=
range
(
3
)
assert
pickle
.
HIGHEST_PROTOCOL
==
cPickle
.
HIGHEST_PROTOCOL
==
2
protocols
=
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
)
# Return True if opcode code appears in the pickle, else False.
# Return True if opcode code appears in the pickle, else False.
...
...
Modules/cPickle.c
View file @
8587b3c0
...
@@ -15,7 +15,7 @@ PyDoc_STRVAR(cPickle_module_documentation,
...
@@ -15,7 +15,7 @@ PyDoc_STRVAR(cPickle_module_documentation,
#define WRITE_BUF_SIZE 256
#define WRITE_BUF_SIZE 256
/* Bump this when new opcodes are added to the pickle protocol. */
/* Bump this when new opcodes are added to the pickle protocol. */
#define
CURRENT_PROTOCOL_NUMBER
2
#define
HIGHEST_PROTOCOL
2
/*
/*
* Pickle opcodes. These must be kept in synch with pickle.py. Extensive
* Pickle opcodes. These must be kept in synch with pickle.py. Extensive
...
@@ -2743,11 +2743,11 @@ newPicklerobject(PyObject *file, int proto)
...
@@ -2743,11 +2743,11 @@ newPicklerobject(PyObject *file, int proto)
Picklerobject
*
self
;
Picklerobject
*
self
;
if
(
proto
<
0
)
if
(
proto
<
0
)
proto
=
CURRENT_PROTOCOL_NUMBER
;
proto
=
HIGHEST_PROTOCOL
;
if
(
proto
>
CURRENT_PROTOCOL_NUMBER
)
{
if
(
proto
>
HIGHEST_PROTOCOL
)
{
PyErr_Format
(
PyExc_ValueError
,
"pickle protocol %d asked for; "
PyErr_Format
(
PyExc_ValueError
,
"pickle protocol %d asked for; "
"the highest available protocol is %d"
,
"the highest available protocol is %d"
,
proto
,
CURRENT_PROTOCOL_NUMBER
);
proto
,
HIGHEST_PROTOCOL
);
return
NULL
;
return
NULL
;
}
}
...
@@ -4308,7 +4308,7 @@ load_proto(Unpicklerobject *self)
...
@@ -4308,7 +4308,7 @@ load_proto(Unpicklerobject *self)
* int when chewing on 1 byte.
* int when chewing on 1 byte.
*/
*/
assert
(
i
>=
0
);
assert
(
i
>=
0
);
if
(
i
<=
CURRENT_PROTOCOL_NUMBER
)
if
(
i
<=
HIGHEST_PROTOCOL
)
return
0
;
return
0
;
PyErr_Format
(
PyExc_ValueError
,
"unsupported pickle protocol: %d"
,
i
);
PyErr_Format
(
PyExc_ValueError
,
"unsupported pickle protocol: %d"
,
i
);
...
@@ -5562,6 +5562,10 @@ initcPickle(void)
...
@@ -5562,6 +5562,10 @@ initcPickle(void)
}
}
Py_DECREF
(
di
);
Py_DECREF
(
di
);
i
=
PyModule_AddIntConstant
(
m
,
"HIGHEST_PROTOCOL"
,
HIGHEST_PROTOCOL
);
if
(
i
<
0
)
return
;
/* These are purely informational; no code uses them. */
/* These are purely informational; no code uses them. */
/* File format version we write. */
/* File format version we write. */
format_version
=
PyString_FromString
(
"2.0"
);
format_version
=
PyString_FromString
(
"2.0"
);
...
...
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