Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
5007c04
a34afa4
78fcd90
f386b07
34af6e5
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -192,63 +192,52 @@ PyCapsule_SetContext(PyObject *o, void *context) | ||
void * | ||
PyCapsule_Import(const char *name, intPy_UNUSED(no_block)) | ||
{ | ||
PyObject *object = NULL; | ||
void *return_value = NULL; | ||
char *trace; | ||
char *name_dup = _PyMem_Strdup(name); | ||
if (!name_dup) { | ||
return NULL; | ||
} | ||
trace = name_dup; | ||
while (1) { | ||
char *dot = strchr(trace, '.'); | ||
if (dot) { | ||
*dot = '\0'; | ||
} | ||
if (object) { | ||
Py_SETREF(object, PyObject_GetAttrString(object, trace)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
object = PyImport_ImportModule(name_dup); | ||
if (!object) { | ||
break; | ||
} | ||
} | ||
*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 if (object || trace == name_dup) { | ||
PyErr_Format(PyExc_AttributeError, | ||
"PyCapsule_Import \"%s\" is not valid", | ||
name); | ||
} | ||
Py_XDECREF(object); | ||
PyMem_Free(name_dup); | ||
return return_value; | ||
} | ||