Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dill.loads doesn't return pickled class when class was imported from a file #330

Closed
bariod opened this issue Aug 16, 2019 · 4 comments
Closed
Milestone

Comments

@bariod
Copy link

bariod commented Aug 16, 2019

I'm facing the same problem as @pranav9056 in #243.
When the to-be-pickled class is in a file and edited after doing so, dill.loads will return the updated class, despite using the ignore=True option.

The follwoing code is pretty much the same as this SO example from @mmckerns but with a class defined in a file:

>>> import dill
>>> import importlib
>>> import test_file  # file with class
>>> f = test_file.Foo()
>>> _f = dill.dumps(f)

*changing class Foo in test_file.py*

>>> importlib.reload(test_file)
<module 'test_file' from 'C:\\test_file.py'>
>>> g = test_file.Foo()
>>> f_ = dill.loads(_f, ignore=True)
>>> f_.bar()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: bar() missing 1 required positional argument: 'x'
>>> g.bar()  # expected behavior
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: bar() missing 1 required positional argument: 'x'
@bariod
Copy link
Author

bariod commented Aug 16, 2019

I have noticed that dumps returns a rather small byte string in this case (b'\x80\x03ctest_file\nFoo\nq\x00)\x81q\x01.');
whereas the SO example returns this: b'\x80\x03cdill._dill\n_create_type\nq\x00(cdill._dill\n_load_type\nq\x01X\x04\x00\x00\x00typeq\x02\x85q\x03Rq\x04X\x03\x00\x00\x00Fooq\x05h\x01X\x06\x00\x00\x00objectq\x06\x85q\x07Rq\x08\x85q\t}q\n(X\n\x00\x00\x00__module__q\x0bX\x08\x00\x00\x00__main__q\x0cX\x03\x00\x00\x00barq\rcdill._dill\n_create_function\nq\x0e(h\x01X\x08\x00\x00\x00CodeTypeq\x0f\x85q\x10Rq\x11(K\x01K\x00K\x01K\x01KCC\x04d\x01S\x00q\x12NK\x05\x86q\x13)X\x04\x00\x00\x00selfq\x14\x85q\x15X\x07\x00\x00\x00<input>q\x16h\rK\x04C\x02\x00\x01q\x17))tq\x18Rq\x19c__builtin__\n__main__\nh\rNN}q\x1atq\x1bRq\x1cX\x07\x00\x00\x00__doc__q\x1dNX\r\x00\x00\x00__slotnames__q\x1e]q\x1futq Rq!)\x81q".'

@vtyw
Copy link

vtyw commented Jan 5, 2021

I believe this limitation is the same limitation @mmckerns explained in their answer on Stack Overflow, specifically the last example: https://stackoverflow.com/a/32364403/1048456

So, back in 2015, it appeared that there was no intended support for saving a class defined in a module. Has anything specifically changed since then? (It would seem not as the same limitation persists.)

@vtyw
Copy link

vtyw commented Jan 5, 2021

On further reading, I found #128, which was created from that SO post. I believe this issue (#330) is equivalent to #128.

@mmckerns
Copy link
Member

mmckerns commented Jan 5, 2021

@vtyw: you are correct.

@mmckerns mmckerns closed this as completed Jan 5, 2021
@mmckerns mmckerns added this to the dill-0.3.4 milestone Jun 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants