[Python-checkins] CVS: python/dist/src/Lib copy.py,1.20,1.21 copy_reg.py,1.7,1.8

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 28 Sep 2001 11:13:31 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv22311/Lib

Modified Files:
	copy.py copy_reg.py 
Log Message:
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
fallback for objects that are neither supported by our dispatch table
nor have a __copy__ or __deepcopy__ method.

Changes to _reduce() in copy_reg.py to support reducing objects that
don't have a __dict__ -- copy.copy(complex()) now invokes _reduce().

Add tests for copy.copy() and copy.deepcopy() to test_regrtest.py.



Index: copy.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/copy.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** copy.py	2001/08/17 18:39:24	1.20
--- copy.py	2001/09/28 18:13:29	1.21
***************
*** 62,66 ****
      PyStringMap = None
  
! __all__ = ["Error","error","copy","deepcopy"]
  
  def copy(x):
--- 62,66 ----
      PyStringMap = None
  
! __all__ = ["Error", "error", "copy", "deepcopy"]
  
  def copy(x):
***************
*** 76,82 ****
              copier = x.__copy__
          except AttributeError:
!             raise error, \
!                   "un(shallow)copyable object of type %s" % type(x)
!         y = copier()
      else:
          y = copierfunction(x)
--- 76,88 ----
              copier = x.__copy__
          except AttributeError:
!             try:
!                 reductor = x.__reduce__
!             except AttributeError:
!                 raise error, \
!                       "un(shallow)copyable object of type %s" % type(x)
!             else:
!                 y = _reconstruct(x, reductor(), 0)
!         else:
!             y = copier()
      else:
          y = copierfunction(x)
***************
*** 157,163 ****
              copier = x.__deepcopy__
          except AttributeError:
!             raise error, \
!                   "un-deep-copyable object of type %s" % type(x)
!         y = copier(memo)
      else:
          y = copierfunction(x, memo)
--- 163,175 ----
              copier = x.__deepcopy__
          except AttributeError:
!             try:
!                 reductor = x.__reduce__
!             except AttributeError:
!                 raise error, \
!                       "un-deep-copyable object of type %s" % type(x)
!             else:
!                 y = _reconstruct(x, reductor(), 1)
!         else:
!             y = copier(memo)
      else:
          y = copierfunction(x, memo)
***************
*** 259,262 ****
--- 271,294 ----
      return y
  d[types.InstanceType] = _deepcopy_inst
+ 
+ def _reconstruct(x, info, deep):
+     if isinstance(info, str):
+         return x
+     assert isinstance(info, tuple)
+     n = len(info)
+     assert n in (2, 3)
+     callable, args = info[:2]
+     if n > 2:
+         state = info[2]
+     else:
+         state = {}
+     if deep:
+         args = deepcopy(args)
+     y = callable(*args)
+     if state:
+         if deep:
+             state = deepcopy(state)
+         y.__dict__.update(state)
+     return y
  
  del d

Index: copy_reg.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/copy_reg.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** copy_reg.py	2001/09/25 19:46:05	1.7
--- copy_reg.py	2001/09/28 18:13:29	1.8
***************
*** 55,57 ****
      else:
          state = base(self)
!     return _reconstructor, (self.__class__, base, state), self.__dict__
--- 55,65 ----
      else:
          state = base(self)
!     args = (self.__class__, base, state)
!     try:
!         dict = self.__dict__
!     except AttributeError:
!         dict = None
!     if dict:
!         return _reconstructor, args, dict
!     else:
!         return _reconstructor, args