[Python-checkins] python/dist/src/Lib pickle.py,1.127,1.128

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Thu, 30 Jan 2003 07:41:59 -0800


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

Modified Files:
	pickle.py 
Log Message:
load_inst(), load_obj():  Put the bulk of these into a common new
_instantiate() method.


Index: pickle.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickle.py,v
retrieving revision 1.127
retrieving revision 1.128
diff -C2 -d -r1.127 -r1.128
*** pickle.py	30 Jan 2003 06:37:41 -0000	1.127
--- pickle.py	30 Jan 2003 15:41:46 -0000	1.128
***************
*** 1066,1079 ****
      dispatch[DICT] = load_dict
  
!     def load_inst(self):
!         k = self.marker()
          args = tuple(self.stack[k+1:])
          del self.stack[k:]
-         module = self.readline()[:-1]
-         name = self.readline()[:-1]
-         klass = self.find_class(module, name)
          instantiated = 0
!         if (not args and type(klass) is ClassType and
!             not hasattr(klass, "__getinitargs__")):
              try:
                  value = _EmptyClass()
--- 1066,1081 ----
      dispatch[DICT] = load_dict
  
!     # INST and OBJ differ only in how they get a class object.  It's not
!     # only sensible to do the rest in a common routine, the two routines
!     # previously diverged and grew different bugs.
!     # klass is the class to instantiate, and k points to the topmost mark
!     # object, following which are the arguments for klass.__init__.
!     def _instantiate(self, klass, k):
          args = tuple(self.stack[k+1:])
          del self.stack[k:]
          instantiated = 0
!         if (not args and
!                 type(klass) is ClassType and
!                 not hasattr(klass, "__getinitargs__")):
              try:
                  value = _EmptyClass()
***************
*** 1091,1117 ****
                      klass.__name__, str(err)), sys.exc_info()[2]
          self.append(value)
      dispatch[INST] = load_inst
  
      def load_obj(self):
!         stack = self.stack
          k = self.marker()
!         klass = stack[k + 1]
!         del stack[k + 1]
!         args = tuple(stack[k + 1:])
!         del stack[k:]
!         instantiated = 0
!         if (not args and type(klass) is ClassType and
!             not hasattr(klass, "__getinitargs__")):
!             try:
!                 value = _EmptyClass()
!                 value.__class__ = klass
!                 instantiated = 1
!             except RuntimeError:
!                 # In restricted execution, assignment to inst.__class__ is
!                 # prohibited
!                 pass
!         if not instantiated:
!             value = klass(*args)
!         self.append(value)
      dispatch[OBJ] = load_obj
  
--- 1093,1109 ----
                      klass.__name__, str(err)), sys.exc_info()[2]
          self.append(value)
+ 
+     def load_inst(self):
+         module = self.readline()[:-1]
+         name = self.readline()[:-1]
+         klass = self.find_class(module, name)
+         self._instantiate(klass, self.marker())
      dispatch[INST] = load_inst
  
      def load_obj(self):
!         # Stack is ... markobject classobject arg1 arg2 ...
          k = self.marker()
!         klass = self.stack.pop(k+1)
!         self._instantiate(klass, k)
      dispatch[OBJ] = load_obj