[issue12290] __setstate__ is called for false values

2011-06-08 Thread Eugene Toder

New submission from Eugene Toder :

Pickle documentation [1] says:

""" Note: If __getstate__() returns a false value, the __setstate__() method 
will not be called upon unpickling. """

However, this isn't quite true. This depends on the version of pickle protocol. 
A small example:

>>> class Pockle(object):
def __getstate__(self):
return 0
def __setstate__(self, state):
sys.stdout.write('__setstate__ is called!\n')

>>> for p in range(4):
sys.stdout.write('protocol %d: ' % p)
pickle.loads(pickle.dumps(Pockle(), p))

protocol 0: <__main__.Pockle object at 0x02EAE3C8>
protocol 1: <__main__.Pockle object at 0x02EAE358>
protocol 2: __setstate__ is called!
<__main__.Pockle object at 0x02EAE3C8>
protocol 3: __setstate__ is called!
<__main__.Pockle object at 0x02EAE358>

So for protocols >= 2 setstate is called. This is caused by 
object.__reduce_ex__ returning different tuples for different protocol versions:

>>> for p in range(4):
sys.stdout.write('protocol %d: %s\n' % (p, Pockle().__reduce_ex__(p)))


protocol 0: (, (, , None))
protocol 1: (, (, , None))
protocol 2: (, (,), 0, None, None)
protocol 3: (, (,), 0, None, None)

Implementation of reduce_ex for protos 0-1 in copy_reg.py contains the 
documented check: 
http://hg.python.org/cpython/file/f1509fc75435/Lib/copy_reg.py#l85

Implementation for proto 2+ in typeobject.c is happy with any value: 
http://hg.python.org/cpython/file/f1509fc75435/Objects/typeobject.c#l3205

Pickle itself only ignores None, not any false value: 
http://hg.python.org/cpython/file/f1509fc75435/Lib/pickle.py#l418

I think this is a documentation issue at this point.

[1] http://docs.python.org/py3k/library/pickle.html#pickle.object.__setstate__

--
components: Library (Lib)
messages: 137935
nosy: eltoder
priority: normal
severity: normal
status: open
title: __setstate__ is called for false values
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3

___
Python tracker 

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



[issue12290] __setstate__ is called for false values

2011-06-08 Thread R. David Murray

R. David Murray  added the comment:

See also #6827, just for some background on the current docs.

--
nosy: +r.david.murray

___
Python tracker 

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



[issue12290] __setstate__ is called for false values

2011-06-08 Thread Eugene Toder

Changes by Eugene Toder :


--
nosy: +alexandre.vassalotti, pitrou

___
Python tracker 

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



[issue12290] __setstate__ is called for false values

2011-06-15 Thread Eugene Toder

Eugene Toder  added the comment:

So how about this correction?

--
keywords: +patch
nosy: +belopolsky, georg.brandl
Added file: http://bugs.python.org/file22375/setstate.diff

___
Python tracker 

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



[issue12290] __setstate__ is called for false values

2011-06-18 Thread Georg Brandl

Georg Brandl  added the comment:

Well, this looks correct then.

--

___
Python tracker 

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