python: pidl: Port Python interface generator
authorLumir Balhar <lbalhar@redhat.com>
Mon, 23 Jan 2017 20:03:17 +0000 (21:03 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 10 Mar 2017 06:31:11 +0000 (07:31 +0100)
Port PIDL generator of Python interfaces to generate interfaces in
Python 3 compatible form.

Python 2.7 is now required, so we can use PyCapsule in both versions.

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>
pidl/lib/Parse/Pidl/Samba4/Python.pm

index f871e04..88e8870 100644 (file)
@@ -302,7 +302,7 @@ sub PythonStruct($$$$$$)
                $self->deindent;
                $self->pidl("}");
                $self->pidl("");
-               $self->pidl("ret = PyString_FromStringAndSize((char *)blob.data, blob.length);");
+               $self->pidl("ret = PyBytes_FromStringAndSize((char *)blob.data, blob.length);");
                $self->pidl("TALLOC_FREE(tmp_ctx);");
                $self->pidl("return ret;");
                $self->deindent;
@@ -367,7 +367,7 @@ sub PythonStruct($$$$$$)
                $self->pidl("char *retstr;");
                $self->pidl("");
                $self->pidl("retstr = ndr_print_struct_string(pytalloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);");
-               $self->pidl("ret = PyString_FromString(retstr);");
+               $self->pidl("ret = PyStr_FromString(retstr);");
                $self->pidl("talloc_free(retstr);");
                $self->pidl("");
                $self->pidl("return ret;");
@@ -393,7 +393,7 @@ sub PythonStruct($$$$$$)
        my $typeobject = "$name\_Type";
        $self->pidl("static PyTypeObject $typeobject = {");
        $self->indent;
-       $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
+       $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)");
        $self->pidl(".tp_name = \"$modulename.$prettyname\",");
        $self->pidl(".tp_getset = $getsetters,");
        if ($docstring) {
@@ -548,7 +548,7 @@ sub PythonFunctionStruct($$$$)
        $self->deindent;
        $self->pidl("}");
        $self->pidl("blob = ndr_push_blob(push);");
-       $self->pidl("ret = PyString_FromStringAndSize((char *)blob.data, blob.length);");
+       $self->pidl("ret = PyBytes_FromStringAndSize((char *)blob.data, blob.length);");
        $self->pidl("TALLOC_FREE(push);");
        $self->pidl("return ret;");
        $self->deindent;
@@ -806,7 +806,7 @@ sub PythonFunctionStruct($$$$)
        $self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];");
        $self->pidl("");
        $self->pidl("retstr = ndr_print_function_string(pytalloc_get_mem_ctx(py_obj), call->ndr_print, name, ndr_inout_flags, object);");
-       $self->pidl("ret = PyString_FromString(retstr);");
+       $self->pidl("ret = PyStr_FromString(retstr);");
        $self->pidl("TALLOC_FREE(retstr);");
        $self->pidl("");
        $self->pidl("return ret;");
@@ -866,7 +866,7 @@ sub PythonFunctionStruct($$$$)
        my $typeobject = "$name\_Type";
        $self->pidl("static PyTypeObject $typeobject = {");
        $self->indent;
-       $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
+       $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)");
        $self->pidl(".tp_name = \"$modulename.$prettyname\",");
        $self->pidl(".tp_getset = $getsetters,");
        if ($docstring) {
@@ -1294,7 +1294,7 @@ sub PythonType($$$$)
                $self->pidl_hdr("static PyTypeObject $typeobject;\n");
                $self->pidl("static PyTypeObject $typeobject = {");
                $self->indent;
-               $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
+               $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)");
                $self->pidl(".tp_name = \"$modulename.$prettyname\",");
                $self->pidl(".tp_getset = $getsetters,");
                if ($docstring) {
@@ -1417,7 +1417,7 @@ sub Interface($$$)
 
                $self->pidl("static PyTypeObject $if_typename = {");
                $self->indent;
-               $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
+               $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)");
                $self->pidl(".tp_name = \"$basename.$interface->{NAME}\",");
                $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),");
                $self->pidl(".tp_doc = $docstring,");
@@ -1431,7 +1431,7 @@ sub Interface($$$)
                $self->register_module_typeobject($interface->{NAME}, "&$if_typename", $interface->{ORIGINAL});
                my $dcerpc_typename = $self->import_type_variable("samba.dcerpc.base", "ClientConnection");
                $self->register_module_prereadycode(["$if_typename.tp_base = $dcerpc_typename;", ""]);
-               $self->register_module_postreadycode(["if (!PyInterface_AddNdrRpcMethods(&$if_typename, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]);
+               $self->register_module_postreadycode(["if (!PyInterface_AddNdrRpcMethods(&$if_typename, py_ndr_$interface->{NAME}\_methods))", "\treturn NULL;", ""]);
 
 
                $self->pidl("static PyObject *syntax_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
@@ -1457,7 +1457,7 @@ sub Interface($$$)
 
                $self->pidl("static PyTypeObject $syntax_typename = {");
                $self->indent;
-               $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
+               $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)");
                $self->pidl(".tp_name = \"$basename.$interface->{NAME}_abstract_syntax\",");
                $self->pidl(".tp_doc = $docstring,");
                $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
@@ -1637,11 +1637,11 @@ sub ConvertStringFromPythonData($$$$$)
        $self->deindent;
        $self->pidl("}");
 
-       $self->pidl("test_str = PyString_AS_STRING(unicode);");
+       $self->pidl("test_str = PyBytes_AS_STRING(unicode);");
        $self->deindent;
-       $self->pidl("} else if (PyString_Check($py_var)) {");
+       $self->pidl("} else if (PyBytes_Check($py_var)) {");
        $self->indent;
-       $self->pidl("test_str = PyString_AS_STRING($py_var);");
+       $self->pidl("test_str = PyBytes_AS_STRING($py_var);");
        $self->deindent;
        $self->pidl("} else {");
        $self->indent;
@@ -1827,7 +1827,7 @@ sub ConvertObjectFromPythonData($$$$$$;$$)
        }
 
        if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") {
-               $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AS_STRING($cvar), PyString_GET_SIZE($cvar));");
+               $self->pidl("$target = data_blob_talloc($mem_ctx, PyBytes_AS_STRING($cvar), PyBytes_GET_SIZE($cvar));");
                return;
        }
 
@@ -2032,7 +2032,7 @@ sub ConvertScalarToPython($$$$)
        }
 
        if ($ctypename eq "DATA_BLOB") {
-               return "PyString_FromStringAndSize((char *)($cvar).data, ($cvar).length)";
+               return "PyBytes_FromStringAndSize((char *)($cvar).data, ($cvar).length)";
        }
 
        if ($ctypename eq "NTSTATUS") {
@@ -2228,6 +2228,7 @@ sub Parse($$$$$)
 /* Python wrapper functions auto-generated by pidl */
 #define PY_SSIZE_T_CLEAN 1 /* We use Py_ssize_t for PyArg_ParseTupleAndKeywords */
 #include <Python.h>
+#include \"python/py3compat.h\"
 #include \"includes.h\"
 #include <pytalloc.h>
 #include \"librpc/rpc/pyrpc.h\"
@@ -2310,8 +2311,17 @@ static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)
 
        $self->pidl("");
 
-       $self->pidl_hdr("void init$basename(void);");
-       $self->pidl("void init$basename(void)");
+       $self->pidl("static struct PyModuleDef moduledef = {");
+       $self->indent;
+       $self->pidl("PyModuleDef_HEAD_INIT,");
+       $self->pidl(".m_name = \"$basename\",");
+       $self->pidl(".m_doc = \"$basename DCE/RPC\",");
+       $self->pidl(".m_size = -1,");
+       $self->pidl(".m_methods = $basename\_methods,");
+       $self->deindent;
+       $self->pidl("};");
+
+       $self->pidl("MODULE_INIT_FUNC($basename)");
        $self->pidl("{");
        $self->indent;
        $self->pidl("PyObject *m;");
@@ -2325,7 +2335,7 @@ static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)
                my $module_path = $h->{'val'};
                $self->pidl("$var_name = PyImport_ImportModule(\"$module_path\");");
                $self->pidl("if ($var_name == NULL)");
-               $self->pidl("\treturn;");
+               $self->pidl("\treturn NULL;");
                $self->pidl("");
        }
 
@@ -2338,7 +2348,7 @@ static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)
                $module_var =~ s/\./_/g;
                $self->pidl("$type_var = (PyTypeObject *)PyObject_GetAttrString($module_var, \"$pretty_name\");");
                $self->pidl("if ($type_var == NULL)");
-               $self->pidl("\treturn;");
+               $self->pidl("\treturn NULL;");
                $self->pidl("");
        }
 
@@ -2346,7 +2356,7 @@ static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)
 
        foreach (@{$self->{ready_types}}) {
                $self->pidl("if (PyType_Ready($_) < 0)");
-               $self->pidl("\treturn;");
+               $self->pidl("\treturn NULL;");
        }
 
        $self->pidl($_) foreach (@{$self->{postreadycode}});
@@ -2360,9 +2370,9 @@ static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)
 
        $self->pidl("");
 
-       $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");");
+       $self->pidl("m = PyModule_Create(&moduledef);");
        $self->pidl("if (m == NULL)");
-       $self->pidl("\treturn;");
+       $self->pidl("\treturn NULL;");
        $self->pidl("");
        foreach my $h (@{$self->{constants}}) {
                my $pretty_name = PrettifyTypeName($h->{'key'}, $basename);
@@ -2371,7 +2381,7 @@ static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)
                if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) {
                        $py_obj = "ndr_PyLong_FromUnsignedLongLong($cvar)";
                } elsif ($cvar =~ /^".*"$/) {
-                       $py_obj = "PyString_FromString($cvar)";
+                       $py_obj = "PyStr_FromString($cvar)";
                } else {
                        $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar, undef);
                }
@@ -2389,6 +2399,7 @@ static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)
        $self->pidl("PY_MOD_".uc($basename)."_PATCH(m);");
        $self->pidl("#endif");
 
+       $self->pidl("return m;");
        $self->pidl("");
        $self->deindent;
        $self->pidl("}");