From: Andrew Bartlett Date: Sun, 24 Mar 2024 23:44:29 +0000 (+1300) Subject: pyldb: Improve search for error string in PyErr_SetLdbError X-Git-Tag: tdb-1.4.11~1351 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=565314f448236ff41d9c6c532949c19ee85b6425;p=thirdparty%2Fsamba.git pyldb: Improve search for error string in PyErr_SetLdbError We allow a fallback to ldb_strerror() even if there was an LDB context, allowing failing functions to reset a previous error string but not set a new one. Signed-off-by: Andrew Bartlett Reviewed-by: Douglas Bagnall --- diff --git a/lib/ldb/pyldb_util.c b/lib/ldb/pyldb_util.c index e09114f4ab8..87407f0eaa6 100644 --- a/lib/ldb/pyldb_util.c +++ b/lib/ldb/pyldb_util.c @@ -188,11 +188,21 @@ PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn) void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx) { PyObject *exc = NULL; + const char *ldb_error_string = 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 (ldb_ctx != NULL) { + ldb_error_string = ldb_errstring(ldb_ctx); + } + /* either no LDB context, no string stored or string reset */ + if (ldb_error_string == NULL) { + ldb_error_string = ldb_strerror(ret); + } + + exc = Py_BuildValue("(i,s)", ret, ldb_error_string); if (exc == NULL) { /* * Py_BuildValue failed, and will have set its own exception.