-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-111784: Add PyCapsule_ImportCapsule
and fix segfault in _elementtree.c
#112053
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,57 @@ _PyCapsule_SetTraverse(PyObject *op, traverseproc traverse_func, inquiry clear_f | |
} | ||
|
||
|
||
PyObject * | ||
PyCapsule_ImportCapsule(const char *name, int no_block) | ||
{ | ||
PyObject *object = NULL; | ||
char *trace; | ||
size_t name_length = (strlen(name) + 1) * sizeof(char); | ||
char *name_dup = (char *)PyMem_Malloc(name_length); | ||
|
||
if (!name_dup) { | ||
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) { | ||
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_SETREF(object, object2); | ||
} | ||
if (!object) { | ||
goto EXIT; | ||
} | ||
|
||
trace = dot; | ||
} | ||
|
||
/* compare attribute name to module.name by hand */ | ||
if (!PyCapsule_IsValid(object, name)) { | ||
PyErr_Format(PyExc_AttributeError, | ||
"PyCapsule_Import \"%s\" is not valid", | ||
name); | ||
} | ||
EXIT: | ||
if (name_dup) { | ||
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.
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. It's not a final version 😅 |
||
PyMem_Free(name_dup); | ||
} | ||
return object; | ||
} | ||
|
||
|
||
void * | ||
PyCapsule_Import(const char *name, int no_block) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need here
Py_XDECREF(object)
as in thePyCapsule_Import
, because we do it later manually. So, we can control a process of capsule's deallocation