Skip to content

Report attribute errors from set_option() correctly - #1087

Open
rpcope1 wants to merge 1 commit into
ibmdb:masterfrom
rpcope1:fix/set-option-error-result
Open

rpcope1 wants to merge 1 commit into
ibmdb:masterfrom
rpcope1:fix/set-option-error-result

Conversation

@rpcope1

@rpcope1 rpcope1 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

When the CLI driver rejects an attribute value:

  • Statement attributes (set_option(stmt, ..., 0), or options passed to prepare()/exec_immediate()) now raise the driver's error as an Exception, the same class and message other ibm_db calls raise. Before, the result was a SystemError ("returned a result with an exception set"), and exec_immediate() segfaulted. The statement allocated by a rejected prepare() is released. exec_immediate() now releases its statement on any options error, including the existing validation errors, where it used to leak it.
  • Connection attributes (set_option(conn, ..., 1)) now return False, as documented, and the driver's message is available from conn_errormsg(conn). It is read from the connection handle, so read it right after the call. If the same dict sets further attributes after the rejected one, they are still applied, as before, and a later successful call on the handle can replace the message. The process-wide conn_errormsg() is left alone, so a successful connect() given a rejected option doesn't leave a stale message there. The diagnostic used to be requested through the statement-handle field with SQL_HANDLE_STMT, so nothing was recorded and True was returned. connect() with such an option still succeeds, as before.

Otherwise nothing changes. The existing messages for invalid ATTR_CASE and SQL_ATTR_ROW_ARRAY_SIZE values are kept, and the zLOAD attribute branch is untouched.

Closes #1086

Added ibm_db_tests/test_6796_SetOptionDriverError.py. It runs against Db2 LUW servers and skips elsewhere, because I could only verify the driver's behaviour there.

Testing (Linux x86_64, Python 3.13, clidriver v11.5.9, Db2 LUW 11.5.8):

  • On master the new test crashes the runner (the exec_immediate() case). With this change it passes.
  • The full sync suite fails the same set of tests before and after this change. Those failures are environment gaps in my test database (missing sample tables).

Note: this touches the same lines as #1085 (reference leaks in option parsing), so whichever merges second needs a rebase. The conflict is in the two string-value branches of _python_ibm_db_assign_options(). Keep both sides, with #<PR 1>'s Py_DECREF(data); before this PR's if (rc == SQL_ERROR) return ...;. #<PR 1>'s Py_XDECREF(keys) lines in _python_ibm_db_parse_options() merge cleanly. I ran both new tests on the combined code resolved that way, and test 6796 checks that a rejected string option is released the same way as an accepted one, so a resolution that drops the decref on the new return shows up as a leak.

When the CLI rejected a statement attribute, set_option() recorded the
error but still returned True, so Python raised SystemError ("returned
a result with an exception set"). prepare() given such an option failed
the same way, and exec_immediate() crashed. All three now raise the
driver's error as an Exception, as other ibm_db calls do. prepare() and
exec_immediate() release the statement they allocated; exec_immediate()
now does so for any options error.

When the CLI rejected a connection attribute, the diagnostic was read
from the connection handle as if it were a statement handle, so nothing
was recorded and set_option() returned True. It now returns False, as
documented, and conn_errormsg(conn) holds the driver's message.
connect() with such an option still succeeds, as before.

Add a regression test.

Signed-off-by: Robert Cope <robert@copesystems.com>
@rpcope1
rpcope1 force-pushed the fix/set-option-error-result branch from 2a88e20 to e6b60ff Compare November 17, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

set_option() reports driver-rejected attributes as SystemError (statements) or True (connections); exec_immediate() with such an option segfaults

2 participants