pyodbc icon indicating copy to clipboard operation
pyodbc copied to clipboard

Better handling of "<class 'pyodbc.Error'> returned a result" errors

Open Arithmomaniac opened this issue 7 months ago • 0 comments

A search of this repo shows cases where an exception like the following is thrown by PyODBC (We got one of these recently on 5.2.0):

<class 'pyodbc.Error'> returned a result with an exception set

As opposed to a normal pyodbc exception, which is something like

(pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Parse error at line: 1, column: 38: Incorrect syntax near ':'. (103010) (SQLExecDirectW)")

It looks like a base pyodbc.Error is being thrown somewhere in C (on some non-mapped error code, maybe?), but isn't handled properly. I don't understand the C layer of Python, but this is discussed a few places on StackOverflow:

https://stackoverflow.com/a/53796516:

In general: "[R]eturned a result with an error set" is something that can only be done at the C level. In general, the Python/C API expects most C functions to do one of two things:

  • Set an exception using one of these functions and return NULL (corresponds to throwing an exception).
  • Don't set an exception and return a "real" value, usually a PyObject* (corresponds to returning a value, including returning None).

These two cases are normally incorrect:

  • Set an exception (or fail to clear one that already exists), but then return some value other than NULL.
  • Don't set an exception, but then return NULL. Cf. https://stackoverflow.com/a/75846676.

Is the current behavior by design, or could the screws be tightened somewhere?

Arithmomaniac avatar Apr 06 '25 08:04 Arithmomaniac