Skip to content

gh-76595: PyCapsule_Import() now imports submodules if needed. #6898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Doc/c-api/capsule.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 18 additions & 29 deletions Objects/capsule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect error handling here, if PyObject_GetAttrString() fails for whatever reason. It seems like PyImport_ImportModule() is tried on the substring on error.

}
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;
}

Expand Down