diff options
-rw-r--r-- | source4/lib/ldb/pyldb_util.c | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/source4/lib/ldb/pyldb_util.c b/source4/lib/ldb/pyldb_util.c index 170c291698..3e015d0939 100644 --- a/source4/lib/ldb/pyldb_util.c +++ b/source4/lib/ldb/pyldb_util.c @@ -41,6 +41,26 @@ typedef intargfunc ssizeargfunc; #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. * @@ -53,7 +73,7 @@ bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *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)); @@ -61,17 +81,12 @@ bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *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; } @@ -89,15 +104,10 @@ PyObject *PyLdbDn_FromDn(struct ldb_dn *dn) 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) { |