pickletools
index
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickletools.py
Module Docs

"Executable documentation" for the pickle module.
 
Extensive comments about the pickle protocols and pickle-machine opcodes
can be found here.  Some functions meant for external use:
 
genops(pickle)
   Generate all the opcodes in a pickle, as (opcode, arg, position) triples.
 
dis(pickle, out=None, memo=None, indentlevel=4)
   Print a symbolic disassembly of a pickle.

 
Functions
       
dis(pickle, out=None, memo=None, indentlevel=4)
Produce a symbolic disassembly of a pickle.
 
'pickle' is a file-like object, or string, containing a (at least one)
pickle.  The pickle is disassembled from the current position, through
the first STOP opcode encountered.
 
Optional arg 'out' is a file-like object to which the disassembly is
printed.  It defaults to sys.stdout.
 
Optional arg 'memo' is a Python dict, used as the pickle's memo.  It
may be mutated by dis(), if the pickle contains PUT or BINPUT opcodes.
Passing the same memo object to another dis() call then allows disassembly
to proceed across multiple pickles that were all created by the same
pickler with the same memo.  Ordinarily you don't need to worry about this.
 
Optional arg indentlevel is the number of blanks by which to indent
a new MARK level.  It defaults to 4.
 
In addition to printing the disassembly, some sanity checks are made:
 
+ All embedded opcode arguments "make sense".
 
+ Explicit and implicit pop operations have enough items on the stack.
 
+ When an opcode implicitly refers to a markobject, a markobject is
  actually on the stack.
 
+ A memo entry isn't referenced before it's defined.
 
+ The markobject isn't stored in the memo.
 
+ A memo entry isn't redefined.
genops(pickle)
Generate all the opcodes in a pickle.
 
'pickle' is a file-like object, or string, containing the pickle.
 
Each opcode in the pickle is generated, from the current pickle position,
stopping after a STOP opcode is delivered.  A triple is generated for
each opcode:
 
    opcode, arg, pos
 
opcode is an OpcodeInfo record, describing the current opcode.
 
If the opcode has an argument embedded in the pickle, arg is its decoded
value, as a Python object.  If the opcode doesn't have an argument, arg
is None.
 
If the pickle has a tell() method, pos was the value of pickle.tell()
before reading the current opcode.  If the pickle is a string object,
it's wrapped in a StringIO object, and the latter's tell() result is
used.  Else (the pickle doesn't have a tell(), and it's not obvious how
to query its current position) pos is None.
optimize(p)
Optimize a pickle string by removing unused PUT opcodes

 
Data
        __all__ = ['dis', 'genops', 'optimize']
__test__ = {'disassembler_memo_test': '\n>>> import pickle\n>>> from StringIO import Stri...18: . STOP\nhighest protocol among opcodes = 2\n', 'disassembler_test': "\n>>> import pickle\n>>> x = [1, 2, (3, 4), {'abc'...14: . STOP\nhighest protocol among opcodes = 2\n"}