Skip to content

BUG: do not change size 0 description when viewing data #8971

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

Merged
merged 1 commit into from
Apr 23, 2017

Conversation

juliantaylor
Copy link
Contributor

@juliantaylor juliantaylor commented Apr 21, 2017

If the array being viewed is a zero sized array, e.g. 'S0' from
structured types, do not change the description to itemsize 1 as this
would expose uninitialized data, e.g. in
TestStructured.test_zero_width_string

See also #6430

@eric-wieser
Copy link
Member

eric-wieser commented Apr 21, 2017

Looks good to me, although this is made moot by #8970.

Probably should test the ravel case that brought this up

@juliantaylor
Copy link
Contributor Author

added a test

@@ -1073,6 +1073,9 @@ def test_zero_width_string(self):
xx = x['S'].reshape((2, 2))
assert_equal(xx.itemsize, 0)
assert_equal(xx, [[b'', b''], [b'', b'']])
# check for no uninitialized memory due to viewing S0 array
repr_str = ''.join(repr(xx).replace('array', 'np.array').splitlines())
assert_array_equal(eval(repr_str), xx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a assert_equal(xx.ravel().dtype, xx.dtype) too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ravel makes a copy here which converts it to 'S1'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a test with a simple indexing view

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure ravel makes a copy? I've found a bunch of places with zeros-sized items that it returns a view, and generally does the right thing.

Either way, the indexing tests the right thing, so no changes needed.

Copy link
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good

# check for no uninitialized memory due to viewing S0 array
assert_equal(xx[:].dtype, xx.dtype)
repr_str = ''.join(repr(xx).replace('array', 'np.array').splitlines())
assert_array_equal(eval(repr_str), xx)
Copy link
Member

@eric-wieser eric-wieser Apr 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about eval(repr_str, dict(array=np.array))? I don't think you need any of the line-mangling either

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed that is nicer, thanks

@charris charris added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Apr 22, 2017
@charris
Copy link
Member

charris commented Apr 22, 2017

@juliantaylor Needs release note.

@juliantaylor
Copy link
Contributor Author

this is a bugfix, no release notes needed

If the array being viewed is a zero sized array, e.g. 'S0' from
structured types, do not change the description to itemsize 1 as this
would expose uninitialized data, e.g. in
TestStructured.test_zero_width_string
@eric-wieser
Copy link
Member

I'll wait for @charris to confirm this is OK without a release note, but this looks ready to merge to me (and is no longer moot, since #8970 is somewhat rejected)

@charris charris removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Apr 23, 2017
@charris charris merged commit 4408f74 into numpy:master Apr 23, 2017
@charris
Copy link
Member

charris commented Apr 23, 2017

My bad, no release note needed. Merged, thanks @juliantaylor @eric-wieser .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants