Skip to content

bpo-32414: use string after the last dot as the capsule name in PyCapsule_Import #4988

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

Closed
wants to merge 1 commit into from
Closed
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
78 changes: 25 additions & 53 deletions Objects/capsule.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,62 +194,35 @@ PyCapsule_SetContext(PyObject *o, void *context)
void *
PyCapsule_Import(const char *name, int 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);

if (!name_dup) {
return NULL;
PyObject *module = NULL, *capsule = NULL;
size_t name_len = strlen(name) + 1;
char *module_name = NULL, *capsule_name = NULL;
void *result = NULL;

module_name = PyMem_MALLOC(name_len);
if (module_name == NULL) {
return PyErr_NoMemory();
}

memcpy(name_dup, name, name_length);

trace = name_dup;
while (trace) {
char *dot = strchr(trace, '.');
if (dot) {
*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);
}
memcpy(module_name, name, name_len);
capsule_name = strrchr(module_name, '.');
if (capsule_name == NULL) {
PyErr_Format(PyExc_ImportError,
"PyCapsule_Import malformed name '%s'", name);
}
else {
*capsule_name++ = '\0';
module = PyImport_ImportModule(module_name);
if (module != NULL) {
capsule = PyObject_GetAttrString(module, capsule_name);
if (capsule != NULL) {
result = PyCapsule_GetPointer(capsule, name);
Py_DECREF(capsule);
}
} else {
PyObject *object2 = PyObject_GetAttrString(object, trace);
Py_DECREF(object);
object = object2;
}
if (!object) {
goto EXIT;
Py_DECREF(module);
}

trace = dot;
}

/* compare attribute name to module.name by hand */
if (PyCapsule_IsValid(object, name)) {
PyCapsule *capsule = (PyCapsule *)object;
return_value = capsule->pointer;
} else {
PyErr_Format(PyExc_AttributeError,
"PyCapsule_Import \"%s\" is not valid",
name);
}

EXIT:
Py_XDECREF(object);
if (name_dup) {
PyMem_FREE(name_dup);
}
return return_value;
PyMem_FREE(module_name);
return result;
}


Expand Down Expand Up @@ -321,4 +294,3 @@ PyTypeObject PyCapsule_Type = {
PyCapsule_Type__doc__ /*tp_doc*/
};