Skip to content

Commit 4350c20

Browse files
committed
use PyExpat_CheckCompatibility in _elementtree.c
1 parent 734a09d commit 4350c20

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

Modules/_elementtree.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ typedef struct {
9898
PyTypeObject *TreeBuilder_Type;
9999
PyTypeObject *XMLParser_Type;
100100

101-
PyObject *expat_capsule;
102101
struct PyExpat_CAPI *expat_capi;
103102
} elementtreestate;
104103

@@ -156,7 +155,6 @@ elementtree_clear(PyObject *m)
156155
Py_CLEAR(st->ElementIter_Type);
157156
Py_CLEAR(st->TreeBuilder_Type);
158157
Py_CLEAR(st->XMLParser_Type);
159-
Py_CLEAR(st->expat_capsule);
160158

161159
st->expat_capi = NULL;
162160
return 0;
@@ -177,7 +175,7 @@ elementtree_traverse(PyObject *m, visitproc visit, void *arg)
177175
Py_VISIT(st->ElementIter_Type);
178176
Py_VISIT(st->TreeBuilder_Type);
179177
Py_VISIT(st->XMLParser_Type);
180-
Py_VISIT(st->expat_capsule);
178+
181179
return 0;
182180
}
183181

@@ -4430,28 +4428,18 @@ module_exec(PyObject *m)
44304428
if (st->deepcopy_obj == NULL) {
44314429
goto error;
44324430
}
4433-
4434-
assert(!PyErr_Occurred());
4435-
if (!(st->elementpath_obj = PyImport_ImportModule("xml.etree.ElementPath")))
4431+
st->elementpath_obj = PyImport_ImportModule("xml.etree.ElementPath");
4432+
if (st->elementpath_obj == NULL) {
44364433
goto error;
4434+
}
44374435

44384436
/* link against pyexpat */
4439-
if (!(st->expat_capsule = PyImport_ImportModuleAttrString("pyexpat", "expat_CAPI")))
4440-
goto error;
4441-
if (!(st->expat_capi = PyCapsule_GetPointer(st->expat_capsule, PyExpat_CAPSULE_NAME)))
4437+
st->expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0);
4438+
if (st->expat_capi == NULL) {
44424439
goto error;
4443-
if (st->expat_capi) {
4444-
/* check that it's usable */
4445-
if (strcmp(st->expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 ||
4446-
(size_t)st->expat_capi->size < sizeof(struct PyExpat_CAPI) ||
4447-
st->expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION ||
4448-
st->expat_capi->MINOR_VERSION != XML_MINOR_VERSION ||
4449-
st->expat_capi->MICRO_VERSION != XML_MICRO_VERSION) {
4450-
PyErr_SetString(PyExc_ImportError,
4451-
"pyexpat version is incompatible");
4452-
goto error;
4453-
}
4454-
} else {
4440+
}
4441+
if (!PyExpat_CheckCompatibility(st->expat_capi)) {
4442+
PyErr_SetString(PyExc_ImportError, "pyexpat version is incompatible");
44554443
goto error;
44564444
}
44574445

0 commit comments

Comments
 (0)