Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
480ab05d
Commit
480ab05d
authored
Apr 14, 2018
by
Antoine Pitrou
Committed by
GitHub
Apr 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-33176: Add a toreadonly() method to memoryviews. (GH-6466)
parent
b1dc0750
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
16 deletions
+73
-16
Doc/library/stdtypes.rst
Doc/library/stdtypes.rst
+19
-0
Lib/test/test_buffer.py
Lib/test/test_buffer.py
+23
-16
Lib/test/test_memoryview.py
Lib/test/test_memoryview.py
+11
-0
Misc/NEWS.d/next/Core and Builtins/2018-04-13-22-31-09.bpo-33176.PB9com.rst
...ore and Builtins/2018-04-13-22-31-09.bpo-33176.PB9com.rst
+1
-0
Objects/memoryobject.c
Objects/memoryobject.c
+19
-0
No files found.
Doc/library/stdtypes.rst
View file @
480ab05d
...
...
@@ -3591,6 +3591,25 @@ copying.
:mod:`struct` module syntax as well as multi-dimensional
representations.
.. method:: toreadonly()
Return a readonly version of the memoryview object. The original
memoryview object is unchanged. ::
>>> m = memoryview(bytearray(b'abc'))
>>> mm = m.toreadonly()
>>> mm.tolist()
[89, 98, 99]
>>> mm[0] = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot modify read-only memory
>>> m[0] = 43
>>> mm.tolist()
[43, 98, 99]
.. versionadded:: 3.8
.. method:: release()
Release the underlying buffer exposed by the memoryview object. Many
...
...
Lib/test/test_buffer.py
View file @
480ab05d
...
...
@@ -924,23 +924,30 @@ class TestBufferProtocol(unittest.TestCase):
except
BufferError
:
# re-exporter does not provide full information
return
ex
=
result
.
obj
if
isinstance
(
result
,
memoryview
)
else
result
self
.
assertIs
(
m
.
obj
,
ex
)
self
.
assertEqual
(
m
.
nbytes
,
expected_len
)
self
.
assertEqual
(
m
.
itemsize
,
itemsize
)
self
.
assertEqual
(
m
.
format
,
fmt
)
self
.
assertEqual
(
m
.
readonly
,
readonly
)
self
.
assertEqual
(
m
.
ndim
,
ndim
)
self
.
assertEqual
(
m
.
shape
,
tuple
(
shape
))
if
not
(
sliced
and
suboffsets
):
self
.
assertEqual
(
m
.
strides
,
tuple
(
strides
))
self
.
assertEqual
(
m
.
suboffsets
,
tuple
(
suboffsets
))
n
=
1
if
ndim
==
0
else
len
(
lst
)
self
.
assertEqual
(
len
(
m
),
n
)
rep
=
result
.
tolist
()
if
fmt
else
result
.
tobytes
()
self
.
assertEqual
(
rep
,
lst
)
self
.
assertEqual
(
m
,
result
)
def
check_memoryview
(
m
,
expected_readonly
=
readonly
):
self
.
assertIs
(
m
.
obj
,
ex
)
self
.
assertEqual
(
m
.
nbytes
,
expected_len
)
self
.
assertEqual
(
m
.
itemsize
,
itemsize
)
self
.
assertEqual
(
m
.
format
,
fmt
)
self
.
assertEqual
(
m
.
readonly
,
expected_readonly
)
self
.
assertEqual
(
m
.
ndim
,
ndim
)
self
.
assertEqual
(
m
.
shape
,
tuple
(
shape
))
if
not
(
sliced
and
suboffsets
):
self
.
assertEqual
(
m
.
strides
,
tuple
(
strides
))
self
.
assertEqual
(
m
.
suboffsets
,
tuple
(
suboffsets
))
n
=
1
if
ndim
==
0
else
len
(
lst
)
self
.
assertEqual
(
len
(
m
),
n
)
rep
=
result
.
tolist
()
if
fmt
else
result
.
tobytes
()
self
.
assertEqual
(
rep
,
lst
)
self
.
assertEqual
(
m
,
result
)
check_memoryview
(
m
)
with
m
.
toreadonly
()
as
mm
:
check_memoryview
(
mm
,
expected_readonly
=
True
)
m
.
tobytes
()
# Releasing mm didn't release m
def
verify_getbuf
(
self
,
orig_ex
,
ex
,
req
,
sliced
=
False
):
def
simple_fmt
(
ex
):
...
...
Lib/test/test_memoryview.py
View file @
480ab05d
...
...
@@ -362,6 +362,17 @@ class AbstractMemoryTests:
self
.
assertEqual
(
list
(
reversed
(
m
)),
aslist
)
self
.
assertEqual
(
list
(
reversed
(
m
)),
list
(
m
[::
-
1
]))
def
test_toreadonly
(
self
):
for
tp
in
self
.
_types
:
b
=
tp
(
self
.
_source
)
m
=
self
.
_view
(
b
)
mm
=
m
.
toreadonly
()
self
.
assertTrue
(
mm
.
readonly
)
self
.
assertTrue
(
memoryview
(
mm
).
readonly
)
self
.
assertEqual
(
mm
.
tolist
(),
m
.
tolist
())
mm
.
release
()
m
.
tolist
()
def
test_issue22668
(
self
):
a
=
array
.
array
(
'H'
,
[
256
,
256
,
256
,
256
])
x
=
memoryview
(
a
)
...
...
Misc/NEWS.d/next/Core and Builtins/2018-04-13-22-31-09.bpo-33176.PB9com.rst
0 → 100644
View file @
480ab05d
Add a ``toreadonly()`` method to memoryviews.
Objects/memoryobject.c
View file @
480ab05d
...
...
@@ -1398,6 +1398,20 @@ error:
return
NULL
;
}
static
PyObject
*
memory_toreadonly
(
PyMemoryViewObject
*
self
,
PyObject
*
noargs
)
{
CHECK_RELEASED
(
self
);
/* Even if self is already readonly, we still need to create a new
* object for .release() to work correctly.
*/
self
=
(
PyMemoryViewObject
*
)
mbuf_add_view
(
self
->
mbuf
,
&
self
->
view
);
if
(
self
!=
NULL
)
{
self
->
view
.
readonly
=
1
;
};
return
(
PyObject
*
)
self
;
}
/**************************************************************************/
/* getbuffer */
...
...
@@ -3061,6 +3075,10 @@ PyDoc_STRVAR(memory_cast_doc,
"cast($self, /, format, *, shape)
\n
--
\n
\
\n
\
Cast a memoryview to a new format or shape."
);
PyDoc_STRVAR
(
memory_toreadonly_doc
,
"toreadonly($self, /)
\n
--
\n
\
\n
\
Return a readonly version of the memoryview."
);
static
PyMethodDef
memory_methods
[]
=
{
{
"release"
,
(
PyCFunction
)
memory_release
,
METH_NOARGS
,
memory_release_doc
},
...
...
@@ -3068,6 +3086,7 @@ static PyMethodDef memory_methods[] = {
{
"hex"
,
(
PyCFunction
)
memory_hex
,
METH_NOARGS
,
memory_hex_doc
},
{
"tolist"
,
(
PyCFunction
)
memory_tolist
,
METH_NOARGS
,
memory_tolist_doc
},
{
"cast"
,
(
PyCFunction
)
memory_cast
,
METH_VARARGS
|
METH_KEYWORDS
,
memory_cast_doc
},
{
"toreadonly"
,
(
PyCFunction
)
memory_toreadonly
,
METH_NOARGS
,
memory_toreadonly_doc
},
{
"__enter__"
,
memory_enter
,
METH_NOARGS
,
NULL
},
{
"__exit__"
,
memory_exit
,
METH_VARARGS
,
NULL
},
{
NULL
,
NULL
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment