Strange KeyError using cPickle

Rune Strand rune.strand at
Thu Jun 2 02:13:50 CEST 2005

I'm experiencing strange errors both with pickle and cPickle in the
below code:

import cPickle as pickle
#import pickle
from string import ascii_uppercase
from string import ascii_lowercase

def createData():
    d1 = list("Something's rotten")
    d2 = tuple('in the state of Denmark')

    d3 = [('a', 'b'), ('c', 'd')]
    #d3 = [('s a', 's b'), ('s c', 's d')]
    #d3 = [('sa', 'sb'), ('sc', 'sd')]
    #d3 = [['s a', 's b'], ['s c', 's d']]

    d4 = dict(zip(ascii_uppercase,ascii_lowercase))
    return [d1, d2, d3, d4]

def doPickle(data, pickleFile = 'pickleTest.p', proto = 2):
    f = XWwz(pickleFile, 'w')
    p = pickle.Pickler(f, proto)

def doUnpicle(pickleFile = 'pickleTest.p'):
    f = XWwz(pickleFile, 'rb')
    up = pickle.Unpickler(f)
    data = up.load()
    return data

data = createData()
xdata = doUnpicle()

1) The above code works, but if I use pickle instead of cPickle, I get
   "KeyError: '\n'" when unpickling.

2) If I uncomment any of the other d3 bindings in createData(), I get
   "KeyError: '\n'", or
   "cPickle.UnpicklingError: invalid load key, ''" (newline)

3) If I don't use the d1, d2 and d4 bindings, no error occurs.

I can't find the explanation. Why the newline error? There are no
newlines in the data. Are not all those structures pickable?

Thanks for all help!

(on WinXP, CPython 2.4.1)

More information about the Python-list mailing list