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

Skip to content

Commit db3a26a

Browse files
committed
Rename buffer_at to memoryview_at
It's a more descriptive and precise name.
1 parent eaa2d13 commit db3a26a

5 files changed

Lines changed: 26 additions & 19 deletions

File tree

Doc/library/ctypes.rst

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

21112111

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

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

2119-
This function is similar to :func:`string_at` with the key difference
2120-
of not making a copy of the specified memory.
2119+
This function is similar to :func:`string_at` with the key
2120+
difference of not making a copy of the specified memory. It is a
2121+
semantically equivalent (but more efficient) alternative to
2122+
``memoryview((c_byte * size).from_address(address))``
21212123

2122-
.. audit-event:: ctypes.buffer_at address,size,allow_write ctypes.buffer_at
2124+
.. audit-event:: ctypes.memoryview_at address,size,allow_write ctypes.buffer_at
21232125

21242126
.. versionadded:: 3.13
21252127

Doc/whatsnew/3.13.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,12 @@ copy
171171
ctypes
172172
------
173173

174-
* :func:`ctypes.buffer_at` now exists to create a :class:`memoryview` object that refers to
175-
the supplied pointer and length. Works just like :func:`ctypes.string_at` except
176-
it avoids a buffer copy.
174+
* :func:`ctypes.memoryview_at` now exists to create a
175+
:class:`memoryview` object that refers to the supplied pointer and
176+
length. Works just like :func:`ctypes.string_at` except it avoids a
177+
buffer copy. Useful when implementing callback functions in Python
178+
that are passed dynamically-sized buffers.
179+
(Contributed by Rian Hunter in :gh:`112018`.)
177180

178181
dbm
179182
---

Lib/ctypes/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,14 @@ def wstring_at(ptr, size=-1):
538538
Return the string at addr."""
539539
return _wstring_at(ptr, size)
540540

541-
from _ctypes import _buffer_at_addr
541+
from _ctypes import _memoryview_at_addr
542542

543-
_buffer_at = PYFUNCTYPE(py_object, c_void_p, c_ssize_t, c_int)(_buffer_at_addr)
544-
def buffer_at(ptr, size, allow_write=False):
545-
"""buffer_at(addr, size[, allow_write]) -> memoryview
543+
_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
546546
547-
Return the buffer at addr."""
548-
return _buffer_at(ptr, size, bool(allow_write))
547+
Return a memoryview representing the memory at addr."""
548+
return _memoryview_at(ptr, size, bool(allow_write))
549549

550550
if _os.name == "nt": # COM stuff
551551
def DllGetClassObject(rclsid, riid, ppv):
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
:func:`ctypes.buffer_at` now exists to create a :class:`memoryview` object that refers to
2-
the supplied pointer and length. Works just like :func:`ctypes.string_at` except
3-
it avoids a buffer copy.
1+
:func:`ctypes.memoryview_at` now exists to create a
2+
:class:`memoryview` object that refers to the supplied pointer and
3+
length. Works just like :func:`ctypes.string_at` except it avoids a
4+
buffer copy. Useful when implementing callback functions in Python
5+
that are passed dynamically-sized buffers.

Modules/_ctypes/_ctypes.c

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

56455645
static PyObject *
5646-
buffer_at(char *ptr, Py_ssize_t size, int allow_write)
5646+
memoryview_at(char *ptr, Py_ssize_t size, int allow_write)
56475647
{
5648-
if (PySys_Audit("ctypes.buffer_at", "nni", (Py_ssize_t)ptr, size,
5648+
if (PySys_Audit("ctypes.memoryview_at", "nni", (Py_ssize_t)ptr, size,
56495649
allow_write) < 0) {
56505650
return NULL;
56515651
}
@@ -5788,7 +5788,7 @@ _ctypes_add_objects(PyObject *mod)
57885788
MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at));
57895789
MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast));
57905790
MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
5791-
MOD_ADD("_buffer_at_addr", PyLong_FromVoidPtr(buffer_at));
5791+
MOD_ADD("_memoryview_at_addr", PyLong_FromVoidPtr(memoryview_at));
57925792

57935793
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
57945794
#if !HAVE_DECL_RTLD_LOCAL

0 commit comments

Comments
 (0)