From f19909e67e5a20b568ada5c5ccec97c5300b5d74 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 01:32:56 +0900 Subject: [PATCH 01/26] gh-142419: Add mmap.set_name method for annotation --- Modules/clinic/mmapmodule.c.h | 38 ++++++++++++++++++++++++++++++++++- Modules/mmapmodule.c | 18 +++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Modules/clinic/mmapmodule.c.h b/Modules/clinic/mmapmodule.c.h index f7fc172b3af705..b63f7df2a7e334 100644 --- a/Modules/clinic/mmapmodule.c.h +++ b/Modules/clinic/mmapmodule.c.h @@ -479,6 +479,42 @@ mmap_mmap_seek(PyObject *self, PyObject *const *args, Py_ssize_t nargs) return return_value; } +PyDoc_STRVAR(mmap_mmap_set_name__doc__, +"set_name($self, name, /)\n" +"--\n" +"\n"); + +#define MMAP_MMAP_SET_NAME_METHODDEF \ + {"set_name", (PyCFunction)mmap_mmap_set_name, METH_O, mmap_mmap_set_name__doc__}, + +static PyObject * +mmap_mmap_set_name_impl(mmap_object *self, const char *name); + +static PyObject * +mmap_mmap_set_name(PyObject *self, PyObject *arg) +{ + PyObject *return_value = NULL; + const char *name; + + if (!PyUnicode_Check(arg)) { + _PyArg_BadArgument("set_name", "argument", "str", arg); + goto exit; + } + Py_ssize_t name_length; + name = PyUnicode_AsUTF8AndSize(arg, &name_length); + if (name == NULL) { + goto exit; + } + if (strlen(name) != (size_t)name_length) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); + goto exit; + } + return_value = mmap_mmap_set_name_impl((mmap_object *)self, name); + +exit: + return return_value; +} + PyDoc_STRVAR(mmap_mmap_seekable__doc__, "seekable($self, /)\n" "--\n" @@ -796,4 +832,4 @@ mmap_mmap_madvise(PyObject *self, PyObject *const *args, Py_ssize_t nargs) #ifndef MMAP_MMAP_MADVISE_METHODDEF #define MMAP_MMAP_MADVISE_METHODDEF #endif /* !defined(MMAP_MMAP_MADVISE_METHODDEF) */ -/*[clinic end generated code: output=381f6cf4986ac867 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fd9ca0ef425af934 input=a9049054013a1b77]*/ diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index ac8521f8aa9b6e..a730caf3cb747c 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1116,6 +1116,24 @@ mmap_mmap_seek_impl(mmap_object *self, Py_ssize_t dist, int how) return NULL; } +/*clinic*/ + +/*[clinic input] +mmap.mmap.set_name + + name: str + / + +[clinic start generated code]*/ + +static PyObject * +mmap_mmap_set_name_impl(mmap_object *self, const char *name) +/*[clinic end generated code: output=1edaf4fd51277760 input=6c7dd91cad205f07]*/ +{ + _PyAnnotateMemoryMap(self->data, self->size, name); + Py_RETURN_NONE; +} + /*[clinic input] mmap.mmap.seekable From d354eba5de2944b63f86830a2df5655b13d35bae Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 01:41:11 +0900 Subject: [PATCH 02/26] nit --- Modules/mmapmodule.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 0cd2560700ca2b..df761b2e162d7d 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1131,7 +1131,14 @@ static PyObject * mmap_mmap_set_name_impl(mmap_object *self, const char *name) /*[clinic end generated code: output=1edaf4fd51277760 input=6c7dd91cad205f07]*/ { - _PyAnnotateMemoryMap(self->data, self->size, name); + char buf[80] = {0, }; + const char* prefix = "cpython:mmap:"; + if (strlen(name) + strlen(prefix) > 80) { + PyErr_SetString(PyExc_ValueError, "name too long"); + return NULL; + } + sprintf(buf, "%s%s", prefix, name); + _PyAnnotateMemoryMap(self->data, self->size, buf); Py_RETURN_NONE; } @@ -1415,6 +1422,7 @@ static struct PyMethodDef mmap_object_methods[] = { MMAP_MMAP_RESIZE_METHODDEF MMAP_MMAP_SEEK_METHODDEF MMAP_MMAP_SEEKABLE_METHODDEF + MMAP_MMAP_SET_NAME_METHODDEF MMAP_MMAP_SIZE_METHODDEF MMAP_MMAP_TELL_METHODDEF MMAP_MMAP_WRITE_METHODDEF From 91d4aead1c73532dd0bad37c8b64e0b698f01e13 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 02:08:50 +0900 Subject: [PATCH 03/26] nit --- Modules/mmapmodule.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index df761b2e162d7d..db2c88c73659ae 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1131,14 +1131,29 @@ static PyObject * mmap_mmap_set_name_impl(mmap_object *self, const char *name) /*[clinic end generated code: output=1edaf4fd51277760 input=6c7dd91cad205f07]*/ { - char buf[80] = {0, }; const char* prefix = "cpython:mmap:"; if (strlen(name) + strlen(prefix) > 80) { - PyErr_SetString(PyExc_ValueError, "name too long"); + PyErr_SetString(PyExc_ValueError, "name is too long"); return NULL; } - sprintf(buf, "%s%s", prefix, name); - _PyAnnotateMemoryMap(self->data, self->size, buf); +#ifdef MAP_ANONYMOUS + if (self->flags & MAP_ANONYMOUS) { + char buf[80] = {0, }; + sprintf(buf, "%s%s", prefix, name); + _PyAnnotateMemoryMap(self->data, self->size, buf); + } + else { + /* cannot name non-anonymous mappings */ + PyErr_SetString(PyExc_ValueError, + "Cannot set annotation on non-anonymous mappings"); + return NULL; + } +#else + /* naming not supported on this platform */ + PyErr_SetString(PyExc_ValueError, + "Annotation of mmap is not supported on this platform"); + return NULL; +#endif Py_RETURN_NONE; } @@ -1978,7 +1993,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) PyErr_SetFromErrno(PyExc_OSError); return NULL; } - _PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:mmap"); +#ifdef MAP_ANONYMOUS + if (m_obj->flags & MAP_ANONYMOUS) { + _PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:mmap"); + } +#endif m_obj->access = (access_mode)access; return (PyObject *)m_obj; } From 59198517a08eb7a40cfdfab4be222ad73af719d0 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 02:23:43 +0900 Subject: [PATCH 04/26] Add test --- Lib/test/test_mmap.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 368af0cf89c300..91883e4ae35fd7 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1165,6 +1165,35 @@ def test_flush_parameters(self): m.flush(PAGESIZE) m.flush(PAGESIZE, PAGESIZE) + @unittest.skipUnless(hasattr(mmap, 'MAP_ANONYMOUS'), 'requires MAP_ANONYMOUS') + def test_set_name(self): + # Test setting name on anonymous mmap + m = mmap.mmap(-1, PAGESIZE) + self.addCleanup(m.close) + result = m.set_name('test_mapping') + self.assertIsNone(result) + + # Test name length limit (80 chars including prefix "cpython:mmap:") + # Prefix is 13 chars, so max name is 67 chars + long_name = 'x' * 30 + result = m.set_name(long_name) + self.assertIsNone(result) + + # Test name too long + too_long_name = 'x' * 80 + with self.assertRaises(ValueError): + m.set_name(too_long_name) + + # Test that file-backed mmap raises error + with open(TESTFN, 'wb+') as f: + f.write(b'x' * PAGESIZE) + f.flush() + m2 = mmap.mmap(f.fileno(), PAGESIZE) + self.addCleanup(m2.close) + + with self.assertRaises(ValueError): + m2.set_name('should_fail') + class LargeMmapTests(unittest.TestCase): From 6c13efc31fce97443992bdb6774a10c933d64a10 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 02:29:24 +0900 Subject: [PATCH 05/26] Update Docs --- Doc/library/mmap.rst | 10 ++++++++++ Doc/whatsnew/3.15.rst | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index f32aa322c40dbb..ee8598cc8227c5 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -328,6 +328,16 @@ To map anonymous memory, -1 should be passed as the fileno along with the length .. versionadded:: 3.13 + .. method:: set_name(name) + + Annotate the memory map with the given *name* for easier identification + in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed + to the Python or Python is built in :ref:`debug mode ` + + Availability: Linux kernel 5.17 or newer. + + .. versionadded:: 3.15 + .. method:: size() Return the length of the file, which can be larger than the size of the diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 853c47d4402f20..5173933f3c58c2 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -581,6 +581,10 @@ mmap not be duplicated. (Contributed by Serhiy Storchaka in :gh:`78502`.) +* :meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map + if Linux kernel supports :manpage:`PR_SET_VMA_ANON_NAME ` (Linux 5.17 or newer). + (Contributed by Donghee Na in :gh:`142419`.) + os -- From 5586acdc3f8fe0ca57db45e777f52c25fa4d995f Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 02:31:46 +0900 Subject: [PATCH 06/26] Update --- .../Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst diff --git a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst new file mode 100644 index 00000000000000..8ea0b3f6cf7155 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst @@ -0,0 +1,3 @@ +meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map +if Linux kernel supports `PR_SET_VMA_ANON_NAME` (Linux 5.17 or newer). Patch +by Donghee Na From 73ac4b9fb97eb88401652f8e9d4fb4c02540c3f6 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 02:34:41 +0900 Subject: [PATCH 07/26] nit --- Doc/library/mmap.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index ee8598cc8227c5..ac1d6f3fc69e2d 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -329,7 +329,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length .. versionadded:: 3.13 .. method:: set_name(name) - + Annotate the memory map with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed to the Python or Python is built in :ref:`debug mode ` From a931201ae46348e079cfe47543b7290c7b1a431c Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 02:35:22 +0900 Subject: [PATCH 08/26] nit --- .../Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst index 8ea0b3f6cf7155..747534e8070656 100644 --- a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst +++ b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst @@ -1,3 +1,3 @@ meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map -if Linux kernel supports `PR_SET_VMA_ANON_NAME` (Linux 5.17 or newer). Patch -by Donghee Na +if Linux kernel supports `PR_SET_VMA_ANON_NAME` (Linux 5.17 or newer). +Patch by Donghee Na. From 831bd7edabcfb7b2d541a5bf7525bc3e7faef44e Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 09:04:02 +0900 Subject: [PATCH 09/26] lint --- .../next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst index 747534e8070656..bff7705f8efa6c 100644 --- a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst +++ b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst @@ -1,3 +1,3 @@ -meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map +:meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map if Linux kernel supports `PR_SET_VMA_ANON_NAME` (Linux 5.17 or newer). Patch by Donghee Na. From c7185f09cf87db9a630170e7de07aebaf82346ce Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 10 Dec 2025 09:07:09 +0900 Subject: [PATCH 10/26] nit --- .../next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst index bff7705f8efa6c..2bcba4e55ba9a6 100644 --- a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst +++ b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst @@ -1,3 +1,3 @@ :meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map -if Linux kernel supports `PR_SET_VMA_ANON_NAME` (Linux 5.17 or newer). +if Linux kernel supports ``PR_SET_VMA_ANON_NAME`` (Linux 5.17 or newer). Patch by Donghee Na. From 3a2654dff9c9713ec6274aa0f18f680b07510a4f Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Dec 2025 02:41:47 +0900 Subject: [PATCH 11/26] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/mmap.rst | 4 ++-- Doc/whatsnew/3.15.rst | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index ac1d6f3fc69e2d..1aa5392e0c5e0c 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -334,9 +334,9 @@ To map anonymous memory, -1 should be passed as the fileno along with the length in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed to the Python or Python is built in :ref:`debug mode ` - Availability: Linux kernel 5.17 or newer. + .. availability:: Linux >= 5.17 - .. versionadded:: 3.15 + .. versionadded:: next .. method:: size() diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 5173933f3c58c2..a1f8411b6f778c 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -581,7 +581,8 @@ mmap not be duplicated. (Contributed by Serhiy Storchaka in :gh:`78502`.) -* :meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map +* Added the :meth:`mmap.mmap.set_name` method + to annotate an anonymous memory map if Linux kernel supports :manpage:`PR_SET_VMA_ANON_NAME ` (Linux 5.17 or newer). (Contributed by Donghee Na in :gh:`142419`.) From cc3cf8aefc4b4017ab21377ccf34887170cc83e1 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Dec 2025 02:42:14 +0900 Subject: [PATCH 12/26] Update Doc/library/mmap.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/mmap.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 1aa5392e0c5e0c..316aa91e55d90c 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -328,7 +328,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length .. versionadded:: 3.13 - .. method:: set_name(name) + .. method:: set_name(name, /) Annotate the memory map with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed From bd02cc584f9aa7c3606c82d8ce09f30d77caabd0 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Dec 2025 02:51:31 +0900 Subject: [PATCH 13/26] Address code review --- Lib/test/test_mmap.py | 2 +- Modules/mmapmodule.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 91883e4ae35fd7..a99f0e5ed3b985 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1165,7 +1165,7 @@ def test_flush_parameters(self): m.flush(PAGESIZE) m.flush(PAGESIZE, PAGESIZE) - @unittest.skipUnless(hasattr(mmap, 'MAP_ANONYMOUS'), 'requires MAP_ANONYMOUS') + @unittest.skipUnless(hasattr(mmap, 'MAP_ANONYMOUS') and sys.platform == "linux", 'requires MAP_ANONYMOUS and should be linux') def test_set_name(self): # Test setting name on anonymous mmap m = mmap.mmap(-1, PAGESIZE) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index db2c88c73659ae..a8739a4266f7ba 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1131,14 +1131,14 @@ static PyObject * mmap_mmap_set_name_impl(mmap_object *self, const char *name) /*[clinic end generated code: output=1edaf4fd51277760 input=6c7dd91cad205f07]*/ { - const char* prefix = "cpython:mmap:"; +#if defined(MAP_ANONYMOUS) && defined(__linux__) + const char *prefix = "cpython:mmap:"; if (strlen(name) + strlen(prefix) > 80) { PyErr_SetString(PyExc_ValueError, "name is too long"); return NULL; } -#ifdef MAP_ANONYMOUS if (self->flags & MAP_ANONYMOUS) { - char buf[80] = {0, }; + char buf[81] = {0, }; sprintf(buf, "%s%s", prefix, name); _PyAnnotateMemoryMap(self->data, self->size, buf); } @@ -1150,7 +1150,7 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name) } #else /* naming not supported on this platform */ - PyErr_SetString(PyExc_ValueError, + PyErr_SetString(NotImplementedError, "Annotation of mmap is not supported on this platform"); return NULL; #endif From fa52b0e768c33659186ec11413628af920d5cea7 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Dec 2025 02:54:28 +0900 Subject: [PATCH 14/26] Addrss code review --- Doc/library/mmap.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 316aa91e55d90c..3a6ac8d120eee8 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -333,6 +333,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Annotate the memory map with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed to the Python or Python is built in :ref:`debug mode ` + The length of *name* must not exceed 67 bytes. .. availability:: Linux >= 5.17 From 73bf76228b75e980257d63cc5d635c79049a09d8 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Dec 2025 10:23:38 +0900 Subject: [PATCH 15/26] fix --- Modules/mmapmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index a8739a4266f7ba..d2b252de88c05c 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1150,7 +1150,7 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name) } #else /* naming not supported on this platform */ - PyErr_SetString(NotImplementedError, + PyErr_SetString(PyExc_NotImplementedError, "Annotation of mmap is not supported on this platform"); return NULL; #endif From 94d02682ee3215e593d7c89848ec96aae229845f Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Dec 2025 10:26:01 +0900 Subject: [PATCH 16/26] nit --- Lib/test/test_mmap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index a99f0e5ed3b985..0085359a9ec9d1 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1165,7 +1165,7 @@ def test_flush_parameters(self): m.flush(PAGESIZE) m.flush(PAGESIZE, PAGESIZE) - @unittest.skipUnless(hasattr(mmap, 'MAP_ANONYMOUS') and sys.platform == "linux", 'requires MAP_ANONYMOUS and should be linux') + @support.requires_linux_version(5, 17, 0) def test_set_name(self): # Test setting name on anonymous mmap m = mmap.mmap(-1, PAGESIZE) From a9df12f213a85ec26b10afb4e08bcce91e7c1404 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 12 Dec 2025 04:08:01 +0900 Subject: [PATCH 17/26] Apply suggestions from code review Co-authored-by: Victor Stinner --- Doc/library/mmap.rst | 2 +- .../next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 3a6ac8d120eee8..f44698f109dc87 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -332,7 +332,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Annotate the memory map with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed - to the Python or Python is built in :ref:`debug mode ` + to Python or if Python is built in :ref:`debug mode ` The length of *name* must not exceed 67 bytes. .. availability:: Linux >= 5.17 diff --git a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst index 2bcba4e55ba9a6..63955923cd157c 100644 --- a/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst +++ b/Misc/NEWS.d/next/Library/2025-12-10-02-31-43.gh-issue-142419.C8_LES.rst @@ -1,3 +1,3 @@ -:meth:`mmap.mmap.set_name` method added to annotation an anonymous memory map +:meth:`mmap.mmap.set_name` method added to annotate an anonymous memory map if Linux kernel supports ``PR_SET_VMA_ANON_NAME`` (Linux 5.17 or newer). Patch by Donghee Na. From 2d6d632095cde586b56abd6ab46c2841233f3d2e Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 12 Dec 2025 04:15:21 +0900 Subject: [PATCH 18/26] Address code review --- Modules/mmapmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index d2b252de88c05c..1718e33c871fcf 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1141,6 +1141,7 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name) char buf[81] = {0, }; sprintf(buf, "%s%s", prefix, name); _PyAnnotateMemoryMap(self->data, self->size, buf); + Py_RETURN_NONE; } else { /* cannot name non-anonymous mappings */ @@ -1154,7 +1155,6 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name) "Annotation of mmap is not supported on this platform"); return NULL; #endif - Py_RETURN_NONE; } /*[clinic input] From ca4bcec4672654990a68bbb0ad4ff5797fd3ecc7 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 12 Dec 2025 04:29:27 +0900 Subject: [PATCH 19/26] Fix test --- Lib/test/test_mmap.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 0085359a9ec9d1..df2a74626c4ce1 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1165,6 +1165,7 @@ def test_flush_parameters(self): m.flush(PAGESIZE) m.flush(PAGESIZE, PAGESIZE) + @unittest.skipUnless(sys.platform == 'linux', 'Linux only') @support.requires_linux_version(5, 17, 0) def test_set_name(self): # Test setting name on anonymous mmap From 987cf09a1ff6ccad34c22e104a594e033b2e6793 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 13 Dec 2025 18:44:56 +0900 Subject: [PATCH 20/26] Apply suggestions from code review Co-authored-by: Victor Stinner --- Doc/library/mmap.rst | 4 ++-- Doc/whatsnew/3.15.rst | 2 +- Lib/test/test_mmap.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index f44698f109dc87..7fe39e96563768 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -330,12 +330,12 @@ To map anonymous memory, -1 should be passed as the fileno along with the length .. method:: set_name(name, /) - Annotate the memory map with the given *name* for easier identification + Annotate the memory mapping with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed to Python or if Python is built in :ref:`debug mode ` The length of *name* must not exceed 67 bytes. - .. availability:: Linux >= 5.17 + .. availability:: Linux >= 5.17 (kernel built with `CONFIG_ANON_VMA_NAME` option) .. versionadded:: next diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index a1f8411b6f778c..b879abdb664e50 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -582,7 +582,7 @@ mmap (Contributed by Serhiy Storchaka in :gh:`78502`.) * Added the :meth:`mmap.mmap.set_name` method - to annotate an anonymous memory map + to annotate an anonymous memory mapping if Linux kernel supports :manpage:`PR_SET_VMA_ANON_NAME ` (Linux 5.17 or newer). (Contributed by Donghee Na in :gh:`142419`.) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index df2a74626c4ce1..75b9a65075e7f3 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1176,12 +1176,12 @@ def test_set_name(self): # Test name length limit (80 chars including prefix "cpython:mmap:") # Prefix is 13 chars, so max name is 67 chars - long_name = 'x' * 30 + long_name = 'x' * 67 result = m.set_name(long_name) self.assertIsNone(result) # Test name too long - too_long_name = 'x' * 80 + too_long_name = 'x' * 68 with self.assertRaises(ValueError): m.set_name(too_long_name) From 2a5d9a4adf7c1c4f49d5e3c7562990920ee5bb6c Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 13 Dec 2025 18:52:14 +0900 Subject: [PATCH 21/26] Address code review --- Doc/library/mmap.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 7fe39e96563768..b5b8f1d6a5e246 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -335,7 +335,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length to Python or if Python is built in :ref:`debug mode ` The length of *name* must not exceed 67 bytes. - .. availability:: Linux >= 5.17 (kernel built with `CONFIG_ANON_VMA_NAME` option) + .. availability:: Linux >= 5.17 (kernel built with ``CONFIG_ANON_VMA_NAME`` option) .. versionadded:: next From 9adcf8e49a0fc0236fa59f43034ff1c04d595ade Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 13 Dec 2025 18:52:45 +0900 Subject: [PATCH 22/26] Update Modules/mmapmodule.c Co-authored-by: Victor Stinner --- Modules/mmapmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 1718e33c871fcf..483592a0471c2d 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1138,7 +1138,7 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name) return NULL; } if (self->flags & MAP_ANONYMOUS) { - char buf[81] = {0, }; + char buf[81]; sprintf(buf, "%s%s", prefix, name); _PyAnnotateMemoryMap(self->data, self->size, buf); Py_RETURN_NONE; From 522ef4909f3c585066cca5cc776ab21cb4e83a85 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sun, 14 Dec 2025 17:54:34 +0900 Subject: [PATCH 23/26] Fix --- Doc/library/mmap.rst | 2 +- Include/internal/pycore_mmap.h | 1 + Lib/test/test_mmap.py | 8 ++++---- Modules/mmapmodule.c | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index b5b8f1d6a5e246..4055421581e923 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -333,7 +333,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Annotate the memory mapping with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed to Python or if Python is built in :ref:`debug mode ` - The length of *name* must not exceed 67 bytes. + The length of *name* must not exceed 67 bytes including '\0' .. availability:: Linux >= 5.17 (kernel built with ``CONFIG_ANON_VMA_NAME`` option) diff --git a/Include/internal/pycore_mmap.h b/Include/internal/pycore_mmap.h index 214fd4362a55fe..d5864416781414 100644 --- a/Include/internal/pycore_mmap.h +++ b/Include/internal/pycore_mmap.h @@ -25,6 +25,7 @@ _PyAnnotateMemoryMap(void *addr, size_t size, const char *name) return; } #endif + // The name length cannot exceed 80 (including the '\0'). assert(strlen(name) < 80); int old_errno = errno; prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name); diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 75b9a65075e7f3..f48541426d56f7 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1174,14 +1174,14 @@ def test_set_name(self): result = m.set_name('test_mapping') self.assertIsNone(result) - # Test name length limit (80 chars including prefix "cpython:mmap:") - # Prefix is 13 chars, so max name is 67 chars - long_name = 'x' * 67 + # Test name length limit (80 chars including prefix "cpython:mmap:" and '\0') + # Prefix is 13 chars, so max name is 66 chars + long_name = 'x' * 66 result = m.set_name(long_name) self.assertIsNone(result) # Test name too long - too_long_name = 'x' * 68 + too_long_name = 'x' * 67 with self.assertRaises(ValueError): m.set_name(too_long_name) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 483592a0471c2d..d53483da8930d7 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1133,12 +1133,12 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name) { #if defined(MAP_ANONYMOUS) && defined(__linux__) const char *prefix = "cpython:mmap:"; - if (strlen(name) + strlen(prefix) > 80) { + if (strlen(name) + strlen(prefix) > 79) { PyErr_SetString(PyExc_ValueError, "name is too long"); return NULL; } if (self->flags & MAP_ANONYMOUS) { - char buf[81]; + char buf[80]; sprintf(buf, "%s%s", prefix, name); _PyAnnotateMemoryMap(self->data, self->size, buf); Py_RETURN_NONE; From 13138d9d321168d45292ed9e0f91567945275ff2 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 16 Dec 2025 01:33:54 +0900 Subject: [PATCH 24/26] Apply suggestions from code review Co-authored-by: Victor Stinner --- Doc/library/mmap.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 4055421581e923..9475c4ab133606 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -332,8 +332,8 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Annotate the memory mapping with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed - to Python or if Python is built in :ref:`debug mode ` - The length of *name* must not exceed 67 bytes including '\0' + to Python or if Python is built in :ref:`debug mode `. + The length of *name* must not exceed 67 bytes including '\0'. .. availability:: Linux >= 5.17 (kernel built with ``CONFIG_ANON_VMA_NAME`` option) From 2ece41b7aff0c9ecc34c3e6535d876f1622dc641 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 16 Dec 2025 02:32:03 +0900 Subject: [PATCH 25/26] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/mmap.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 9475c4ab133606..41b90f2c3b3111 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -333,7 +333,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Annotate the memory mapping with the given *name* for easier identification in ``/proc//maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed to Python or if Python is built in :ref:`debug mode `. - The length of *name* must not exceed 67 bytes including '\0'. + The length of *name* must not exceed 67 bytes including the ``'\0'`` terminator. .. availability:: Linux >= 5.17 (kernel built with ``CONFIG_ANON_VMA_NAME`` option) From cf96361107145922f9dc9b9a537a434f0b0166eb Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 16 Dec 2025 02:35:29 +0900 Subject: [PATCH 26/26] Update --- Modules/mmapmodule.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index d53483da8930d7..92f610bc9c9974 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1117,8 +1117,6 @@ mmap_mmap_seek_impl(mmap_object *self, Py_ssize_t dist, int how) return NULL; } -/*clinic*/ - /*[clinic input] mmap.mmap.set_name