samba:python - Py_RETURN_NONE remove compatibility code for releases < 2.4
[obnox/samba/samba-obnox.git] / 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 <Python.h>
27 #include "ldb.h"
28 #include "pyldb.h"
29
30 static PyObject *ldb_module = NULL;
31
32 /* There's no Py_ssize_t in 2.4, apparently */
33 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
34 typedef int Py_ssize_t;
35 typedef inquiry lenfunc;
36 typedef intargfunc ssizeargfunc;
37 #endif
38
39 /**
40  * Find out PyTypeObject in ldb module for a given typename
41  */
42 static PyTypeObject * PyLdb_GetPyType(const char *typename)
43 {
44         PyObject *py_obj = NULL;
45
46         if (ldb_module == NULL) {
47                 ldb_module = PyImport_ImportModule("ldb");
48                 if (ldb_module == NULL) {
49                         return NULL;
50                 }
51         }
52
53         py_obj = PyObject_GetAttrString(ldb_module, typename);
54
55         return (PyTypeObject*)py_obj;
56 }
57
58 /**
59  * Obtain a ldb DN from a Python object.
60  *
61  * @param mem_ctx Memory context
62  * @param object Python object
63  * @param ldb_ctx LDB context
64  * @return Whether or not the conversion succeeded
65  */
66 bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 
67                    struct ldb_context *ldb_ctx, struct ldb_dn **dn)
68 {
69         struct ldb_dn *odn;
70         PyTypeObject *PyLdb_Dn_Type;
71
72         if (ldb_ctx != NULL && PyString_Check(object)) {
73                 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
74                 *dn = odn;
75                 return true;
76         }
77
78         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
79         if (PyLdb_Dn_Type == NULL) {
80                 return false;
81         }
82
83         if (PyObject_TypeCheck(object, PyLdb_Dn_Type)) {
84                 *dn = pyldb_Dn_AsDn(object);
85                 return true;
86         }
87
88         PyErr_SetString(PyExc_TypeError, "Expected DN");
89         return false;
90 }
91
92 PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn)
93 {
94         PyLdbDnObject *py_ret;
95         PyTypeObject *PyLdb_Dn_Type;
96
97         if (dn == NULL) {
98                 Py_RETURN_NONE;
99         }
100
101         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
102         if (PyLdb_Dn_Type == NULL) {
103                 return NULL;
104         }
105
106         py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
107         if (py_ret == NULL) {
108                 PyErr_NoMemory();
109                 return NULL;
110         }
111         py_ret->mem_ctx = talloc_new(NULL);
112         py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
113         return (PyObject *)py_ret;
114 }