Pickling a dictionary

  • Thread starter Devashish Tyagi
  • Start date
D

Devashish Tyagi

So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this

obj = InteractiveInterpreter()
local = obj.locals()
pickle.dump(local, open('obj.dump','rw'))

But I received an error say
TypeError: can't pickle ellipsis objects

From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour?
 
I

Ian Kelly

So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this

obj = InteractiveInterpreter()
local = obj.locals()
pickle.dump(local, open('obj.dump','rw'))

But I received an error say
TypeError: can't pickle ellipsis objects

From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour?

The contents of the dictionary need to be pickleable as well. You
probably have an ellipsis object in the dict somewhere.
 
P

Peter Otten

Devashish said:
So I want to store the current state of a InteractiveInterpreter Object in
database. In order to achieve this I tried this

obj = InteractiveInterpreter()
local = obj.locals()
pickle.dump(local, open('obj.dump','rw'))

Assuming InteractiveInterpreter is imported from the code module the above
will fail with a TypeError. Please copy-and paste code snippets to avoid
guessing games.
But I received an error say
TypeError: can't pickle ellipsis objects

From what I understand this shouldn't occur as local is a dictionary. Any
particular reason for this behaviour?

For a dict to be pickled all its keys and values have to be pickled, too.
 
I

Ian Kelly

The contents of the dictionary need to be pickleable as well. You
probably have an ellipsis object in the dict somewhere.

By the way, if you use Python 3 and pickle protocol 3, then Ellipsis
*is* pickleable:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.b'\x80\x03cbuiltins\nEllipsis\nq\x00.'

Cheers,
Ian
 
D

Devashish Tyagi

Devashish Tyagi wrote:









Assuming InteractiveInterpreter is imported from the code module the above

will fail with a TypeError. Please copy-and paste code snippets to avoid

guessing games.

Here is the code

from code import InteractiveInterpreter
import StringIO
import pickle

src = StringIO.StringIO()
inter = InteractiveInterpreter()
inter.runcode('a = 5')
local = inter.locals

pickle.dump(local,open('obj.dump','wb'))

Here is the error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle ellipsis objects
 
D

Devashish Tyagi

Devashish Tyagi wrote:









Assuming InteractiveInterpreter is imported from the code module the above

will fail with a TypeError. Please copy-and paste code snippets to avoid

guessing games.

Here is the code

from code import InteractiveInterpreter
import StringIO
import pickle

src = StringIO.StringIO()
inter = InteractiveInterpreter()
inter.runcode('a = 5')
local = inter.locals

pickle.dump(local,open('obj.dump','wb'))

Here is the error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle ellipsis objects
 
I

Ian Kelly

Here is the code

from code import InteractiveInterpreter
import StringIO
import pickle

src = StringIO.StringIO()
inter = InteractiveInterpreter()
inter.runcode('a = 5')
local = inter.locals

pickle.dump(local,open('obj.dump','wb'))

After calling runcode it seems that the __builtins__ dict containing
an 'Ellipsis' binding has been added to inter.locals. You probably
don't want to include the contents of __builtins__ in your pickle
anyway, so just delete it:

del local['__builtins__']
pickle.dump(local, ...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top