[Python-bugs-list] [ python-Bugs-520644 ] __slots__ are not pickled

noreply@sourceforge.net noreply@sourceforge.net
Thu, 21 Feb 2002 09:51:27 -0800


Bugs item #520644, was opened at 2002-02-20 12:50
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=520644&group_id=5470

Category: Type/class unification
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Samuele Pedroni (pedronis)
Assigned to: Nobody/Anonymous (nobody)
Summary: __slots__ are not pickled

Initial Comment:
[Posted on behalf of Kevin Jacobs]

I have been hacking on ways to make lighter-weight 
Python objects using the
__slots__ mechanism that came with Python 2.2 new-
style class.  Everything
has gone swimmingly until I noticed that slots do not 
get pickled/cPickled
at all!

Here is a simple test case:

  import pickle,cPickle
  class Test(object):
    __slots__ = ['x']
    def __init__(self):
      self.x = 66666

  test = Test()

  pickle_str  = pickle.dumps( test )
  cpickle_str = cPickle.dumps( test )

  untest  = pickle.loads( pickle_str )
  untestc = cPickle.loads( cpickle_str )

  print untest.x    # raises AttributeError
  print untextc.x   # raises AttributeError

...

see 

http://aspn.activestate.com/ASPN/Mail/Message/python-
dev/1031499

----------------------------------------------------------------------

Comment By: Kevin Jacobs (jacobs99)
Date: 2002-02-21 09:51

Message:
Logged In: YES 
user_id=459565

This bug raises questions about what a slot really is.
After a fair amount of discussion on Python-dev, we
have come up with basically two answers:

  1) a slot is a struct-member that is part of the
     private implementation of an object.  Slots should
     have their own semantics and not be expected to
     act like Python instance attributes.

  2) slots should be treated just like dict instance
     attributes except they are allocated statically
     within the object itself, and require slightly 
     different reflection methods.

Under (1), this bug isn't really a bug.  The class should
implement a __reduce__ function or otherwise hook into
the copy_reg system.

Under (2), this bug is just the tip of the iceberg. 
There are about 8 other problems with the current 
slot implementation that need to be resolved before
slots act almost identically to normal instance attributes.
Thankfully, I am fairly confident that I can supply 
patches that can achieve this, though I am waiting for
Guido to comment on this issue when he returns from his
trip.


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=520644&group_id=5470