Skip to content

Commit

Permalink
Improved exception handling when importing the module (#596)
Browse files Browse the repository at this point in the history
The current expection handling is too vague, and in certain
circumstances, the error message may confuse the user.

For example, if an error occurs while importing the "_mysql" module, the
original error message is as follows:

```
  File "MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: /lib64/libstdc++.so.6: cannot allocate memory in static TLS block
```

But on the user side, he can only see the exception message like this: 

```
/MySQLdb/__init__.py", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
```

This PR fixes this issue by making the exception handling statements
more precise.
  • Loading branch information
piglei committed May 10, 2023
1 parent 1f4fb4d commit b7255d3
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions MySQLdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
MySQLdb.converters module.
"""

try:
from MySQLdb.release import version_info
from . import _mysql
# Check if the version of _mysql matches the version of MySQLdb.
from MySQLdb.release import version_info
from . import _mysql

assert version_info == _mysql.version_info
except Exception:
if version_info != _mysql.version_info:
raise ImportError(
"this is MySQLdb version {}, but _mysql is version {!r}\n_mysql: {!r}".format(
version_info, _mysql.version_info, _mysql.__file__
Expand Down

0 comments on commit b7255d3

Please sign in to comment.