2 Unix SMB/CIFS implementation.
4 Python interface to ldb - utility functions.
6 Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 static PyObject *ldb_module = NULL;
33 * Find out PyTypeObject in ldb module for a given typename
35 static PyTypeObject * PyLdb_GetPyType(const char *typename)
37 PyTypeObject *type = NULL;
40 if (ldb_module == NULL) {
41 ldb_module = PyImport_ImportModule("ldb");
42 if (ldb_module == NULL) {
47 type = (PyTypeObject *)PyObject_GetAttrString(ldb_module, typename);
51 PyErr_Format(PyExc_NameError,
52 "Unable to find type %s in ldb module",
57 ok = PyType_Check(type);
59 PyErr_Format(PyExc_TypeError,
60 "Expected type ldb.%s, not %s",
61 typename, Py_TYPE(type)->tp_name);
69 bool pyldb_check_type(PyObject *obj, const char *typename)
72 PyTypeObject *type = PyLdb_GetPyType(typename);
74 ok = PyObject_TypeCheck(obj, type);
81 * Obtain a ldb DN from a Python object.
83 * @param mem_ctx Memory context
84 * @param object Python object
85 * @param ldb_ctx LDB context
86 * @return Whether or not the conversion succeeded
88 bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
89 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
92 PyTypeObject *PyLdb_Dn_Type;
94 if (ldb_ctx != NULL && (PyUnicode_Check(object))) {
95 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyUnicode_AsUTF8(object));
100 if (ldb_ctx != NULL && PyBytes_Check(object)) {
101 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyBytes_AsString(object));
106 PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
107 if (PyLdb_Dn_Type == NULL) {
111 if (PyObject_TypeCheck(object, PyLdb_Dn_Type)) {
112 *dn = pyldb_Dn_AS_DN(object);
116 PyErr_SetString(PyExc_TypeError, "Expected DN");
120 PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn)
122 PyLdbDnObject *py_ret;
123 PyTypeObject *PyLdb_Dn_Type;
129 PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
130 if (PyLdb_Dn_Type == NULL) {
134 py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
135 if (py_ret == NULL) {
139 py_ret->mem_ctx = talloc_new(NULL);
140 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
141 return (PyObject *)py_ret;