Skip to content
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

MySQLdb exceptions should reflect their public API name in tracebacks #522

Closed
metzen opened this issue Jan 7, 2022 · 2 comments · Fixed by #523
Closed

MySQLdb exceptions should reflect their public API name in tracebacks #522

metzen opened this issue Jan 7, 2022 · 2 comments · Fixed by #523

Comments

@metzen
Copy link

metzen commented Jan 7, 2022

Currently, the exception types used by MySQLdb are defined within the MySQLdb._exceptions submodule, but are also aliased as symbols within the MySQLdb._mysql C extension module, as well as imported from there to be exposed in the top level MySQLdb package, where they are intended to be used by clients.

However, when one of these exceptions is raised by the MySQLdb implementation, the name which is depicted in the resulting traceback will reference the original definition location of the exception in MySQLdb._exceptions, e.g.,

MySQLdb._exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax ...")

When users encounter such an error, they may be inclined to use the name as it is depicted in the traceback when attempting to add exception catching behavior for the error rather than the intended public alias in MySQLdb.ProgrammingError (if they are not aware that this is provided).

It would be prudent to steer users in the right direction by avoiding this private submodule exposure and having the name used in tracebacks reflect the intended public interface for referencing the symbol.

@metzen
Copy link
Author

metzen commented Jan 7, 2022

I see a couple of options for achieving this:

  1. Define the exceptions in the top level MySQLdb/__init__.py file itself.
  2. Define a __module__ = 'MySQLdb' attribute for each exception type declared in MySQLdb._exceptions

The first is the most obvious, but I suspect the motivation for having the exceptions defined in the separate module was to avoid circular import issues given that the exceptions are imported by the _mysql.c C extension, which is itself imported in the top level __init__.py package?

If that's the case, then simply overriding the __module__ attribute of these exception classes could be used to let them reflect the public interface for referencing them, regardless of their actual definition being in the _exceptions submodule.

@methane
Copy link
Member

methane commented Jan 8, 2022

Thank you. You are completely right.

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 a pull request may close this issue.

2 participants