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: only tell/seek np.fromfile when buffered #6330

Merged
merged 2 commits into from Oct 7, 2015

Conversation

cjermain
Copy link
Contributor

This fixes bug #6246 in Python 3, by using the io module to check if the file object is a child of the RawIOBase, which are always unbuffered. Since it is not buffered, there is no reason to call tell or seek, which can fail on some legitimate file objects (e.g. wking/pycomedi#3).

cjermain added a commit to pymeasure/pymeasure that referenced this pull request Sep 18, 2015
@mhvk
Copy link
Contributor

mhvk commented Sep 18, 2015

Wouldn't it make more sense to duck-type this, i.e., only check whether the file is unbuffered when tell has failed? That avoids needless work for the likely more common case where it is a regular file.

@pv
Copy link
Member

pv commented Sep 18, 2015

tell() doesn't fail for buffered files, the Python-side buffering just
ends up in an inconsistent state, so I don't think the check can be avoided.
EDIT: sorry, ok, it's indeed possible to do the check as a fallback after failure

@cjermain
Copy link
Contributor Author

Ok. How about that? I can squash the commits into 1 if needed.

/* File object instances of RawIOBase are unbuffered */
io_raw = PyObject_GetAttrString(io, "RawIOBase");
if (io_raw == NULL) {
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not really my forte at all, but shouldn't io be decref'd here?

@cjermain
Copy link
Contributor Author

Py_DECREF must not be called on NULL, so I don't think they apply. Also see the os import in the previous part of that file which does not. I did make a minor fix on the positioning of the Py_DECREF statements for io.

@mhvk
Copy link
Contributor

mhvk commented Sep 18, 2015

@cjermain - The fix you did is what I had in mind; I was worried one might be left with a hanging reference to io itself.

@cjermain
Copy link
Contributor Author

I've rebased this as a single commit. Anything else needed?

}
unbuf = PyObject_IsInstance(file, io_raw);
Py_DECREF(io_raw);
if (unbuf == 1) { // Unbuffered
Copy link
Member

Choose a reason for hiding this comment

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

No C++ comments. Put on next line with /*...*/

@charris
Copy link
Member

charris commented Oct 3, 2015

LGTM modulo style. @pv, @mhvk Comments?

@charris
Copy link
Member

charris commented Oct 3, 2015

@cjermain Can you think of a way to test this?

@cjermain
Copy link
Contributor Author

cjermain commented Oct 4, 2015

I've fixed the C styling and squashed the changes.

I think the best way to test it is to construct an unbuffered FileIO object and then modify the seek/tell methods to raise an error. Then run numpy.fromfile on it.

@charris
Copy link
Member

charris commented Oct 4, 2015

@cjermain Great. The proper place for a test looks to be numpy/core/tests/test_multiarray.py. Note that there are also some data files in numpy/core/tests/data if you want to add one for the original failure.

/* The io module is needed to determine if buffering is used */
io = PyImport_ImportModule("io");
if (io == NULL) {
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Here and below, shouldn't the file handle be closed before returning NULL?

@charris
Copy link
Member

charris commented Oct 5, 2015

Looks like you merged master somewhere down the line, hence endolith's commit. I suspect that will go away if you update your master from upstream and rebase this branch on it.

@cjermain
Copy link
Contributor Author

cjermain commented Oct 7, 2015

Ok, I've rebased on the master which fixed the problem.

charris added a commit that referenced this pull request Oct 7, 2015
BUG: only tell/seek np.fromfile when buffered
@charris charris merged commit d573c63 into numpy:master Oct 7, 2015
@charris
Copy link
Member

charris commented Oct 7, 2015

Merged. Thanks @cjermain .

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.

None yet

4 participants