1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-25 13:23:02 +04:00

fix: wrong reference counter after module initialization

This commit is contained in:
Vadim Levin
2022-01-27 12:04:56 +03:00
parent 16661847a7
commit ef85b24a78
2 changed files with 21 additions and 9 deletions
+9 -5
View File
@@ -2184,18 +2184,22 @@ static PyObject* createSubmodule(PyObject* parent_module, const std::string& nam
submodule_name.c_str());
if (!submodule)
{
/// Populates global modules dictionary and returns borrowed reference to it
submodule = PyImport_AddModule(full_submodule_name.c_str());
if (!submodule)
{
/// Return `PyImport_AddModule` NULL with an exception set on failure.
return NULL;
}
/// Populates parent module dictionary. Submodule lifetime should be managed
/// by the global modules dictionary and parent module dictionary, so Py_DECREF after
/// successfull call to the `PyDict_SetItemString` is redundant.
if (PyDict_SetItemString(parent_module_dict, submodule_name.c_str(), submodule) < 0) {
Py_CLEAR(submodule);
return PyErr_Format(PyExc_ImportError,
"Can't register a submodule '%s' (full name: '%s')",
submodule_name.c_str(), full_submodule_name.c_str()
);
}
/// PyDict_SetItemString doesn't steal a reference so the reference counter
/// of the submodule should be decremented to bind submodule lifetime to the
/// parent module
Py_DECREF(submodule);
}
submodule_name_start = submodule_name_end + 1;