python: Port the samba.net module to Python 3
authorPetr Viktorin <pviktori@redhat.com>
Mon, 23 Jan 2017 19:34:08 +0000 (20:34 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 10 Mar 2017 06:31:12 +0000 (07:31 +0100)
Signed-off-by: Petr Viktorin <pviktori@redhat.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
source4/libnet/py_net.c
source4/libnet/wscript_build

index 5180c247a716b44f0fa1dc91f0c2005bc5d28f12..bf39415cf32aee951733dbd8c65c51dc4df40267 100644 (file)
@@ -20,6 +20,7 @@
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
 #include <pyldb.h>
 #include <pytalloc.h>
@@ -36,8 +37,6 @@
 #include "py_net.h"
 #include "librpc/rpc/pyrpc_util.h"
 
-void initnet(void);
-
 static void PyErr_SetDsExtendedError(enum drsuapi_DsExtendedError ext_err, const char *error_description)
 {
        PyObject *error = PyObject_GetAttrString(PyImport_ImportModule("samba"),
@@ -290,7 +289,7 @@ static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwar
        tm = localtime(&r.generic.out.time);
        strftime(timestr, sizeof(timestr)-1, "%c %Z",tm);
        
-       ret = PyString_FromString(timestr);
+       ret = PyStr_FromString(timestr);
 
        talloc_free(mem_ctx);
 
@@ -720,7 +719,7 @@ static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwarg
 
 
 PyTypeObject py_net_Type = {
-       PyObject_HEAD_INIT(NULL) 0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "net.Net",
        .tp_basicsize = sizeof(py_net_Object),
        .tp_dealloc = (destructor)py_net_dealloc,
@@ -728,21 +727,29 @@ PyTypeObject py_net_Type = {
        .tp_new = net_obj_new,
 };
 
-void initnet(void)
+static struct PyModuleDef moduledef = {
+       PyModuleDef_HEAD_INIT,
+       .m_name = "net",
+       .m_size = -1,
+};
+
+MODULE_INIT_FUNC(net)
 {
        PyObject *m;
 
        if (PyType_Ready(&py_net_Type) < 0)
-               return;
+               return NULL;
 
-       m = Py_InitModule3("net", NULL, NULL);
+       m = PyModule_Create(&moduledef);
        if (m == NULL)
-               return;
+               return NULL;
 
        Py_INCREF(&py_net_Type);
        PyModule_AddObject(m, "Net", (PyObject *)&py_net_Type);
-       PyModule_AddObject(m, "LIBNET_JOINDOMAIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOINDOMAIN_AUTOMATIC));
-       PyModule_AddObject(m, "LIBNET_JOINDOMAIN_SPECIFIED", PyInt_FromLong(LIBNET_JOINDOMAIN_SPECIFIED));
-       PyModule_AddObject(m, "LIBNET_JOIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOIN_AUTOMATIC));
-       PyModule_AddObject(m, "LIBNET_JOIN_SPECIFIED", PyInt_FromLong(LIBNET_JOIN_SPECIFIED));
+       PyModule_AddIntConstant(m, "LIBNET_JOINDOMAIN_AUTOMATIC", LIBNET_JOINDOMAIN_AUTOMATIC);
+       PyModule_AddIntConstant(m, "LIBNET_JOINDOMAIN_SPECIFIED", LIBNET_JOINDOMAIN_SPECIFIED);
+       PyModule_AddIntConstant(m, "LIBNET_JOIN_AUTOMATIC", LIBNET_JOIN_AUTOMATIC);
+       PyModule_AddIntConstant(m, "LIBNET_JOIN_SPECIFIED", LIBNET_JOIN_SPECIFIED);
+
+       return m;
 }
index 1274a82d04c5e7ac723aec42de185689f5204673..db113b2f71e065e2a3c8cfa541a523d24e9c5112 100644 (file)
@@ -8,11 +8,14 @@ bld.SAMBA_LIBRARY('samba-net',
        )
 
 
-bld.SAMBA_PYTHON('python_net',
-       source='py_net.c',
-       deps='samba-net pyrpc_util pytalloc-util',
-       realname='samba/net.so'
-       )
+for env in bld.gen_python_environments():
+       pytalloc_util = bld.pyembed_libname('pytalloc-util')
+       pyrpc_util = bld.pyembed_libname('pyrpc_util')
+       bld.SAMBA_PYTHON('python_net',
+               source='py_net.c',
+               deps='samba-net %s %s' % (pyrpc_util, pytalloc_util),
+               realname='samba/net.so'
+               )
 
 bld.SAMBA_PYTHON('python_dckeytab',
        source='py_net_dckeytab.c libnet_export_keytab.c',