python: samba.dcerpc: Port RPC related stuff to Python 3
authorLumir Balhar <lbalhar@redhat.com>
Wed, 15 Feb 2017 08:19:33 +0000 (09:19 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 10 Mar 2017 06:31:11 +0000 (07:31 +0100)
Port RPC related stuff like samba.dcerpc.misc and samba.dcerpc
Python modules and pyrpc_util to Python 3 compatible form.

Signed-off-by: Lumir Balhar <lbalhar@redhat.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Pair-programmed-by: Andrew Bartlett <abartlet@samba.org>
source4/librpc/ndr/py_misc.c
source4/librpc/rpc/dcerpc.py
source4/librpc/rpc/pyrpc.c
source4/librpc/rpc/pyrpc_util.c
source4/librpc/wscript_build

index d8edff899aca0ca376fb5fda346f5fc13833639a..6316b9b6704d72a5bf728ca1ad4bf0736dcfe95a 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include <Python.h>
+#include "python/py3compat.h"
 #include "librpc/gen_ndr/misc.h"
 
+#if PY_MAJOR_VERSION >= 3
+static PyObject *py_GUID_richcmp(PyObject *py_self, PyObject *py_other, int op)
+{
+       int ret;
+       struct GUID *self = pytalloc_get_ptr(py_self), *other;
+       other = pytalloc_get_ptr(py_other);
+       if (other == NULL) {
+               Py_INCREF(Py_NotImplemented);
+               return Py_NotImplemented;
+       }
+
+       ret = GUID_compare(self, other);
+
+       switch (op) {
+               case Py_EQ: if (ret == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
+               case Py_NE: if (ret != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
+               case Py_LT: if (ret <  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
+               case Py_GT: if (ret >  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
+               case Py_LE: if (ret <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
+               case Py_GE: if (ret >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
+       }
+       Py_INCREF(Py_NotImplemented);
+       return Py_NotImplemented;
+}
+#else
 static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
 {
        int ret;
@@ -37,12 +63,13 @@ static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
                return 0;
        }
 }
+#endif
 
 static PyObject *py_GUID_str(PyObject *py_self)
 {
        struct GUID *self = pytalloc_get_ptr(py_self);
        char *str = GUID_string(NULL, self);
-       PyObject *ret = PyString_FromString(str);
+       PyObject *ret = PyStr_FromString(str);
        talloc_free(str);
        return ret;
 }
@@ -51,7 +78,7 @@ static PyObject *py_GUID_repr(PyObject *py_self)
 {
        struct GUID *self = pytalloc_get_ptr(py_self);
        char *str = GUID_string(NULL, self);
-       PyObject *ret = PyString_FromFormat("GUID('%s')", str);
+       PyObject *ret = PyStr_FromFormat("GUID('%s')", str);
        talloc_free(str);
        return ret;
 }
@@ -68,13 +95,14 @@ static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
 
        if (str != NULL) {
                DATA_BLOB guid_val;
+               Py_ssize_t _size;
 
-               if (!PyString_Check(str)) {
+               if (!PyStr_Check(str)) {
                        PyErr_SetString(PyExc_TypeError, "Expected a string argument to GUID()");
                        return -1;
                }
-               guid_val.data = (uint8_t *)PyString_AsString(str);
-               guid_val.length = PyString_Size(str);
+               guid_val.data = (uint8_t *)PyStr_AsUTF8AndSize(str, &_size);
+               guid_val.length = _size;
                status = GUID_from_data_blob(&guid_val, guid);
                if (!NT_STATUS_IS_OK(status)) {
                        PyErr_SetNTSTATUS(status);
@@ -90,7 +118,11 @@ static void py_GUID_patch(PyTypeObject *type)
        type->tp_init = py_GUID_init;
        type->tp_str = py_GUID_str;
        type->tp_repr = py_GUID_repr;
+#if PY_MAJOR_VERSION >= 3
+       type->tp_richcompare = py_GUID_richcmp;
+#else
        type->tp_compare = py_GUID_cmp;
+#endif
 }
 
 #define PY_GUID_PATCH py_GUID_patch
@@ -120,7 +152,7 @@ static PyObject *py_policy_handle_repr(PyObject *py_self)
 {
        struct policy_handle *self = pytalloc_get_ptr(py_self);
        char *uuid_str = GUID_string(NULL, &self->uuid);
-       PyObject *ret = PyString_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str);
+       PyObject *ret = PyStr_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str);
        talloc_free(uuid_str);
        return ret;
 }
@@ -129,7 +161,7 @@ static PyObject *py_policy_handle_str(PyObject *py_self)
 {
        struct policy_handle *self = pytalloc_get_ptr(py_self);
        char *uuid_str = GUID_string(NULL, &self->uuid);
-       PyObject *ret = PyString_FromFormat("%d, %s", self->handle_type, uuid_str);
+       PyObject *ret = PyStr_FromFormat("%d, %s", self->handle_type, uuid_str);
        talloc_free(uuid_str);
        return ret;
 }
index 830720cde002fa0e5ff6fccb1e2f364ab1473d6f..64dd6e3a433006aa88cae3b05e8103f9e02d2cf4 100644 (file)
@@ -15,4 +15,4 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-from base import *
+from samba.dcerpc.base import *
index 834000c65746da19303f8b34c13630a67c2066e6..d4f13ba89f6743dd1d39e69cba95bf0754d58f4b 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
 #include <structmember.h>
 #include "librpc/rpc/pyrpc.h"
@@ -37,7 +38,7 @@ static PyTypeObject *ndr_syntax_id_Type;
 static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
 {
        NTSTATUS status;
-       status = GUID_from_string(PyString_AsString(object), uuid);
+       status = GUID_from_string(PyStr_AsString(object), uuid);
        if (NT_STATUS_IS_ERR(status)) {
                PyErr_SetNTSTATUS(status);
                return false;
@@ -49,7 +50,7 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy
 {
        ZERO_STRUCTP(syntax_id);
 
-       if (PyString_Check(object)) {
+       if (PyStr_Check(object)) {
                return PyString_AsGUID(object, &syntax_id->uuid);
        } else if (PyTuple_Check(object)) {
                if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
@@ -57,7 +58,7 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy
                        return false;
                }
 
-               if (!PyString_Check(PyTuple_GetItem(object, 0))) {
+               if (!PyStr_Check(PyTuple_GetItem(object, 0))) {
                        PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
                        return false;
                }
@@ -87,7 +88,7 @@ static PyObject *py_iface_server_name(PyObject *obj, void *closure)
        if (server_name == NULL)
                Py_RETURN_NONE;
 
-       return PyString_FromString(server_name);
+       return PyStr_FromString(server_name);
 }
 
 static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id)
@@ -128,7 +129,7 @@ static PyObject *py_iface_session_key(PyObject *obj, void *closure)
        NTSTATUS status = dcerpc_fetch_session_key(iface->pipe, &session_key);
        PyErr_NTSTATUS_IS_ERR_RAISE(status);
 
-       return PyString_FromStringAndSize((const char *)session_key.data, session_key.length);
+       return PyBytes_FromStringAndSize((const char *)session_key.data, session_key.length);
 }
 
 static PyObject *py_iface_user_session_key(PyObject *obj, void *closure)
@@ -166,7 +167,7 @@ static PyObject *py_iface_user_session_key(PyObject *obj, void *closure)
                return NULL;
        }
 
-       session_key_obj = PyString_FromStringAndSize((const char *)session_key.data,
+       session_key_obj = PyBytes_FromStringAndSize((const char *)session_key.data,
                                                     session_key.length);
        talloc_free(mem_ctx);
        return session_key_obj;
@@ -260,7 +261,7 @@ static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwar
                return NULL;
        }
 
-       ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
+       ret = PyBytes_FromStringAndSize((char *)data_out.data, data_out.length);
 
        talloc_free(mem_ctx);
        return ret;
@@ -337,7 +338,7 @@ static PyObject *dcerpc_interface_new(PyTypeObject *type, PyObject *args, PyObje
 }
 
 static PyTypeObject dcerpc_InterfaceType = {
-       PyObject_HEAD_INIT(NULL) 0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "dcerpc.ClientConnection",
        .tp_basicsize = sizeof(dcerpc_InterfaceObject),
        .tp_dealloc = dcerpc_interface_dealloc,
@@ -359,7 +360,7 @@ static PyObject *py_transfer_syntax_ndr_new(PyTypeObject *type, PyObject *args,
 }
 
 static PyTypeObject py_transfer_syntax_ndr_SyntaxType = {
-       PyObject_HEAD_INIT(NULL) 0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "base.transfer_syntax_ndr",
        .tp_doc = "transfer_syntax_ndr()\n",
        .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
@@ -372,7 +373,7 @@ static PyObject *py_transfer_syntax_ndr64_new(PyTypeObject *type, PyObject *args
 }
 
 static PyTypeObject py_transfer_syntax_ndr64_SyntaxType = {
-       PyObject_HEAD_INIT(NULL) 0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "base.transfer_syntax_ndr64",
        .tp_doc = "transfer_syntax_ndr64()\n",
        .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
@@ -410,25 +411,32 @@ static PyObject *py_bind_time_features_syntax_new(PyTypeObject *type, PyObject *
 }
 
 static PyTypeObject py_bind_time_features_syntax_SyntaxType = {
-       PyObject_HEAD_INIT(NULL) 0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "base.bind_time_features_syntax",
        .tp_doc = "bind_time_features_syntax(features)\n",
        .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
        .tp_new = py_bind_time_features_syntax_new,
 };
 
-void initbase(void)
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "base",
+    .m_doc = "DCE/RPC protocol implementation",
+    .m_size = -1,
+};
+
+MODULE_INIT_FUNC(base)
 {
        PyObject *m;
        PyObject *dep_samba_dcerpc_misc;
 
        dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc");
        if (dep_samba_dcerpc_misc == NULL)
-               return;
+               return NULL;
 
        ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id");
        if (ndr_syntax_id_Type == NULL)
-               return;
+               return NULL;
 
        py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type;
        py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
@@ -438,18 +446,18 @@ void initbase(void)
        py_bind_time_features_syntax_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
 
        if (PyType_Ready(&dcerpc_InterfaceType) < 0)
-               return;
+               return NULL;
 
        if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0)
-               return;
+               return NULL;
        if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0)
-               return;
+               return NULL;
        if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0)
-               return;
+               return NULL;
 
-       m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
+       m = PyModule_Create(&moduledef);
        if (m == NULL)
-               return;
+               return NULL;
 
        Py_INCREF((PyObject *)&dcerpc_InterfaceType);
        PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
@@ -460,4 +468,5 @@ void initbase(void)
        PyModule_AddObject(m, "transfer_syntax_ndr64", (PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType);
        Py_INCREF((PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);
        PyModule_AddObject(m, "bind_time_features_syntax", (PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);
+       return m;
 }
index fe0e2de8355e1904d620c79aa830fcb23d23dadc..21d94308cb91e736bf3b18feabd9e4aab2d2d643 100644 (file)
@@ -21,6 +21,7 @@
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
 #include "librpc/rpc/pyrpc_util.h"
 #include "librpc/rpc/dcerpc.h"
@@ -388,7 +389,7 @@ PyObject *PyString_FromStringOrNULL(const char *str)
        if (str == NULL) {
                Py_RETURN_NONE;
        }
-       return PyString_FromString(str);
+       return PyStr_FromString(str);
 }
 
 PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
index 60404f8e08d16cb14991c2e537cc5ab32339e743..cc03e10fa160759730ca6cfc87f14c0c1a2f0e54 100644 (file)
@@ -174,18 +174,23 @@ bld.SAMBA_LIBRARY('dcerpc',
        vnum='0.0.1'
        )
 
-bld.SAMBA_SUBSYSTEM('pyrpc_util',
-       source='rpc/pyrpc_util.c',
-       public_deps='pytalloc-util pyparam_util dcerpc MESSAGING',
-       pyext=True,
-       )
+for env in bld.gen_python_environments():
+    pyrpc_util = bld.pyembed_libname('pyrpc_util')
+    pytalloc_util = bld.pyembed_libname('pytalloc-util')
+    pyparam_util = bld.pyembed_libname('pyparam_util')
+
+    bld.SAMBA_SUBSYSTEM(pyrpc_util,
+        source='rpc/pyrpc_util.c',
+        public_deps='%s %s dcerpc MESSAGING' % (pytalloc_util, pyparam_util),
+        pyext=True,
+        )
 
+    bld.SAMBA_PYTHON('python_dcerpc',
+        source='rpc/pyrpc.c',
+        public_deps='LIBCLI_SMB samba-util samba-hostconfig dcerpc-samr RPC_NDR_LSA DYNCONFIG %s gensec' % (pyrpc_util),
+        realname='samba/dcerpc/base.so'
+        )
 
-bld.SAMBA_PYTHON('python_dcerpc',
-       source='rpc/pyrpc.c',
-       public_deps='LIBCLI_SMB samba-util samba-hostconfig dcerpc-samr RPC_NDR_LSA DYNCONFIG pyrpc_util gensec',
-       realname='samba/dcerpc/base.so'
-       )
 
 bld.SAMBA_PYTHON('python_srvsvc',
     source='../../librpc/gen_ndr/py_srvsvc.c',
@@ -224,11 +229,14 @@ bld.SAMBA_PYTHON('python_winreg',
        )
 
 
-bld.SAMBA_PYTHON('python_dcerpc_misc',
-       source='../../librpc/gen_ndr/py_misc.c',
-       deps='pytalloc-util pyrpc_util ndr-krb5pac',
-       realname='samba/dcerpc/misc.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_dcerpc_misc',
+               source='../../librpc/gen_ndr/py_misc.c',
+               deps='%s %s ndr-krb5pac' % (pytalloc_util, pyrpc_util),
+               realname='samba/dcerpc/misc.so'
+               )
 
 
 bld.SAMBA_PYTHON('python_initshutdown',