build: move the import near the place where need it, so that we can build on hosts...
[metze/samba/wip.git] / source4 / lib / ldb / pyldb_util.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Python interface to ldb - utility functions.
5
6    Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
7
8          ** NOTE! The following LGPL license applies to the ldb
9          ** library. This does NOT imply that all of Samba is released
10          ** under the LGPL
11
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.
16
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.
21
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/>.
24 */
25
26 #include "pyldb.h"
27
28 static PyObject *ldb_module = NULL;
29
30 /* There's no Py_ssize_t in 2.4, apparently */
31 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
32 typedef int Py_ssize_t;
33 typedef inquiry lenfunc;
34 typedef intargfunc ssizeargfunc;
35 #endif
36
37 #ifndef Py_RETURN_NONE
38 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
39 #endif
40
41
42 /**
43  * Find out PyTypeObject in ldb module for a given typename
44  */
45 static PyTypeObject * PyLdb_GetPyType(const char *typename)
46 {
47         PyObject *py_obj = NULL;
48
49         if (ldb_module == NULL) {
50                 ldb_module = PyImport_ImportModule("ldb");
51                 if (ldb_module == NULL) {
52                         return NULL;
53                 }
54         }
55
56         py_obj = PyObject_GetAttrString(ldb_module, typename);
57
58         return (PyTypeObject*)py_obj;
59 }
60
61 /**
62  * Obtain a ldb DN from a Python object.
63  *
64  * @param mem_ctx Memory context
65  * @param object Python object
66  * @param ldb_ctx LDB context
67  * @return Whether or not the conversion succeeded
68  */
69 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 
70                    struct ldb_context *ldb_ctx, struct ldb_dn **dn)
71 {
72         struct ldb_dn *odn;
73         PyTypeObject *PyLdb_Dn_Type;
74
75         if (ldb_ctx != NULL && PyString_Check(object)) {
76                 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
77                 *dn = odn;
78                 return true;
79         }
80
81         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
82         if (PyLdb_Dn_Type == NULL) {
83                 return false;
84         }
85
86         if (PyObject_TypeCheck(object, PyLdb_Dn_Type)) {
87                 *dn = PyLdbDn_AsDn(object);
88                 return true;
89         }
90
91         PyErr_SetString(PyExc_TypeError, "Expected DN");
92         return false;
93 }
94
95 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
96 {
97         PyLdbDnObject *py_ret;
98         PyTypeObject *PyLdb_Dn_Type;
99
100         if (dn == NULL) {
101                 Py_RETURN_NONE;
102         }
103
104         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
105         if (PyLdb_Dn_Type == NULL) {
106                 return NULL;
107         }
108
109         py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
110         if (py_ret == NULL) {
111                 PyErr_NoMemory();
112                 return NULL;
113         }
114         py_ret->mem_ctx = talloc_new(NULL);
115         py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
116         return (PyObject *)py_ret;
117 }