From ec96fce3aed30d13e384767666a5776e02924b3a Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 12 Mar 2021 21:02:23 +0000 Subject: [PATCH 1/2] Use _create_code Logic when loading Pickles --- dill/_dill.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/dill/_dill.py b/dill/_dill.py index 7658aba1..07debb35 100644 --- a/dill/_dill.py +++ b/dill/_dill.py @@ -534,6 +534,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: @@ -565,6 +582,7 @@ def _create_typemap(): 'PyBufferedReaderType': PyBufferedReaderType, 'PyBufferedWriterType': PyBufferedWriterType, 'PyTextWrapperType': PyTextWrapperType, + 'CodeType': _create_code, }) if ExitType: _reverse_typemap['ExitType'] = ExitType @@ -602,23 +620,6 @@ def _create_function(fcode, fglobals, fname=None, fdefaults=None, func.__kwdefaults__ = fkwdefaults 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 = {} From 0b7fd9a1760aee0ef02886ca6c8c68cb32539957 Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 1 Jun 2021 20:01:43 +0100 Subject: [PATCH 2/2] add tests case --- tests/lambda.pkl | Bin 0 -> 217 bytes tests/test_functions.py | 10 ++++++++++ 2 files changed, 10 insertions(+) create mode 100644 tests/lambda.pkl diff --git a/tests/lambda.pkl b/tests/lambda.pkl new file mode 100644 index 0000000000000000000000000000000000000000..7d7c09cc17e843dc957260e3f2549b19e5174921 GIT binary patch literal 217 zcmY+6v1-FG7=-1-O;aUJ`vjfi!J!ExP%v3M8a%iiy#2_MLIkqx3oMd7HkHa)yP-elBrjTcO{dODZyLwJ*8h6j2u1$dYL1|sGse8~MU(qmd6TxUVX z1_fD~hCl3+;UVuai^^aaf8(*oynGulG40i#8y6ebc0S(5@2BlS$&X6v-TsKD@jS7j ou9P@}XOom+5k(%$0?(4D>WuZq5pkkgjBw_8{)?5zOM%zT9o6nXhyVZp literal 0 HcmV?d00001 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()