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. ***
I discovered this subtle change when one of my unit tests mysteriously failed once I…
Exception chaining solves two problems in Python 2. 1. Swallowed Exceptions If, while handling exception…
Here's the recording of my talk "15 minutes w/ Beeswax: Safe Password Storage - How…
My presentation from the NYC Python Meetup: Python at Scale; Concurrency at Beeswax. Concurrent Python…
My BazelCon 2019 lightning talk, Python Dependencies The Right Way*, has been posted. Please excuse…
One of my favorite features of Python 3 is f-strings (Formatted String Literals). Each of…
This website uses cookies.
View Comments
Thank you for the information.
I got the same bug in my code...