Skip to content

BUG: Incorrect assignment to recarray items #3561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
magistere opened this issue Jul 31, 2013 · 4 comments
Closed

BUG: Incorrect assignment to recarray items #3561

magistere opened this issue Jul 31, 2013 · 4 comments

Comments

@magistere
Copy link

Assigning to field of recarray is not working correctly. Example script is below.

import numpy as np

rec = np.recarray(1, dtype=[('x', float, 5)])

# Bug behaviour
rec[0].x = 1
print rec[0] # should print: (array([ 1.,  1.,  1.,  1.,  1.]),)
             # but prints uninitialized float values

# Correct behaviour
rec.x = 2
print rec[0] # prints: (array([ 2.,  2.,  2.,  2.,  2.]),)
mbyt added a commit to mbyt/numpy that referenced this issue Mar 30, 2014
Raising an error for non supported operations. Also adapting related
statement in test. Resolves numpy#3126 and numpy#3561.
@charris
Copy link
Member

charris commented Mar 30, 2014

Looks to be a duplicate of #3126.

@ahaldane
Copy link
Member

This is a bug in ndarray, nor recarray. For example:

>>> a = np.zeros(1, dtype=[('x', float, 5)])
>>> a[0]
([0.0, 0.0, 0.0, 0.0, 0.0],)
>>> a[0]['x']
array([ 0.,  0.,  0.,  0.,  0.])
>>> a[0]['x'] = 1
>>> a
array([([1.0, 0.0, 0.0, 0.0, 0.0],)], 
      dtype=[('x', '<f8', (5,))])

In particular, I think it is a bug in ndarray.setfield bwing too permissive. Maybe this is what the user should be writing:?

>>> a[0]['x'][:] = 1

I think ndarray.getfield and ndarray.setfield are buggy (eg, see the segfauly I cause in #3256 (comment)). I think it might be possible to make them simply call np.view in the right way to solve all these problems. But it may need to wait until some other issues with views are solved (see #5508 #3256 #3286 #2599)

@mbyt
Copy link
Contributor

mbyt commented Feb 2, 2015

@ahaldane, I have proposed a possible solution to this (raise at least an error) in the pull request #4556 --- which is still under review...

Does this really have to wait? It's just a 15 line long change / patch in production code.

@ahaldane
Copy link
Member

ahaldane commented Feb 2, 2015

Oh, I missed that link to your pull request above.

I took at your pull request and it seems like a good fix for now. I tested it and it worked, and the code looks OK (though I am a numpy code novice).

What I mentioned about waiting is that I think this problem (plus others) might be solved by making getfield and setfield go through the 'view' machinery. This way all the checks that happen for views would automatically happen for get/setfield.

In #4556 you gave this example:

>>> x = np.empty(1, dtype=[('field', 'i4', 10), ('b', 'O')])
>>> x[0]['field'] = 4 * ones # doesn't work properly currently

I imagine setfield could do something like:

>>> t,o = x[0].dtype.fields['field']
>>> xv = x[0].view(dtype({'names': ['field'], 
                          'formats': [t], 
                          'offsets': [o], 
                          'itemsize': x.dtype.itemsize}))
>>> #then do array_assign(xv, 4*ones)

Taking a view would also solve the following problem:

>>> x[0].setfield(1, 'i8', x.dtype.fields['b'][1])
>>> x
zsh: segmentation fault (core dumped)

Now I admit I have not looked deeply into the assignment machinery yet so maybe this isn't possible...

In my opinion your pull should be merged for now. I mentioned "waiting" only with repsect to trying out these additional ideas about views.

ahaldane added a commit to ahaldane/numpy that referenced this issue Mar 13, 2015
This commit modifiesvoidtype_get/setfield to that they call the ndarray
get/setfield. This solves bugs related to void-scalar assignment.

This fixes issues numpy#3126,
numpy#3561.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants