From a1c2206e6bff03067147e1e6c4c370d742e58e36 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Tue, 28 Jan 2025 14:57:53 +0100 Subject: [PATCH] s4:pyrpc: allow connections with raise_result_exceptions=False This is needed in order to do useful tests with specific error codes and still checking all other out parameters. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14213 Signed-off-by: Stefan Metzmacher Reviewed-by: Douglas Bagnall (cherry picked from commit 6b1ff9a38fcddbe72b00e28960414526a42bde14) --- pidl/lib/Parse/Pidl/Samba4/Python.pm | 3 +-- source4/librpc/rpc/pyrpc.h | 1 + source4/librpc/rpc/pyrpc_util.c | 27 +++++++++++++++++++++++---- source4/librpc/rpc/pyrpc_util.h | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pidl/lib/Parse/Pidl/Samba4/Python.pm b/pidl/lib/Parse/Pidl/Samba4/Python.pm index 7a415c99c3a..285e4321b19 100644 --- a/pidl/lib/Parse/Pidl/Samba4/Python.pm +++ b/pidl/lib/Parse/Pidl/Samba4/Python.pm @@ -967,10 +967,9 @@ sub PythonFunctionUnpackOut($$$) my $env = GenerateFunctionOutEnv($fn, "r->"); my $result_size = 0; - $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)"); + $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r, bool raise_result_exception)"); $self->pidl("{"); $self->indent; - $self->pidl("_UNUSED_ bool raise_result_exception = true;"); foreach my $e (@{$fn->{ELEMENTS}}) { next unless (grep(/out/,@{$e->{DIRECTION}})); next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or diff --git a/source4/librpc/rpc/pyrpc.h b/source4/librpc/rpc/pyrpc.h index 390e01a1196..09f4a1abd7e 100644 --- a/source4/librpc/rpc/pyrpc.h +++ b/source4/librpc/rpc/pyrpc.h @@ -50,6 +50,7 @@ typedef struct { struct dcerpc_pipe *pipe; struct dcerpc_binding_handle *binding_handle; struct tevent_context *ev; + bool raise_result_exceptions; } dcerpc_InterfaceObject; diff --git a/source4/librpc/rpc/pyrpc_util.c b/source4/librpc/rpc/pyrpc_util.c index 43c29af0e76..a0910b4f802 100644 --- a/source4/librpc/rpc/pyrpc_util.c +++ b/source4/librpc/rpc/pyrpc_util.c @@ -102,11 +102,29 @@ PyObject *py_dcerpc_interface_init_helper(PyTypeObject *type, PyObject *args, Py PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None; NTSTATUS status; unsigned int timeout = (unsigned int)-1; + int raise_result_exceptions = 1; const char *kwnames[] = { - "binding", "lp_ctx", "credentials", "timeout", "basis_connection", NULL + "binding", + "lp_ctx", + "credentials", + "timeout", + "basis_connection", + "raise_result_exceptions", + NULL }; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOIO:samr", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &timeout, &py_basis)) { + bool ok; + + ok = PyArg_ParseTupleAndKeywords(args, + kwargs, + "s|OOIOp", + discard_const_p(char *, kwnames), + &binding_string, + &py_lp_ctx, + &py_credentials, + &timeout, + &py_basis, + &raise_result_exceptions); + if (!ok) { return NULL; } @@ -122,6 +140,7 @@ PyObject *py_dcerpc_interface_init_helper(PyTypeObject *type, PyObject *args, Py return NULL; } + ret->raise_result_exceptions = (raise_result_exceptions != 0); ret->pipe = NULL; ret->binding_handle = NULL; ret->ev = NULL; @@ -300,7 +319,7 @@ static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface, return NULL; } - result = md->unpack_out_data(r); + result = md->unpack_out_data(r, iface->raise_result_exceptions); talloc_free(mem_ctx); return result; diff --git a/source4/librpc/rpc/pyrpc_util.h b/source4/librpc/rpc/pyrpc_util.h index 73157fcba1c..8da68a3555a 100644 --- a/source4/librpc/rpc/pyrpc_util.h +++ b/source4/librpc/rpc/pyrpc_util.h @@ -34,7 +34,7 @@ void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status); typedef NTSTATUS (*py_dcerpc_call_fn) (struct dcerpc_binding_handle *, TALLOC_CTX *, void *); typedef bool (*py_data_pack_fn) (PyObject *args, PyObject *kwargs, void *r); -typedef PyObject *(*py_data_unpack_fn) (void *r); +typedef PyObject *(*py_data_unpack_fn) (void *r, bool raise_result_exception); struct PyNdrRpcMethodDef { const char *name; -- 2.47.2