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

Skip to content

Commit d7e2f25

Browse files
committed
Make mutable objects the default
It's more common that the user will want a memoryview object that can be mutated. An immutable object is more rare, so make the default case return a mutable object.
1 parent db3a26a commit d7e2f25

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

Doc/library/ctypes.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,19 +2109,19 @@ Utility functions
21092109
.. audit-event:: ctypes.wstring_at address,size ctypes.wstring_at
21102110

21112111

2112-
.. function:: memoryview_at(address, size, allow_write=False)
2112+
.. function:: memoryview_at(address, size, readonly=False)
21132113

21142114
This function returns a :class:`memoryview` object that references the
21152115
memory starting at *address* up to (but not including) *address +
2116-
size*. If *allow_write* is set to a truthy value then the
2117-
:class:`!memoryview` object is mutable.
2116+
size*. If *readonly* is set to a truthy value then the
2117+
:class:`!memoryview` object is immutable.
21182118

21192119
This function is similar to :func:`string_at` with the key
21202120
difference of not making a copy of the specified memory. It is a
21212121
semantically equivalent (but more efficient) alternative to
21222122
``memoryview((c_byte * size).from_address(address))``
21232123

2124-
.. audit-event:: ctypes.memoryview_at address,size,allow_write ctypes.buffer_at
2124+
.. audit-event:: ctypes.memoryview_at address,size,readonly ctypes.buffer_at
21252125

21262126
.. versionadded:: 3.13
21272127

Lib/ctypes/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,11 @@ def wstring_at(ptr, size=-1):
541541
from _ctypes import _memoryview_at_addr
542542

543543
_memoryview_at = PYFUNCTYPE(py_object, c_void_p, c_ssize_t, c_int)(_memoryview_at_addr)
544-
def memoryview_at(ptr, size, allow_write=False):
545-
"""memoryview_at(addr, size[, allow_write]) -> memoryview
544+
def memoryview_at(ptr, size, readonly=False):
545+
"""memoryview_at(addr, size[, readonly]) -> memoryview
546546
547547
Return a memoryview representing the memory at addr."""
548-
return _memoryview_at(ptr, size, bool(allow_write))
548+
return _memoryview_at(ptr, size, bool(readonly))
549549

550550
if _os.name == "nt": # COM stuff
551551
def DllGetClassObject(rclsid, riid, ppv):

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5643,15 +5643,15 @@ wstring_at(const wchar_t *ptr, int size)
56435643
}
56445644

56455645
static PyObject *
5646-
memoryview_at(char *ptr, Py_ssize_t size, int allow_write)
5646+
memoryview_at(char *ptr, Py_ssize_t size, int readonly)
56475647
{
56485648
if (PySys_Audit("ctypes.memoryview_at", "nni", (Py_ssize_t)ptr, size,
5649-
allow_write) < 0) {
5649+
readonly) < 0) {
56505650
return NULL;
56515651
}
56525652

56535653
return PyMemoryView_FromMemory(ptr, size,
5654-
allow_write ? PyBUF_WRITE : PyBUF_READ);
5654+
readonly ? PyBUF_READ : PyBUF_WRITE);
56555655
}
56565656

56575657
static struct PyModuleDef _ctypesmodule = {

0 commit comments

Comments
 (0)