-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Saving a modules __dict__ attribute #41
Conversation
Ok, I'm taking a look at your code. It would be nice if your test failed with an AssertionError instead of an AttributeError.
|
Oops... assert module.a == 1234 to assert hasattr(module, "a") and module.a == 1234 I'll wait to see if there is anything else, though. |
should probably clean up the I'm poking around on the different python versions and different tests now. |
Done |
Possibly an issue of getting rid of |
😩 Sorry, mistake in condition. |
Do you want me to squash the commits? |
It still doesn't seem to work for me with
|
😩 Still the condition. I've combined it with the old condition, so the |
tests/test_nested.py doesn't clean up when run by python 3. Shall I file a separate issue? |
Looks like matsjoyce@7bcf040 works both in a file and with I could accept it, as is… because it adds a feature and doesn't remove any… however, I'd like to ask your opinion on proceeding. Should the following be the correct behavior? It currently is, b/c "standard" modules are stored by reference… but it is definitely possible to add something to a standard module's dict.
|
Note that this is also the current behavior, so maybe it makes sense? I'm not sure.
|
The reason for excluding standard modules is that most of them don't pickle (sys, re, etc). If a list of picklable or non-picklable modules could be created, that behaviour could be changed to be more logical. Could a list be made though dill._objects? from dill import _objects
{getattr(i.__class__, '__module__', "") for i in _objects.x.values()} |
Using a function like: def _can_pickle_module(mod, _cache={}, _recursion_protection=[None]):
if _recursion_protection[0] is not None:
return mod is _recursion_protection[0]
if mod not in _cache:
_recursion_protection[0] = mod
try:
dumps(mod)
except:
_cache[mod] = False
else:
_cache[mod] = True
finally:
_recursion_protection[0] = None
return _cache[mod] and the condition in if _can_pickle_module(obj) or is_dill(pickler) and obj is pickler._main_module: it'll work. However, this approach is less efficient. |
The issue with >>> dill.dumps(p)
T4: <class 'abc.ABCMeta'>
b'\x80\x03cabc\nABCMeta\nq\x00.' The type is handed to pickle, which does not preserve the |
Right. What I'm saying, is what do you think the expected behavior should be? My solution to My solution to an edited |
Yes, as if
All the builtin modules (why do I keep calling them standard...) serialize by reference, but the >>> import dill, sys
>>> dill.dumps(sys.__dict__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/matthew/GitHub/dill/dill/dill.py", line 143, in dumps
dump(obj, file, protocol, byref)
File "/home/matthew/GitHub/dill/dill/dill.py", line 136, in dump
pik.dump(obj)
File "/usr/lib/python3.4/pickle.py", line 410, in dump
self.save(obj)
File "/usr/lib/python3.4/pickle.py", line 477, in save
f(self, obj) # Call unbound method with explicit self
File "/home/matthew/GitHub/dill/dill/dill.py", line 504, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/usr/lib/python3.4/pickle.py", line 812, in save_dict
self._batch_setitems(obj.items())
File "/usr/lib/python3.4/pickle.py", line 838, in _batch_setitems
save(v)
File "/usr/lib/python3.4/pickle.py", line 522, in save
self.save_reduce(obj=obj, *rv)
File "/usr/lib/python3.4/pickle.py", line 600, in save_reduce
save(func)
File "/usr/lib/python3.4/pickle.py", line 477, in save
f(self, obj) # Call unbound method with explicit self
File "/home/matthew/GitHub/dill/dill/dill.py", line 827, in save_type
StockPickler.save_global(pickler, obj)
File "/usr/lib/python3.4/pickle.py", line 918, in save_global
(obj, module_name, name))
_pickle.PicklingError: Can't pickle <class 'sys.flags'>: it's not the same object as sys.flags If we have a way to test whether a module's |
Saving a modules __dict__ attribute
This issues should stay open, or be a new issue. |
And, with regard to your test above… you have to be extra careful with a |
Shall I file new issues for:
|
I was just typing the same. Sure. |
ref this issue |
Possibly a follow up for #35, but saving a session doesn't save the state of the modules:
test.py:
I think this code does the job, but it may need a few adjustments, depending which modules need to be pickled.