pidl:Python: use of pytalloc_GenericObject_reference*() for pyrpc_{ex,im}port_union...
authorStefan Metzmacher <metze@samba.org>
Mon, 20 Feb 2017 17:02:09 +0000 (18:02 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 25 Feb 2017 05:33:33 +0000 (06:33 +0100)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12601

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Sat Feb 25 06:33:33 CET 2017 on sn-devel-144

pidl/lib/Parse/Pidl/Samba4/Python.pm
source4/librpc/rpc/pyrpc_util.c

index e7b127d40b7d04e8f4198cd2a6a2082ade638d44..f871e04be60f9f465e3c3509130d0373519ec280 100644 (file)
@@ -1181,29 +1181,15 @@ sub PythonType($$$$)
                my $py_methods = "NULL";
                my $typename = mapTypeName($d);
 
-               ##
-               ## PyCapsule (starting with 2.7) vs. PyCObject (up to 3.2)
-               ##
-               ## As we need to support python 2.6, we can't use PyCapsule yet.
-               ##
-               ## When we'll get support fpr Python3 we'll have to emulate
-               ## PyCObject using PyCapsule and convert these functions to
-               ## use PyCapsule.
-               ##
                $self->pidl("static PyObject *py_$d->{NAME}\_import(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
                $self->pidl("{");
                $self->indent;
                $self->pidl("const char * const kwnames[] = { \"mem_ctx\", \"level\", \"in\", NULL };");
                $self->pidl("PyObject *mem_ctx_obj = NULL;");
-               $self->pidl("static const char *mem_ctx_type = \"TALLOC_CTX\";");
-               $self->pidl("const char *mem_ctx_desc = NULL;");
                $self->pidl("TALLOC_CTX *mem_ctx = NULL;");
                $self->pidl("int level = 0;");
                $self->pidl("PyObject *in_obj = NULL;");
-               $self->pidl("static const char *in_type = \"$typename\";");
-               $self->pidl("const char *in_desc = NULL;");
                $self->pidl("$typename *in = NULL;");
-               $self->pidl("int cmp;");
                $self->pidl("");
                $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OiO:import\",");
                $self->indent;
@@ -1216,54 +1202,20 @@ sub PythonType($$$$)
                $self->pidl("return NULL;");
                $self->deindent;
                $self->pidl("}");
-               $self->pidl("if (!PyCObject_Check(mem_ctx_obj)) {");
-               $self->indent;
-               $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx needs to be of type PyCObject!\");");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("mem_ctx_desc = (const char *)PyCObject_GetDesc(mem_ctx_obj);");
-               $self->indent;
-               $self->pidl("if (mem_ctx_desc == NULL) {");
-               $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx hash no PyCObject_GetDesc()!\");");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("cmp = strncmp(mem_ctx_type, mem_ctx_desc, strlen(mem_ctx_type) + 1);");
-               $self->pidl("if (cmp != 0) {");
-               $self->indent;
-               $self->pidl("PyErr_Format(PyExc_TypeError, \"mem_ctx should have PyCObject_GetDesc() = %s!\", mem_ctx_type);");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("mem_ctx = PyCObject_AsVoidPtr(mem_ctx_obj);");
+               $self->pidl("mem_ctx = pytalloc_get_ptr(mem_ctx_obj);");
                $self->pidl("if (mem_ctx == NULL) {");
                $self->indent;
                $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx is NULL)!\");");
                $self->pidl("return NULL;");
                $self->deindent;
                $self->pidl("}");
-               $self->pidl("if (!PyCObject_Check(in_obj)) {");
-               $self->indent;
-               $self->pidl("PyErr_SetString(PyExc_TypeError, \"in needs to be of type PyCObject!\");");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("in_desc = (const char *)PyCObject_GetDesc(in_obj);");
+               $self->pidl("in = ($typename *)pytalloc_get_ptr(in_obj);");
+               $self->pidl("if (in == NULL) {");
                $self->indent;
-               $self->pidl("if (in_desc == NULL) {");
-               $self->pidl("PyErr_SetString(PyExc_TypeError, \"in hash no PyCObject_GetDesc()!\");");
+               $self->pidl("PyErr_Format(PyExc_TypeError, \"in needs to be a pointer to $typename!\");");
                $self->pidl("return NULL;");
                $self->deindent;
                $self->pidl("}");
-               $self->pidl("cmp = strncmp(in_type, in_desc, strlen(in_type) + 1);");
-               $self->pidl("if (cmp != 0) {");
-               $self->indent;
-               $self->pidl("PyErr_Format(PyExc_TypeError, \"in should have PyCObject_GetDesc() = %s!\", in_type);");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("in = ($typename *)PyCObject_AsVoidPtr(in_obj);");
                $self->pidl("");
                $self->pidl("return py_import_$d->{NAME}(mem_ctx, level, in);");
                $self->deindent;
@@ -1275,14 +1227,10 @@ sub PythonType($$$$)
                $self->indent;
                $self->pidl("const char * const kwnames[] = { \"mem_ctx\", \"level\", \"in\", NULL };");
                $self->pidl("PyObject *mem_ctx_obj = NULL;");
-               $self->pidl("static const char *mem_ctx_type = \"TALLOC_CTX\";");
-               $self->pidl("const char *mem_ctx_desc = NULL;");
                $self->pidl("TALLOC_CTX *mem_ctx = NULL;");
                $self->pidl("int level = 0;");
                $self->pidl("PyObject *in = NULL;");
-               $self->pidl("static const char *out_type = \"$typename\";");
                $self->pidl("$typename *out = NULL;");
-               $self->pidl("int cmp;");
                $self->pidl("");
                $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OiO:import\",");
                $self->indent;
@@ -1295,27 +1243,7 @@ sub PythonType($$$$)
                $self->pidl("return NULL;");
                $self->deindent;
                $self->pidl("}");
-               $self->pidl("if (!PyCObject_Check(mem_ctx_obj)) {");
-               $self->indent;
-               $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx needs to be of type PyCObject!\");");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("mem_ctx_desc = (const char *)PyCObject_GetDesc(mem_ctx_obj);");
-               $self->indent;
-               $self->pidl("if (mem_ctx_desc == NULL) {");
-               $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx hash no PyCObject_GetDesc()!\");");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("cmp = strncmp(mem_ctx_type, mem_ctx_desc, strlen(mem_ctx_type) + 1);");
-               $self->pidl("if (cmp != 0) {");
-               $self->indent;
-               $self->pidl("PyErr_Format(PyExc_TypeError, \"mem_ctx should have PyCObject_GetDesc() = %s!\", mem_ctx_type);");
-               $self->pidl("return NULL;");
-               $self->deindent;
-               $self->pidl("}");
-               $self->pidl("mem_ctx = PyCObject_AsVoidPtr(mem_ctx_obj);");
+               $self->pidl("mem_ctx = pytalloc_get_ptr(mem_ctx_obj);");
                $self->pidl("if (mem_ctx == NULL) {");
                $self->indent;
                $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx is NULL)!\");");
@@ -1329,7 +1257,8 @@ sub PythonType($$$$)
                $self->pidl("return NULL;");
                $self->deindent;
                $self->pidl("}");
-               $self->pidl("return PyCObject_FromVoidPtrAndDesc(out, discard_const_p(char, out_type), NULL);");
+               $self->pidl("");
+               $self->pidl("return pytalloc_GenericObject_reference(out);");
                $self->deindent;
                $self->pidl("}");
                $self->pidl("");
index cfbb9cb19776a6c23d467aa52f2d6c8949d1dfd2..fe0e2de8355e1904d620c79aa830fcb23d23dadc 100644 (file)
@@ -394,21 +394,16 @@ PyObject *PyString_FromStringOrNULL(const char *str)
 PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
                             const void *in, const char *typename)
 {
-       static const char *mem_ctx_type = "TALLOC_CTX";
        PyObject *mem_ctx_obj = NULL;
        PyObject *in_obj = NULL;
        PyObject *ret = NULL;
 
-       mem_ctx_obj = PyCObject_FromVoidPtrAndDesc(mem_ctx,
-                                       discard_const_p(char, mem_ctx_type),
-                                       NULL);
+       mem_ctx_obj = pytalloc_GenericObject_reference(mem_ctx);
        if (mem_ctx_obj == NULL) {
                return NULL;
        }
 
-       in_obj = PyCObject_FromVoidPtrAndDesc(discard_const(in),
-                                       discard_const_p(char, typename),
-                                       NULL);
+       in_obj = pytalloc_GenericObject_reference_ex(mem_ctx, discard_const(in));
        if (in_obj == NULL) {
                Py_XDECREF(mem_ctx_obj);
                return NULL;
@@ -430,16 +425,11 @@ PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
 void *pyrpc_export_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
                         PyObject *in, const char *typename)
 {
-       static const char *mem_ctx_type = "TALLOC_CTX";
        PyObject *mem_ctx_obj = NULL;
        PyObject *ret_obj = NULL;
-       const char *ret_desc = NULL;
        void *ret = NULL;
-       int cmp;
 
-       mem_ctx_obj = PyCObject_FromVoidPtrAndDesc(mem_ctx,
-                                       discard_const_p(char, mem_ctx_type),
-                                       NULL);
+       mem_ctx_obj = pytalloc_GenericObject_reference(mem_ctx);
        if (mem_ctx_obj == NULL) {
                return NULL;
        }
@@ -453,33 +443,7 @@ void *pyrpc_export_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
                return NULL;
        }
 
-       if (!PyCObject_Check(ret_obj)) {
-               Py_XDECREF(ret_obj);
-               PyErr_Format(PyExc_TypeError,
-                            "New %s.__export__() returned no PyCObject!",
-                            type->tp_name);
-               return NULL;
-       }
-
-       ret_desc = (const char *)PyCObject_GetDesc(ret_obj);
-       if (ret_desc == NULL) {
-               Py_XDECREF(ret_obj);
-               PyErr_Format(PyExc_TypeError,
-                            "New %s.__export__() returned no PyCObject_GetDesc()!",
-                            type->tp_name);
-               return NULL;
-       }
-
-       cmp = strncmp(typename, ret_desc, strlen(typename) + 1);
-       if (cmp != 0) {
-               Py_XDECREF(ret_obj);
-               PyErr_Format(PyExc_TypeError,
-                            "New %s.__export__() returned PyCObject_GetDesc() != %s!",
-                            type->tp_name, typename);
-               return NULL;
-       }
-
-       ret = PyCObject_AsVoidPtr(ret_obj);
+       ret = _pytalloc_get_type(ret_obj, typename);
        Py_XDECREF(ret_obj);
        return ret;
 }