Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DEP: Speed up WarnOnWrite deprecation in buffer interface #13977

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

Closed
wants to merge 1 commit into from

Conversation

seberg
Copy link
Member

@seberg seberg commented Jul 13, 2019

When a buffer interface does not request a writeable buffer,
simply pass a read-only one when the warn on write flag is set.

This is to give an easier way forward with avoiding the deprecation
warnings: Simply do not ask for a writeable buffer.

It will break code that expects writeable buffers but does not
ask for them specifically a bit harder than would be nice.
But since such code probably should ask for it specifically, this
is likely fine (an RC release has to find out).

The main reason for this is, that this way it plays very will with
cython, which requests writeable buffers explicitly and if declared
const is happy about read-only (so that using const is the best
way to avoid the warning and makes code cleaner).

Closes gh-13929, gh-13974


There are two things:

  1. We need to decide that we want to do this, to me this seems like a viable option (i.e. if someone does not need a writeable buffer as such, they should be fine, if someone needs a writeable buffer, they should be requesting it specifically.
  2. We could try to add a more descriptive message that tells someone things about memoryview (and even mentions cython) when this path is hit.

When a buffer interface does not request a writeable buffer,
simply pass a read-only one when the warn on write flag is set.

This is to give an easier way forward with avoiding the deprecation
warnings: Simply do not ask for a writeable buffer.

It will break code that expects writeable buffers but does not
ask for them specifically a bit harder than would be nice.
But since such code probably should ask for it specifically, this
is likely fine (an RC release has to find out).

The main reason for this is, that this way it plays very will with
cython, which requests writeable buffers explicitly and if declared
`const` is happy about read-only (so that using `const` is the best
way to avoid the warning and makes code cleaner).

Closes numpygh-13929, numpygh-13974
@charris
Copy link
Member

charris commented Jul 14, 2019

Hmm, the usual procedure is to forward port the release notes after a branch. Since this will be backported, it might be easier be easier to make it against 1.17.x to start with, then forward port the fix without the note to master.

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Jul 14, 2019
@charris charris added this to the 1.17.0 release milestone Jul 14, 2019
@eric-wieser
Copy link
Member

Can we add a test for this? assert memoryview(np.broadcast_arrays(...)).readonly would hit this path, I believe

* If a read-only buffer is requested on a read-write array, we return a
* read-write buffer, which is dubious behavior. But that's why this call
* is guarded by PyArray_ISWRITEABLE rather than (flags &
* PyBUF_WRITEABLE).
Copy link
Member

Choose a reason for hiding this comment

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

This comment still applies, right? Perhaps just needs moving down lower.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, will move it and rephrase slightly. It is not dubious in the sense that I think that is what the buffer protocol documents to happen.

if (array_might_be_written(self) < 0) {
goto fail;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

As an alternative to speeding this deprecation, is it sufficient to change this and the change below to:

if (!PyArray_CHKFLAGS(self, NPY_ARRAY_WARN_ON_WRITE)) {
    readonly = PyArray_ISWRITEABLE(self);  // keep the old weird behavior and comment above
}
else if ((flags & PyBUF_WRITEABLE) == PyBUF_WRITEABLE) {
    // emit the warning from array_might_be_written
    readonly = False;
}
else {
    // return a readonly-array since doing so skirts the need for a warning
    readonly = True
}

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 I misunderstood the below, what I suggest is equivalent to what you already have and probably not any clearer.

* after a deprecation. This jumps the deprecation, but avoiding the
* warning is not convenient here and a warning is given if a writeable
* buffer is requested. (Warn on write is cleared if writeable is requested)
*/
Copy link
Member

Choose a reason for hiding this comment

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

To be clear - the warn on write is already handled above in PyArray_FailUnlessWriteable?

@seberg
Copy link
Member Author

seberg commented Jul 14, 2019

I will reopen this as a PR against 1.17.x, I hope I understood you right chuck.

@seberg seberg closed this Jul 14, 2019
@charris
Copy link
Member

charris commented Jul 14, 2019

@seberg I think that is the path of least hassle.

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 14, 2019
@charris charris removed this from the 1.17.0 release milestone Jul 14, 2019
@eric-wieser
Copy link
Member

For future reference, you can change the target branch without opening a new PR.

New PR is at gh-13993

@seberg seberg deleted the issue-13929 branch July 14, 2019 18:52
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.

False positives for warning about writing to broadcast array in cython code
3 participants