-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Manually setting module to __main__
for callables causes errors starting in 0.3.5
#482
Comments
That is very odd. Based on the error message, it seems like the issue is with pickling something in the import dill
x = attr._compat.make_set_closure_cell()
dill.dumps(x) it pickles just fine. What is clear right now is that dill is delegating a function to the default Python pickler when it shouldn't. |
Ah. I found this issue. I guess this was a very old cryptic bug that was present since the Python 2/3 transition that was never fixed. You stumbled across the bug after I had made modifications to the function pickling code, which now used the code that wasn't transitioned when it didn't before. The problem is that dill is looking for This is the code that is outdated: Lines 1133 to 1139 in 78e3d55
This is the corrected Python 3 code: @mmckerns I assumed that this code made its way into the repo, but I guess I had added it to #450 directly because I needed this feature immediately and had forgotten to open a separate PR for it. Opening one now. |
I am fairly convinced that this is the problem. I will open a PR to add this to dill 0.3.5.2 (if this is still coming out.) Also, dill 0.3.5.* fixes a bug that made it so that dill didn't recurse all of the way down all functions, stopping early in a couple of cases that it shouldn't have. To make this code work in both dill 0.3.5 and earlier versions, use dill.dumps(func, recurse=True) or set the setting globally dill.settings['recurse'] = True I know that it is a little bizarre that to limit the amount of recursion that dill is doing to pickle the function, we need to turn on a setting called recurse, but that is because the setting actually recurses over the global dictionary and finds the smallest subset that the function needs to run, which will limit the number of objects that dill needs to include in the pickle.
Without this setting, dill will save the entire @dwhswenson Does this fix your test suite for dill 0.3.5? |
Thanks for working on this @anivegesana (and sorry for slow replies -- the affected project is passion project; day job gets in the way). Using TypeError: cannot pickle 'EncodedFile' object Full error (from pytest, just showing
|
In your particular case, it makes sense to use |
Context: Our code uses dill to serialize callables only under certain circumstances, including when the callable's module is
__main__
. We want to be able to test this at an integration level, using dill on a function wherefunc.__module__ == __main__
.Prior to the release of 0.3.5, we did this by manually setting the
__module__
attribute. However, our tests have been failing since the release of 0.3.5.I'm not sure if this is supposed to be an allowed use case. But is there supposed to be another way that I make it look like my functions were defined in
__main__
(for the part our code manages) such that dill can also handle it, when really I'm running under pytest or another testing framework?MCVE: In an environment with pytest and dill installed:
Output in 0.3.4:
Output in 0.3.5.1
Tracebacks, as reported by pytest:
test_dill_lambda
test_dill_func
The text was updated successfully, but these errors were encountered: