This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [doc] mmap.resize changes memory address of mmap'd region
Type: enhancement Stage: resolved
Components: ctypes, Documentation, Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: alex, docs@python, eryksun, schmichael
Priority: normal Keywords:

Created on 2011-09-10 20:24 by schmichael, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
killctypes.py schmichael, 2011-09-10 20:24 Example using ctypes.<type>.buffer_from(<mmap>) to get a segfault
Messages (3)
msg143845 - (view) Author: Michael Schurter (schmichael) Date: 2011-09-10 20:24
Since mmap.resize uses MREMAP_MAYMOVE on Linux, it would be nice to warn users that pointers into their memory mapped regions will be invalid after resizes. Linux's manpage offers the following explanation:

"If the mapping is relocated, then absolute pointers into the old mapping location become invalid (offsets relative to the starting address of the mapping should be employed)."

Since the only way I know of to use pointers into mmaps from pure Python is to use ctypes.<type>.from_buffer(<mmap>), perhaps clarification would be better in the ctypes docs.
msg228603 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-10-05 18:02
@Michael can you provide a patch to this effect?
msg407439 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-12-01 10:38
mmap no longer allows resizing if the object has buffer exports. For example:

    >>> m = mmap.mmap(-1, 4096, access=mmap.ACCESS_WRITE)
    >>> p = (ctypes.c_char * 4096).from_buffer(m)
    >>> m.resize(8192)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    BufferError: mmap can't resize with extant buffers exported.

    >>> del p
    >>> m.resize(8192)
    >>> len(m)
    8192
History
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57166
2021-12-01 10:38:24eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg407439

resolution: out of date
stage: needs patch -> resolved
2021-12-01 10:11:10iritkatrielsettitle: mmap.resize changes memory address of mmap'd region -> [doc] mmap.resize changes memory address of mmap'd region
components: + Library (Lib), ctypes
versions: + Python 3.11, - Python 2.7, Python 3.4, Python 3.5
2019-04-26 17:50:52BreamoreBoysetnosy: - BreamoreBoy
2014-10-05 18:02:37BreamoreBoysetnosy: + BreamoreBoy

messages: + msg228603
versions: + Python 3.5, - Python 3.3
2013-07-07 18:31:55christian.heimessetstage: needs patch
versions: + Python 3.3, Python 3.4
2011-09-10 21:19:43alexsetnosy: + alex
2011-09-10 20:24:18schmichaelcreate