python3 port for netbios module
authorNoel Power <noel.power@suse.com>
Fri, 2 Feb 2018 17:08:00 +0000 (17:08 +0000)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 23 Mar 2018 06:28:25 +0000 (07:28 +0100)
Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
libcli/nbt/pynbt.c
libcli/nbt/wscript_build

index 70e08c69de980e3c8a3aa9c61ea3182c60d2b1e5..6337092b2263536780aab121acac361b14a6fb39 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <Python.h>
 #include "includes.h"
+#include "python/py3compat.h"
 #include "libcli/util/pyerrors.h"
 #include "python/modules.h"
 #include "../libcli/nbt/libnbt.h"
@@ -37,7 +38,7 @@ typedef struct {
 static void py_nbt_node_dealloc(nbt_node_Object *self)
 {
        talloc_free(self->mem_ctx);
-       self->ob_type->tp_free(self);
+       Py_TYPE(self)->tp_free(self);
 }
 
 static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject *kwargs)
@@ -56,8 +57,8 @@ static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject *
 
 static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, uint16_t *dest_port)
 {
-       if (PyString_Check(obj)) {
-               *dest_addr = PyString_AsString(obj);
+       if (PyStr_Check(obj)) {
+               *dest_addr = PyStr_AsString(obj);
                *dest_port = NBT_NAME_SERVICE_PORT;
                return true;
        }
@@ -68,12 +69,12 @@ static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, u
                        return false;
                }
 
-               if (!PyString_Check(PyTuple_GetItem(obj, 0))) {
+               if (!PyStr_Check(PyTuple_GetItem(obj, 0))) {
                        PyErr_SetString(PyExc_TypeError, "Destination tuple first element not string");
                        return false;
                }
 
-               *dest_addr = PyString_AsString(obj);
+               *dest_addr = PyStr_AsString(obj);
 
                if (PyTuple_Size(obj) == 1) {
                        *dest_port = NBT_NAME_SERVICE_PORT;
@@ -95,13 +96,13 @@ static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *name_socke
 {
        if (PyTuple_Check(obj)) {
                if (PyTuple_Size(obj) == 2) {
-                       name->name = PyString_AsString(PyTuple_GetItem(obj, 0));
+                       name->name = PyStr_AsString(PyTuple_GetItem(obj, 0));
                        name->type = PyInt_AsLong(PyTuple_GetItem(obj, 1));
                        name->scope = NULL;
                        return true;
                } else if (PyTuple_Size(obj) == 3) {
-                       name->name = PyString_AsString(PyTuple_GetItem(obj, 0));
-                       name->scope = PyString_AsString(PyTuple_GetItem(obj, 1));
+                       name->name = PyStr_AsString(PyTuple_GetItem(obj, 0));
+                       name->scope = PyStr_AsString(PyTuple_GetItem(obj, 1));
                        name->type = PyInt_AsLong(PyTuple_GetItem(obj, 2));
                        return true;
                } else {
@@ -110,9 +111,9 @@ static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *name_socke
                }
        }
 
-       if (PyString_Check(obj)) {
+       if (PyStr_Check(obj)) {
                /* FIXME: Parse string to be able to interpret things like RHONWYN<02> ? */
-               name->name = PyString_AsString(obj);
+               name->name = PyStr_AsString(obj);
                name->scope = NULL;
                name->type = 0;
                return true;
@@ -171,7 +172,7 @@ static PyObject *py_nbt_name_query(PyObject *self, PyObject *args, PyObject *kwa
        ret = PyTuple_New(3);
        if (ret == NULL)
                return NULL;
-       PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
+       PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
 
        py_name = PyObject_FromNBTName(node->socket, &io.out.name);
        if (py_name == NULL)
@@ -186,7 +187,7 @@ static PyObject *py_nbt_name_query(PyObject *self, PyObject *args, PyObject *kwa
        }
 
        for (i = 0; i < io.out.num_addrs; i++) {
-               PyList_SetItem(reply_addrs, i, PyString_FromString(io.out.reply_addrs[i]));
+               PyList_SetItem(reply_addrs, i, PyStr_FromString(io.out.reply_addrs[i]));
        }
 
        PyTuple_SetItem(ret, 2, reply_addrs);
@@ -229,7 +230,7 @@ static PyObject *py_nbt_name_status(PyObject *self, PyObject *args, PyObject *kw
        ret = PyTuple_New(3);
        if (ret == NULL)
                return NULL;
-       PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
+       PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
 
        py_name = PyObject_FromNBTName(node->socket, &io.out.name);
        if (py_name == NULL)
@@ -292,7 +293,7 @@ static PyObject *py_nbt_name_register(PyObject *self, PyObject *args, PyObject *
        ret = PyTuple_New(4);
        if (ret == NULL)
                return NULL;
-       PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
+       PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
 
        py_name = PyObject_FromNBTName(node->socket, &io.out.name);
        if (py_name == NULL)
@@ -300,7 +301,7 @@ static PyObject *py_nbt_name_register(PyObject *self, PyObject *args, PyObject *
 
        PyTuple_SetItem(ret, 1, py_name);
 
-       PyTuple_SetItem(ret, 2, PyString_FromString(io.out.reply_addr));
+       PyTuple_SetItem(ret, 2, PyStr_FromString(io.out.reply_addr));
 
        PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode));
 
@@ -347,7 +348,7 @@ static PyObject *py_nbt_name_refresh(PyObject *self, PyObject *args, PyObject *k
        ret = PyTuple_New(3);
        if (ret == NULL)
                return NULL;
-       PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from));
+       PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from));
 
        py_name = PyObject_FromNBTName(node->socket, &io.out.name);
        if (py_name == NULL)
@@ -355,7 +356,7 @@ static PyObject *py_nbt_name_refresh(PyObject *self, PyObject *args, PyObject *k
 
        PyTuple_SetItem(ret, 1, py_name);
 
-       PyTuple_SetItem(ret, 2, PyString_FromString(io.out.reply_addr));
+       PyTuple_SetItem(ret, 2, PyStr_FromString(io.out.reply_addr));
 
        PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode));
 
@@ -386,7 +387,7 @@ static PyMethodDef py_nbt_methods[] = {
 };
 
 PyTypeObject nbt_node_Type = {
-       PyObject_HEAD_INIT(NULL) 0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "netbios.Node",
        .tp_basicsize = sizeof(nbt_node_Object),
        .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
@@ -397,14 +398,24 @@ PyTypeObject nbt_node_Type = {
                  "Create a new NetBIOS node\n"
 };
 
-void initnetbios(void)
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "netbios",
+    .m_doc = "NetBIOS over TCP/IP support",
+    .m_size = -1,
+    .m_methods = NULL,
+};
+
+MODULE_INIT_FUNC(netbios)
 {
-       PyObject *mod;
+       PyObject *mod = NULL;
        if (PyType_Ready(&nbt_node_Type) < 0)
-               return;
+               return mod;
+
+       mod = PyModule_Create(&moduledef);
 
-       mod = Py_InitModule3("netbios", NULL, "NetBIOS over TCP/IP support");
 
        Py_INCREF((PyObject *)&nbt_node_Type);
        PyModule_AddObject(mod, "Node", (PyObject *)&nbt_node_Type);
+       return mod;
 }
index 090789ce99a9185ed24acf1d6aab5c72be1ed12a..e965a347204a43d50a37f9b40046622a59b92375 100644 (file)
@@ -24,7 +24,8 @@ bld.SAMBA_BINARY('nmblookup' + bld.env.suffix4,
                  install=False
                  )
 
-bld.SAMBA_PYTHON('python_netbios',
+for env in bld.gen_python_environments():
+        bld.SAMBA_PYTHON('python_netbios',
                  source='pynbt.c',
                  public_deps='cli-nbt DYNCONFIG samba-hostconfig',
                  realname='samba/netbios.so'