|
|
@@ -17,27 +17,33 @@
|
|
|
#endif
|
|
|
|
|
|
static void _register_collection(PyTypeObject *type, const char *abc) {
|
|
|
- PyObject *sys_modules = PyImport_GetModuleDict();
|
|
|
- if (sys_modules != nullptr) {
|
|
|
- PyObject *module = PyDict_GetItemString(sys_modules, _COLLECTIONS_ABC);
|
|
|
- if (module != nullptr) {
|
|
|
- PyObject *dict = PyModule_GetDict(module);
|
|
|
- if (module != nullptr) {
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
- PyObject *register_str = PyUnicode_InternFromString("register");
|
|
|
+ PyObject *module_name = PyUnicode_InternFromString(_COLLECTIONS_ABC);
|
|
|
#else
|
|
|
- PyObject *register_str = PyString_InternFromString("register");
|
|
|
+ PyObject *module_name = PyString_InternFromString(_COLLECTIONS_ABC);
|
|
|
#endif
|
|
|
- PyObject *obj = nullptr;
|
|
|
- if (register_str == nullptr ||
|
|
|
- PyDict_GetItemStringRef(dict, abc, &obj) <= 0 ||
|
|
|
- PyObject_CallMethodOneArg(obj, register_str, (PyObject *)type) == nullptr) {
|
|
|
- PyErr_Print();
|
|
|
- }
|
|
|
- Py_XDECREF(obj);
|
|
|
- Py_XDECREF(register_str);
|
|
|
+ PyObject *module = PyImport_GetModule(module_name);
|
|
|
+ Py_DECREF(module_name);
|
|
|
+ if (module != nullptr) {
|
|
|
+ PyObject *dict = PyModule_GetDict(module);
|
|
|
+ if (dict != nullptr) {
|
|
|
+#if PY_MAJOR_VERSION >= 3
|
|
|
+ PyObject *register_str = PyUnicode_InternFromString("register");
|
|
|
+#else
|
|
|
+ PyObject *register_str = PyString_InternFromString("register");
|
|
|
+#endif
|
|
|
+ PyObject *obj = nullptr;
|
|
|
+ if (register_str == nullptr ||
|
|
|
+ PyDict_GetItemStringRef(dict, abc, &obj) <= 0 ||
|
|
|
+ PyObject_CallMethodOneArg(obj, register_str, (PyObject *)type) == nullptr) {
|
|
|
+ PyErr_Print();
|
|
|
}
|
|
|
+ Py_XDECREF(obj);
|
|
|
+ Py_XDECREF(register_str);
|
|
|
+ } else {
|
|
|
+ PyErr_Clear();
|
|
|
}
|
|
|
+ Py_DECREF(module);
|
|
|
}
|
|
|
}
|
|
|
|