Skip to content

Commit bd3c23d

Browse files
committed
Fix issues found by cpychecker.
1 parent 7b16f34 commit bd3c23d

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

bson/_cbsonmodule.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,24 +2163,8 @@ init_cbson(void)
21632163
PyObject *c_api_object;
21642164
static void *_cbson_API[_cbson_API_POINTER_COUNT];
21652165

2166-
#if PY_MAJOR_VERSION >= 3
2167-
m = PyModule_Create(&moduledef);
2168-
#else
2169-
m = Py_InitModule("_cbson", _CBSONMethods);
2170-
#endif
2171-
if (m == NULL) {
2172-
INITERROR;
2173-
}
2174-
21752166
PyDateTime_IMPORT;
21762167
if (PyDateTimeAPI == NULL) {
2177-
Py_DECREF(m);
2178-
INITERROR;
2179-
}
2180-
2181-
/* Import several python objects */
2182-
if (_reload_python_objects(m)) {
2183-
Py_DECREF(m);
21842168
INITERROR;
21852169
}
21862170

@@ -2196,10 +2180,36 @@ init_cbson(void)
21962180
#else
21972181
c_api_object = PyCObject_FromVoidPtr((void *) _cbson_API, NULL);
21982182
#endif
2183+
if (c_api_object == NULL)
2184+
INITERROR;
21992185

2200-
if (c_api_object != NULL) {
2201-
PyModule_AddObject(m, "_C_API", c_api_object);
2186+
#if PY_MAJOR_VERSION >= 3
2187+
m = PyModule_Create(&moduledef);
2188+
#else
2189+
m = Py_InitModule("_cbson", _CBSONMethods);
2190+
#endif
2191+
if (m == NULL) {
2192+
Py_DECREF(c_api_object);
2193+
INITERROR;
22022194
}
2195+
2196+
/* Import several python objects */
2197+
if (_reload_python_objects(m)) {
2198+
Py_DECREF(c_api_object);
2199+
#if PY_MAJOR_VERSION >= 3
2200+
Py_DECREF(m);
2201+
#endif
2202+
INITERROR;
2203+
}
2204+
2205+
if (PyModule_AddObject(m, "_C_API", c_api_object) < 0) {
2206+
Py_DECREF(c_api_object);
2207+
#if PY_MAJOR_VERSION >= 3
2208+
Py_DECREF(m);
2209+
#endif
2210+
INITERROR;
2211+
}
2212+
22032213
#if PY_MAJOR_VERSION >= 3
22042214
return m;
22052215
#endif

pymongo/_cmessagemodule.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,9 @@ init_cmessage(void)
578578
INITERROR;
579579
}
580580
#if PY_VERSION_HEX >= 0x03010000
581-
if (PyCapsule_CheckExact(c_api_object))
582-
_cbson_API = (void **)PyCapsule_GetPointer(c_api_object, "_cbson._C_API");
581+
_cbson_API = (void **)PyCapsule_GetPointer(c_api_object, "_cbson._C_API");
583582
#else
584-
if (PyCObject_Check(c_api_object))
585-
_cbson_API = (void **)PyCObject_AsVoidPtr(c_api_object);
583+
_cbson_API = (void **)PyCObject_AsVoidPtr(c_api_object);
586584
#endif
587585
if (_cbson_API == NULL) {
588586
Py_DECREF(c_api_object);

0 commit comments

Comments
 (0)