diff --git a/Doc/c-api/capsule.rst b/Doc/c-api/capsule.rst index 8eb6695e22de18..3ee9250db58905 100644 --- a/Doc/c-api/capsule.rst +++ b/Doc/c-api/capsule.rst @@ -102,13 +102,15 @@ Refer to :ref:`using-capsules` for more information on using these objects. Import a pointer to a C object from a capsule attribute in a module. The *name* parameter should specify the full name to the attribute, as in ``module.attribute``. The *name* stored in the capsule must match this - string exactly. If *no_block* is true, import the module without blocking - (using :c:func:`PyImport_ImportModuleNoBlock`). If *no_block* is false, - import the module conventionally (using :c:func:`PyImport_ImportModule`). + string exactly. Return the capsule's internal *pointer* on success. On failure, set an exception and return *NULL*. + .. versionchanged:: 3.8 + Supported importing submodules as in ``package.module.attribute``. + *no_block* is ignored. + .. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name) diff --git a/Misc/NEWS.d/next/C API/2018-05-16-13-47-18.bpo-32414.NODPbj.rst b/Misc/NEWS.d/next/C API/2018-05-16-13-47-18.bpo-32414.NODPbj.rst new file mode 100644 index 00000000000000..17f7a096ca8a38 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2018-05-16-13-47-18.bpo-32414.NODPbj.rst @@ -0,0 +1,3 @@ +:c:func:`PyCapsule_Import` now imports submodules if needed. Previously +names like ``package.module.attribute`` worked only if ``package.module`` +was already imported. diff --git a/Objects/capsule.c b/Objects/capsule.c index acd3de637dd52e..e29a0a6c88b42d 100644 --- a/Objects/capsule.c +++ b/Objects/capsule.c @@ -192,63 +192,52 @@ PyCapsule_SetContext(PyObject *o, void *context) void * -PyCapsule_Import(const char *name, int no_block) +PyCapsule_Import(const char *name, int Py_UNUSED(no_block)) { PyObject *object = NULL; void *return_value = NULL; char *trace; - size_t name_length = (strlen(name) + 1) * sizeof(char); - char *name_dup = (char *)PyMem_MALLOC(name_length); + char *name_dup = _PyMem_Strdup(name); if (!name_dup) { return NULL; } - memcpy(name_dup, name, name_length); - trace = name_dup; - while (trace) { + while (1) { char *dot = strchr(trace, '.'); if (dot) { - *dot++ = '\0'; + *dot = '\0'; } - - if (object == NULL) { - if (no_block) { - object = PyImport_ImportModuleNoBlock(trace); - } else { - object = PyImport_ImportModule(trace); - if (!object) { - PyErr_Format(PyExc_ImportError, "PyCapsule_Import could not import module \"%s\"", trace); - } - } - } else { - PyObject *object2 = PyObject_GetAttrString(object, trace); - Py_DECREF(object); - object = object2; + if (object) { + Py_SETREF(object, PyObject_GetAttrString(object, trace)); + } + if (!dot) { + break; } if (!object) { - goto EXIT; + object = PyImport_ImportModule(name_dup); + if (!object) { + break; + } } - - trace = dot; + *dot = '.'; + trace = dot + 1; } /* compare attribute name to module.name by hand */ if (PyCapsule_IsValid(object, name)) { PyCapsule *capsule = (PyCapsule *)object; return_value = capsule->pointer; - } else { + } + else if (object || trace == name_dup) { PyErr_Format(PyExc_AttributeError, "PyCapsule_Import \"%s\" is not valid", name); } -EXIT: Py_XDECREF(object); - if (name_dup) { - PyMem_FREE(name_dup); - } + PyMem_Free(name_dup); return return_value; }