Skip to content

Commit c8adb89

Browse files
White-Reaper0claude
andcommitted
gh-162539: Document that ctypes.resize() can move the buffer
Like realloc(), resize() may move the memory block, so pointers, byref() results and addresses taken before the call become invalid. ctypes cannot track raw addresses, so this is documented rather than enforced. Also document the BufferError raised while the buffer is in use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 752fccc commit c8adb89

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎Doc/library/ctypes.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,17 @@ Utility functions
24902490
than the native size of the objects type, as given by ``sizeof(type(obj))``,
24912491
but it is possible to enlarge the buffer.
24922492

2493+
Like the C ``realloc()`` function, resizing may move the buffer to a new
2494+
address. Pointers into the old buffer, such as those created with
2495+
:func:`pointer`, :func:`cast` or :func:`byref`, and addresses returned by
2496+
:func:`addressof`, are no longer valid after the call and must not be used.
2497+
2498+
:exc:`BufferError` is raised if the buffer is in use and cannot be moved,
2499+
for example while a value is being stored into *obj*.
2500+
2501+
.. versionchanged:: next
2502+
Raise :exc:`BufferError` instead of moving a buffer that is in use.
2503+
24932504

24942505
.. function:: set_errno(value)
24952506

0 commit comments

Comments
 (0)