Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Arithmomaniac opened this issue Apr 6, 2025 · 0 comments
Open

Comments

@Arithmomaniac
Copy link

Arithmomaniac commented Apr 6, 2025

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 Arithmomaniac changed the title Better handling of "Error: <class 'pyodbc.Error'> returned a result"... messages Better handling of "<class 'pyodbc.Error'> returned a result" errors Apr 6, 2025
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

No branches or pull requests

1 participant