]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-85283: Build posixshmem extension with Limited C API (#111087)
authorVictor Stinner <vstinner@python.org>
Thu, 19 Oct 2023 22:23:49 +0000 (00:23 +0200)
committerGitHub <noreply@github.com>
Thu, 19 Oct 2023 22:23:49 +0000 (00:23 +0200)
Build the _multiprocessing.posixshmem extension with the Limited C
API.

* Add <errno.h> include.
* Replace PyUnicode_AsUTF8() with PyUnicode_AsUTF8AndSize().

Doc/whatsnew/3.13.rst
Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst
Modules/_multiprocessing/clinic/posixshmem.c.h
Modules/_multiprocessing/posixshmem.c

index 358bd1747cd2c5483df444ce081144dad168ed2e..804c7a88d947119af09d0ac5e8671d51b505c597 100644 (file)
@@ -938,8 +938,9 @@ Build Changes
   library, GCC built-in atomic functions, or MSVC interlocked intrinsics.
 
 * The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``,
-  ``_scproxy``, ``_stat``, ``_testimportmultiple`` and ``_uuid`` C extensions
-  are now built with the :ref:`limited C API <limited-c-api>`.
+  ``_multiprocessing.posixshmem``, ``_scproxy``, ``_stat``,
+  ``_testimportmultiple`` and ``_uuid`` C extensions are now built with the
+  :ref:`limited C API <limited-c-api>`.
   (Contributed by Victor Stinner in :gh:`85283`.)
 
 
index 49b5da32f8f72e292400311a277bb1f5dcb139e8..399054040f2ec75241f310cd8a715b5caea9017b 100644 (file)
@@ -1,4 +1,5 @@
 The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``,
-``_scproxy``, ``_stat``, ``_testimportmultiple`` and ``_uuid`` C extensions are
-now built with the :ref:`limited C API <limited-c-api>`.
+``_multiprocessing.posixshmem``, ``_scproxy``, ``_stat``,
+``_testimportmultiple`` and ``_uuid`` C extensions are now built with the
+:ref:`limited C API <limited-c-api>`.
 Patch by Victor Stinner.
index 6b6a9b11c0d990c53a36329e37d965bd5fa2a98d..1b894ea4c67adc80ef40ce5ea87aa7597ddd0795 100644 (file)
@@ -2,12 +2,6 @@
 preserve
 [clinic start generated code]*/
 
-#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
-#  include "pycore_gc.h"          // PyGC_Head
-#  include "pycore_runtime.h"     // _Py_ID()
-#endif
-#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
-
 #if defined(HAVE_SHM_OPEN)
 
 PyDoc_STRVAR(_posixshmem_shm_open__doc__,
@@ -17,69 +11,25 @@ PyDoc_STRVAR(_posixshmem_shm_open__doc__,
 "Open a shared memory object.  Returns a file descriptor (integer).");
 
 #define _POSIXSHMEM_SHM_OPEN_METHODDEF    \
-    {"shm_open", _PyCFunction_CAST(_posixshmem_shm_open), METH_FASTCALL|METH_KEYWORDS, _posixshmem_shm_open__doc__},
+    {"shm_open", (PyCFunction)(void(*)(void))_posixshmem_shm_open, METH_VARARGS|METH_KEYWORDS, _posixshmem_shm_open__doc__},
 
 static int
 _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
                           int mode);
 
 static PyObject *
-_posixshmem_shm_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+_posixshmem_shm_open(PyObject *module, PyObject *args, PyObject *kwargs)
 {
     PyObject *return_value = NULL;
-    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
-
-    #define NUM_KEYWORDS 3
-    static struct {
-        PyGC_Head _this_is_not_used;
-        PyObject_VAR_HEAD
-        PyObject *ob_item[NUM_KEYWORDS];
-    } _kwtuple = {
-        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
-        .ob_item = { &_Py_ID(path), &_Py_ID(flags), &_Py_ID(mode), },
-    };
-    #undef NUM_KEYWORDS
-    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
-
-    #else  // !Py_BUILD_CORE
-    #  define KWTUPLE NULL
-    #endif  // !Py_BUILD_CORE
-
-    static const char * const _keywords[] = {"path", "flags", "mode", NULL};
-    static _PyArg_Parser _parser = {
-        .keywords = _keywords,
-        .fname = "shm_open",
-        .kwtuple = KWTUPLE,
-    };
-    #undef KWTUPLE
-    PyObject *argsbuf[3];
-    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
+    static char *_keywords[] = {"path", "flags", "mode", NULL};
     PyObject *path;
     int flags;
     int mode = 511;
     int _return_value;
 
-    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
-    if (!args) {
-        goto exit;
-    }
-    if (!PyUnicode_Check(args[0])) {
-        _PyArg_BadArgument("shm_open", "argument 'path'", "str", args[0]);
-        goto exit;
-    }
-    path = args[0];
-    flags = PyLong_AsInt(args[1]);
-    if (flags == -1 && PyErr_Occurred()) {
-        goto exit;
-    }
-    if (!noptargs) {
-        goto skip_optional_pos;
-    }
-    mode = PyLong_AsInt(args[2]);
-    if (mode == -1 && PyErr_Occurred()) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ui|i:shm_open", _keywords,
+        &path, &flags, &mode))
         goto exit;
-    }
-skip_optional_pos:
     _return_value = _posixshmem_shm_open_impl(module, path, flags, mode);
     if ((_return_value == -1) && PyErr_Occurred()) {
         goto exit;
@@ -105,52 +55,21 @@ PyDoc_STRVAR(_posixshmem_shm_unlink__doc__,
 "region.");
 
 #define _POSIXSHMEM_SHM_UNLINK_METHODDEF    \
-    {"shm_unlink", _PyCFunction_CAST(_posixshmem_shm_unlink), METH_FASTCALL|METH_KEYWORDS, _posixshmem_shm_unlink__doc__},
+    {"shm_unlink", (PyCFunction)(void(*)(void))_posixshmem_shm_unlink, METH_VARARGS|METH_KEYWORDS, _posixshmem_shm_unlink__doc__},
 
 static PyObject *
 _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path);
 
 static PyObject *
-_posixshmem_shm_unlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+_posixshmem_shm_unlink(PyObject *module, PyObject *args, PyObject *kwargs)
 {
     PyObject *return_value = NULL;
-    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
-
-    #define NUM_KEYWORDS 1
-    static struct {
-        PyGC_Head _this_is_not_used;
-        PyObject_VAR_HEAD
-        PyObject *ob_item[NUM_KEYWORDS];
-    } _kwtuple = {
-        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
-        .ob_item = { &_Py_ID(path), },
-    };
-    #undef NUM_KEYWORDS
-    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
-
-    #else  // !Py_BUILD_CORE
-    #  define KWTUPLE NULL
-    #endif  // !Py_BUILD_CORE
-
-    static const char * const _keywords[] = {"path", NULL};
-    static _PyArg_Parser _parser = {
-        .keywords = _keywords,
-        .fname = "shm_unlink",
-        .kwtuple = KWTUPLE,
-    };
-    #undef KWTUPLE
-    PyObject *argsbuf[1];
+    static char *_keywords[] = {"path", NULL};
     PyObject *path;
 
-    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
-    if (!args) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U:shm_unlink", _keywords,
+        &path))
         goto exit;
-    }
-    if (!PyUnicode_Check(args[0])) {
-        _PyArg_BadArgument("shm_unlink", "argument 'path'", "str", args[0]);
-        goto exit;
-    }
-    path = args[0];
     return_value = _posixshmem_shm_unlink_impl(module, path);
 
 exit:
@@ -166,4 +85,4 @@ exit:
 #ifndef _POSIXSHMEM_SHM_UNLINK_METHODDEF
     #define _POSIXSHMEM_SHM_UNLINK_METHODDEF
 #endif /* !defined(_POSIXSHMEM_SHM_UNLINK_METHODDEF) */
-/*[clinic end generated code: output=b525631f5915d7f3 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=be0661dbed83ea23 input=a9049054013a1b77]*/
index f4da9f7ceba69836490a8ce47dc3c6d04c5ead00..317381a47ee65f367adc1d8066ebcb6af9189074 100644 (file)
@@ -2,18 +2,17 @@
 posixshmem - A Python extension that provides shm_open() and shm_unlink()
 */
 
-// clinic/posixshmem.c.h uses internal pycore_modsupport.h API
-#ifndef Py_BUILD_CORE_BUILTIN
-#  define Py_BUILD_CORE_MODULE 1
-#endif
+// Need limited C API version 3.13 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
+#define Py_LIMITED_API 0x030d0000
 
 #include <Python.h>
 
-// for shm_open() and shm_unlink()
+#include <errno.h>                // EINTR
 #ifdef HAVE_SYS_MMAN_H
-#include <sys/mman.h>
+#  include <sys/mman.h>           // shm_open(), shm_unlink()
 #endif
 
+
 /*[clinic input]
 module _posixshmem
 [clinic start generated code]*/
@@ -45,7 +44,7 @@ _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
 {
     int fd;
     int async_err = 0;
-    const char *name = PyUnicode_AsUTF8(path);
+    const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
     if (name == NULL) {
         return -1;
     }
@@ -84,7 +83,7 @@ _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
 {
     int rv;
     int async_err = 0;
-    const char *name = PyUnicode_AsUTF8(path);
+    const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
     if (name == NULL) {
         return NULL;
     }