From: Andrew Bartlett Date: Sun, 24 Mar 2024 23:36:35 +0000 (+1300) Subject: pyldb: Consolidate PyErr_SetLdbError() using the pyldb version X-Git-Tag: tdb-1.4.11~1353 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f379ea8b81251efad05ebb913ed0a0205fa0bcd5;p=thirdparty%2Fsamba.git pyldb: Consolidate PyErr_SetLdbError() using the pyldb version Now that pyldb-util is a private library to Samba, we have no excuses not to consolidate helper functions like this. Signed-off-by: Andrew Bartlett Reviewed-by: Douglas Bagnall --- diff --git a/lib/ldb-samba/pyldb.c b/lib/ldb-samba/pyldb.c index 8154679146b..958b3ad4b16 100644 --- a/lib/ldb-samba/pyldb.c +++ b/lib/ldb-samba/pyldb.c @@ -37,16 +37,6 @@ static PyObject *pyldb_module; static PyObject *py_ldb_error; static PyTypeObject PySambaLdb; -static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx) -{ - if (ret == LDB_ERR_PYTHON_EXCEPTION) - return; /* Python exception should already be set, just keep that */ - - PyErr_SetObject(error, - Py_BuildValue(discard_const_p(char, "(i,s)"), ret, - ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx))); -} - static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args) { PyObject *py_lp_ctx; diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index 67e6141d1b8..cd4268a9a74 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -260,27 +260,6 @@ static PyTypeObject PyLdbControl = { .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, }; -static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx) -{ - PyObject *exc = NULL; - if (ret == LDB_ERR_PYTHON_EXCEPTION) { - return; /* Python exception should already be set, just keep that */ - } - exc = Py_BuildValue("(i,s)", ret, - ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)); - if (exc == NULL) { - /* - * Py_BuildValue failed, and will have set its own exception. - * It isn't the one we wanted, but it will have to do. - * This is all very unexpected. - */ - fprintf(stderr, "could not make LdbError %d!\n", ret); - return; - } - PyErr_SetObject(error, exc); - Py_DECREF(exc); -} - static PyObject *py_ldb_bytes_str(PyBytesObject *self) { char *msg = NULL; diff --git a/lib/ldb/pyldb.h b/lib/ldb/pyldb.h index 34357eada1a..fe5139bef5a 100644 --- a/lib/ldb/pyldb.h +++ b/lib/ldb/pyldb.h @@ -112,6 +112,8 @@ typedef struct { struct ldb_control *data; } PyLdbControlObject; +void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx); + #define PyErr_LDB_ERROR_IS_ERR_RAISE(err,ret,ldb) do { \ if (ret != LDB_SUCCESS) { \ PyErr_SetLdbError(err, ret, ldb); \ diff --git a/lib/ldb/pyldb_util.c b/lib/ldb/pyldb_util.c index 9d7086cf294..e09114f4ab8 100644 --- a/lib/ldb/pyldb_util.c +++ b/lib/ldb/pyldb_util.c @@ -184,3 +184,24 @@ PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn) py_ret->dn = dn; return (PyObject *)py_ret; } + +void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx) +{ + PyObject *exc = NULL; + if (ret == LDB_ERR_PYTHON_EXCEPTION) { + return; /* Python exception should already be set, just keep that */ + } + exc = Py_BuildValue("(i,s)", ret, + ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)); + if (exc == NULL) { + /* + * Py_BuildValue failed, and will have set its own exception. + * It isn't the one we wanted, but it will have to do. + * This is all very unexpected. + */ + fprintf(stderr, "could not make LdbError %d!\n", ret); + return; + } + PyErr_SetObject(error, exc); + Py_DECREF(exc); +} diff --git a/source4/dsdb/pydsdb.c b/source4/dsdb/pydsdb.c index fbbbadd3f3b..5d94fdd9c9a 100644 --- a/source4/dsdb/pydsdb.c +++ b/source4/dsdb/pydsdb.c @@ -49,16 +49,6 @@ static PyObject *py_ldb_get_exception(void) return result; } -static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx) -{ - if (ret == LDB_ERR_PYTHON_EXCEPTION) - return; /* Python exception should already be set, just keep that */ - - PyErr_SetObject(error, - Py_BuildValue(discard_const_p(char, "(i,s)"), ret, - ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx))); -} - static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args) { PyObject *py_ldb, *result;