Pickling a dictionary

Discussion in 'Python' started by Devashish Tyagi, Nov 7, 2012.

  1. 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?
    Devashish Tyagi, Nov 7, 2012
    #1
    1. Advertising

  2. Devashish Tyagi

    Ian Kelly Guest

    On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi
    <> wrote:
    > 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.
    Ian Kelly, Nov 7, 2012
    #2
    1. Advertising

  3. Devashish Tyagi

    Peter Otten Guest

    Devashish Tyagi wrote:

    > 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.
    Peter Otten, Nov 7, 2012
    #3
  4. Devashish Tyagi

    Ian Kelly Guest

    On Wed, Nov 7, 2012 at 9:16 AM, Ian Kelly <> wrote:
    > On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi
    > <> wrote:
    >> 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.


    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.
    >>> import pickle
    >>> pickle.dumps(Ellipsis, protocol=pickle.HIGHEST_PROTOCOL)

    b'\x80\x03cbuiltins\nEllipsis\nq\x00.'

    Cheers,
    Ian
    Ian Kelly, Nov 7, 2012
    #4
  5. On Wednesday, 7 November 2012 21:57:05 UTC+5:30, Peter Otten wrote:
    > Devashish Tyagi wrote:
    >
    >
    >
    > > 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.


    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


    >
    >
    >
    > > 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.
    Devashish Tyagi, Nov 7, 2012
    #5
  6. On Wednesday, 7 November 2012 21:57:05 UTC+5:30, Peter Otten wrote:
    > Devashish Tyagi wrote:
    >
    >
    >
    > > 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.


    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


    >
    >
    >
    > > 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.
    Devashish Tyagi, Nov 7, 2012
    #6
  7. Devashish Tyagi

    Ian Kelly Guest

    On Wed, Nov 7, 2012 at 10:40 AM, Devashish Tyagi
    <> wrote:
    > 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, ...)
    Ian Kelly, Nov 7, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    437
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    322
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,027
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    521
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    183
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page