diff options
author | Kamen Mazdrashki <kamenim@samba.org> | 2010-11-15 07:41:50 +0200 |
---|---|---|
committer | Kamen Mazdrashki <kamenim@samba.org> | 2010-11-18 22:28:07 +0100 |
commit | b4809e4a8240de5faa311bf55ade3d63fd376aa5 (patch) | |
tree | e39f305c513743e0d2adee64cd189a9eeac091ee | |
parent | 88e460190014880d6e4a4eae063a773d9ff5a09c (diff) | |
download | samba-b4809e4a8240de5faa311bf55ade3d63fd376aa5.tar.gz samba-b4809e4a8240de5faa311bf55ade3d63fd376aa5.tar.bz2 samba-b4809e4a8240de5faa311bf55ade3d63fd376aa5.zip |
s4-pyldb_util: Move ldb Type searching into separate function
-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) { |