Title: Fix pickling bug of collections.namedtuple
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: alexandre.vassalotti, georg.brandl, rhettinger
Priority: high Keywords: patch

Created on 2008-06-08 23:54 by alexandre.vassalotti, last changed 2008-06-09 01:30 by rhettinger. This issue is now closed.

File name Uploaded Description Edit
fix_namedtuple_pickling.patch alexandre.vassalotti, 2008-06-08 23:54
Messages (4)
msg67853 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2008-06-08 23:54
There is currently a pickling bug in the namedtuple factory:

  >>> from collections import namedtuple
  >>> MemoRecord = namedtuple("MemoRecord", "key, msg")
  >>> m = MemoRecord(1,"hello")
  >>> import pickle
  >>> pickle.loads(pickle.dumps(m))
  Traceback (most recent call last):
  TypeError: __new__() takes exactly 3 positional arguments (2 given)

The bug is due to the fact that classes created by namedtuple don't
handle the __new__ arguments in the same fashion as tuple.__new__. The
fix is simply to define __getnewargs__.
msg67854 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-06-09 00:17
Georg, this works fine in Py2.6 but not in Py3.0.  Do you know what 
changed and whether other pickles will fail?
msg67856 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-06-09 00:24
Found it.  Py3.0 uses protocol 2 by default and that protocol has a 
different set of calls.
msg67857 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-06-09 01:30
Fixed in r64047.
Thanks for the submission.
Date User Action Args
2008-06-09 01:30:30rhettingersetstatus: open -> closed
assignee: georg.brandl -> rhettinger
messages: + msg67857
resolution: fixed
keywords: patch, patch
2008-06-09 00:24:32rhettingersetkeywords: patch, patch
messages: + msg67856
versions: + Python 2.6
2008-06-09 00:17:37rhettingersetkeywords: patch, patch
assignee: rhettinger -> georg.brandl
messages: + msg67854
nosy: + georg.brandl
versions: - Python 2.6
2008-06-09 00:03:58rhettingersetpriority: normal -> high
keywords: patch, patch
2008-06-08 23:54:13alexandre.vassalotticreate