Categories: Python

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. ***

Ron

https://www.ronrothman.com/public/about+me.shtml

View Comments

Recent Posts

Python 3 Rounding Surprise

I discovered this subtle change when one of my unit tests mysteriously failed once I…

5 years ago

Python 3 Exception Chaining

Exception chaining solves two problems in Python 2. 1. Swallowed Exceptions If, while handling exception…

5 years ago

Safe Password Storage – How Websites Get It Wrong

Here's the recording of my talk "15 minutes w/ Beeswax: Safe Password Storage - How…

5 years ago

Python at Scale: Concurrency at Beeswax

My presentation from the NYC Python Meetup: Python at Scale; Concurrency at Beeswax. Concurrent Python…

6 years ago

Python Dependencies The Right Way

My BazelCon 2019 lightning talk, Python Dependencies The Right Way*, has been posted. Please excuse…

6 years ago

Python 3 f-strings

One of my favorite features of Python 3 is f-strings (Formatted String Literals). Each of…

6 years ago

This website uses cookies.