pidl/Python: make use of pyrpc_PyStr_AsString() in ConvertStringFromPythonData()
authorStefan Metzmacher <metze@samba.org>
Tue, 9 May 2017 06:42:21 +0000 (08:42 +0200)
committerStefan Metzmacher <metze@samba.org>
Mon, 18 Feb 2019 11:55:14 +0000 (12:55 +0100)
This reduces the amount of generated code a lot.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
pidl/lib/Parse/Pidl/Samba4/Python.pm

index 1eaf8e7a9d0c78af5d6df5ab2d780c214f0ec4a4..032ddf74984e5f24eb2a6b254b3bcc65fd475d6b 100644 (file)
@@ -1671,47 +1671,10 @@ sub ConvertStringFromPythonData($$$$$)
 {
        my ($self, $mem_ctx, $py_var, $target, $fail) = @_;
 
-       $self->pidl("{");
-       $self->indent;
-       $self->pidl("const char *test_str;");
-       $self->pidl("const char *talloc_str;");
-       $self->pidl("PyObject *unicode = NULL;");
-       $self->pidl("if (PyUnicode_Check($py_var)) {");
-       $self->indent;
-       # FIXME: Use Unix charset setting rather than utf-8
-       $self->pidl("unicode = PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\");");
-       $self->pidl("if (unicode == NULL) {");
-       $self->indent;
-       $self->pidl("PyErr_NoMemory();");
-       $self->pidl("$fail");
-       $self->deindent;
-       $self->pidl("}");
-
-       $self->pidl("test_str = PyBytes_AS_STRING(unicode);");
-       $self->deindent;
-       $self->pidl("} else if (PyBytes_Check($py_var)) {");
-       $self->indent;
-       $self->pidl("test_str = PyBytes_AS_STRING($py_var);");
-       $self->deindent;
-       $self->pidl("} else {");
+       $self->pidl("$target = pyrpc_PyStr_AsString($mem_ctx, $py_var);");
+       $self->pidl("if (PyErr_Occurred() != NULL) {");
        $self->indent;
-       $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected string or unicode object, got %s\", Py_TYPE($py_var)->tp_name);");
-       $self->pidl("$fail");
-       $self->deindent;
-       $self->pidl("}");
-       $self->pidl("talloc_str = talloc_strdup($mem_ctx, test_str);");
-       $self->pidl("if (unicode != NULL) {");
-       $self->indent;
-       $self->pidl("Py_DECREF(unicode);");
-       $self->deindent;
-       $self->pidl("}");
-       $self->pidl("if (talloc_str == NULL) {");
-       $self->indent;
-       $self->pidl("PyErr_NoMemory();");
-       $self->pidl("$fail");
-       $self->deindent;
-       $self->pidl("}");
-       $self->pidl("$target = talloc_str;");
+       $self->pidl($fail);
        $self->deindent;
        $self->pidl("}");
 }
@@ -1919,6 +1882,11 @@ sub ConvertObjectFromPythonLevel($$$$$$$$$)
                        $self->pidl($fail);
                        $self->deindent;
                        $self->pidl("}");
+               } elsif ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::is_scalar($nl->{DATA_TYPE})
+                        and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
+                       $self->pidl("/* $var_name = pyrpc_*() will directly follow */");
+               } elsif ($nl->{TYPE} eq "ARRAY" and is_charset_array($e, $nl)) {
+                       $self->pidl("/* $var_name = pyrpc_PyStr_AsString() will directly follow */");
                } else {
                        $self->pidl("$var_name = NULL;");
                }