Skip to content

MAINT: reduce void type repr code duplication #11830

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
Aug 30, 2018

Conversation

tylerjereddy
Copy link
Contributor

@tylerjereddy tylerjereddy commented Aug 28, 2018

The void type __str__ and __repr__ handling code at the C level was almost completely identical in their respective C functions. Both already called common __repr__ handling code at the Python level, and this PR aims to similarly reduce code duplication / maintenance burden at the C level.

Note that it is, of course, not uncommon for __str__ implementations to simply call __repr__, and the quaternion dtype implementation uses exact duplicate code for both instead of one calling the other.

Here, both functions weren't quite identical, so the common handler function accepts arguments to allow the formatting differences between __repr__ and __str__ to be specified while abstracting the common logic.

void type discussion / work in PR from @ahaldane : #8981


return PyObject_CallFunction(reprfunc, "O", self);
Copy link
Member

@eric-wieser eric-wieser Aug 29, 2018

Choose a reason for hiding this comment

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

I'd be inclined just to extract the contents of this if statement into a C-side helper function _void_scalar_repr- we might want to have different behavior for str and repr of a structured void in future:

static PyObject *
_void_scalar_repr(PyObject *obj) {
    static PyObject *reprfunc = NULL;	
    npy_cache_import("numpy.core.arrayprint",	
                     "_void_scalar_repr", &reprfunc);	
    if (reprfunc == NULL) {	
        return NULL;	
    }	
    return PyObject_CallFunction(reprfunc, "O", obj);
}

Copy link
Member

@eric-wieser eric-wieser Aug 29, 2018

Choose a reason for hiding this comment

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

And then

static PyObject *
voidtype_str(PyObject *self)
{
    PyVoidScalarObject *s = (PyVoidScalarObject*) self;
    if (PyDataType_HASFIELDS(s->descr)) {
        return _void_scalar_repr(self);
    }
    return _void_to_hex(s->obval, s->descr->elsize, "b'", "\\x", "'");
}

I'd prefer for every void member function to have an if (PyDataType_HASFIELDS ...) branch to reinforce the idea that structured types and raw-byte types should be two separate things, and at some point we should try and split void into two pieces

* voidtype_str and voidtype_repr previously
had nearly-identical code; the shared code
has now been abstracted to a handler function
@tylerjereddy
Copy link
Contributor Author

Thanks Eric, revised accordingly (I hope).

@eric-wieser eric-wieser merged commit fdbf432 into numpy:master Aug 30, 2018
@eric-wieser
Copy link
Member

Thanks!

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