[Python-checkins] python/dist/src/Lib pickle.py,1.115,1.116
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Tue, 28 Jan 2003 11:48:21 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv19333
Modified Files:
pickle.py
Log Message:
The default __reduce__ on the base object type obscured any
possibility of calling save_reduce(). Add a special hack for this.
The tests for this are much simpler now (no __getstate__ or
__getnewargs__ needed).
Index: pickle.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickle.py,v
retrieving revision 1.115
retrieving revision 1.116
diff -C2 -d -r1.115 -r1.116
*** pickle.py 28 Jan 2003 18:22:35 -0000 1.115
--- pickle.py 28 Jan 2003 19:48:16 -0000 1.116
***************
*** 28,32 ****
from types import *
! from copy_reg import dispatch_table, safe_constructors
import marshal
import sys
--- 28,32 ----
from types import *
! from copy_reg import dispatch_table, safe_constructors, _reconstructor
import marshal
import sys
***************
*** 321,324 ****
--- 321,331 ----
"exactly two or three elements" % reduce)
+ # XXX Temporary hack XXX
+ # Override the default __reduce__ for new-style class instances
+ if self.proto >= 2:
+ if func is _reconstructor:
+ self.save_newobj(obj)
+ return
+
# Save the reduce() output and finally memoize the object
self.save_reduce(func, args, state)
***************
*** 370,381 ****
# XXX Much of this is still experimental.
t = type(obj)
- args = ()
getnewargs = getattr(obj, "__getnewargs__", None)
if getnewargs:
args = getnewargs() # This better not reference obj
self.save_global(t)
! self.save(args)
! self.write(NEWOBJ)
self.memoize(obj)
getstate = getattr(obj, "__getstate__", None)
if getstate:
--- 377,411 ----
# XXX Much of this is still experimental.
t = type(obj)
getnewargs = getattr(obj, "__getnewargs__", None)
if getnewargs:
args = getnewargs() # This better not reference obj
+ else:
+ for cls in int, long, float, complex, str, unicode, tuple:
+ if isinstance(obj, cls):
+ args = (cls(obj),)
+ break
+ else:
+ args = ()
+
+ save = self.save
+ write = self.write
+
self.save_global(t)
! save(args)
! write(NEWOBJ)
self.memoize(obj)
+
+ if isinstance(obj, list):
+ write(MARK)
+ for x in obj:
+ save(x)
+ write(APPENDS)
+ elif isinstance(obj, dict):
+ write(MARK)
+ for k, v in obj.iteritems():
+ save(k)
+ save(v)
+ write(SETITEMS)
+
getstate = getattr(obj, "__getstate__", None)
if getstate:
***************
*** 385,391 ****
# XXX What about __slots__?
if state is not None:
! self.save(state)
! self.write(BUILD)
! return
# Methods below this point are dispatched through the dispatch table
--- 415,420 ----
# XXX What about __slots__?
if state is not None:
! save(state)
! write(BUILD)
# Methods below this point are dispatched through the dispatch table
***************
*** 1174,1177 ****
--- 1203,1208 ----
>>>
"""
+ # XXX This is still a quadratic algorithm.
+ # Should use hex() to get started.
digits = []
while not -128 <= x < 128:
***************
*** 1196,1199 ****
--- 1227,1231 ----
127L
"""
+ # XXX This is quadratic too.
x = 0L
i = 0L