[Python-checkins] python/dist/src/Lib pickle.py,1.106,1.107

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
2003年1月28日 07:10:27 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv12345
Modified Files:
	pickle.py 
Log Message:
Rename all variables 'object' to 'obj' to avoid conflicts with the
type 'object'. Also minor docstring tweakage, and rearranged a few
lines in save().
Index: pickle.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickle.py,v
retrieving revision 1.106
retrieving revision 1.107
diff -C2 -d -r1.106 -r1.107
*** pickle.py	28 Jan 2003 14:40:16 -0000	1.106
--- pickle.py	28 Jan 2003 15:10:22 -0000	1.107
***************
*** 197,202 ****
 self.memo.clear()
 
! def dump(self, object):
! """Write a pickled representation of object to the open file object.
 
 Either the binary or ASCII format will be used, depending on the
--- 197,202 ----
 self.memo.clear()
 
! def dump(self, obj):
! """Write a pickled representation of obj to the open file.
 
 Either the binary or ASCII format will be used, depending on the
***************
*** 206,210 ****
 if self.proto >= 2:
 self.write(PROTO + chr(self.proto))
! self.save(object)
 self.write(STOP)
 
--- 206,210 ----
 if self.proto >= 2:
 self.write(PROTO + chr(self.proto))
! self.save(obj)
 self.write(STOP)
 
***************
*** 248,267 ****
 return GET + `i` + '\n'
 
! def save(self, object):
! memo = self.memo
! 
! pid = self.persistent_id(object)
 if pid is not None:
 self.save_pers(pid)
 return
 
! d = id(object)
! 
! t = type(object)
! 
 if d in memo:
 self.write(self.get(memo[d][0]))
 return
 
 try:
 f = self.dispatch[t]
--- 248,264 ----
 return GET + `i` + '\n'
 
! def save(self, obj):
! pid = self.persistent_id(obj)
 if pid is not None:
 self.save_pers(pid)
 return
 
! memo = self.memo
! d = id(obj)
 if d in memo:
 self.write(self.get(memo[d][0]))
 return
 
+ t = type(obj)
 try:
 f = self.dispatch[t]
***************
*** 269,273 ****
 pass
 else:
! f(self, object)
 return
 
--- 266,270 ----
 pass
 else:
! f(self, obj)
 return
 
***************
*** 278,282 ****
 issc = 0
 if issc:
! self.save_global(object)
 return
 
--- 275,279 ----
 issc = 0
 if issc:
! self.save_global(obj)
 return
 
***************
*** 285,300 ****
 except KeyError:
 try:
! reduce = object.__reduce__
 except AttributeError:
 raise PicklingError, \
 "can't pickle %s object: %s" % (`t.__name__`,
! `object`)
 else:
 tup = reduce()
 else:
! tup = reduce(object)
 
 if type(tup) is StringType:
! self.save_global(object, tup)
 return
 
--- 282,297 ----
 except KeyError:
 try:
! reduce = obj.__reduce__
 except AttributeError:
 raise PicklingError, \
 "can't pickle %s object: %s" % (`t.__name__`,
! `obj`)
 else:
 tup = reduce()
 else:
! tup = reduce(obj)
 
 if type(tup) is StringType:
! self.save_global(obj, tup)
 return
 
***************
*** 322,328 ****
 
 self.save_reduce(callable, arg_tup, state)
! self.memoize(object)
 
! def persistent_id(self, object):
 return None
 
--- 319,325 ----
 
 self.save_reduce(callable, arg_tup, state)
! self.memoize(obj)
 
! def persistent_id(self, obj):
 return None
 
***************
*** 352,367 ****
 dispatch = {}
 
! def save_none(self, object):
 self.write(NONE)
 dispatch[NoneType] = save_none
 
! def save_bool(self, object):
 if self.proto >= 2:
! self.write(object and NEWTRUE or NEWFALSE)
 else:
! self.write(object and TRUE or FALSE)
 dispatch[bool] = save_bool
 
! def save_int(self, object, pack=struct.pack):
 if self.bin:
 # If the int is small enough to fit in a signed 4-byte 2's-comp
--- 349,364 ----
 dispatch = {}
 
! def save_none(self, obj):
 self.write(NONE)
 dispatch[NoneType] = save_none
 
! def save_bool(self, obj):
 if self.proto >= 2:
! self.write(obj and NEWTRUE or NEWFALSE)
 else:
! self.write(obj and TRUE or FALSE)
 dispatch[bool] = save_bool
 
! def save_int(self, obj, pack=struct.pack):
 if self.bin:
 # If the int is small enough to fit in a signed 4-byte 2's-comp
***************
*** 369,393 ****
 # case.
 # First one- and two-byte unsigned ints:
! if object >= 0:
! if object <= 0xff:
! self.write(BININT1 + chr(object))
 return
! if object <= 0xffff:
! self.write(BININT2 + chr(object&0xff) + chr(object>>8))
 return
 # Next check for 4-byte signed ints:
! high_bits = object >> 31 # note that Python shift sign-extends
 if high_bits == 0 or high_bits == -1:
 # All high bits are copies of bit 2**31, so the value
 # fits in a 4-byte signed int.
! self.write(BININT + pack("<i", object))
 return
 # Text pickle, or int too big to fit in signed 4-byte format.
! self.write(INT + `object` + '\n')
 dispatch[IntType] = save_int
 
! def save_long(self, object, pack=struct.pack):
 if self.proto >= 2:
! bytes = encode_long(object)
 n = len(bytes)
 if n < 256:
--- 366,390 ----
 # case.
 # First one- and two-byte unsigned ints:
! if obj >= 0:
! if obj <= 0xff:
! self.write(BININT1 + chr(obj))
 return
! if obj <= 0xffff:
! self.write(BININT2 + chr(obj&0xff) + chr(obj>>8))
 return
 # Next check for 4-byte signed ints:
! high_bits = obj >> 31 # note that Python shift sign-extends
 if high_bits == 0 or high_bits == -1:
 # All high bits are copies of bit 2**31, so the value
 # fits in a 4-byte signed int.
! self.write(BININT + pack("<i", obj))
 return
 # Text pickle, or int too big to fit in signed 4-byte format.
! self.write(INT + `obj` + '\n')
 dispatch[IntType] = save_int
 
! def save_long(self, obj, pack=struct.pack):
 if self.proto >= 2:
! bytes = encode_long(obj)
 n = len(bytes)
 if n < 256:
***************
*** 395,465 ****
 else:
 self.write(LONG4 + pack("<i", n) + bytes)
! self.write(LONG + `object` + '\n')
 dispatch[LongType] = save_long
 
! def save_float(self, object, pack=struct.pack):
 if self.bin:
! self.write(BINFLOAT + pack('>d', object))
 else:
! self.write(FLOAT + `object` + '\n')
 dispatch[FloatType] = save_float
 
! def save_string(self, object, pack=struct.pack):
 if self.bin:
! n = len(object)
 if n < 256:
! self.write(SHORT_BINSTRING + chr(n) + object)
 else:
! self.write(BINSTRING + pack("<i", n) + object)
 else:
! self.write(STRING + `object` + '\n')
! self.memoize(object)
 dispatch[StringType] = save_string
 
! def save_unicode(self, object, pack=struct.pack):
 if self.bin:
! encoding = object.encode('utf-8')
 n = len(encoding)
 self.write(BINUNICODE + pack("<i", n) + encoding)
 else:
! object = object.replace("\\", "\\u005c")
! object = object.replace("\n", "\\u000a")
! self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
! self.memoize(object)
 dispatch[UnicodeType] = save_unicode
 
 if StringType == UnicodeType:
 # This is true for Jython
! def save_string(self, object, pack=struct.pack):
! unicode = object.isunicode()
 
 if self.bin:
 if unicode:
! object = object.encode("utf-8")
! l = len(object)
 if l < 256 and not unicode:
! self.write(SHORT_BINSTRING + chr(l) + object)
 else:
 s = pack("<i", l)
 if unicode:
! self.write(BINUNICODE + s + object)
 else:
! self.write(BINSTRING + s + object)
 else:
 if unicode:
! object = object.replace("\\", "\\u005c")
! object = object.replace("\n", "\\u000a")
! object = object.encode('raw-unicode-escape')
! self.write(UNICODE + object + '\n')
 else:
! self.write(STRING + `object` + '\n')
! self.memoize(object)
 dispatch[StringType] = save_string
 
! def save_tuple(self, object):
 write = self.write
 proto = self.proto
 
! n = len(object)
 if n == 0 and proto:
 write(EMPTY_TUPLE)
--- 392,462 ----
 else:
 self.write(LONG4 + pack("<i", n) + bytes)
! self.write(LONG + `obj` + '\n')
 dispatch[LongType] = save_long
 
! def save_float(self, obj, pack=struct.pack):
 if self.bin:
! self.write(BINFLOAT + pack('>d', obj))
 else:
! self.write(FLOAT + `obj` + '\n')
 dispatch[FloatType] = save_float
 
! def save_string(self, obj, pack=struct.pack):
 if self.bin:
! n = len(obj)
 if n < 256:
! self.write(SHORT_BINSTRING + chr(n) + obj)
 else:
! self.write(BINSTRING + pack("<i", n) + obj)
 else:
! self.write(STRING + `obj` + '\n')
! self.memoize(obj)
 dispatch[StringType] = save_string
 
! def save_unicode(self, obj, pack=struct.pack):
 if self.bin:
! encoding = obj.encode('utf-8')
 n = len(encoding)
 self.write(BINUNICODE + pack("<i", n) + encoding)
 else:
! obj = obj.replace("\\", "\\u005c")
! obj = obj.replace("\n", "\\u000a")
! self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n')
! self.memoize(obj)
 dispatch[UnicodeType] = save_unicode
 
 if StringType == UnicodeType:
 # This is true for Jython
! def save_string(self, obj, pack=struct.pack):
! unicode = obj.isunicode()
 
 if self.bin:
 if unicode:
! obj = obj.encode("utf-8")
! l = len(obj)
 if l < 256 and not unicode:
! self.write(SHORT_BINSTRING + chr(l) + obj)
 else:
 s = pack("<i", l)
 if unicode:
! self.write(BINUNICODE + s + obj)
 else:
! self.write(BINSTRING + s + obj)
 else:
 if unicode:
! obj = obj.replace("\\", "\\u005c")
! obj = obj.replace("\n", "\\u000a")
! obj = obj.encode('raw-unicode-escape')
! self.write(UNICODE + obj + '\n')
 else:
! self.write(STRING + `obj` + '\n')
! self.memoize(obj)
 dispatch[StringType] = save_string
 
! def save_tuple(self, obj):
 write = self.write
 proto = self.proto
 
! n = len(obj)
 if n == 0 and proto:
 write(EMPTY_TUPLE)
***************
*** 469,481 ****
 memo = self.memo
 if n <= 3 and proto >= 2:
! for element in object:
 save(element)
 # Subtle. Same as in the big comment below.
! if id(object) in memo:
! get = self.get(memo[id(object)][0])
 write(POP * n + get)
 else:
 write(_tuplesize2code[n])
! self.memoize(object)
 return
 
--- 466,478 ----
 memo = self.memo
 if n <= 3 and proto >= 2:
! for element in obj:
 save(element)
 # Subtle. Same as in the big comment below.
! if id(obj) in memo:
! get = self.get(memo[id(obj)][0])
 write(POP * n + get)
 else:
 write(_tuplesize2code[n])
! self.memoize(obj)
 return
 
***************
*** 483,490 ****
 # has more than 3 elements.
 write(MARK)
! for element in object:
 save(element)
 
! if n and id(object) in memo:
 # Subtle. d was not in memo when we entered save_tuple(), so
 # the process of saving the tuple's elements must have saved
--- 480,487 ----
 # has more than 3 elements.
 write(MARK)
! for element in obj:
 save(element)
 
! if n and id(obj) in memo:
 # Subtle. d was not in memo when we entered save_tuple(), so
 # the process of saving the tuple's elements must have saved
***************
*** 494,498 ****
 # could have been done in the "for element" loop instead, but
 # recursive tuples are a rare thing.
! get = self.get(memo[id(object)][0])
 if proto:
 write(POP_MARK + get)
--- 491,495 ----
 # could have been done in the "for element" loop instead, but
 # recursive tuples are a rare thing.
! get = self.get(memo[id(obj)][0])
 if proto:
 write(POP_MARK + get)
***************
*** 503,515 ****
 # No recursion (including the empty-tuple case for protocol 0).
 self.write(TUPLE)
! if object: # No need to memoize empty tuple
! self.memoize(object)
 
 dispatch[TupleType] = save_tuple
 
! def save_empty_tuple(self, object):
 self.write(EMPTY_TUPLE)
 
! def save_list(self, object):
 write = self.write
 save = self.save
--- 500,512 ----
 # No recursion (including the empty-tuple case for protocol 0).
 self.write(TUPLE)
! if obj: # No need to memoize empty tuple
! self.memoize(obj)
 
 dispatch[TupleType] = save_tuple
 
! def save_empty_tuple(self, obj):
 self.write(EMPTY_TUPLE)
 
! def save_list(self, obj):
 write = self.write
 save = self.save
***************
*** 517,530 ****
 if self.bin:
 write(EMPTY_LIST)
! self.memoize(object)
! n = len(object)
 if n > 1:
 write(MARK)
! for element in object:
 save(element)
 write(APPENDS)
 elif n:
 assert n == 1
! save(object[0])
 write(APPEND)
 # else the list is empty, and we're already done
--- 514,527 ----
 if self.bin:
 write(EMPTY_LIST)
! self.memoize(obj)
! n = len(obj)
 if n > 1:
 write(MARK)
! for element in obj:
 save(element)
 write(APPENDS)
 elif n:
 assert n == 1
! save(obj[0])
 write(APPEND)
 # else the list is empty, and we're already done
***************
*** 532,537 ****
 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
 write(MARK + LIST)
! self.memoize(object)
! for element in object:
 save(element)
 write(APPEND)
--- 529,534 ----
 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
 write(MARK + LIST)
! self.memoize(obj)
! for element in obj:
 save(element)
 write(APPEND)
***************
*** 539,551 ****
 dispatch[ListType] = save_list
 
! def save_dict(self, object):
 write = self.write
 save = self.save
! items = object.iteritems()
 
 if self.bin:
 write(EMPTY_DICT)
! self.memoize(object)
! if len(object) > 1:
 write(MARK)
 for key, value in items:
--- 536,548 ----
 dispatch[ListType] = save_list
 
! def save_dict(self, obj):
 write = self.write
 save = self.save
! items = obj.iteritems()
 
 if self.bin:
 write(EMPTY_DICT)
! self.memoize(obj)
! if len(obj) > 1:
 write(MARK)
 for key, value in items:
***************
*** 557,563 ****
 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
 write(MARK + DICT)
! self.memoize(object)
 
! # proto 0 or len(object) < 2
 for key, value in items:
 save(key)
--- 554,560 ----
 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
 write(MARK + DICT)
! self.memoize(obj)
 
! # proto 0 or len(obj) < 2
 for key, value in items:
 save(key)
***************
*** 569,574 ****
 dispatch[PyStringMap] = save_dict
 
! def save_inst(self, object):
! cls = object.__class__
 
 memo = self.memo
--- 566,571 ----
 dispatch[PyStringMap] = save_dict
 
! def save_inst(self, obj):
! cls = obj.__class__
 
 memo = self.memo
***************
*** 576,581 ****
 save = self.save
 
! if hasattr(object, '__getinitargs__'):
! args = object.__getinitargs__()
 len(args) # XXX Assert it's a sequence
 _keep_alive(args, memo)
--- 573,578 ----
 save = self.save
 
! if hasattr(obj, '__getinitargs__'):
! args = obj.__getinitargs__()
 len(args) # XXX Assert it's a sequence
 _keep_alive(args, memo)
***************
*** 595,604 ****
 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
 
! self.memoize(object)
 
 try:
! getstate = object.__getstate__
 except AttributeError:
! stuff = object.__dict__
 else:
 stuff = getstate()
--- 592,601 ----
 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
 
! self.memoize(obj)
 
 try:
! getstate = obj.__getstate__
 except AttributeError:
! stuff = obj.__dict__
 else:
 stuff = getstate()
***************
*** 609,623 ****
 dispatch[InstanceType] = save_inst
 
! def save_global(self, object, name = None):
 write = self.write
 memo = self.memo
 
 if name is None:
! name = object.__name__
 
 try:
! module = object.__module__
 except AttributeError:
! module = whichmodule(object, name)
 
 try:
--- 606,620 ----
 dispatch[InstanceType] = save_inst
 
! def save_global(self, obj, name = None):
 write = self.write
 memo = self.memo
 
 if name is None:
! name = obj.__name__
 
 try:
! module = obj.__module__
 except AttributeError:
! module = whichmodule(obj, name)
 
 try:
***************
*** 628,640 ****
 raise PicklingError(
 "Can't pickle %r: it's not found as %s.%s" %
! (object, module, name))
 else:
! if klass is not object:
 raise PicklingError(
 "Can't pickle %r: it's not the same object as %s.%s" %
! (object, module, name))
 
 write(GLOBAL + module + '\n' + name + '\n')
! self.memoize(object)
 
 dispatch[ClassType] = save_global
--- 625,637 ----
 raise PicklingError(
 "Can't pickle %r: it's not found as %s.%s" %
! (obj, module, name))
 else:
! if klass is not obj:
 raise PicklingError(
 "Can't pickle %r: it's not the same object as %s.%s" %
! (obj, module, name))
 
 write(GLOBAL + module + '\n' + name + '\n')
! self.memoize(obj)
 
 dispatch[ClassType] = save_global
***************
*** 701,705 ****
 object can be a file object opened for reading, a StringIO object,
 or any other custom object that meets this interface.
- 
 """
 self.readline = file.readline
--- 698,701 ----
***************
*** 708,716 ****
 
 def load(self):
! """Read a pickled object representation from the open file object.
! 
! Return the reconstituted object hierarchy specified in the file
! object.
 
 """
 self.mark = object() # any new unique object
--- 704,710 ----
 
 def load(self):
! """Read a pickled object representation from the open file.
 
+ Return the reconstituted object hierarchy specified in the file.
 """
 self.mark = object() # any new unique object
***************
*** 992,995 ****
--- 986,996 ----
 dispatch[OBJ] = load_obj
 
+ def load_newobj(self):
+ args = self.stack.pop()
+ cls = self.stack[-1]
+ obj = cls.__new__(cls, *args)
+ self.stack[-1:] = obj
+ dispatch[NEWOBJ] = load_newobj
+ 
 def load_global(self):
 module = self.readline()[:-1]
***************
*** 1198,1207 ****
 from StringIO import StringIO
 
! def dump(object, file, proto=1):
! Pickler(file, proto).dump(object)
 
! def dumps(object, proto=1):
 file = StringIO()
! Pickler(file, proto).dump(object)
 return file.getvalue()
 
--- 1199,1208 ----
 from StringIO import StringIO
 
! def dump(obj, file, proto=1):
! Pickler(file, proto).dump(obj)
 
! def dumps(obj, proto=1):
 file = StringIO()
! Pickler(file, proto).dump(obj)
 return file.getvalue()
 

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