Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b26a97a

Browse files
committed
Get rid of __safe_for_unpickling__ and safe_constructors.
Also tidied up a few lines, got rid of apply(), added a comment.
1 parent dcaa24e commit b26a97a

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

Lib/pickle.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
__version__ = "$Revision$" # Code version
2828

2929
from types import *
30-
from copy_reg import dispatch_table, safe_constructors, _reconstructor
30+
from copy_reg import dispatch_table, _reconstructor
3131
import marshal
3232
import sys
3333
import struct
@@ -375,8 +375,8 @@ def save_newobj(self, obj):
375375
if getnewargs:
376376
args = getnewargs() # This bette not reference obj
377377
else:
378-
for cls in int, long, float, complex, str, unicode, tuple:
379-
if isinstance(obj, cls):
378+
for cls in int, long, float, complex, str, UnicodeType, tuple:
379+
if cls and isinstance(obj, cls):
380380
args = (cls(obj),)
381381
break
382382
else:
@@ -1030,10 +1030,7 @@ def load_inst(self):
10301030
pass
10311031
if not instantiated:
10321032
try:
1033-
if not hasattr(klass, '__safe_for_unpickling__'):
1034-
raise UnpicklingError('%s is not safe for unpickling' %
1035-
klass)
1036-
value = apply(klass, args)
1033+
value = klass(*args)
10371034
except TypeError, err:
10381035
raise TypeError, "in constructor for %s: %s" % (
10391036
klass.__name__, str(err)), sys.exc_info()[2]
@@ -1059,7 +1056,7 @@ def load_obj(self):
10591056
# prohibited
10601057
pass
10611058
if not instantiated:
1062-
value = apply(klass, args)
1059+
value = klass(*args)
10631060
self.append(value)
10641061
dispatch[OBJ] = load_obj
10651062

@@ -1078,37 +1075,24 @@ def load_global(self):
10781075
dispatch[GLOBAL] = load_global
10791076

10801077
def find_class(self, module, name):
1078+
# Subclasses may override this
10811079
__import__(module)
10821080
mod = sys.modules[module]
10831081
klass = getattr(mod, name)
10841082
return klass
10851083

10861084
def load_reduce(self):
10871085
stack = self.stack
1088-
1089-
callable = stack[-2]
1090-
arg_tup = stack[-1]
1091-
del stack[-2:]
1092-
1093-
if type(callable) is not ClassType:
1094-
if not callable in safe_constructors:
1095-
try:
1096-
safe = callable.__safe_for_unpickling__
1097-
except AttributeError:
1098-
safe = None
1099-
1100-
if not safe:
1101-
raise UnpicklingError, "%s is not safe for " \
1102-
"unpickling" % callable
1103-
1104-
if arg_tup is None:
1086+
args = stack.pop()
1087+
func = stack[-1]
1088+
if args is None:
11051089
# A hack for Jim Fulton's ExtensionClass, now deprecated
11061090
warnings.warn("__basicnew__ special case is deprecated",
11071091
DeprecationWarning)
1108-
value = callable.__basicnew__()
1092+
value = func.__basicnew__()
11091093
else:
1110-
value = apply(callable, arg_tup)
1111-
self.append(value)
1094+
value = func(*args)
1095+
stack[-1] = value
11121096
dispatch[REDUCE] = load_reduce
11131097

11141098
def load_pop(self):

0 commit comments

Comments
 (0)