[issue3816] __newobj__ pickle feature is not documented

2009-06-08 Thread Alexandre Vassalotti

Alexandre Vassalotti alexan...@peadrop.com added the comment:

Closing as the feature is documented in the section The __newobj__
unpickling function of PEP 307.

--
assignee: georg.brandl - 
resolution:  - invalid
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue3816
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3816] __newobj__ pickle feature is not documented

2008-10-29 Thread Alexandre Vassalotti

Alexandre Vassalotti [EMAIL PROTECTED] added the comment:

Could explain me how this feature could be used, other than for
providing the efficient and backward-compatible pickling mechanism for
new-style classes?

--
nosy: +alexandre.vassalotti

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3816
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3816] __newobj__ pickle feature is not documented

2008-10-29 Thread Christian Heimes

Christian Heimes [EMAIL PROTECTED] added the comment:

Alexandre Vassalotti wrote:
 Alexandre Vassalotti [EMAIL PROTECTED] added the comment:
 
 Could explain me how this feature could be used, other than for
 providing the efficient and backward-compatible pickling mechanism for
 new-style classes?

The feature makes it easy to write __reduce__ methods for subclasses of 
builtins. Take this example:

def __newobj__(cls, *args):
 return cls.__new__(cls, *args)

class mydict(dict):
 def __reduce__(self):
 state = (dict(self), self.__dict__)
 return (__newobj__, (self.__class__,), state)

Without the __reduce__ method the information in __dict__ and the class 
would be lost.

Christian

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3816
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3816] __newobj__ pickle feature is not documented

2008-10-29 Thread Christian Heimes

Christian Heimes [EMAIL PROTECTED] added the comment:

Christian Heimes wrote:
 Christian Heimes [EMAIL PROTECTED] added the comment:
 
 Alexandre Vassalotti wrote:
 Alexandre Vassalotti [EMAIL PROTECTED] added the comment:

 Could explain me how this feature could be used, other than for
 providing the efficient and backward-compatible pickling mechanism for
 new-style classes?
 
 The feature makes it easy to write __reduce__ methods for subclasses of 
 builtins. Take this example:
 
 def __newobj__(cls, *args):
  return cls.__new__(cls, *args)
 
 class mydict(dict):
  def __reduce__(self):
  state = (dict(self), self.__dict__)
  return (__newobj__, (self.__class__,), state)

Of course one has to provide a __setstate__ method, too.

 def __setstate__(self, state):
 dict.update(self, state[0])
 self.__dict__.update(state[1])

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3816
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3816] __newobj__ pickle feature is not documented

2008-10-29 Thread Alexandre Vassalotti

Alexandre Vassalotti [EMAIL PROTECTED] added the comment:

 Without the __reduce__ method the information in __dict__ and 
 the class would be lost.

Are you sure about that?


Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
 class mydict(dict): pass
... 
 D = mydict({1:one,2:two})
 D.foo = 3
 import pickle
 E = pickle.loads(pickle.dumps(D))
 E.foo
3
 E
{1: 'one', 2: 'two'}
 type(E)
class '__main__.mydict'
 F = pickle.loads(pickle.dumps(D, 2))
 F.foo
3
 F
{1: 'one', 2: 'two'}
 type(F)
class '__main__.mydict'

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3816
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3816] __newobj__ pickle feature is not documented

2008-09-09 Thread Christian Heimes

New submission from Christian Heimes [EMAIL PROTECTED]:

The pickle system has an undocumented but very useful feature. When the
first element of the tuple returned by __reduce__ is a function named
__newobj__, a special obcode is generated. __newobj__ doesn't need to be
registered as safe for unpickling, too.

From pickle.py:

# use the more efficient NEWOBJ opcode, while still
# allowing protocol 0 and 1 to work normally.  For this to
# work, the function returned by __reduce__ should be
# called __newobj__, and its first argument should be a
# new-style class.  The implementation for __newobj__
# should be as follows, although pickle has no way to
# verify this:
#
# def __newobj__(cls, *args):
# return cls.__new__(cls, *args)
#
# Protocols 0 and 1 will pickle a reference to __newobj__,
# while protocol 2 (and above) will pickle a reference to
# cls, the remaining args tuple, and the NEWOBJ code,
# which calls cls.__new__(cls, *args) at unpickling time
# (see load_newobj below).  If __reduce__ returns a
# three-tuple, the state from the third tuple item will be
# pickled regardless of the protocol, calling __setstate__
# at unpickling time (see load_build below).
#
# Note that no standard __newobj__ implementation exists;
# you have to provide your own.  This is to enforce
# compatibility with Python 2.2 (pickles written using
# protocol 0 or 1 in Python 2.3 should be unpicklable by
# Python 2.2).

--
assignee: georg.brandl
components: Documentation
keywords: easy
messages: 72850
nosy: christian.heimes, georg.brandl
priority: low
severity: normal
status: open
title: __newobj__ pickle feature is not documented
versions: Python 2.5, Python 2.6, Python 3.0

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3816
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com