Skip to content

Recursive functions with recurse=True not serializing #344

@sandan

Description

@sandan

Hi, I've been using dill to serialize objects and deserialize them in different python sessions. I encountered an issue with deserializing a function that calls a recursive function. Here is a reprex using python 3.4.5 with dill==0.2.9 via ipython (6.5.0):

In [1]: def add_helper(x, y):
   ...:     if x < 0 or y < 0:
   ...:         raise ValueException("x and y must be non-negative.")
   ...:     else:
   ...:         return add(x, y)
   ...:

In [2]: def add(x, y):
   ...:     if x == 0:
   ...:         return y
   ...:     else:
   ...:         return  add(x - 1, y + 1)
   ...:

In [3]: import dill

In [4]: dill.dumps(add_helper, recurse = True)
Out[4]: b'\x80\x03cdill._dill\n_create_function\nq\x00(cdill._dill\n_load_type\nq\x01X\x08\x00\x00\x00CodeTypeq\x02\x85q\x03Rq\x04(K\x02K\x00K\x02K\x03KCC8|\x00\x00d\x01\x00k\x00\x00s\x18\x00|\x01\x00d\x01\x00k\x00\x00r\'\x00t\x00\x00d\x02\x00\x83\x01\x00\x82\x01\x00n\r\x00t\x01\x00|\x00\x00|\x01\x00\x83\x02\x00Sd\x00\x00Sq\x05NK\x00X\x1d\x00\x00\x00x and y must be non-negative.q\x06\x87q\x07X\x0e\x00\x00\x00ValueExceptionq\x08X\x03\x00\x00\x00addq\t\x86q\nX\x01\x00\x00\x00xq\x0bX\x01\x00\x00\x00yq\x0c\x86q\rX\x1e\x00\x00\x00<ipython-input-1-e5e45fe5f7b3>q\x0eX\n\x00\x00\x00add_helperq\x0fK\x01C\x06\x00\x01\x18\x01\x0f\x02q\x10))tq\x11Rq\x12}q\x13X\x03\x00\x00\x00addq\x14h\x00(h\x04(K\x02K\x00K\x02K\x04KCC)|\x00\x00d\x01\x00k\x02\x00r\x10\x00|\x01\x00St\x00\x00|\x00\x00d\x02\x00\x18|\x01\x00d\x02\x00\x17\x83\x02\x00Sd\x00\x00Sq\x15NK\x00K\x01\x87q\x16h\t\x85q\x17h\x0bh\x0c\x86q\x18X\x1e\x00\x00\x00<ipython-input-2-9fb32d25e78f>q\x19h\tK\x01C\x06\x00\x01\x0c\x01\x04\x02q\x1a))tq\x1bRq\x1c}q\x1dX\x03\x00\x00\x00addq\x1eh\x00(h\x1cc__builtin__\n__main__\nh\tNN}q\x1ftq Rq!sh\tNNh\x1ftq"R0h!sh\x0fNN}q#tq$Rq%.'

In [5]: e = dill.loads(b'\x80\x03cdill._dill\n_create_function\nq\x00(cdill._dill\n_load_type\nq\x01X\x08\x00\x00\x00CodeTypeq\x02\x85q\x03Rq\x04(K\x02K\x00K\x02K\x03KCC8|\x00\x00d\x01\x00k\x00\x00s\x18\x
   ...: 00|\x01\x00d\x01\x00k\x00\x00r\'\x00t\x00\x00d\x02\x00\x83\x01\x00\x82\x01\x00n\r\x00t\x01\x00|\x00\x00|\x01\x00\x83\x02\x00Sd\x00\x00Sq\x05NK\x00X\x1d\x00\x00\x00x and y must be non-negative.q\x0
   ...: 6\x87q\x07X\x0e\x00\x00\x00ValueExceptionq\x08X\x03\x00\x00\x00addq\t\x86q\nX\x01\x00\x00\x00xq\x0bX\x01\x00\x00\x00yq\x0c\x86q\rX\x1e\x00\x00\x00<ipython-input-1-e5e45fe5f7b3>q\x0eX\n\x00\x00\x00
   ...: add_helperq\x0fK\x01C\x06\x00\x01\x18\x01\x0f\x02q\x10))tq\x11Rq\x12}q\x13X\x03\x00\x00\x00addq\x14h\x00(h\x04(K\x02K\x00K\x02K\x04KCC)|\x00\x00d\x01\x00k\x02\x00r\x10\x00|\x01\x00St\x00\x00|\x00\
   ...: x00d\x02\x00\x18|\x01\x00d\x02\x00\x17\x83\x02\x00Sd\x00\x00Sq\x15NK\x00K\x01\x87q\x16h\t\x85q\x17h\x0bh\x0c\x86q\x18X\x1e\x00\x00\x00<ipython-input-2-9fb32d25e78f>q\x19h\tK\x01C\x06\x00\x01\x0c\x
   ...: 01\x04\x02q\x1a))tq\x1bRq\x1c}q\x1dX\x03\x00\x00\x00addq\x1eh\x00(h\x1cc__builtin__\n__main__\nh\tNN}q\x1ftq Rq!sh\tNNh\x1ftq"R0h!sh\x0fNN}q#tq$Rq%.')

In [6]: e(1,2)
Out[6]: 3

In [7]: exit
(ml_dev) ~/Desktop/hackathon/:ls-client$ ipython
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:47:57)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import dill

In [2]: e = dill.loads(b'\x80\x03cdill._dill\n_create_function\nq\x00(cdill._dill\n_load_type\nq\x01X\x08\x00\x00\x00CodeTypeq\x02\x85q\x03Rq\x04(K\x02K\x00K\x02K\x03KCC8|\x00\x00d\x01\x00k\x00\x00s\x18\x
   ...: 00|\x01\x00d\x01\x00k\x00\x00r\'\x00t\x00\x00d\x02\x00\x83\x01\x00\x82\x01\x00n\r\x00t\x01\x00|\x00\x00|\x01\x00\x83\x02\x00Sd\x00\x00Sq\x05NK\x00X\x1d\x00\x00\x00x and y must be non-negative.q\x0
   ...: 6\x87q\x07X\x0e\x00\x00\x00ValueExceptionq\x08X\x03\x00\x00\x00addq\t\x86q\nX\x01\x00\x00\x00xq\x0bX\x01\x00\x00\x00yq\x0c\x86q\rX\x1e\x00\x00\x00<ipython-input-1-e5e45fe5f7b3>q\x0eX\n\x00\x00\x00
   ...: add_helperq\x0fK\x01C\x06\x00\x01\x18\x01\x0f\x02q\x10))tq\x11Rq\x12}q\x13X\x03\x00\x00\x00addq\x14h\x00(h\x04(K\x02K\x00K\x02K\x04KCC)|\x00\x00d\x01\x00k\x02\x00r\x10\x00|\x01\x00St\x00\x00|\x00\
   ...: x00d\x02\x00\x18|\x01\x00d\x02\x00\x17\x83\x02\x00Sd\x00\x00Sq\x15NK\x00K\x01\x87q\x16h\t\x85q\x17h\x0bh\x0c\x86q\x18X\x1e\x00\x00\x00<ipython-input-2-9fb32d25e78f>q\x19h\tK\x01C\x06\x00\x01\x0c\x
   ...: 01\x04\x02q\x1a))tq\x1bRq\x1c}q\x1dX\x03\x00\x00\x00addq\x1eh\x00(h\x1cc__builtin__\n__main__\nh\tNN}q\x1ftq Rq!sh\tNNh\x1ftq"R0h!sh\x0fNN}q#tq$Rq%.')

In [3]: e(1,2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-c65c902cd747> in <module>()
----> 1 e(1,2)

<ipython-input-1-e5e45fe5f7b3> in add_helper(x, y)

<ipython-input-2-9fb32d25e78f> in add(x, y)

NameError: name 'add' is not defined

I was expecting add to be serialized as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions