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
2c7a6932
Commit
2c7a6932
authored
Jul 02, 1999
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix a few markup nits, improve some index entries.
parent
657ebe1f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
38 deletions
+41
-38
Doc/lib/libpickle.tex
Doc/lib/libpickle.tex
+41
-38
No files found.
Doc/lib/libpickle.tex
View file @
2c7a6932
...
@@ -12,29 +12,30 @@
...
@@ -12,29 +12,30 @@
\indexii
{
pickling
}{
objects
}
\indexii
{
pickling
}{
objects
}
The
\module
{
pickle
}
module implements a basic but powerful algorithm
for
The
\module
{
pickle
}
module implements a basic but powerful algorithm
``pickling'' (a.k.a.
\
serializing, marshalling or flattening) nearly
for ``pickling'' (a.k.a.
\
serializing, marshalling or flattening)
arbitrary Python objects. This is the act of converting objects to a
nearly arbitrary Python objects. This is the act of converting
stream of bytes (and back: ``unpickling'').
objects to a stream of bytes (and back: ``unpickling''). This is a
This is a more primitive notion than
more primitive notion than persistency --- although
\module
{
pickle
}
persistency --- although
\module
{
pickle
}
reads and writes file objects,
reads and writes file objects, it does not handle the issue of naming
it does not handle the issue of naming persistent objects, nor the
persistent objects, nor the (even more complicated) area of concurrent
(even more complicated) area of concurrent access to persistent
access to persistent objects. The
\module
{
pickle
}
module can
objects. The
\module
{
pickle
}
module can transform a complex object into
transform a complex object into a byte stream and it can transform the
a byte stream and it can transform the byte stream into an object with
byte stream into an object with the same internal structure. The most
the same internal structure. The most obvious thing to do with these
obvious thing to do with these byte streams is to write them onto a
byte streams is to write them onto a file, but it is also conceivable
file, but it is also conceivable to send them across a network or
to send them across a network or
store them in a database. The module
store them in a database. The module
\refmodule
{
shelve
}
\refstmodindex
{
shelve
}
provides a simple interface
\refmodule
{
shelve
}
\refstmodindex
{
shelve
}
provides a simple interface
to pickle and unpickle objects on DBM-style database files.
to pickle and unpickle objects on DBM-style database files.
\strong
{
Note:
}
The
\module
{
pickle
}
module is rather slow. A
\strong
{
Note:
}
The
\module
{
pickle
}
module is rather slow. A
reimplementation of the same algorithm in C, which is up to 1000 times
reimplementation of the same algorithm in C, which is up to 1000 times
faster, is available as the
\refmodule
{
cPickle
}
\refbimodindex
{
cPickle
}
faster, is available as the
module. This has the same interface except that
\code
{
Pickler
}
and
\refmodule
{
cPickle
}
\refbimodindex
{
cPickle
}
module. This has the same
\code
{
Unpickler
}
are factory functions, not classes (so they cannot be
interface except that
\class
{
Pickler
}
and
\class
{
Unpickler
}
are
used as base classes for inheritance).
factory functions, not classes (so they cannot be used as base classes
for inheritance).
Unlike the built-in module
\refmodule
{
marshal
}
\refbimodindex
{
marshal
}
,
Unlike the built-in module
\refmodule
{
marshal
}
\refbimodindex
{
marshal
}
,
\module
{
pickle
}
handles the following correctly:
\module
{
pickle
}
handles the following correctly:
...
@@ -72,12 +73,11 @@ compatibility with the Python 1.4 pickle module. In a future version,
...
@@ -72,12 +73,11 @@ compatibility with the Python 1.4 pickle module. In a future version,
the default may change to binary.
the default may change to binary.
The
\module
{
pickle
}
module doesn't handle code objects, which the
The
\module
{
pickle
}
module doesn't handle code objects, which the
\refmodule
{
marshal
}
module does. I suppose
\module
{
pickle
}
could, and maybe
\refmodule
{
marshal
}
\refbimodindex
{
marshal
}
module does. I suppose
it should, but there's probably no great need for it right now (as
\module
{
pickle
}
could, and maybe it should, but there's probably no
long as
\refmodule
{
marshal
}
continues to be used for reading and writing
great need for it right now (as long as
\refmodule
{
marshal
}
continues
code objects), and at least this avoids the possibility of smuggling
to be used for reading and writing code objects), and at least this
Trojan horses into a program.
avoids the possibility of smuggling Trojan horses into a program.
\refbimodindex
{
marshal
}
For the benefit of persistency modules written using
\module
{
pickle
}
, it
For the benefit of persistency modules written using
\module
{
pickle
}
, it
supports the notion of a reference to an object outside the pickled
supports the notion of a reference to an object outside the pickled
...
@@ -109,10 +109,15 @@ which should return a \emph{tuple} containing the arguments to be
...
@@ -109,10 +109,15 @@ which should return a \emph{tuple} containing the arguments to be
passed to the class constructor (
\method
{__
init
__
()
}
). This method is
passed to the class constructor (
\method
{__
init
__
()
}
). This method is
called at pickle time; the tuple it returns is incorporated in the
called at pickle time; the tuple it returns is incorporated in the
pickle for the instance.
pickle for the instance.
\ttindex
{__
getinitargs
__
()
}
\withsubitem
{
(copy protocol)
}{
\ttindex
{__
getinitargs
__
()
}}
\ttindex
{__
init
__
()
}
\withsubitem
{
(instance constructor)
}{
\ttindex
{__
init
__
()
}}
Classes can further influence how their instances are pickled --- if the class
Classes can further influence how their instances are pickled --- if
the class
\withsubitem
{
(copy protocol)
}{
\ttindex
{__
getstate
__
()
}
\ttindex
{__
setstate
__
()
}}
\withsubitem
{
(instance attribute)
}{
\ttindex
{__
dict
__}}
defines the method
\method
{__
getstate
__
()
}
, it is called and the return
defines the method
\method
{__
getstate
__
()
}
, it is called and the return
state is pickled as the contents for the instance, and if the class
state is pickled as the contents for the instance, and if the class
defines the method
\method
{__
setstate
__
()
}
, it is called with the
defines the method
\method
{__
setstate
__
()
}
, it is called with the
...
@@ -126,9 +131,6 @@ and \method{__setstate__()}, the state object needn't be a dictionary
...
@@ -126,9 +131,6 @@ and \method{__setstate__()}, the state object needn't be a dictionary
--- these methods can do what they want.) This protocol is also used
--- these methods can do what they want.) This protocol is also used
by the shallow and deep copying operations defined in the
by the shallow and deep copying operations defined in the
\refmodule
{
copy
}
\refstmodindex
{
copy
}
module.
\refmodule
{
copy
}
\refstmodindex
{
copy
}
module.
\ttindex
{__
getstate
__
()
}
\ttindex
{__
setstate
__
()
}
\ttindex
{__
dict
__}
Note that when class instances are pickled, their class's code and
Note that when class instances are pickled, their class's code and
data are not pickled along with them. Only the instance data are
data are not pickled along with them. Only the instance data are
...
@@ -175,12 +177,12 @@ x = pickle.load(f)
...
@@ -175,12 +177,12 @@ x = pickle.load(f)
\end{verbatim}
\end{verbatim}
The
\class
{
Pickler
}
class only calls the method
\code
{
f.write()
}
with a
The
\class
{
Pickler
}
class only calls the method
\code
{
f.write()
}
with a
\withsubitem
{
(class in pickle)
}{
\ttindex
{
Unpickler
}
\ttindex
{
Pickler
}}
string argument. The
\class
{
Unpickler
}
calls the methods
\code
{
f.read()
}
string argument. The
\class
{
Unpickler
}
calls the methods
\code
{
f.read()
}
(with an integer argument) and
\code
{
f.readline()
}
(without argument),
(with an integer argument) and
\code
{
f.readline()
}
(without argument),
both returning a string. It is explicitly allowed to pass non-file
both returning a string. It is explicitly allowed to pass non-file
objects here, as long as they have the right methods.
objects here, as long as they have the right methods.
\ttindex
{
Unpickler
}
\ttindex
{
Pickler
}
The constructor for the
\class
{
Pickler
}
class has an optional second
The constructor for the
\class
{
Pickler
}
class has an optional second
argument,
\var
{
bin
}
. If this is present and nonzero, the binary
argument,
\var
{
bin
}
. If this is present and nonzero, the binary
...
@@ -190,6 +192,7 @@ but backwards compatible) text pickle format is used. The
...
@@ -190,6 +192,7 @@ but backwards compatible) text pickle format is used. The
between binary and text pickle formats; it accepts either format.
between binary and text pickle formats; it accepts either format.
The following types can be pickled:
The following types can be pickled:
\begin{itemize}
\begin{itemize}
\item
\code
{
None
}
\item
\code
{
None
}
...
@@ -257,7 +260,7 @@ the string past the pickled object's representation are ignored.
...
@@ -257,7 +260,7 @@ the string past the pickled object's representation are ignored.
\begin{excdesc}
{
PicklingError
}
\begin{excdesc}
{
PicklingError
}
This exception is raised when an unpicklable object is passed to
This exception is raised when an unpicklable object is passed to
\
code
{
Pickler.dump()
}
.
\
method
{
Pickler.dump()
}
.
\end{excdesc}
\end{excdesc}
...
@@ -283,11 +286,11 @@ This exception is raised when an unpicklable object is passed to
...
@@ -283,11 +286,11 @@ This exception is raised when an unpicklable object is passed to
The
\module
{
cPickle
}
module provides a similar interface and identical
The
\module
{
cPickle
}
module provides a similar interface and identical
functionality as the
\refmodule
{
pickle
}
module, but can be up to 1000
functionality as the
\refmodule
{
pickle
}
\refstmodindex
{
pickle
}
module,
times faster since it is implemented in C. The only other
but can be up to 1000 times faster since it is implemented in C. The
important difference to note is that
\function
{
Pickler()
}
and
only other important difference to note is that
\function
{
Pickler()
}
\function
{
Unpickler()
}
are functions and not classes, and so cannot be
and
\function
{
Unpickler()
}
are functions and not classes, and so
subclassed. This should not be an issue in most cases.
cannot be
subclassed. This should not be an issue in most cases.
The format of the pickle data is identical to that produced using the
The format of the pickle data is identical to that produced using the
\refmodule
{
pickle
}
module, so it is possible to use
\refmodule
{
pickle
}
and
\refmodule
{
pickle
}
module, so it is possible to use
\refmodule
{
pickle
}
and
...
...
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