[NumPy-Tickets] [NumPy] #1415: data corruption unpickling a numpy.void

NumPy Trac numpy-tickets@scipy....
Fri Feb 26 19:31:12 CST 2010

#1415: data corruption unpickling a numpy.void
 Reporter:  mspacek     |       Owner:  somebody
     Type:  defect      |      Status:  new     
 Priority:  high        |   Milestone:          
Component:  numpy.core  |     Version:  1.4.0   
 Keywords:              |  
 See thread at

 Pickling and then unpickling an entry of <type 'numpy.void'> or <type
 'numpy.record'> from a structured array or recarray results in data
 corruption on the round trip:

 >>> import numpy as np
 >>> x = np.zeros((2,), dtype=('i4,f4,a10'))
 >>> x[:] = [(1,2.,'Hello'), (2,3.,"World")]
 >>> x
 array([(1, 2.0, 'Hello'), (2, 3.0, 'World')],
        dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])
 >>> x[0]
 (1, 2.0, 'Hello')
 >>> type(x[0])
 <type 'numpy.void'>
 >>> import pickle
 >>> s = pickle.dumps(x[0])
 >>> newx0 = pickle.loads(s)
 >>> newx0
 (30917960, 1.6904535998413144e-38, '\xd0\xef\x1c\x1eZ\x03\x00d')
 >>> s
 >>> type(newx0)
 <type 'numpy.void'>
 >>> newx0.dtype
 dtype([('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])
 >>> x[0].dtype
 dtype([('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])
 >>> np.version.version

 It seems that the pickling works fine, but there's corruption on

 Robert Kern says: "The implementation of numpy.core.multiarray.scalar is
 doing something wrong."

 Pauli Virtanen says: "The relevant code path to look at is
 multiarraymodule:array_scalar -> scalarapi.c:PyArray_Scalar. Needs some
 cgdb'ing to find out what's going on there."

 A nice temporary workaround is to convert the original structured array
 "x" into a list of scalar arrays, each of whose entries then
 pickle/unpickle correctly, and still allow dict-style named field access:

 >>> x2 = map(np.asarray, x)

Ticket URL: <http://projects.scipy.org/numpy/ticket/1415>
NumPy <http://projects.scipy.org/numpy>
My example project

More information about the NumPy-Tickets mailing list