[Numpy-discussion] Problems with pickling an array with type Object

Sue Giller sag at hydrosphere.com
Mon Oct 15 12:41:12 EDT 2001


I am trying to pickle an array of objects with a typecode of Object.  
Ultimately this array will be in a class I create based on UserArray, 
but the following illustrates a problem I am having.  See code at the 
end of the message.

I am working in Windows 2000, Service Pack 1, using PythonWin, 
win32all build 140.  I am using the following versions of python stuff

Python:  2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)]
Numeric:  20.2.0
cPickle:  1.71

I create a simple array with a type of Object, pickle it to a file using 
cPickle.  I then run this script in PythonWin. I can unpickle this file 
from PythonWin, but not from DOS window.  Alternatively, I can 
pickle the array from DOS, and unpickle it from DOS, but not from 
PythonWin.  If I save both versions of the file and compare them, 
they are the same size, but not the same bytes.  This does not 
happen with other types for the array.

When unpickling, I get a GPF, either from PythonWin.exe or 

Any ideas?

My ultimate goal is to be able to store simple class objects in the 
array and be able to pickle and unpickle them.

----- CODE ----

# simple test for pickling problem
import Numeric
import cPickle
import sys

basePath = "c:\\python21\\scripts\\pickle\\"

print "++++++"
# create a simple array of object, populated by integer data
ds1 = Numeric.array([1,2,3,4], 'O')
print ds1, ds1.typecode()
print "Python: ", sys.version
print "Numeric: " , Numeric.__version__
print "cPickle: ", cPickle.__version__
pickle = 1 # change to 0 to skip the pickling step
for ii in (range(1,2)):
    if pickle:
        fileName = "%s%s%d%s" % (basePath, "simple", ii, ".pck")
        print "pickling ", fileName
        fp = open(fileName, 'wb')
        cPickle.dump(ds1, fp, 1)
for ii in range(1,2):
    fileName = "%s%s%d%s" % (basePath, "simple", ii, ".pck")
    print "unpickling ", fileName
    fp = open(fileName, 'rb')
    obj = cPickle.load(fp)      
    print obj, obj.typecode()

More information about the NumPy-Discussion mailing list