ron rothman.ron rothman
selectively conformist

Python 2.7.3 (Bug) Broke My Namedtuple Unpickling

I just learned (*ahem* the Hard Way) that CPython 2.7.3 namedtuple is buggy when combined with pickling and older Python versions.

If you pickle a namedtuple using CPython 2.7.3, you won’t be able to successfully unpickle it using 2.7.2 (nor, presumably, using even earlier versions).

Here’s a short example. First, run this with Python 2.7.3:

# Pickle a namedtuple with python 2.7.3

import pickle
from collections import namedtuple
Point = namedtuple('Point', ('x', 'y'))

a = Point(4, 9)

with open('foo', 'w') as f:
    pickle.dump(a, f, -1)

Then, run this with 2.7.2:

# Try to load the pickled namedtuple
# (works on 2.7.3 but fails on python 2.7.2)

import pickle
from collections import namedtuple
Point = namedtuple('Point', ('x', 'y'))

with open('foo', 'r') as f:
    a = pickle.load(f)

That reader code will work fine on 2.7.3 but fails on 2.7.2:

7:36pm ronr@fox[/tmp]: python ./readpf.py
Traceback (most recent call last):
  File "./readpf.py", line 10, in 
    a = pickle.load(f)
  File "/opt/cbsi-python2.7/lib/python2.7/pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "/opt/cbsi-python2.7/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/opt/cbsi-python2.7/lib/python2.7/pickle.py", line 1224, in load_build
    d = inst.__dict__
AttributeError: 'Point' object has no attribute '__dict__'

Looks like it’s a weakly acknowledged bug in 2.7.3. Granted, it seems like an esoteric case–but I did encounter it in the wild. Anyone else running into this?

*** Update 2013-Apr-9: Python 2.7.4 has the same problem. ***

One Response to “Python 2.7.3 (Bug) Broke My Namedtuple Unpickling” [Leave yours »]

  1. toshihito kamiya tracked back:

    Thank you for the information.
    I got the same bug in my code…

    1

Leave a Reply