Skip to content
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

BUG: floating types should override tp_print #10763

Merged
merged 1 commit into from
Apr 5, 2018

Conversation

ahaldane
Copy link
Member

Fixes #10753

It turns out that (in python2 only) python's float type implements the tp_print method slot, and we inherit it in numpy float types. This is a mostly obsolete method which overrides str and repr in some situations. Its a bit of an anachronism that float implements it.

Since we now override the float str/repr in numpy (with dragon4), we should also override tp_print. This PR does that.

In case it is useful, here is the python2 doc on tp_print: (link)

This might be a candidate for putting in a 1.14.3 release, though maybe this is too small a bug for that.

@ahaldane ahaldane force-pushed the fix_float_repr_tp_print_override branch from b316305 to 3a10dc6 Compare March 17, 2018 23:53
@ahaldane ahaldane added this to the 1.14.3 release milestone Mar 18, 2018
output = f.read()
f.close()
assert_equal(output, '0.1999999999999\n')
# (compare to "print 0.1999999999999" in python2)
Copy link
Member

Choose a reason for hiding this comment

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

Can you say what the output is here, rather than making the reader run it?

@@ -45,6 +46,20 @@ def check(v):
check(1e15)
check(1e16)

# gh-10753
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be its own test function - it doesn't really test the same thing.

/*
* In python2, the `float` type still implements the obsolete "tp_print"
* method, which uses CPython's float-printing routines to print the float.
* Numpy's floating types PyFloatingArr_Type inherit from float, but override
Copy link
Member

Choose a reason for hiding this comment

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

Not true - only PyDoubleArr_Type does - perhaps just replace that one?

@ahaldane ahaldane force-pushed the fix_float_repr_tp_print_override branch from 3a10dc6 to 52ca71f Compare March 18, 2018 03:08
@ahaldane
Copy link
Member Author

Thanks, updated.

* In python3 the tp_print method is reserved/unused.
*/
static int
float_print(PyObject *o, FILE *fp, int flags)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please pick a name that differs from the name used by the builtin float type. Something like doubletype_print would be a reasonable choice and consistent with the other PyDoubleArrType_Type methods.

f.seek(0)
output = f.read()
f.close()
assert_equal(output, '0.1999999999999\n')
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps assert equality with str(x) + '\n'? Not sure if there ever can be rounding issues here, but what we care about is that str and print give the same result...

@ahaldane ahaldane force-pushed the fix_float_repr_tp_print_override branch 2 times, most recently from 68dff2d to b2f99d0 Compare March 19, 2018 16:31
print(x, file=f)
f.seek(0)
output = f.read()
f.close()
Copy link
Member

Choose a reason for hiding this comment

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

Can you use a with statement here?

@ahaldane ahaldane force-pushed the fix_float_repr_tp_print_override branch from b2f99d0 to 0d6296a Compare March 19, 2018 18:09
@charris charris merged commit a3c2801 into numpy:master Apr 5, 2018
@charris
Copy link
Member

charris commented Apr 5, 2018

Thanks Allan.

return -1;
}

ret = PyObject_Print(to_print, fp, flags);
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong, and adds extra quotes.

Should be PyObject_Print(to_print, fp, Py_PRINT_RAW);

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.

5 participants