diff --git a/dill/_dill.py b/dill/_dill.py index 664ce3fc..33af0843 100644 --- a/dill/_dill.py +++ b/dill/_dill.py @@ -578,6 +578,23 @@ def use_diff(on=True): import diff as d diff = d +def _create_code(*args): + if PY3 and hasattr(args[-3], 'encode'): #FIXME: from PY2 fails (optcode) + args = list(args) + args[-3] = args[-3].encode() # co_lnotab + args[-10] = args[-10].encode() # co_code + if hasattr(CodeType, 'co_posonlyargcount'): + if len(args) == 16: return CodeType(*args) + elif len(args) == 15: return CodeType(args[0], 0, *args[1:]) + return CodeType(args[0], 0, 0, *args[1:]) + elif hasattr(CodeType, 'co_kwonlyargcount'): + if len(args) == 16: return CodeType(args[0], *args[2:]) + elif len(args) == 15: return CodeType(*args) + return CodeType(args[0], 0, *args[1:]) + if len(args) == 16: return CodeType(args[0], *args[3:]) + elif len(args) == 15: return CodeType(args[0], *args[2:]) + return CodeType(*args) + def _create_typemap(): import types if PY3: @@ -609,6 +626,7 @@ def _create_typemap(): 'PyBufferedReaderType': PyBufferedReaderType, 'PyBufferedWriterType': PyBufferedWriterType, 'PyTextWrapperType': PyTextWrapperType, + 'CodeType': _create_code, }) if ExitType: _reverse_typemap['ExitType'] = ExitType @@ -650,23 +668,6 @@ def _create_function(fcode, fglobals, fname=None, fdefaults=None, func.__globals__["__builtins__"] = globals()["__builtins__"] return func -def _create_code(*args): - if PY3 and hasattr(args[-3], 'encode'): #FIXME: from PY2 fails (optcode) - args = list(args) - args[-3] = args[-3].encode() # co_lnotab - args[-10] = args[-10].encode() # co_code - if hasattr(CodeType, 'co_posonlyargcount'): - if len(args) == 16: return CodeType(*args) - elif len(args) == 15: return CodeType(args[0], 0, *args[1:]) - return CodeType(args[0], 0, 0, *args[1:]) - elif hasattr(CodeType, 'co_kwonlyargcount'): - if len(args) == 16: return CodeType(args[0], *args[2:]) - elif len(args) == 15: return CodeType(*args) - return CodeType(args[0], 0, *args[1:]) - if len(args) == 16: return CodeType(args[0], *args[3:]) - elif len(args) == 15: return CodeType(args[0], *args[2:]) - return CodeType(*args) - def _create_ftype(ftypeobj, func, args, kwds): if kwds is None: kwds = {} diff --git a/tests/lambda.pkl b/tests/lambda.pkl new file mode 100644 index 00000000..7d7c09cc Binary files /dev/null and b/tests/lambda.pkl differ diff --git a/tests/test_functions.py b/tests/test_functions.py index 23de5f09..1213fca3 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -6,6 +6,7 @@ # - https://github.com/uqfoundation/dill/blob/master/LICENSE import dill +import os import sys dill.settings['recurse'] = True @@ -30,6 +31,14 @@ def function_d(d, d1, d2=1): return d + d1 + d2 +def lambda_a(): + pkl = os.path.join( + os.path.dirname(__file__), + "lambda.pkl") + with open(pkl,"rb") as fh: + fn = dill.load(fh) + assert fn is not None + if is_py3(): exec(''' def function_e(e, *e1, e2=1, e3=2): @@ -65,3 +74,4 @@ def test_functions(): if __name__ == '__main__': test_functions() + lambda_a()