Skip to content

bpo-33176: Add a toreadonly() method to memoryviews.#6466

Merged
pitrou merged 2 commits into
python:masterfrom
pitrou:memoryview_toreadonly
Apr 14, 2018
Merged

bpo-33176: Add a toreadonly() method to memoryviews.#6466
pitrou merged 2 commits into
python:masterfrom
pitrou:memoryview_toreadonly

Conversation

@pitrou
Copy link
Copy Markdown
Member

@pitrou pitrou commented Apr 13, 2018

Comment thread Objects/memoryobject.c Outdated
{
CHECK_RELEASED(self);
if (self->view.readonly) {
Py_INCREF(self);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we always need to use mbuf_add_view because of the release() thingy:

>>> b = bytes(10)
>>> m = memoryview(b)
>>> with m.toreadonly() as x:
...     pass
... 
>>> m[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operation forbidden on released memoryview object

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for catching this. It's indeed necessary to create a new object.

@skrah
Copy link
Copy Markdown
Contributor

skrah commented Apr 14, 2018

Looks good!

@pitrou pitrou merged commit 480ab05 into python:master Apr 14, 2018
@pitrou pitrou deleted the memoryview_toreadonly branch April 14, 2018 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants