[Python-checkins] CVS: python/dist/src/Doc/lib libpickle.tex,1.28,1.29

Fred L. Drake fdrake@users.sourceforge.net
2001年9月25日 09:29:19 -0700


Update of /cvsroot/python/python/dist/src/Doc/lib
In directory usw-pr-cvs1:/tmp/cvs-serv13605/lib
Modified Files:
	libpickle.tex 
Log Message:
Revise the example to be more resiliant in the face of continued use after
the object has been pickled; don't mutate the instance dict in the
__getstate__() method. Other minor changes for style. Broke up the
displayed interactive session to get better page-breaking behavior for
typeset versions, and to point out an important aspect of the example.
This closes SF bug #453914.
Index: libpickle.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libpickle.tex,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** libpickle.tex	2000年10月18日 16:47:52	1.28
--- libpickle.tex	2001年09月25日 16:29:17	1.29
***************
*** 306,317 ****
 
 \begin{verbatim}
- # illustrate __setstate__ and __getstate__ methods
- # used in pickling.
- 
 class TextReader:
! "Print and number lines in a text file."
! def __init__(self,file):
 self.file = file
! self.fh = open(file,'r')
 self.lineno = 0
 
--- 306,314 ----
 
 \begin{verbatim}
 class TextReader:
! """Print and number lines in a text file."""
! def __init__(self, file):
 self.file = file
! self.fh = open(file)
 self.lineno = 0
 
***************
*** 321,342 ****
 if not line:
 return None
! return "%d: %s" % (self.lineno,line[:-1])
 
- # return data representation for pickled object
 def __getstate__(self):
! odict = self.__dict__ # get attribute dictionary
! del odict['fh'] # remove filehandle entry
 return odict
 
- # restore object state from data representation generated 
- # by __getstate__
 def __setstate__(self,dict):
! fh = open(dict['file']) # reopen file
! count = dict['lineno'] # read from file...
! while count: # until line count is restored
 fh.readline()
 count = count - 1
! dict['fh'] = fh # create filehandle entry
! self.__dict__ = dict # make dict our attribute dictionary
 \end{verbatim}
 
--- 318,338 ----
 if not line:
 return None
! if line.endswith("\n"):
! line = line[:-1]
! return "%d: %s" % (self.lineno, line)
 
 def __getstate__(self):
! odict = self.__dict__.copy() # copy the dict since we change it
! del odict['fh'] # remove filehandle entry
 return odict
 
 def __setstate__(self,dict):
! fh = open(dict['file']) # reopen file
! count = dict['lineno'] # read from file...
! while count: # until line count is restored
 fh.readline()
 count = count - 1
! self.__dict__.update(dict) # update attributes
! self.fh = fh # save the file object
 \end{verbatim}
 
***************
*** 353,359 ****
 >>> import pickle
 >>> pickle.dump(obj,open('save.p','w'))
 
! (start another Python session)
 
 >>> import pickle
 >>> reader = pickle.load(open('save.p'))
--- 349,359 ----
 >>> import pickle
 >>> pickle.dump(obj,open('save.p','w'))
+ \end{verbatim}
 
! If you want to see that \refmodule{pickle} works across Python
! processes, start another Python session, before continuing. What
! follows can happen from either the same process or a new process.
 
+ \begin{verbatim}
 >>> import pickle
 >>> reader = pickle.load(open('save.p'))

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