Skip to content

Commit 1476818

Browse files
committed
Use bare FunctionType instead of _create_function()
1 parent d2f916c commit 1476818

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

dill/_dill.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -756,22 +756,6 @@ def _load_type(name):
756756
def _create_type(typeobj, *args):
757757
return typeobj(*args)
758758

759-
def _create_function(fcode, fglobals, fname=None, fdefaults=None,
760-
fclosure=None, fdict=None, fkwdefaults=None):
761-
# same as FunctionType, but enable passing __dict__ to new function,
762-
# __dict__ is the storehouse for attributes added after function creation
763-
func = FunctionType(fcode, fglobals or dict(), fname, fdefaults, fclosure)
764-
if fdict is not None:
765-
func.__dict__.update(fdict) #XXX: better copy? option to copy?
766-
if fkwdefaults is not None:
767-
func.__kwdefaults__ = fkwdefaults
768-
# 'recurse' only stores referenced modules/objects in fglobals,
769-
# thus we need to make sure that we have __builtins__ as well
770-
if "__builtins__" not in func.__globals__:
771-
func.__globals__["__builtins__"] = globals()["__builtins__"]
772-
# assert id(fglobals) == id(func.__globals__)
773-
return func
774-
775759
def _create_code(*args):
776760
if PY3 and hasattr(args[-3], 'encode'): #FIXME: from PY2 fails (optcode)
777761
args = list(args)
@@ -1905,6 +1889,7 @@ def save_function(pickler, obj):
19051889
# recurse to get all globals referred to by obj
19061890
from .detect import globalvars
19071891
globs_copy = globalvars(obj, recurse=True, builtin=True)
1892+
globs_copy.setdefault('__builtins__', globals()['__builtins__'])
19081893

19091894
# Add the name of the module to the globs dictionary to prevent
19101895
# the duplication of the dictionary. Pickle the unpopulated
@@ -1960,7 +1945,7 @@ def save_function(pickler, obj):
19601945
if state_dict:
19611946
state = state, state_dict
19621947

1963-
_save_with_postproc(pickler, (_create_function, (
1948+
_save_with_postproc(pickler, (FunctionType, (
19641949
obj.__code__, globs, obj.__name__, obj.__defaults__,
19651950
closure
19661951
), state), obj=obj, postproc_list=postproc_list)
@@ -1973,7 +1958,7 @@ def save_function(pickler, obj):
19731958
if obj.__dict__:
19741959
postproc_list.append((setattr, (obj, '__dict__', obj.__dict__)))
19751960

1976-
_save_with_postproc(pickler, (_create_function, (
1961+
_save_with_postproc(pickler, (FunctionType, (
19771962
obj.func_code, globs, obj.func_name, obj.func_defaults,
19781963
closure
19791964
)), obj=obj, postproc_list=postproc_list)

dill/_shims.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,22 @@ def _delattr(cell, name):
264264

265265
_setattr = Getattr(_dill, '_setattr', setattr)
266266
_delattr = Getattr(_dill, '_delattr', delattr)
267+
268+
269+
# Kept for backward compatibility, substituted by bare FunctionType.
270+
@move_to(_dill)
271+
def _create_function(fcode, fglobals, fname=None, fdefaults=None,
272+
fclosure=None, fdict=None, fkwdefaults=None):
273+
# 'recurse' only stores referenced modules/objects in fglobals,
274+
# thus we need to make sure that we have __builtins__ as well
275+
if fglobals is None:
276+
fglobals = {}
277+
fglobals.setdefault('__builtins__', globals()['__builtins__'])
278+
# same as FunctionType, but enable passing __dict__ to new function,
279+
# __dict__ is the storehouse for attributes added after function creation
280+
func = _dill.FunctionType(fcode, fglobals, fname, fdefaults, fclosure)
281+
if fdict:
282+
func.__dict__.update(fdict) #XXX: better copy? option to copy?
283+
if fkwdefaults is not None:
284+
func.__kwdefaults__ = fkwdefaults
285+
return func

0 commit comments

Comments
 (0)