Skip to content

MAINT/TST: Tidy dtype indexing #9947

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 2 commits into from
Nov 2, 2017
Merged

Conversation

eric-wieser
Copy link
Member

Previously descr_subscript would recurse, which was needlessly confusing.

Extracted from #8814, which fell into the trap of combining ENH with MAINT.

@eric-wieser eric-wieser changed the title MAINT/TEST: Tidy dtype indexing MAINT/TST: Tidy dtype indexing Nov 1, 2017
…tions

Benefits of new structure:
 * Checking of fields doesn't happen twice
 * No longer any fear of possible recursion
 * Makes it easy to add sq_item later

Also tidies initialization of the PySequenceMethods struct
static PyObject *
_subscript_by_index(PyArray_Descr *self, Py_ssize_t i)
{
PyObject *name = PySequence_GetItem(self->names, i);
Copy link
Member Author

Choose a reason for hiding this comment

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

Using PySequence_GetItem here means we don't have to implement our own handling of negative indices

int value = PyArray_PyIntAsInt(op);
int orig_value = value;

Py_ssize_t i = PyArray_PyIntAsIntp(op);
Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to Py_ssize_t to avoid compiler warnings.

return NULL;
}
name = PyTuple_GET_ITEM(self->names, value);
retval = descr_subscript(self, name);
Copy link
Member Author

@eric-wieser eric-wieser Nov 1, 2017

Choose a reason for hiding this comment

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

This recursion was a strange way to handle this, and caused the field check to occur twice.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Looks good, code quite a bit clearer now (though I'm surprised we need the special unicode/bytes handling - one would hope there is some standard way of doing this; anyway, beyond this PR...)

@eric-wieser
Copy link
Member Author

eric-wieser commented Nov 2, 2017

I'm surprised we need the special unicode/bytes handling

Really that's just because we're too lazy to call PyObject_Format and PyErr_SetObject with error checking. I can do that in a separate PR after this one, if you'd like.

EDIT: Actually, this is not about laziness - the problem is that if you hit an exception while trying to build an exception message in the C code, you end up with no stack trace, and have no idea what actually went wrong.

@mhvk
Copy link
Contributor

mhvk commented Nov 2, 2017

Don't worry about it - or at least not just for this piece - it would seem something that could be cleaned up more generally. This one looks all OK and tests passed, so I'll merge.

@mhvk mhvk merged commit 103f23c into numpy:master Nov 2, 2017
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.

2 participants