]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python: Port dsdb module to Python 3 compatible form.
authorLumir Balhar <lbalhar@redhat.com>
Tue, 30 Jan 2018 17:53:38 +0000 (18:53 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 14 Feb 2018 23:18:29 +0000 (00:18 +0100)
Signed-off-by: Lumir Balhar <lbalhar@redhat.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source4/dsdb/pydsdb.c
source4/dsdb/wscript_build

index 8cf3ef5e4f686a440732ead9eef5f5492c2e015d..d38d7095efae3875357cd98b7354aab5511d0bba 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
 #include <ldb.h>
 #include <pyldb.h>
@@ -32,7 +33,6 @@
 #include "lib/util/dlinklist.h"
 #include "dsdb/kcc/garbage_collect_tombstones.h"
 
-void initdsdb(void);
 
 /* FIXME: These should be in a header file somewhere */
 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
@@ -93,7 +93,7 @@ static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args)
                return NULL;
        }
 
-       result = PyString_FromString(site);
+       result = PyStr_FromString(site);
        talloc_free(mem_ctx);
        return result;
 }
@@ -119,7 +119,7 @@ static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self,
                return NULL;
        } 
 
-       ret = PyString_FromString(retstr);
+       ret = PyStr_FromString(retstr);
        talloc_free(retstr);
        return ret;
 }
@@ -136,7 +136,7 @@ static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
        
        PyErr_LDB_OR_RAISE(py_ldb, ldb);
 
-       sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
+       sid = dom_sid_parse_talloc(NULL, PyStr_AsString(py_sid));
        if (sid == NULL) {
                PyErr_NoMemory();
                return NULL;
@@ -209,7 +209,7 @@ static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
                PyErr_NoMemory();
                return NULL;
        }
-       ret = PyString_FromString(retstr);
+       ret = PyStr_FromString(retstr);
        talloc_free(retstr);
        return ret;
 }
@@ -239,7 +239,7 @@ static PyObject *py_samdb_ntds_invocation_id(PyObject *self, PyObject *args)
                PyErr_NoMemory();
                return NULL;
        }
-       result = PyString_FromString(retstr);
+       result = PyStr_FromString(retstr);
        talloc_free(retstr);
        return result;
 }
@@ -281,7 +281,7 @@ static PyObject *py_dsdb_get_oid_from_attid(PyObject *self, PyObject *args)
                return NULL;
        }
 
-       ret = PyString_FromString(oid);
+       ret = PyStr_FromString(oid);
 
        talloc_free(mem_ctx);
 
@@ -436,7 +436,7 @@ static PyObject *py_dsdb_get_backlink_from_lDAPDisplayName(PyObject *self, PyObj
                Py_RETURN_NONE;
        }
 
-       return PyString_FromString(target_attr->lDAPDisplayName);
+       return PyStr_FromString(target_attr->lDAPDisplayName);
 }
 
 
@@ -466,7 +466,7 @@ static PyObject *py_dsdb_get_lDAPDisplayName_by_attid(PyObject *self, PyObject *
                return NULL;
        }
 
-       return PyString_FromString(a->lDAPDisplayName);
+       return PyStr_FromString(a->lDAPDisplayName);
 }
 
 
@@ -499,7 +499,7 @@ static PyObject *py_dsdb_get_syntax_oid_from_lDAPDisplayName(PyObject *self, PyO
                return NULL;
        }
 
-       return PyString_FromString(attribute->syntax->ldap_oid);
+       return PyStr_FromString(attribute->syntax->ldap_oid);
 }
 
 /*
@@ -518,6 +518,7 @@ static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args)
        TALLOC_CTX *tmp_ctx;
        WERROR werr;
        Py_ssize_t i;
+       Py_ssize_t _size;
 
        if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
                return NULL;
@@ -579,13 +580,13 @@ static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args)
 
                for (i = 0; i < el->num_values; i++) {
                        PyObject *item = PyList_GetItem(el_list, i);
-                       if (!PyString_Check(item)) {
+                       if (!PyStr_Check(item)) {
                                PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
                                talloc_free(tmp_ctx);
                                return NULL;
                        }
-                       el->values[i].data = (uint8_t *)PyString_AsString(item);
-                       el->values[i].length = PyString_Size(item);
+                       el->values[i].data = (uint8_t *)PyStr_AsUTF8AndSize(item, &_size);
+                       el->values[i].length = _size;
                }
        }
 
@@ -624,6 +625,7 @@ static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
        TALLOC_CTX *tmp_ctx;
        WERROR werr;
        Py_ssize_t i;
+       Py_ssize_t _size;
        PyTypeObject *py_type = NULL;
        PyObject *module = NULL;
 
@@ -686,13 +688,13 @@ static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
 
                for (i = 0; i < el->num_values; i++) {
                        PyObject *item = PyList_GetItem(el_list, i);
-                       if (!PyString_Check(item)) {
+                       if (!PyStr_Check(item)) {
                                PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
                                talloc_free(tmp_ctx);
                                return NULL;
                        }
-                       el->values[i].data = (uint8_t *)PyString_AsString(item);
-                       el->values[i].length = PyString_Size(item);
+                       el->values[i].data = (uint8_t *)PyStr_AsUTF8AndSize(item, &_size);;
+                       el->values[i].length = _size;
                }
        }
 
@@ -767,7 +769,7 @@ static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
                return NULL;
 
        PyErr_LDB_OR_RAISE(py_ldb, ldb);
-       GUID_from_string(PyString_AsString(py_guid), &guid);
+       GUID_from_string(PyStr_AsString(py_guid), &guid);
 
        if (GUID_all_zero(&guid)) {
                PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id rejected due to all-zero invocation ID");
@@ -806,7 +808,7 @@ static PyObject *py_samdb_ntds_objectGUID(PyObject *self, PyObject *args)
                PyErr_NoMemory();
                return NULL;
        }
-       result = PyString_FromString(retstr);
+       result = PyStr_FromString(retstr);
        talloc_free(retstr);
        return result;
 }
@@ -1200,7 +1202,7 @@ static PyObject *py_dsdb_garbage_collect_tombstones(PyObject *self, PyObject *ar
        length = PyList_GET_SIZE(py_list_dn);
 
        for (i = 0; i < length; i++) {
-               char *part_str = PyString_AsString(PyList_GetItem(py_list_dn, i));
+               char *part_str = PyStr_AsString(PyList_GetItem(py_list_dn, i));
                struct ldb_dn *p;
                struct dsdb_ldb_dn_list_node *node;
 
@@ -1389,14 +1391,22 @@ static PyMethodDef py_dsdb_methods[] = {
        { NULL }
 };
 
-void initdsdb(void)
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "dsdb",
+    .m_doc = "Python bindings for the directory service databases.",
+    .m_size = -1,
+    .m_methods = py_dsdb_methods,
+};
+
+MODULE_INIT_FUNC(dsdb)
 {
        PyObject *m;
 
-       m = Py_InitModule3("dsdb", py_dsdb_methods, 
-                          "Python bindings for the directory service databases.");
+       m = PyModule_Create(&moduledef);
+
        if (m == NULL)
-               return;
+               return NULL;
 
 #define ADD_DSDB_FLAG(val)  PyModule_AddObject(m, #val, PyInt_FromLong(val))
 
@@ -1562,7 +1572,7 @@ void initdsdb(void)
        ADD_DSDB_FLAG(GPO_INHERIT);
        ADD_DSDB_FLAG(GPO_BLOCK_INHERITANCE);
 
-#define ADD_DSDB_STRING(val)  PyModule_AddObject(m, #val, PyString_FromString(val))
+#define ADD_DSDB_STRING(val)  PyModule_AddObject(m, #val, PyStr_FromString(val))
 
        ADD_DSDB_STRING(DSDB_SYNTAX_BINARY_DN);
        ADD_DSDB_STRING(DSDB_SYNTAX_STRING_DN);
@@ -1586,4 +1596,6 @@ void initdsdb(void)
        ADD_DSDB_STRING(DS_GUID_PROGRAM_DATA_CONTAINER);
        ADD_DSDB_STRING(DS_GUID_SYSTEMS_CONTAINER);
        ADD_DSDB_STRING(DS_GUID_USERS_CONTAINER);
+
+       return m;
 }
index 29c6f0e4a701f7be0cf1c650687974dd01e3ba81..e1f1e61c55e7fb129eac950fd053b7048ce1113a 100644 (file)
@@ -62,11 +62,15 @@ bld.SAMBA_MODULE('service_dns_update',
        enabled=bld.AD_DC_BUILD_IS_ENABLED()
        )
 
-bld.SAMBA_PYTHON('python_dsdb',
-       source='pydsdb.c',
-       # the dependency on dcerpc here is because gensec
-       # depends on dcerpc but the waf circular dependency finder
-       # removes it so we end up with unresolved symbols.
-       deps='samdb pyldb-util dcerpc com_err pyrpc_util pyparam_util dsdb_garbage_collect_tombstones',
-       realname='samba/dsdb.so'
-       )
+for env in bld.gen_python_environments():
+       pyldb_util = bld.pyembed_libname('pyldb-util')
+       pyrpc_util = bld.pyembed_libname('pyrpc_util')
+       pyparam_util = bld.pyembed_libname('pyparam_util')
+       bld.SAMBA_PYTHON('python_dsdb',
+               source='pydsdb.c',
+               # the dependency on dcerpc here is because gensec
+               # depends on dcerpc but the waf circular dependency finder
+               # removes it so we end up with unresolved symbols.
+               deps='samdb %s dcerpc com_err %s %s dsdb_garbage_collect_tombstones' % (pyldb_util, pyrpc_util, pyparam_util),
+               realname='samba/dsdb.so'
+               )