8000 bpo-42327: Add PyModule_Add(). by serhiy-storchaka · Pull Request #23240 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

bpo-42327: Add PyModule_Add(). #23240

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 12 commits into from
Prev Previous commit
Next Next commit
Fix possible leaks in _elementtree
  • Loading branch information
serhiy-storchaka committed Nov 15, 2020
commit ca9f69694e63a6aa2b4df6d69cf2c8f1a4bd1be6
36 changes: 19 additions & 17 deletions Modules/_elementtree.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4410,42 +4410,40 @@ PyInit__elementtree(void)
st = get_elementtree_state(m);

if (!(temp = PyImport_ImportModule("copy")))
return NULL;
goto error;
st->deepcopy_obj = PyObject_GetAttrString(temp, "deepcopy");
Py_XDECREF(temp);

if (st->deepcopy_obj == NULL) {
return NULL;
goto error;
}

assert(!PyErr_Occurred());
if (!(st->elementpath_obj = PyImport_ImportModule("xml.etree.ElementPath")))
return NULL;
goto error;

/* link against pyexpat */
expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0);
if (expat_capi) {
/* check that it's usable */
if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 ||
if (!expat_capi) {
goto error;
}
/* check that it's usable */
if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 ||
(size_t)expat_capi->size < sizeof(struct PyExpat_CAPI) ||
expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION ||
expat_capi->MINOR_VERSION != XML_MINOR_VERSION ||
expat_capi->MICRO_VERSION != XML_MICRO_VERSION) {
PyErr_SetString(PyExc_ImportError,
"pyexpat version is incompatible");
return NULL;
}
} else {
return NULL;
PyErr_SetString(PyExc_ImportError,
"pyexpat version is incompatible");
goto error;
}

st->parseerror_obj = PyErr_NewException(
"xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL
);
Py_INCREF(st->parseerror_obj);
if (PyModule_AddObject(m, "ParseError", st->parseerror_obj) < 0) {
Py_DECREF(st->parseerror_obj);
return NULL;
Py_XINCREF(st->parseerror_obj);
if (PyModule_Add(m, "ParseError", st->parseerror_obj) < 0) {
goto error;
}

PyTypeObject *types[] = {
Expand All @@ -4456,9 +4454,13 @@ PyInit__elementtree(void)

for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
if (PyModule_AddType(m, types[i]) < 0) {
return NULL;
goto error;
}
}

return m;

error:
Py_DECREF(m);
return NULL;
}
0