You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is cross posted from pytest-dev/pytest#12448, because the pytest devs suggest that this is a problem with dill.
Description of the Problem
When I attempt to dill a function defined inside of a doctest, pytest raises an error, complaining that TypeError: cannot pickle 'EncodedFile' object. Best I can tell, this is the EncodedFile object in pytest.
This error only occurs when I run those doctests via pytest. They run just fine via vanilla doctest. The same code also works just fine in a script and as a regular pytest test function.
I fear that this is related to pytest-dev/pytest#10845, which doesn't seem to have a solution.
Minimal Reproducible Example (Serialization)
classMyClass:
"""Running a file containing this class with `python -m pytest this_file.py --doctest-modules` will fail with `TypeError: cannot pickle 'EncodedFile' object`. Examples: >>> def template_function(): ... return "Hello, World!" >>> import dill >>> dill.dumps(template_function) """
Using pytest to run those doctests will cause the TypeError: cannot pickle 'EncodedFile' object.
Using vanilla doctest works fine. (Ignore the fact that the test failed. The expected output did not match. Note that it did successfully dump the function using dill.)
1 items had failures:
1 of 3 in test_client.MyClass
Using a regular pytest function to perform the test also works fine. This is only when attempting to dill a function defined in a doctest run with pytest.
Minimal Reproducible Example (Deserialization after disabling pytest's logging capture)
If I extend the example to include dill.loads, and then disable capture with pytest -s, I get a recursion error. The serialization is successful (because disabling capture means that EncodedFile isn't injected), but dill is trying to load something recursively, I think? Again, this code works as expected with vanilla doctest and in a script.
classMyClass:
"""Running a file containing this class with `python -m pytest -s this_file.py --doctest-modules` will fail with `RecursionError('maximum recursion depth exceeded'). Examples: >>> def template_function(): ... return "Hello, World!" >>> import dill >>> string = dill.dumps(template_function) >>> dill.loads(string)() 'Hello, World!' """
010 >>> string = dill.dumps(template_function)
011 >>> dill.loads(string)()
UNEXPECTED EXCEPTION: RecursionError('maximum recursion depth exceeded')
Traceback (most recent call last):
File "/opt/homebrew/Caskroom/miniforge/base/envs/my-env/lib/python3.11/doctest.py", line 1350, in __run
exec(compile(example.source, filename, "single",
File "<doctest test_client.MyClass[3]>", line 1, in <module>
File "/opt/homebrew/Caskroom/miniforge/base/envs/my-env/lib/python3.11/site-packages/dill/_dill.py", line 286, in loads
return load(file, ignore, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniforge/base/envs/my-env/lib/python3.11/site-packages/dill/_dill.py", line 272, in load
return Unpickler(file, ignore=ignore, **kwds).load()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniforge/base/envs/my-env/lib/python3.11/site-packages/dill/_dill.py", line 419, in load
obj = StockUnpickler.load(self)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniforge/base/envs/my-env/lib/python3.11/site-packages/pluggy/_manager.py", line 74, in __getattr__
return getattr(self._dist, attr, default)
^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniforge/base/envs/my-env/lib/python3.11/site-packages/pluggy/_manager.py", line 74, in __getattr__
return getattr(self._dist, attr, default)
^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniforge/base/envs/my-env/lib/python3.11/site-packages/pluggy/_manager.py", line 74, in __getattr__
return getattr(self._dist, attr, default)
^^^^^^^^^^
[Previous line repeated 955 more times]
RecursionError: maximum recursion depth exceeded
Environment
This was run on Pytest 8.2.2, Python 3.11 on an M1 Macbook Air (macOS Sonoma 14.4.1). The same errors were also seen on Python 3.9 on the same Macbook and on Python 3.11 on an amd64 Ubuntu Machine (Ubuntu 22.04 LTS). All experiments were run with dill==0.3.8.
This is cross posted from pytest-dev/pytest#12448, because the
pytest
devs suggest that this is a problem withdill
.Description of the Problem
When I attempt to
dill
a function defined inside of adoctest
,pytest
raises an error, complaining thatTypeError: cannot pickle 'EncodedFile' object
. Best I can tell, this is theEncodedFile
object in pytest.This error only occurs when I run those doctests via pytest. They run just fine via vanilla doctest. The same code also works just fine in a script and as a regular pytest test function.
I fear that this is related to pytest-dev/pytest#10845, which doesn't seem to have a solution.
Minimal Reproducible Example (Serialization)
Using pytest to run those doctests will cause the
TypeError: cannot pickle 'EncodedFile' object
.Using vanilla doctest works fine. (Ignore the fact that the test failed. The expected output did not match. Note that it did successfully dump the function using dill.)
Using a regular pytest function to perform the test also works fine. This is only when attempting to dill a function defined in a doctest run with pytest.
Minimal Reproducible Example (Deserialization after disabling pytest's logging capture)
If I extend the example to include
dill.loads
, and then disable capture withpytest -s
, I get a recursion error. The serialization is successful (because disabling capture means thatEncodedFile
isn't injected), butdill
is trying to load something recursively, I think? Again, this code works as expected with vanilla doctest and in a script.Environment
This was run on Pytest 8.2.2, Python 3.11 on an M1 Macbook Air (macOS Sonoma 14.4.1). The same errors were also seen on Python 3.9 on the same Macbook and on Python 3.11 on an amd64 Ubuntu Machine (Ubuntu 22.04 LTS). All experiments were run with
dill==0.3.8
.Pip list
The text was updated successfully, but these errors were encountered: