Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix segfault during deallocation of ``_elementtree.XMLParser`` by keeping pyexpat module alive for capsule lifetime. Patch by Kumar Aditya.
11 changes: 9 additions & 2 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ typedef struct {
PyTypeObject *TreeBuilder_Type;
PyTypeObject *XMLParser_Type;

/* Strong ref for capsule */
PyObject *expat_capsule;
struct PyExpat_CAPI *expat_capi;
} elementtreestate;

Expand Down Expand Up @@ -155,7 +157,7 @@ elementtree_clear(PyObject *m)
Py_CLEAR(st->ElementIter_Type);
Py_CLEAR(st->TreeBuilder_Type);
Py_CLEAR(st->XMLParser_Type);

Py_CLEAR(st->expat_capsule);
st->expat_capi = NULL;
return 0;
}
Expand All @@ -169,6 +171,7 @@ elementtree_traverse(PyObject *m, visitproc visit, void *arg)
Py_VISIT(st->elementpath_obj);
Py_VISIT(st->comment_factory);
Py_VISIT(st->pi_factory);
Py_VISIT(st->expat_capsule);

// Heap types
Py_VISIT(st->Element_Type);
Expand Down Expand Up @@ -4343,7 +4346,11 @@ module_exec(PyObject *m)
goto error;

/* link against pyexpat */
st->expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0);
st->expat_capsule = _PyImport_GetModuleAttrString("pyexpat", "expat_CAPI");
if (st->expat_capsule == NULL) {
goto error;
}
st->expat_capi = PyCapsule_GetPointer(st->expat_capsule, PyExpat_CAPSULE_NAME);
if (st->expat_capi) {
/* check that it's usable */
if (strcmp(st->expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 ||
Expand Down