[Python-checkins] python/dist/src/Lib pickletools.py,1.25,1.26

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2003年2月05日 11:55:56 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv13931/Lib
Modified Files:
	pickletools.py 
Log Message:
dis(): Added an optional memo argument, so that multiple pickles in a
file can be dumped without (bogus) complaint if the the pickles were
created using a single pickle memo.
Index: pickletools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickletools.py,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -d -r1.25 -r1.26
*** pickletools.py	31 Jan 2003 16:43:39 -0000	1.25
--- pickletools.py	5 Feb 2003 19:55:53 -0000	1.26
***************
*** 1862,1866 ****
 # A symbolic pickle disassembler.
 
! def dis(pickle, out=None, indentlevel=4):
 """Produce a symbolic disassembly of a pickle.
 
--- 1862,1866 ----
 # A symbolic pickle disassembler.
 
! def dis(pickle, out=None, memo=None, indentlevel=4):
 """Produce a symbolic disassembly of a pickle.
 
***************
*** 1872,1875 ****
--- 1872,1881 ----
 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.
***************
*** 1896,1900 ****
 
 stack = [] # crude emulation of unpickler stack
! memo = {} # crude emulation of unpicker memo
 maxproto = -1 # max protocol number seen
 markstack = [] # bytecode positions of MARK opcodes
--- 1902,1907 ----
 
 stack = [] # crude emulation of unpickler stack
! if memo is None:
! memo = {} # crude emulation of unpicker memo
 maxproto = -1 # max protocol number seen
 markstack = [] # bytecode positions of MARK opcodes
***************
*** 2196,2200 ****
--- 2203,2236 ----
 """
 
+ _memo_test = r"""
+ >>> import pickle
+ >>> from StringIO import StringIO
+ >>> f = StringIO()
+ >>> p = pickle.Pickler(f, 2)
+ >>> x = [1, 2, 3]
+ >>> p.dump(x)
+ >>> p.dump(x)
+ >>> f.seek(0)
+ >>> memo = {}
+ >>> dis(f, memo=memo)
+ 0: \x80 PROTO 2
+ 2: ] EMPTY_LIST
+ 3: q BINPUT 0
+ 5: ( MARK
+ 6: K BININT1 1
+ 8: K BININT1 2
+ 10: K BININT1 3
+ 12: e APPENDS (MARK at 5)
+ 13: . STOP
+ highest protocol among opcodes = 2
+ >>> dis(f, memo=memo)
+ 14: \x80 PROTO 2
+ 16: h BINGET 0
+ 18: . STOP
+ highest protocol among opcodes = 2
+ """
+ 
 __test__ = {'disassembler_test': _dis_test,
+ 'disassembler_memo_test': _memo_test,
 }
 

AltStyle によって変換されたページ (->オリジナル) /