Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: defaultdict became unmarshallable object in 2.6?

Tags:

python

Did defaultdict's become not marshal'able as of Python 2.6? The following works under 2.5, fails under 2.6 with "ValueError: unmarshallable object" on OS X 1.5.6, python-2.6.1-macosx2008-12-06.dmg from python.org:

from collections import defaultdict
import marshal
dd = defaultdict(list)
marshal.dump(dd, file('/tmp/junk.bin','wb') )
like image 250
Parand Avatar asked Mar 20 '09 05:03

Parand


1 Answers

Marshal was deliberately changed to not support subclasses of built-in types. Marshal was never supposed to handle defaultdicts, but happened to since they are a subclass of dict. Marshal is not a general "persistence" module; only None, integers, long integers, floating point numbers, strings, Unicode objects, tuples, lists, sets, dictionaries, and code objects are supported.

Python 2.5:

>>> marshal.dumps(defaultdict(list))
'{0'
>>> marshal.dumps(dict())
'{0'

If for some reason you really want to marshal a defaultdict you can convert it to a dict first, but odds are you should be using a different serialization mechanism, like pickling.

like image 68
Miles Avatar answered Oct 10 '22 19:10

Miles