[Python-checkins] CVS: python/dist/src/Lib pickle.py,1.54,1.55

Barry Warsaw bwarsaw@users.sourceforge.net
Thu, 15 Nov 2001 15:43:00 -0800


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

Modified Files:
	pickle.py 
Log Message:
Two changes:

load_inst(): Implement the security hook that cPickle already had.
When unpickling callables which are not classes, we look to see if the
object has an attribute __safe_for_unpickling__.  If this exists and
has a true value, then we can call it to create the unpickled object.
Otherwise we raise an UnpicklingError.

find_class(): We no longer mask ImportError, KeyError, and
AttributeError by transforming them into SystemError.  The latter is
definitely not the right thing to do, so we let the former three
exceptions simply propagate up if they occur, i.e. we remove the
try/except!


Index: pickle.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickle.py,v
retrieving revision 1.54
retrieving revision 1.55
diff -C2 -d -r1.54 -r1.55
*** pickle.py	2001/11/09 16:15:04	1.54
--- pickle.py	2001/11/15 23:42:58	1.55
***************
*** 770,773 ****
--- 770,776 ----
          if not instantiated:
              try:
+                 if not hasattr(klass, '__safe_for_unpickling__'):
+                     raise UnpicklingError('%s is not safe for unpickling' %
+                                           klass)
                  value = apply(klass, args)
              except TypeError, err:
***************
*** 808,819 ****
  
      def find_class(self, module, name):
!         try:
!             __import__(module)
!             mod = sys.modules[module]
!             klass = getattr(mod, name)
!         except (ImportError, KeyError, AttributeError):
!             raise SystemError, \
!                   "Failed to import class %s from module %s" % \
!                   (name, module)
          return klass
  
--- 811,817 ----
  
      def find_class(self, module, name):
!         __import__(module)
!         mod = sys.modules[module]
!         klass = getattr(mod, name)
          return klass