Comments (8)
Yup, its the circular dependency I think I've highlighted:
I don't know why it differs so much from a "normal" class:
from dill.
In the test above if I change line 9 to setattr(cls, 'methodname', proto_method)
instead of explicitly using MethodType
then the test passes.
But when I try that approach with the real program, dill still fails on circular references (but with a cell involved)
INFO:dill:Co: <code object method at 0x7f9be4e08230, file "/path/to.py", line 625>
INFO:dill:F2: <function _unmarshal at 0x7f9bed9d2140>
INFO:dill:D4: <dict object at 0x7f9be5bbfb40>
INFO:dill:Ce: <cell at 0x7f9be58a6be8: str object at 0x7f9bf418c7b0>
INFO:dill:F2: <function _create_cell at 0x7f9bed9d2578>
INFO:dill:Ce: <cell at 0x7f9be58a6bb0: type object at 0x251b6d0>
INFO:dill:T2: <class '__main__.NewCls'>
INFO:dill:F2: <function _create_type at 0x7f9bed9d2230>
INFO:dill:T1: <type 'type'>
INFO:dill:D2: <dict object at 0x7f9bdf2266e0>
INFO:dill:F1: <function f at 0x7f9be1c106e0>
INFO:dill:D4: <dict object at 0x7f9be5bbfb40>
INFO:dill:Ce: <cell at 0x7f9be58a6bb0: type object at 0x251b6d0>
INFO:dill:T2: <class '__main__.NewCls'>
INFO:dill:D2: <dict object at 0x7f9bdf370050>
INFO:dill:F1: <function f at 0x7f9be1c106e0>
INFO:dill:D4: <dict object at 0x7f9be5bbfb40>
INFO:dill:Ce: <cell at 0x7f9be58a6bb0: type object at 0x251b6d0>
INFO:dill:T2: <class '__main__.NewCls'>
INFO:dill:D2: <dict object at 0x7f9bdf222a28>
INFO:dill:F1: <function f at 0x7f9be1c106e0>
INFO:dill:D4: <dict object at 0x7f9be5bbfb40>
INFO:dill:Ce: <cell at 0x7f9be58a6bb0: type object at 0x251b6d0>
INFO:dill:T2: <class '__main__.NewCls'>
I will try to find a minimal test case to reproduce this too.
from dill.
In the test above if I change line 9 to setattr(cls, 'methodname', proto_method) instead of explicitly using MethodType then the test passes.
Interesting. Maybe this is one of the things they simplified in python3, for reasons like this.
from dill.
I think in essence, this is a problem with pickle, not dill, as when saving an object, pickle resolved the dependencies before marking the object as memorised, which means that circular references are not well handled.
from dill.
This may mean digging into exactly how the object is created by python, and seeing if there's a way to rebuild it from any component parts that are pickleable. In this case, looking at what exactly setattr
is doing, seeing if there's a simple way to detect that setattr
was used, and then building a function to replicate the object construction.
from dill.
I've found the difference with the first issue:
>>> import types
>>> def f(): pass
...
>>> class A:
... def f():pass
...
>>> class A2: pass
...
>>> f2=types.MethodType(f, None, A2)
>>> A2.f=f2
>>> A.__dict__["f"]
<function f at 0x7f3d83ec7b90>
>>> A.f
<unbound method A.f>
>>> A2.__dict__["f"]
<unbound method A2.f>
>>> A2.f
<unbound method A2.f>
>>>
from dill.
Ach.. that's bordering on being a python
bug!
A loose plan to serialize A2
might be:
- Identify unbound methods in the
__dict__
- serialize the methods as:
A2.f.im_func
(or possiblyA2.f
) inA2
, or"with A2"
as a tuple of(A2, dict)
where dict has string names (i.e."f"
) as keys and the objects as values - in unpickling, deserialize
A2
, and then "reapply" the "special" methods exactly how they were built.
from dill.
Turns out this has indeed been fixed in python3. The instancemethod
type has been removed and all unbound methods are now type function
.
setattr(cls, 'methodname', f)
is the only idom that works in both versions so it seems people should avoid types.MethodType
for portable code in any case. I'll focus on trying to isolate the second issue.
from dill.
Related Issues (20)
- dill.source.findsource fails when in asyncio REPL
- `save_function()` can't save function in a submodule that has the same name as an attribute of the parent module HOT 2
- bytes length not a multiple of item size
- dill.source.getsource returns decorated function instead of function HOT 2
- 0.3.7 incorrectly pickles the class definition for module/class with the same name HOT 7
- Exception: --- Logging error --- HOT 3
- Use dump_module() in jupyter notebook with remote kernel HOT 1
- UnpicklingError using dill but not stdlib pickle HOT 2
- Compatibility with Pyodide HOT 6
- tfp.math.psd_kernels.ExponentiatedQuadratic no longer serialiases with latest versions
- `dill.load_session()` raises `NameError: name 'UnpicklingError' is not defined` HOT 5
- pydantic>=2.5 classes can't be serialized HOT 4
- Failure to load dumped partial function (in older python) properly (in newer python)
- threading.Thread fails to pickle in python > 3.13.0a5 HOT 4
- Error with typing.AbstractContextManager in Python 3.8 and newer dill versions (>=0.3.6) HOT 1
- File gets truncated upon unpickling
- PyTorch C++-generated module "not found as" itself? HOT 1
- Importing dill changes the python pickler behavior HOT 8
- Recursive self-references that trigger a RecursionError
- Attempting to `dill` a function defined in a `doctest` run with `pytest` causes a `TypeError: cannot pickle 'EncodedFile' object`.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dill.