#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
#endif
+
+/**
+ * Find out PyTypeObject in ldb module for a given typename
+ */
+static PyTypeObject * PyLdb_GetPyType(const char *typename)
+{
+ PyObject *py_obj = NULL;
+
+ if (ldb_module == NULL) {
+ ldb_module = PyImport_ImportModule("ldb");
+ if (ldb_module == NULL) {
+ return NULL;
+ }
+ }
+
+ py_obj = PyObject_GetAttrString(ldb_module, typename);
+
+ return (PyTypeObject*)py_obj;
+}
+
/**
* Obtain a ldb DN from a Python object.
*
struct ldb_context *ldb_ctx, struct ldb_dn **dn)
{
struct ldb_dn *odn;
- PyObject *PyLdb_Dn_Type;
+ PyTypeObject *PyLdb_Dn_Type;
if (ldb_ctx != NULL && PyString_Check(object)) {
odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
return true;
}
- if (ldb_module == NULL) {
- ldb_module = PyImport_ImportModule("ldb");
- if (ldb_module == NULL)
- return false;
- }
-
- PyLdb_Dn_Type = PyObject_GetAttrString(ldb_module, "Dn");
- if (PyLdb_Dn_Type == NULL)
+ PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
+ if (PyLdb_Dn_Type == NULL) {
return false;
+ }
- if (PyObject_TypeCheck(object, (PyTypeObject *) PyLdb_Dn_Type)) {
+ if (PyObject_TypeCheck(object, PyLdb_Dn_Type)) {
*dn = PyLdbDn_AsDn(object);
return true;
}
Py_RETURN_NONE;
}
- if (ldb_module == NULL) {
- ldb_module = PyImport_ImportModule("ldb");
- if (ldb_module == NULL)
- return NULL;
- }
-
- PyLdb_Dn_Type = (PyTypeObject *)PyObject_GetAttrString(ldb_module, "Dn");
- if (PyLdb_Dn_Type == NULL)
+ PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
+ if (PyLdb_Dn_Type == NULL) {
return NULL;
+ }
py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
if (py_ret == NULL) {