Get rid of __safe_for_unpickling__ and safe_constructors.

Also tidied up a few lines, got rid of apply(), added a comment.
This commit is contained in:
Guido van Rossum 2003-01-28 22:29:13 +00:00
parent dcaa24e503
commit b26a97aa50
1 changed files with 12 additions and 28 deletions

View File

@ -27,7 +27,7 @@ Misc variables:
__version__ = "$Revision$" # Code version __version__ = "$Revision$" # Code version
from types import * from types import *
from copy_reg import dispatch_table, safe_constructors, _reconstructor from copy_reg import dispatch_table, _reconstructor
import marshal import marshal
import sys import sys
import struct import struct
@ -375,8 +375,8 @@ class Pickler:
if getnewargs: if getnewargs:
args = getnewargs() # This bette not reference obj args = getnewargs() # This bette not reference obj
else: else:
for cls in int, long, float, complex, str, unicode, tuple: for cls in int, long, float, complex, str, UnicodeType, tuple:
if isinstance(obj, cls): if cls and isinstance(obj, cls):
args = (cls(obj),) args = (cls(obj),)
break break
else: else:
@ -1030,10 +1030,7 @@ class Unpickler:
pass pass
if not instantiated: if not instantiated:
try: try:
if not hasattr(klass, '__safe_for_unpickling__'): value = klass(*args)
raise UnpicklingError('%s is not safe for unpickling' %
klass)
value = apply(klass, args)
except TypeError, err: except TypeError, err:
raise TypeError, "in constructor for %s: %s" % ( raise TypeError, "in constructor for %s: %s" % (
klass.__name__, str(err)), sys.exc_info()[2] klass.__name__, str(err)), sys.exc_info()[2]
@ -1059,7 +1056,7 @@ class Unpickler:
# prohibited # prohibited
pass pass
if not instantiated: if not instantiated:
value = apply(klass, args) value = klass(*args)
self.append(value) self.append(value)
dispatch[OBJ] = load_obj dispatch[OBJ] = load_obj
@ -1078,6 +1075,7 @@ class Unpickler:
dispatch[GLOBAL] = load_global dispatch[GLOBAL] = load_global
def find_class(self, module, name): def find_class(self, module, name):
# Subclasses may override this
__import__(module) __import__(module)
mod = sys.modules[module] mod = sys.modules[module]
klass = getattr(mod, name) klass = getattr(mod, name)
@ -1085,30 +1083,16 @@ class Unpickler:
def load_reduce(self): def load_reduce(self):
stack = self.stack stack = self.stack
args = stack.pop()
callable = stack[-2] func = stack[-1]
arg_tup = stack[-1] if args is None:
del stack[-2:]
if type(callable) is not ClassType:
if not callable in safe_constructors:
try:
safe = callable.__safe_for_unpickling__
except AttributeError:
safe = None
if not safe:
raise UnpicklingError, "%s is not safe for " \
"unpickling" % callable
if arg_tup is None:
# A hack for Jim Fulton's ExtensionClass, now deprecated # A hack for Jim Fulton's ExtensionClass, now deprecated
warnings.warn("__basicnew__ special case is deprecated", warnings.warn("__basicnew__ special case is deprecated",
DeprecationWarning) DeprecationWarning)
value = callable.__basicnew__() value = func.__basicnew__()
else: else:
value = apply(callable, arg_tup) value = func(*args)
self.append(value) stack[-1] = value
dispatch[REDUCE] = load_reduce dispatch[REDUCE] = load_reduce
def load_pop(self): def load_pop(self):