-
-
Notifications
You must be signed in to change notification settings - Fork 181
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
Cannot use callable that was pickled within pytest #623
Comments
Funnily enough, it works when I do this before pickling: foo.__globals__.pop(foo.__name__) |
I want to make sure I'm understanding this correctly, but running your script normally works, however if you run under the control of |
That's what I thought, but now I realized this is actually a pathing issue. $ python tests/dill_test.py
ok
$ cd tests
$ pytest dill_test.py
ok
$ pytest tests/dill_test.py
NOT OK So in the latter case, test_script = dedent(f"""
import dill
import sys
sys.path.append("{os.path.dirname(__file__)}")
with open("{picklefile}", "rb") as f:
func = dill.load(f)
func()
""") Is there a way to pickle a function so it can be executed even if the original module isn't available when unpickling? |
Generally, |
But why is this module a dependency in the first place? The function doesn't access any globals. |
The global dict is required to create a function object.
However, |
Thanks, I think I understand the problem now. |
you can often see what's going on with |
Okay here goes nothing. This is the case that works: $ python tests/dill_test.py
┬ F1: <function foo at 0x102580040>
├┬ F2: <function _create_function at 0x102fb32e0>
│└ # F2 [34 B]
├┬ Co: <code object foo at 0x102755b00, file "/private/tmp/tests/dill_test.py", line 6>
│├┬ F2: <function _create_code at 0x102fb3370>
││└ # F2 [19 B]
│└ # Co [102 B]
├┬ D2: <dict object at 0x0102fc49c0>
│└ # D2 [25 B]
├┬ D2: <dict object at 0x0102956a00>
│└ # D2 [2 B]
├┬ D2: <dict object at 0x0102fc4b80>
│├┬ D2: <dict object at 0x0102938ac0>
││└ # D2 [2 B]
│└ # D2 [23 B]
└ # F1 [198 B] This is the one that doesn't: $ pytest tests/dill_test.py
┬ F2: <function foo at 0x104473be0>
└ # F2 [20 B] So if pytest is involved, dill doesn't even try to pickle any of the function's attributes...? |
Essentially, yes. "F2" is passing the function off to |
Isn't it the other way around? According to https://github.com/uqfoundation/dill/blob/master/dill/_dill.py#L1881C12-L1881C12, dill uses the stock pickler when |
Yes, you are correct. I missed the |
Could you imagine having a flag similar to |
yes, there is a PR that is mostly done that handles a bunch of module serialization variants. work on it seems to have stalled a bit though. |
I am running tests that serialize callables with
dill
and try to load them in a subprocess to make sure everything worked correctly. I am getting a cryptic error when trying to load the callable from the subprocess, presumably because dill is failing to load the test module.Example:
Calling through
pytest
gives this error:Calling it directly works:
The text was updated successfully, but these errors were encountered: