pidl:Samba4/Python: add an optional 'allow_remaining' argument to __ndr_unpack__...
authorStefan Metzmacher <metze@samba.org>
Thu, 5 Jan 2012 15:33:13 +0000 (16:33 +0100)
committerStefan Metzmacher <metze@samba.org>
Mon, 9 Jan 2012 07:55:51 +0000 (08:55 +0100)
Thanks to Amitay Isaacs <amitay@gmail.com> for the help with this.

metze

pidl/lib/Parse/Pidl/Samba4/Python.pm

index 63f4b2baefdfe53f22da981457efbe5bd409ff06..63f41a10eac2986ef1b03e46fcef393567db2bda 100644 (file)
@@ -271,21 +271,44 @@ sub PythonStruct($$$$$$)
                $self->pidl("}");
                $self->pidl("");
 
-               $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args)");
+               $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args, PyObject *kwargs)");
                $self->pidl("{");
                $self->indent;
                $self->pidl("$cname *object = ($cname *)pytalloc_get_ptr(py_obj);");
                $self->pidl("DATA_BLOB blob;");
                $self->pidl("int blob_length = 0;");
                $self->pidl("enum ndr_err_code err;");
-               $self->pidl("if (!PyArg_ParseTuple(args, \"s#:__ndr_unpack__\", &blob.data, &blob_length)) {");
+               $self->pidl("const char * const kwnames[] = { \"data_blob\", \"allow_remaining\", NULL };");
+               $self->pidl("PyObject *allow_remaining_obj = NULL;");
+               $self->pidl("bool allow_remaining = false;");
+               $self->pidl("");
+               $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"s#|O:__ndr_unpack__\",");
+               $self->indent;
+               $self->pidl("discard_const_p(char *, kwnames),");
+               $self->pidl("&blob.data, &blob_length,");
+               $self->pidl("&allow_remaining_obj)) {");
+               $self->deindent;
                $self->indent;
                $self->pidl("return NULL;");
                $self->deindent;
                $self->pidl("}");
                $self->pidl("blob.length = blob_length;");
                $self->pidl("");
+               $self->pidl("if (allow_remaining_obj && PyObject_IsTrue(allow_remaining_obj)) {");
+               $self->indent;
+               $self->pidl("allow_remaining = true;");
+               $self->deindent;
+               $self->pidl("}");
+               $self->pidl("");
+               $self->pidl("if (allow_remaining) {");
+               $self->indent;
+               $self->pidl("err = ndr_pull_struct_blob(&blob, pytalloc_get_mem_ctx(py_obj), object, (ndr_pull_flags_fn_t)ndr_pull_$name);");
+               $self->deindent;
+               $self->pidl("} else {");
+               $self->indent;
                $self->pidl("err = ndr_pull_struct_blob_all(&blob, pytalloc_get_mem_ctx(py_obj), object, (ndr_pull_flags_fn_t)ndr_pull_$name);");
+               $self->deindent;
+               $self->pidl("}");
                $self->pidl("if (err != NDR_ERR_SUCCESS) {");
                $self->indent;
                $self->pidl("PyErr_SetNdrError(err);");
@@ -318,7 +341,7 @@ sub PythonStruct($$$$$$)
                $self->pidl("static PyMethodDef $py_methods\[] = {");
                $self->indent;
                $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.ndr_pack(object) -> blob\\nNDR pack\" },");
-               $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS, \"S.ndr_unpack(class, blob) -> None\\nNDR unpack\" },");
+               $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS|METH_KEYWORDS, \"S.ndr_unpack(class, blob, allow_remaining=False) -> None\\nNDR unpack\" },");
                $self->pidl("{ \"__ndr_print__\", (PyCFunction)py_$name\_ndr_print, METH_VARARGS, \"S.ndr_print(object) -> None\\nNDR print\" },");
                $self->pidl("{ NULL, NULL, 0, NULL }");
                $self->deindent;